sqlglot.dialects.sqlite API documentation

Edit on GitHub

# sqlglot.dialects.sqlite

View Source

      1from __future__ import annotations
      2
      3from sqlglot import exp, generator, parser, tokens
      4from sqlglot.dialects.dialect import (
      5    Dialect,
      6    arrow_json_extract_scalar_sql,
      7    arrow_json_extract_sql,
      8    no_ilike_sql,
      9    no_tablesample_sql,
     10    no_trycast_sql,
     11    rename_func,
     12)
     13from sqlglot.tokens import TokenType
     14
     15
     16def _fetch_sql(self, expression):
     17    return self.limit_sql(exp.Limit(expression=expression.args.get("count")))
     18
     19
     20# https://www.sqlite.org/lang_aggfunc.html#group_concat
     21def _group_concat_sql(self, expression):
     22    this = expression.this
     23    distinct = expression.find(exp.Distinct)
     24    if distinct:
     25        this = distinct.expressions[0]
     26        distinct = "DISTINCT "
     27
     28    if isinstance(expression.this, exp.Order):
     29        self.unsupported("SQLite GROUP_CONCAT doesn't support ORDER BY.")
     30        if expression.this.this and not distinct:
     31            this = expression.this.this
     32
     33    separator = expression.args.get("separator")
     34    return f"GROUP_CONCAT({distinct or ''}{self.format_args(this, separator)})"
     35
     36
     37def _date_add_sql(self, expression):
     38    modifier = expression.expression
     39    modifier = expression.name if modifier.is_string else self.sql(modifier)
     40    unit = expression.args.get("unit")
     41    modifier = f"'{modifier} {unit.name}'" if unit else f"'{modifier}'"
     42    return f"{self.normalize_func('DATE')}({self.format_args(expression.this, modifier)})"
     43
     44
     45class SQLite(Dialect):
     46    class Tokenizer(tokens.Tokenizer):
     47        IDENTIFIERS = ['"', ("[", "]"), "`"]
     48        HEX_STRINGS = [("x'", "'"), ("X'", "'"), ("0x", ""), ("0X", "")]
     49
     50        KEYWORDS = {
     51            **tokens.Tokenizer.KEYWORDS,
     52        }
     53
     54    class Parser(parser.Parser):
     55        FUNCTIONS = {
     56            **parser.Parser.FUNCTIONS,  # type: ignore
     57            "EDITDIST3": exp.Levenshtein.from_arg_list,
     58        }
     59
     60    class Generator(generator.Generator):
     61        TYPE_MAPPING = {
     62            **generator.Generator.TYPE_MAPPING,  # type: ignore
     63            exp.DataType.Type.BOOLEAN: "INTEGER",
     64            exp.DataType.Type.TINYINT: "INTEGER",
     65            exp.DataType.Type.SMALLINT: "INTEGER",
     66            exp.DataType.Type.INT: "INTEGER",
     67            exp.DataType.Type.BIGINT: "INTEGER",
     68            exp.DataType.Type.FLOAT: "REAL",
     69            exp.DataType.Type.DOUBLE: "REAL",
     70            exp.DataType.Type.DECIMAL: "REAL",
     71            exp.DataType.Type.CHAR: "TEXT",
     72            exp.DataType.Type.NCHAR: "TEXT",
     73            exp.DataType.Type.VARCHAR: "TEXT",
     74            exp.DataType.Type.NVARCHAR: "TEXT",
     75            exp.DataType.Type.BINARY: "BLOB",
     76            exp.DataType.Type.VARBINARY: "BLOB",
     77        }
     78
     79        TOKEN_MAPPING = {
     80            TokenType.AUTO_INCREMENT: "AUTOINCREMENT",
     81        }
     82
     83        TRANSFORMS = {
     84            **generator.Generator.TRANSFORMS,  # type: ignore
     85            exp.DateAdd: _date_add_sql,
     86            exp.ILike: no_ilike_sql,
     87            exp.JSONExtract: arrow_json_extract_sql,
     88            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
     89            exp.JSONBExtract: arrow_json_extract_sql,
     90            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
     91            exp.Levenshtein: rename_func("EDITDIST3"),
     92            exp.TableSample: no_tablesample_sql,
     93            exp.DateStrToDate: lambda self, e: self.sql(e, "this"),
     94            exp.TimeStrToTime: lambda self, e: self.sql(e, "this"),
     95            exp.TryCast: no_trycast_sql,
     96            exp.GroupConcat: _group_concat_sql,
     97            exp.Fetch: _fetch_sql,
     98        }
     99
    100        def transaction_sql(self, expression):
    101            this = expression.this
    102            this = f" {this}" if this else ""
    103            return f"BEGIN{this} TRANSACTION"

class SQLite(sqlglot.dialects.dialect.Dialect): View Source

     46class SQLite(Dialect):
     47    class Tokenizer(tokens.Tokenizer):
     48        IDENTIFIERS = ['"', ("[", "]"), "`"]
     49        HEX_STRINGS = [("x'", "'"), ("X'", "'"), ("0x", ""), ("0X", "")]
     50
     51        KEYWORDS = {
     52            **tokens.Tokenizer.KEYWORDS,
     53        }
     54
     55    class Parser(parser.Parser):
     56        FUNCTIONS = {
     57            **parser.Parser.FUNCTIONS,  # type: ignore
     58            "EDITDIST3": exp.Levenshtein.from_arg_list,
     59        }
     60
     61    class Generator(generator.Generator):
     62        TYPE_MAPPING = {
     63            **generator.Generator.TYPE_MAPPING,  # type: ignore
     64            exp.DataType.Type.BOOLEAN: "INTEGER",
     65            exp.DataType.Type.TINYINT: "INTEGER",
     66            exp.DataType.Type.SMALLINT: "INTEGER",
     67            exp.DataType.Type.INT: "INTEGER",
     68            exp.DataType.Type.BIGINT: "INTEGER",
     69            exp.DataType.Type.FLOAT: "REAL",
     70            exp.DataType.Type.DOUBLE: "REAL",
     71            exp.DataType.Type.DECIMAL: "REAL",
     72            exp.DataType.Type.CHAR: "TEXT",
     73            exp.DataType.Type.NCHAR: "TEXT",
     74            exp.DataType.Type.VARCHAR: "TEXT",
     75            exp.DataType.Type.NVARCHAR: "TEXT",
     76            exp.DataType.Type.BINARY: "BLOB",
     77            exp.DataType.Type.VARBINARY: "BLOB",
     78        }
     79
     80        TOKEN_MAPPING = {
     81            TokenType.AUTO_INCREMENT: "AUTOINCREMENT",
     82        }
     83
     84        TRANSFORMS = {
     85            **generator.Generator.TRANSFORMS,  # type: ignore
     86            exp.DateAdd: _date_add_sql,
     87            exp.ILike: no_ilike_sql,
     88            exp.JSONExtract: arrow_json_extract_sql,
     89            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
     90            exp.JSONBExtract: arrow_json_extract_sql,
     91            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
     92            exp.Levenshtein: rename_func("EDITDIST3"),
     93            exp.TableSample: no_tablesample_sql,
     94            exp.DateStrToDate: lambda self, e: self.sql(e, "this"),
     95            exp.TimeStrToTime: lambda self, e: self.sql(e, "this"),
     96            exp.TryCast: no_trycast_sql,
     97            exp.GroupConcat: _group_concat_sql,
     98            exp.Fetch: _fetch_sql,
     99        }
    100
    101        def transaction_sql(self, expression):
    102            this = expression.this
    103            this = f" {this}" if this else ""
    104            return f"BEGIN{this} TRANSACTION"

SQLite()

##### Inherited Members

sqlglot.dialects.dialect.Dialect
  get_or_raise
  format_time
  parse
  parse_into
  generate
  transpile
  parser
  generator

class SQLite.Tokenizer(sqlglot.tokens.Tokenizer): View Source

    47    class Tokenizer(tokens.Tokenizer):
    48        IDENTIFIERS = ['"', ("[", "]"), "`"]
    49        HEX_STRINGS = [("x'", "'"), ("X'", "'"), ("0x", ""), ("0X", "")]
    50
    51        KEYWORDS = {
    52            **tokens.Tokenizer.KEYWORDS,
    53        }

##### Inherited Members

sqlglot.tokens.Tokenizer
  Tokenizer
  reset
  tokenize

class SQLite.Parser(sqlglot.parser.Parser): View Source

    55    class Parser(parser.Parser):
    56        FUNCTIONS = {
    57            **parser.Parser.FUNCTIONS,  # type: ignore
    58            "EDITDIST3": exp.Levenshtein.from_arg_list,
    59        }

Parser consumes a list of tokens produced by the `sqlglot.tokens.Tokenizer` and produces a parsed syntax tree.

###### Arguments:

- error_level: the desired error level. Default: ErrorLevel.RAISE
- error_message_context: determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 50.
- index_offset: Index offset for arrays eg ARRAY[0] vs ARRAY[1] as the head of a list. Default: 0
- alias_post_tablesample: If the table alias comes after tablesample. Default: False
- max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3
- null_ordering: Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"

##### Inherited Members

sqlglot.parser.Parser
  Parser
  reset
  parse
  parse_into
  check_errors
  raise_error
  expression
  validate_expression

class SQLite.Generator(sqlglot.generator.Generator): View Source

     61    class Generator(generator.Generator):
     62        TYPE_MAPPING = {
     63            **generator.Generator.TYPE_MAPPING,  # type: ignore
     64            exp.DataType.Type.BOOLEAN: "INTEGER",
     65            exp.DataType.Type.TINYINT: "INTEGER",
     66            exp.DataType.Type.SMALLINT: "INTEGER",
     67            exp.DataType.Type.INT: "INTEGER",
     68            exp.DataType.Type.BIGINT: "INTEGER",
     69            exp.DataType.Type.FLOAT: "REAL",
     70            exp.DataType.Type.DOUBLE: "REAL",
     71            exp.DataType.Type.DECIMAL: "REAL",
     72            exp.DataType.Type.CHAR: "TEXT",
     73            exp.DataType.Type.NCHAR: "TEXT",
     74            exp.DataType.Type.VARCHAR: "TEXT",
     75            exp.DataType.Type.NVARCHAR: "TEXT",
     76            exp.DataType.Type.BINARY: "BLOB",
     77            exp.DataType.Type.VARBINARY: "BLOB",
     78        }
     79
     80        TOKEN_MAPPING = {
     81            TokenType.AUTO_INCREMENT: "AUTOINCREMENT",
     82        }
     83
     84        TRANSFORMS = {
     85            **generator.Generator.TRANSFORMS,  # type: ignore
     86            exp.DateAdd: _date_add_sql,
     87            exp.ILike: no_ilike_sql,
     88            exp.JSONExtract: arrow_json_extract_sql,
     89            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
     90            exp.JSONBExtract: arrow_json_extract_sql,
     91            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
     92            exp.Levenshtein: rename_func("EDITDIST3"),
     93            exp.TableSample: no_tablesample_sql,
     94            exp.DateStrToDate: lambda self, e: self.sql(e, "this"),
     95            exp.TimeStrToTime: lambda self, e: self.sql(e, "this"),
     96            exp.TryCast: no_trycast_sql,
     97            exp.GroupConcat: _group_concat_sql,
     98            exp.Fetch: _fetch_sql,
     99        }
    100
    101        def transaction_sql(self, expression):
    102            this = expression.this
    103            this = f" {this}" if this else ""
    104            return f"BEGIN{this} TRANSACTION"

Generator interprets the given syntax tree and produces a SQL string as an output.

###### Arguments:

- time_mapping (dict): the dictionary of custom time mappings in which the key represents a python time format and the output the target time format
- time_trie (trie): a trie of the time_mapping keys
- pretty (bool): if set to True the returned string will be formatted. Default: False.
- quote_start (str): specifies which starting character to use to delimit quotes. Default: '.
- quote_end (str): specifies which ending character to use to delimit quotes. Default: '.
- identifier_start (str): specifies which starting character to use to delimit identifiers. Default: ".
- identifier_end (str): specifies which ending character to use to delimit identifiers. Default: ".
- identify (bool): if set to True all identifiers will be delimited by the corresponding character.
- normalize (bool): if set to True all identifiers will lower cased
- string_escape (str): specifies a string escape character. Default: '.
- identifier_escape (str): specifies an identifier escape character. Default: ".
- pad (int): determines padding in a formatted string. Default: 2.
- indent (int): determines the size of indentation in a formatted string. Default: 4.
- unnest_column_only (bool): if true unnest table aliases are considered only as column aliases
- normalize_functions (str): normalize function names, "upper", "lower", or None Default: "upper"
- alias_post_tablesample (bool): if the table alias comes after tablesample Default: False
- unsupported_level (ErrorLevel): determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- null_ordering (str): Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"
- max_unsupported (int): Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
- leading_comma (bool): if the the comma is leading or trailing in select statements Default: False
- max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
- comments: Whether or not to preserve comments in the output SQL code. Default: True

def transaction_sql(self, expression): View Source

    101        def transaction_sql(self, expression):
    102            this = expression.this
    103            this = f" {this}" if this else ""
    104            return f"BEGIN{this} TRANSACTION"

##### Inherited Members

sqlglot.generator.Generator
  Generator
  generate
  unsupported
  sep
  seg
  pad_comment
  maybe_comment
  wrap
  no_identify
  normalize_func
  indent
  sql
  uncache_sql
  cache_sql
  characterset_sql
  column_sql
  columndef_sql
  columnconstraint_sql
  autoincrementcolumnconstraint_sql
  checkcolumnconstraint_sql
  commentcolumnconstraint_sql
  collatecolumnconstraint_sql
  encodecolumnconstraint_sql
  defaultcolumnconstraint_sql
  generatedasidentitycolumnconstraint_sql
  notnullcolumnconstraint_sql
  primarykeycolumnconstraint_sql
  uniquecolumnconstraint_sql
  create_sql
  describe_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  except_op
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  national_sql
  partition_sql
  properties_sql
  root_properties
  properties
  with_properties
  locate_properties
  property_sql
  likeproperty_sql
  fallbackproperty_sql
  journalproperty_sql
  freespaceproperty_sql
  afterjournalproperty_sql
  checksumproperty_sql
  mergeblockratioproperty_sql
  datablocksizeproperty_sql
  blockcompressionproperty_sql
  isolatedloadingproperty_sql
  insert_sql
  intersect_sql
  intersect_op
  introducer_sql
  pseudotype_sql
  rowformatdelimitedproperty_sql
  table_sql
  tablesample_sql
  pivot_sql
  tuple_sql
  update_sql
  values_sql
  var_sql
  into_sql
  from_sql
  group_sql
  having_sql
  join_sql
  lambda_sql
  lateral_sql
  limit_sql
  offset_sql
  lock_sql
  literal_sql
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  select_sql
  schema_sql
  star_sql
  structkwarg_sql
  parameter_sql
  sessionparameter_sql
  placeholder_sql
  subquery_sql
  qualify_sql
  union_sql
  union_op
  unnest_sql
  where_sql
  window_sql
  partition_by_sql
  window_spec_sql
  withingroup_sql
  between_sql
  bracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  extract_sql
  trim_sql
  concat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  unique_sql
  if_sql
  in_sql
  in_unnest_op
  interval_sql
  return_sql
  reference_sql
  anonymous_sql
  paren_sql
  neg_sql
  not_sql
  alias_sql
  aliases_sql
  attimezone_sql
  add_sql
  and_sql
  connector_sql
  bitwiseand_sql
  bitwiseleftshift_sql
  bitwisenot_sql
  bitwiseor_sql
  bitwiserightshift_sql
  bitwisexor_sql
  cast_sql
  currentdate_sql
  collate_sql
  command_sql
  commit_sql
  rollback_sql
  altercolumn_sql
  renametable_sql
  altertable_sql
  droppartition_sql
  addconstraint_sql
  distinct_sql
  ignorenulls_sql
  respectnulls_sql
  intdiv_sql
  dpipe_sql
  div_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  is_sql
  like_sql
  similarto_sql
  lt_sql
  lte_sql
  mod_sql
  mul_sql
  neq_sql
  nullsafeeq_sql
  nullsafeneq_sql
  or_sql
  slice_sql
  sub_sql
  trycast_sql
  use_sql
  binary
  function_fallback_sql
  format_args
  text_width
  format_time
  expressions
  op_expressions
  naked_property
  set_operation
  tag_sql
  token_sql
  userdefinedfunction_sql
  userdefinedfunctionkwarg_sql
  joinhint_sql
  kwarg_sql
  when_sql
  merge_sql