splitgraph.core.sql package

Module contents

Routines for managing SQL statements

splitgraph.core.sql.insert(table: str, columns: Sequence[str], schema: str = 'splitgraph_meta')psycopg2.sql.Composed

A generic SQL SELECT constructor to simplify metadata access queries so that we don’t have to repeat the same identifiers everywhere.

Parameters
  • table – Table to select from.

  • columns – Columns to insert as a list of strings.

  • schema – Schema that contains the table

Returns

A psycopg2.sql.SQL object with the query (parameterized)

splitgraph.core.sql.prepare_splitfile_sql(sql: str, image_mapper: Callable)Tuple[str, str]

Transform an SQL query to prepare for it to be used in a Splitfile SQL command and validate it. The rules are:

  • Only basic DDL (CREATE/ALTER/DROP table) and DML (SELECT/INSERT/UPDATE/DELETE) are permitted.

  • All tables must be either non-schema qualified (the statement is run with search_path

set to the single schema that a Splitgraph image is checked out into) or have schemata of format namespace/repository:hash_or_tag. In the second case, the schema is rewritten to point at a temporary mount of the Splitgraph image. * Function invocations are forbidden.

Parameters
  • sql – SQL query

  • image_mapper – Takes in an image and gives back the schema it should be rewritten to (for the purposes of execution) and the canonical form of the image.

  • schema – Schema to add to all non-qualified tables

Returns

Transformed form of the SQL with substituted schema shims for Splitfile execution and the canonical form (with e.g. tags resolved into at-the-time full image hashes)

Raises

UnsupportedSQLException if validation failed

splitgraph.core.sql.recover_original_schema_name(sql: str, schema_name: str)str

Postgres truncates identifiers to 63 characters at parse time and, as pglast uses bits of PG to parse queries, image names like noaa/climate:64_chars_of_hash get truncated which can cause ambiguities and issues in provenance. We can’t get pglast to give us back the full identifier, but we can try and figure out what it used to be and patch the AST to have it again.

splitgraph.core.sql.select(table: str, columns: str = '*', where: str = '', schema: str = 'splitgraph_meta', table_args: Optional[str] = None)psycopg2.sql.Composed

A generic SQL SELECT constructor to simplify metadata access queries so that we don’t have to repeat the same identifiers everywhere.

Parameters
  • table – Table to select from.

  • columns – Columns to select as a string. WARN: concatenated directly without any formatting.

  • where – If specified, added to the query with a “WHERE” keyword. WARN also concatenated directly.

  • schema – Defaults to SPLITGRAPH_META_SCHEMA.

  • table_args – If specified, appends to the FROM clause after the table specification, for example, SELECT * FROM “splitgraph_api”.”get_images” (%s, %s) …

Returns

A psycopg2.sql.SQL object with the query.

splitgraph.core.sql.validate_import_sql(sql: str)str

Check an SQL query to see if it can be safely used in an IMPORT statement (e.g. FROM noaa/climate:latest IMPORT {SELECT * FROM rainfall WHERE state = ‘AZ’} AS rainfall. In this case, only a single SELECT statement is supported.

Parameters

sql – SQL query

Returns

Canonical (formatted) form of the SQL statement

Raises

UnsupportedSQLException if validation failed