sqlglot.dialects.duckdb API documentation

Edit on GitHub

# sqlglot.dialects.duckdb

View Source

      1from __future__ import annotations
      2
      3from sqlglot import exp, generator, parser, tokens
      4from sqlglot.dialects.dialect import (
      5    Dialect,
      6    approx_count_distinct_sql,
      7    arrow_json_extract_scalar_sql,
      8    arrow_json_extract_sql,
      9    datestrtodate_sql,
     10    format_time_lambda,
     11    no_pivot_sql,
     12    no_properties_sql,
     13    no_safe_divide_sql,
     14    rename_func,
     15    str_position_sql,
     16    str_to_time_sql,
     17    timestrtotime_sql,
     18    ts_or_ds_to_date_sql,
     19)
     20from sqlglot.helper import seq_get
     21from sqlglot.tokens import TokenType
     22
     23
     24def _ts_or_ds_add(self, expression):
     25    this = expression.args.get("this")
     26    unit = self.sql(expression, "unit").strip("'") or "DAY"
     27    return f"CAST({this} AS DATE) + {self.sql(exp.Interval(this=expression.expression, unit=unit))}"
     28
     29
     30def _date_add(self, expression):
     31    this = self.sql(expression, "this")
     32    unit = self.sql(expression, "unit").strip("'") or "DAY"
     33    return f"{this} + {self.sql(exp.Interval(this=expression.expression, unit=unit))}"
     34
     35
     36def _array_sort_sql(self, expression):
     37    if expression.expression:
     38        self.unsupported("DUCKDB ARRAY_SORT does not support a comparator")
     39    return f"ARRAY_SORT({self.sql(expression, 'this')})"
     40
     41
     42def _sort_array_sql(self, expression):
     43    this = self.sql(expression, "this")
     44    if expression.args.get("asc") == exp.false():
     45        return f"ARRAY_REVERSE_SORT({this})"
     46    return f"ARRAY_SORT({this})"
     47
     48
     49def _sort_array_reverse(args):
     50    return exp.SortArray(this=seq_get(args, 0), asc=exp.false())
     51
     52
     53def _struct_sql(self, expression):
     54    args = [
     55        f"'{e.name or e.this.name}': {self.sql(e, 'expression')}" for e in expression.expressions
     56    ]
     57    return f"{{{', '.join(args)}}}"
     58
     59
     60def _datatype_sql(self, expression):
     61    if expression.this == exp.DataType.Type.ARRAY:
     62        return f"{self.expressions(expression, flat=True)}[]"
     63    return self.datatype_sql(expression)
     64
     65
     66def _regexp_extract_sql(self, expression):
     67    bad_args = list(filter(expression.args.get, ("position", "occurrence")))
     68    if bad_args:
     69        self.unsupported(f"REGEXP_EXTRACT does not support arg(s) {bad_args}")
     70    return self.func(
     71        "REGEXP_EXTRACT",
     72        expression.args.get("this"),
     73        expression.args.get("expression"),
     74        expression.args.get("group"),
     75    )
     76
     77
     78class DuckDB(Dialect):
     79    class Tokenizer(tokens.Tokenizer):
     80        KEYWORDS = {
     81            **tokens.Tokenizer.KEYWORDS,
     82            ":=": TokenType.EQ,
     83            "ATTACH": TokenType.COMMAND,
     84            "CHARACTER VARYING": TokenType.VARCHAR,
     85            "EXCLUDE": TokenType.EXCEPT,
     86        }
     87
     88    class Parser(parser.Parser):
     89        FUNCTIONS = {
     90            **parser.Parser.FUNCTIONS,  # type: ignore
     91            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
     92            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
     93            "ARRAY_SORT": exp.SortArray.from_arg_list,
     94            "ARRAY_REVERSE_SORT": _sort_array_reverse,
     95            "EPOCH": exp.TimeToUnix.from_arg_list,
     96            "EPOCH_MS": lambda args: exp.UnixToTime(
     97                this=exp.Div(
     98                    this=seq_get(args, 0),
     99                    expression=exp.Literal.number(1000),
    100                )
    101            ),
    102            "LIST_SORT": exp.SortArray.from_arg_list,
    103            "LIST_REVERSE_SORT": _sort_array_reverse,
    104            "LIST_VALUE": exp.Array.from_arg_list,
    105            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
    106            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
    107            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
    108            "STR_SPLIT": exp.Split.from_arg_list,
    109            "STRING_SPLIT": exp.Split.from_arg_list,
    110            "STRING_TO_ARRAY": exp.Split.from_arg_list,
    111            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    112            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    113            "STRUCT_PACK": exp.Struct.from_arg_list,
    114            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
    115            "UNNEST": exp.Explode.from_arg_list,
    116        }
    117
    118    class Generator(generator.Generator):
    119        STRUCT_DELIMITER = ("(", ")")
    120
    121        TRANSFORMS = {
    122            **generator.Generator.TRANSFORMS,  # type: ignore
    123            exp.ApproxDistinct: approx_count_distinct_sql,
    124            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    125            if isinstance(seq_get(e.expressions, 0), exp.Select)
    126            else rename_func("LIST_VALUE")(self, e),
    127            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    128            exp.ArraySort: _array_sort_sql,
    129            exp.ArraySum: rename_func("LIST_SUM"),
    130            exp.DataType: _datatype_sql,
    131            exp.DateAdd: _date_add,
    132            exp.DateDiff: lambda self, e: self.func(
    133                "DATE_DIFF", e.args.get("unit") or exp.Literal.string("day"), e.expression, e.this
    134            ),
    135            exp.DateStrToDate: datestrtodate_sql,
    136            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.dateint_format}) AS INT)",
    137            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.dateint_format}) AS DATE)",
    138            exp.Explode: rename_func("UNNEST"),
    139            exp.JSONExtract: arrow_json_extract_sql,
    140            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    141            exp.JSONBExtract: arrow_json_extract_sql,
    142            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    143            exp.LogicalOr: rename_func("BOOL_OR"),
    144            exp.Pivot: no_pivot_sql,
    145            exp.Properties: no_properties_sql,
    146            exp.RegexpExtract: _regexp_extract_sql,
    147            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    148            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    149            exp.SafeDivide: no_safe_divide_sql,
    150            exp.Split: rename_func("STR_SPLIT"),
    151            exp.SortArray: _sort_array_sql,
    152            exp.StrPosition: str_position_sql,
    153            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    154            exp.StrToTime: str_to_time_sql,
    155            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    156            exp.Struct: _struct_sql,
    157            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    158            exp.TimeStrToTime: timestrtotime_sql,
    159            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    160            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    161            exp.TimeToUnix: rename_func("EPOCH"),
    162            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    163            exp.TsOrDsAdd: _ts_or_ds_add,
    164            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    165            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    166            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    167            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    168        }
    169
    170        TYPE_MAPPING = {
    171            **generator.Generator.TYPE_MAPPING,  # type: ignore
    172            exp.DataType.Type.VARCHAR: "TEXT",
    173            exp.DataType.Type.NVARCHAR: "TEXT",
    174        }
    175
    176        STAR_MAPPING = {
    177            **generator.Generator.STAR_MAPPING,
    178            "except": "EXCLUDE",
    179        }
    180
    181        def tablesample_sql(self, expression: exp.TableSample, seed_prefix: str = "SEED") -> str:
    182            return super().tablesample_sql(expression, seed_prefix="REPEATABLE")

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

     79class DuckDB(Dialect):
     80    class Tokenizer(tokens.Tokenizer):
     81        KEYWORDS = {
     82            **tokens.Tokenizer.KEYWORDS,
     83            ":=": TokenType.EQ,
     84            "ATTACH": TokenType.COMMAND,
     85            "CHARACTER VARYING": TokenType.VARCHAR,
     86            "EXCLUDE": TokenType.EXCEPT,
     87        }
     88
     89    class Parser(parser.Parser):
     90        FUNCTIONS = {
     91            **parser.Parser.FUNCTIONS,  # type: ignore
     92            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
     93            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
     94            "ARRAY_SORT": exp.SortArray.from_arg_list,
     95            "ARRAY_REVERSE_SORT": _sort_array_reverse,
     96            "EPOCH": exp.TimeToUnix.from_arg_list,
     97            "EPOCH_MS": lambda args: exp.UnixToTime(
     98                this=exp.Div(
     99                    this=seq_get(args, 0),
    100                    expression=exp.Literal.number(1000),
    101                )
    102            ),
    103            "LIST_SORT": exp.SortArray.from_arg_list,
    104            "LIST_REVERSE_SORT": _sort_array_reverse,
    105            "LIST_VALUE": exp.Array.from_arg_list,
    106            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
    107            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
    108            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
    109            "STR_SPLIT": exp.Split.from_arg_list,
    110            "STRING_SPLIT": exp.Split.from_arg_list,
    111            "STRING_TO_ARRAY": exp.Split.from_arg_list,
    112            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    113            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    114            "STRUCT_PACK": exp.Struct.from_arg_list,
    115            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
    116            "UNNEST": exp.Explode.from_arg_list,
    117        }
    118
    119    class Generator(generator.Generator):
    120        STRUCT_DELIMITER = ("(", ")")
    121
    122        TRANSFORMS = {
    123            **generator.Generator.TRANSFORMS,  # type: ignore
    124            exp.ApproxDistinct: approx_count_distinct_sql,
    125            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    126            if isinstance(seq_get(e.expressions, 0), exp.Select)
    127            else rename_func("LIST_VALUE")(self, e),
    128            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    129            exp.ArraySort: _array_sort_sql,
    130            exp.ArraySum: rename_func("LIST_SUM"),
    131            exp.DataType: _datatype_sql,
    132            exp.DateAdd: _date_add,
    133            exp.DateDiff: lambda self, e: self.func(
    134                "DATE_DIFF", e.args.get("unit") or exp.Literal.string("day"), e.expression, e.this
    135            ),
    136            exp.DateStrToDate: datestrtodate_sql,
    137            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.dateint_format}) AS INT)",
    138            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.dateint_format}) AS DATE)",
    139            exp.Explode: rename_func("UNNEST"),
    140            exp.JSONExtract: arrow_json_extract_sql,
    141            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    142            exp.JSONBExtract: arrow_json_extract_sql,
    143            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    144            exp.LogicalOr: rename_func("BOOL_OR"),
    145            exp.Pivot: no_pivot_sql,
    146            exp.Properties: no_properties_sql,
    147            exp.RegexpExtract: _regexp_extract_sql,
    148            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    149            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    150            exp.SafeDivide: no_safe_divide_sql,
    151            exp.Split: rename_func("STR_SPLIT"),
    152            exp.SortArray: _sort_array_sql,
    153            exp.StrPosition: str_position_sql,
    154            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    155            exp.StrToTime: str_to_time_sql,
    156            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    157            exp.Struct: _struct_sql,
    158            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    159            exp.TimeStrToTime: timestrtotime_sql,
    160            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    161            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    162            exp.TimeToUnix: rename_func("EPOCH"),
    163            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    164            exp.TsOrDsAdd: _ts_or_ds_add,
    165            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    166            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    167            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    168            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    169        }
    170
    171        TYPE_MAPPING = {
    172            **generator.Generator.TYPE_MAPPING,  # type: ignore
    173            exp.DataType.Type.VARCHAR: "TEXT",
    174            exp.DataType.Type.NVARCHAR: "TEXT",
    175        }
    176
    177        STAR_MAPPING = {
    178            **generator.Generator.STAR_MAPPING,
    179            "except": "EXCLUDE",
    180        }
    181
    182        def tablesample_sql(self, expression: exp.TableSample, seed_prefix: str = "SEED") -> str:
    183            return super().tablesample_sql(expression, seed_prefix="REPEATABLE")

##### Inherited Members

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

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

    80    class Tokenizer(tokens.Tokenizer):
    81        KEYWORDS = {
    82            **tokens.Tokenizer.KEYWORDS,
    83            ":=": TokenType.EQ,
    84            "ATTACH": TokenType.COMMAND,
    85            "CHARACTER VARYING": TokenType.VARCHAR,
    86            "EXCLUDE": TokenType.EXCEPT,
    87        }

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize

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

     89    class Parser(parser.Parser):
     90        FUNCTIONS = {
     91            **parser.Parser.FUNCTIONS,  # type: ignore
     92            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
     93            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
     94            "ARRAY_SORT": exp.SortArray.from_arg_list,
     95            "ARRAY_REVERSE_SORT": _sort_array_reverse,
     96            "EPOCH": exp.TimeToUnix.from_arg_list,
     97            "EPOCH_MS": lambda args: exp.UnixToTime(
     98                this=exp.Div(
     99                    this=seq_get(args, 0),
    100                    expression=exp.Literal.number(1000),
    101                )
    102            ),
    103            "LIST_SORT": exp.SortArray.from_arg_list,
    104            "LIST_REVERSE_SORT": _sort_array_reverse,
    105            "LIST_VALUE": exp.Array.from_arg_list,
    106            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
    107            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
    108            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
    109            "STR_SPLIT": exp.Split.from_arg_list,
    110            "STRING_SPLIT": exp.Split.from_arg_list,
    111            "STRING_TO_ARRAY": exp.Split.from_arg_list,
    112            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    113            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    114            "STRUCT_PACK": exp.Struct.from_arg_list,
    115            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
    116            "UNNEST": exp.Explode.from_arg_list,
    117        }

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 DuckDB.Generator(sqlglot.generator.Generator): View Source

    119    class Generator(generator.Generator):
    120        STRUCT_DELIMITER = ("(", ")")
    121
    122        TRANSFORMS = {
    123            **generator.Generator.TRANSFORMS,  # type: ignore
    124            exp.ApproxDistinct: approx_count_distinct_sql,
    125            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    126            if isinstance(seq_get(e.expressions, 0), exp.Select)
    127            else rename_func("LIST_VALUE")(self, e),
    128            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    129            exp.ArraySort: _array_sort_sql,
    130            exp.ArraySum: rename_func("LIST_SUM"),
    131            exp.DataType: _datatype_sql,
    132            exp.DateAdd: _date_add,
    133            exp.DateDiff: lambda self, e: self.func(
    134                "DATE_DIFF", e.args.get("unit") or exp.Literal.string("day"), e.expression, e.this
    135            ),
    136            exp.DateStrToDate: datestrtodate_sql,
    137            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.dateint_format}) AS INT)",
    138            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.dateint_format}) AS DATE)",
    139            exp.Explode: rename_func("UNNEST"),
    140            exp.JSONExtract: arrow_json_extract_sql,
    141            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    142            exp.JSONBExtract: arrow_json_extract_sql,
    143            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    144            exp.LogicalOr: rename_func("BOOL_OR"),
    145            exp.Pivot: no_pivot_sql,
    146            exp.Properties: no_properties_sql,
    147            exp.RegexpExtract: _regexp_extract_sql,
    148            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    149            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    150            exp.SafeDivide: no_safe_divide_sql,
    151            exp.Split: rename_func("STR_SPLIT"),
    152            exp.SortArray: _sort_array_sql,
    153            exp.StrPosition: str_position_sql,
    154            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    155            exp.StrToTime: str_to_time_sql,
    156            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    157            exp.Struct: _struct_sql,
    158            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    159            exp.TimeStrToTime: timestrtotime_sql,
    160            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    161            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    162            exp.TimeToUnix: rename_func("EPOCH"),
    163            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    164            exp.TsOrDsAdd: _ts_or_ds_add,
    165            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    166            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    167            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    168            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    169        }
    170
    171        TYPE_MAPPING = {
    172            **generator.Generator.TYPE_MAPPING,  # type: ignore
    173            exp.DataType.Type.VARCHAR: "TEXT",
    174            exp.DataType.Type.NVARCHAR: "TEXT",
    175        }
    176
    177        STAR_MAPPING = {
    178            **generator.Generator.STAR_MAPPING,
    179            "except": "EXCLUDE",
    180        }
    181
    182        def tablesample_sql(self, expression: exp.TableSample, seed_prefix: str = "SEED") -> str:
    183            return super().tablesample_sql(expression, seed_prefix="REPEATABLE")

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 tablesample_sql( self, expression: sqlglot.expressions.TableSample, seed_prefix: str = 'SEED') -> str: View Source

    182        def tablesample_sql(self, expression: exp.TableSample, seed_prefix: str = "SEED") -> str:
    183            return super().tablesample_sql(expression, seed_prefix="REPEATABLE")

##### 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
  compresscolumnconstraint_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
  lockingproperty_sql
  withdataproperty_sql
  insert_sql
  intersect_sql
  intersect_op
  introducer_sql
  pseudotype_sql
  returning_sql
  rowformatdelimitedproperty_sql
  table_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
  comment_sql
  transaction_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
  floatdiv_sql
  overlaps_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
  func
  format_args
  text_width
  format_time
  expressions
  op_expressions
  naked_property
  set_operation
  tag_sql
  token_sql
  userdefinedfunction_sql
  joinhint_sql
  kwarg_sql
  when_sql
  merge_sql