Edit on GitHub

# sqlglot.dialects.duckdb

View Source

      1from __future__ import annotations
      2
      3import typing as t
      4
      5from sqlglot import exp, generator, parser, tokens
      6from sqlglot.dialects.dialect import (
      7    Dialect,
      8    approx_count_distinct_sql,
      9    arrow_json_extract_scalar_sql,
     10    arrow_json_extract_sql,
     11    datestrtodate_sql,
     12    format_time_lambda,
     13    no_comment_column_constraint_sql,
     14    no_pivot_sql,
     15    no_properties_sql,
     16    no_safe_divide_sql,
     17    rename_func,
     18    str_position_sql,
     19    str_to_time_sql,
     20    timestamptrunc_sql,
     21    timestrtotime_sql,
     22    ts_or_ds_to_date_sql,
     23)
     24from sqlglot.helper import seq_get
     25from sqlglot.tokens import TokenType
     26
     27
     28def _ts_or_ds_add_sql(self: generator.Generator, expression: exp.TsOrDsAdd) -> str:
     29    this = self.sql(expression, "this")
     30    unit = self.sql(expression, "unit").strip("'") or "DAY"
     31    return f"CAST({this} AS DATE) + {self.sql(exp.Interval(this=expression.expression, unit=unit))}"
     32
     33
     34def _date_add_sql(self: generator.Generator, expression: exp.DateAdd) -> str:
     35    this = self.sql(expression, "this")
     36    unit = self.sql(expression, "unit").strip("'") or "DAY"
     37    return f"{this} + {self.sql(exp.Interval(this=expression.expression, unit=unit))}"
     38
     39
     40def _array_sort_sql(self: generator.Generator, expression: exp.ArraySort) -> str:
     41    if expression.expression:
     42        self.unsupported("DUCKDB ARRAY_SORT does not support a comparator")
     43    return f"ARRAY_SORT({self.sql(expression, 'this')})"
     44
     45
     46def _sort_array_sql(self: generator.Generator, expression: exp.SortArray) -> str:
     47    this = self.sql(expression, "this")
     48    if expression.args.get("asc") == exp.false():
     49        return f"ARRAY_REVERSE_SORT({this})"
     50    return f"ARRAY_SORT({this})"
     51
     52
     53def _sort_array_reverse(args: t.Sequence) -> exp.Expression:
     54    return exp.SortArray(this=seq_get(args, 0), asc=exp.false())
     55
     56
     57def _parse_date_diff(args: t.Sequence) -> exp.Expression:
     58    return exp.DateDiff(
     59        this=seq_get(args, 2),
     60        expression=seq_get(args, 1),
     61        unit=seq_get(args, 0),
     62    )
     63
     64
     65def _struct_sql(self: generator.Generator, expression: exp.Struct) -> str:
     66    args = [
     67        f"'{e.name or e.this.name}': {self.sql(e, 'expression')}" for e in expression.expressions
     68    ]
     69    return f"{{{', '.join(args)}}}"
     70
     71
     72def _datatype_sql(self: generator.Generator, expression: exp.DataType) -> str:
     73    if expression.this == exp.DataType.Type.ARRAY:
     74        return f"{self.expressions(expression, flat=True)}[]"
     75    return self.datatype_sql(expression)
     76
     77
     78def _regexp_extract_sql(self: generator.Generator, expression: exp.RegexpExtract) -> str:
     79    bad_args = list(filter(expression.args.get, ("position", "occurrence")))
     80    if bad_args:
     81        self.unsupported(f"REGEXP_EXTRACT does not support arg(s) {bad_args}")
     82
     83    return self.func(
     84        "REGEXP_EXTRACT",
     85        expression.args.get("this"),
     86        expression.args.get("expression"),
     87        expression.args.get("group"),
     88    )
     89
     90
     91class DuckDB(Dialect):
     92    class Tokenizer(tokens.Tokenizer):
     93        KEYWORDS = {
     94            **tokens.Tokenizer.KEYWORDS,
     95            "~": TokenType.RLIKE,
     96            ":=": TokenType.EQ,
     97            "ATTACH": TokenType.COMMAND,
     98            "BINARY": TokenType.VARBINARY,
     99            "BPCHAR": TokenType.TEXT,
    100            "BITSTRING": TokenType.BIT,
    101            "CHAR": TokenType.TEXT,
    102            "CHARACTER VARYING": TokenType.TEXT,
    103            "EXCLUDE": TokenType.EXCEPT,
    104            "INT1": TokenType.TINYINT,
    105            "LOGICAL": TokenType.BOOLEAN,
    106            "NUMERIC": TokenType.DOUBLE,
    107            "SIGNED": TokenType.INT,
    108            "STRING": TokenType.VARCHAR,
    109            "UBIGINT": TokenType.UBIGINT,
    110            "UINTEGER": TokenType.UINT,
    111            "USMALLINT": TokenType.USMALLINT,
    112            "UTINYINT": TokenType.UTINYINT,
    113        }
    114
    115    class Parser(parser.Parser):
    116        FUNCTIONS = {
    117            **parser.Parser.FUNCTIONS,  # type: ignore
    118            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
    119            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
    120            "ARRAY_SORT": exp.SortArray.from_arg_list,
    121            "ARRAY_REVERSE_SORT": _sort_array_reverse,
    122            "DATEDIFF": _parse_date_diff,
    123            "DATE_DIFF": _parse_date_diff,
    124            "EPOCH": exp.TimeToUnix.from_arg_list,
    125            "EPOCH_MS": lambda args: exp.UnixToTime(
    126                this=exp.Div(
    127                    this=seq_get(args, 0),
    128                    expression=exp.Literal.number(1000),
    129                )
    130            ),
    131            "LIST_REVERSE_SORT": _sort_array_reverse,
    132            "LIST_SORT": exp.SortArray.from_arg_list,
    133            "LIST_VALUE": exp.Array.from_arg_list,
    134            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
    135            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
    136            "STRING_SPLIT": exp.Split.from_arg_list,
    137            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    138            "STRING_TO_ARRAY": exp.Split.from_arg_list,
    139            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
    140            "STRUCT_PACK": exp.Struct.from_arg_list,
    141            "STR_SPLIT": exp.Split.from_arg_list,
    142            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    143            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
    144            "UNNEST": exp.Explode.from_arg_list,
    145        }
    146
    147        TYPE_TOKENS = {
    148            *parser.Parser.TYPE_TOKENS,
    149            TokenType.UBIGINT,
    150            TokenType.UINT,
    151            TokenType.USMALLINT,
    152            TokenType.UTINYINT,
    153        }
    154
    155    class Generator(generator.Generator):
    156        JOIN_HINTS = False
    157        TABLE_HINTS = False
    158        LIMIT_FETCH = "LIMIT"
    159        STRUCT_DELIMITER = ("(", ")")
    160
    161        TRANSFORMS = {
    162            **generator.Generator.TRANSFORMS,
    163            exp.ApproxDistinct: approx_count_distinct_sql,
    164            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    165            if isinstance(seq_get(e.expressions, 0), exp.Select)
    166            else rename_func("LIST_VALUE")(self, e),
    167            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    168            exp.ArraySort: _array_sort_sql,
    169            exp.ArraySum: rename_func("LIST_SUM"),
    170            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
    171            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
    172            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
    173            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
    174            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    175            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    176            exp.DayOfYear: rename_func("DAYOFYEAR"),
    177            exp.DataType: _datatype_sql,
    178            exp.DateAdd: _date_add_sql,
    179            exp.DateDiff: lambda self, e: self.func(
    180                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
    181            ),
    182            exp.DateStrToDate: datestrtodate_sql,
    183            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.dateint_format}) AS INT)",
    184            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.dateint_format}) AS DATE)",
    185            exp.Explode: rename_func("UNNEST"),
    186            exp.JSONExtract: arrow_json_extract_sql,
    187            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    188            exp.JSONBExtract: arrow_json_extract_sql,
    189            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    190            exp.LogicalOr: rename_func("BOOL_OR"),
    191            exp.LogicalAnd: rename_func("BOOL_AND"),
    192            exp.Pivot: no_pivot_sql,
    193            exp.Properties: no_properties_sql,
    194            exp.RegexpExtract: _regexp_extract_sql,
    195            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    196            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    197            exp.SafeDivide: no_safe_divide_sql,
    198            exp.Split: rename_func("STR_SPLIT"),
    199            exp.SortArray: _sort_array_sql,
    200            exp.StrPosition: str_position_sql,
    201            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    202            exp.StrToTime: str_to_time_sql,
    203            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    204            exp.Struct: _struct_sql,
    205            exp.TimestampTrunc: timestamptrunc_sql,
    206            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    207            exp.TimeStrToTime: timestrtotime_sql,
    208            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    209            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    210            exp.TimeToUnix: rename_func("EPOCH"),
    211            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    212            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    213            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    214            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    215            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    216            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    217            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    218        }
    219
    220        TYPE_MAPPING = {
    221            **generator.Generator.TYPE_MAPPING,
    222            exp.DataType.Type.BINARY: "BLOB",
    223            exp.DataType.Type.CHAR: "TEXT",
    224            exp.DataType.Type.FLOAT: "REAL",
    225            exp.DataType.Type.NCHAR: "TEXT",
    226            exp.DataType.Type.NVARCHAR: "TEXT",
    227            exp.DataType.Type.UINT: "UINTEGER",
    228            exp.DataType.Type.VARBINARY: "BLOB",
    229            exp.DataType.Type.VARCHAR: "TEXT",
    230        }
    231
    232        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
    233
    234        PROPERTIES_LOCATION = {
    235            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
    236            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    237        }
    238
    239        def tablesample_sql(
    240            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    241        ) -> str:
    242            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)

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

     92class DuckDB(Dialect):
     93    class Tokenizer(tokens.Tokenizer):
     94        KEYWORDS = {
     95            **tokens.Tokenizer.KEYWORDS,
     96            "~": TokenType.RLIKE,
     97            ":=": TokenType.EQ,
     98            "ATTACH": TokenType.COMMAND,
     99            "BINARY": TokenType.VARBINARY,
    100            "BPCHAR": TokenType.TEXT,
    101            "BITSTRING": TokenType.BIT,
    102            "CHAR": TokenType.TEXT,
    103            "CHARACTER VARYING": TokenType.TEXT,
    104            "EXCLUDE": TokenType.EXCEPT,
    105            "INT1": TokenType.TINYINT,
    106            "LOGICAL": TokenType.BOOLEAN,
    107            "NUMERIC": TokenType.DOUBLE,
    108            "SIGNED": TokenType.INT,
    109            "STRING": TokenType.VARCHAR,
    110            "UBIGINT": TokenType.UBIGINT,
    111            "UINTEGER": TokenType.UINT,
    112            "USMALLINT": TokenType.USMALLINT,
    113            "UTINYINT": TokenType.UTINYINT,
    114        }
    115
    116    class Parser(parser.Parser):
    117        FUNCTIONS = {
    118            **parser.Parser.FUNCTIONS,  # type: ignore
    119            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
    120            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
    121            "ARRAY_SORT": exp.SortArray.from_arg_list,
    122            "ARRAY_REVERSE_SORT": _sort_array_reverse,
    123            "DATEDIFF": _parse_date_diff,
    124            "DATE_DIFF": _parse_date_diff,
    125            "EPOCH": exp.TimeToUnix.from_arg_list,
    126            "EPOCH_MS": lambda args: exp.UnixToTime(
    127                this=exp.Div(
    128                    this=seq_get(args, 0),
    129                    expression=exp.Literal.number(1000),
    130                )
    131            ),
    132            "LIST_REVERSE_SORT": _sort_array_reverse,
    133            "LIST_SORT": exp.SortArray.from_arg_list,
    134            "LIST_VALUE": exp.Array.from_arg_list,
    135            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
    136            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
    137            "STRING_SPLIT": exp.Split.from_arg_list,
    138            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    139            "STRING_TO_ARRAY": exp.Split.from_arg_list,
    140            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
    141            "STRUCT_PACK": exp.Struct.from_arg_list,
    142            "STR_SPLIT": exp.Split.from_arg_list,
    143            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    144            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
    145            "UNNEST": exp.Explode.from_arg_list,
    146        }
    147
    148        TYPE_TOKENS = {
    149            *parser.Parser.TYPE_TOKENS,
    150            TokenType.UBIGINT,
    151            TokenType.UINT,
    152            TokenType.USMALLINT,
    153            TokenType.UTINYINT,
    154        }
    155
    156    class Generator(generator.Generator):
    157        JOIN_HINTS = False
    158        TABLE_HINTS = False
    159        LIMIT_FETCH = "LIMIT"
    160        STRUCT_DELIMITER = ("(", ")")
    161
    162        TRANSFORMS = {
    163            **generator.Generator.TRANSFORMS,
    164            exp.ApproxDistinct: approx_count_distinct_sql,
    165            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    166            if isinstance(seq_get(e.expressions, 0), exp.Select)
    167            else rename_func("LIST_VALUE")(self, e),
    168            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    169            exp.ArraySort: _array_sort_sql,
    170            exp.ArraySum: rename_func("LIST_SUM"),
    171            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
    172            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
    173            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
    174            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
    175            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    176            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    177            exp.DayOfYear: rename_func("DAYOFYEAR"),
    178            exp.DataType: _datatype_sql,
    179            exp.DateAdd: _date_add_sql,
    180            exp.DateDiff: lambda self, e: self.func(
    181                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
    182            ),
    183            exp.DateStrToDate: datestrtodate_sql,
    184            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.dateint_format}) AS INT)",
    185            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.dateint_format}) AS DATE)",
    186            exp.Explode: rename_func("UNNEST"),
    187            exp.JSONExtract: arrow_json_extract_sql,
    188            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    189            exp.JSONBExtract: arrow_json_extract_sql,
    190            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    191            exp.LogicalOr: rename_func("BOOL_OR"),
    192            exp.LogicalAnd: rename_func("BOOL_AND"),
    193            exp.Pivot: no_pivot_sql,
    194            exp.Properties: no_properties_sql,
    195            exp.RegexpExtract: _regexp_extract_sql,
    196            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    197            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    198            exp.SafeDivide: no_safe_divide_sql,
    199            exp.Split: rename_func("STR_SPLIT"),
    200            exp.SortArray: _sort_array_sql,
    201            exp.StrPosition: str_position_sql,
    202            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    203            exp.StrToTime: str_to_time_sql,
    204            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    205            exp.Struct: _struct_sql,
    206            exp.TimestampTrunc: timestamptrunc_sql,
    207            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    208            exp.TimeStrToTime: timestrtotime_sql,
    209            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    210            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    211            exp.TimeToUnix: rename_func("EPOCH"),
    212            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    213            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    214            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    215            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    216            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    217            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    218            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    219        }
    220
    221        TYPE_MAPPING = {
    222            **generator.Generator.TYPE_MAPPING,
    223            exp.DataType.Type.BINARY: "BLOB",
    224            exp.DataType.Type.CHAR: "TEXT",
    225            exp.DataType.Type.FLOAT: "REAL",
    226            exp.DataType.Type.NCHAR: "TEXT",
    227            exp.DataType.Type.NVARCHAR: "TEXT",
    228            exp.DataType.Type.UINT: "UINTEGER",
    229            exp.DataType.Type.VARBINARY: "BLOB",
    230            exp.DataType.Type.VARCHAR: "TEXT",
    231        }
    232
    233        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
    234
    235        PROPERTIES_LOCATION = {
    236            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
    237            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    238        }
    239
    240        def tablesample_sql(
    241            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    242        ) -> str:
    243            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)

##### Inherited Members

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

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

     93    class Tokenizer(tokens.Tokenizer):
     94        KEYWORDS = {
     95            **tokens.Tokenizer.KEYWORDS,
     96            "~": TokenType.RLIKE,
     97            ":=": TokenType.EQ,
     98            "ATTACH": TokenType.COMMAND,
     99            "BINARY": TokenType.VARBINARY,
    100            "BPCHAR": TokenType.TEXT,
    101            "BITSTRING": TokenType.BIT,
    102            "CHAR": TokenType.TEXT,
    103            "CHARACTER VARYING": TokenType.TEXT,
    104            "EXCLUDE": TokenType.EXCEPT,
    105            "INT1": TokenType.TINYINT,
    106            "LOGICAL": TokenType.BOOLEAN,
    107            "NUMERIC": TokenType.DOUBLE,
    108            "SIGNED": TokenType.INT,
    109            "STRING": TokenType.VARCHAR,
    110            "UBIGINT": TokenType.UBIGINT,
    111            "UINTEGER": TokenType.UINT,
    112            "USMALLINT": TokenType.USMALLINT,
    113            "UTINYINT": TokenType.UTINYINT,
    114        }

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize

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

    116    class Parser(parser.Parser):
    117        FUNCTIONS = {
    118            **parser.Parser.FUNCTIONS,  # type: ignore
    119            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
    120            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
    121            "ARRAY_SORT": exp.SortArray.from_arg_list,
    122            "ARRAY_REVERSE_SORT": _sort_array_reverse,
    123            "DATEDIFF": _parse_date_diff,
    124            "DATE_DIFF": _parse_date_diff,
    125            "EPOCH": exp.TimeToUnix.from_arg_list,
    126            "EPOCH_MS": lambda args: exp.UnixToTime(
    127                this=exp.Div(
    128                    this=seq_get(args, 0),
    129                    expression=exp.Literal.number(1000),
    130                )
    131            ),
    132            "LIST_REVERSE_SORT": _sort_array_reverse,
    133            "LIST_SORT": exp.SortArray.from_arg_list,
    134            "LIST_VALUE": exp.Array.from_arg_list,
    135            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
    136            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
    137            "STRING_SPLIT": exp.Split.from_arg_list,
    138            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    139            "STRING_TO_ARRAY": exp.Split.from_arg_list,
    140            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
    141            "STRUCT_PACK": exp.Struct.from_arg_list,
    142            "STR_SPLIT": exp.Split.from_arg_list,
    143            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    144            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
    145            "UNNEST": exp.Explode.from_arg_list,
    146        }
    147
    148        TYPE_TOKENS = {
    149            *parser.Parser.TYPE_TOKENS,
    150            TokenType.UBIGINT,
    151            TokenType.UINT,
    152            TokenType.USMALLINT,
    153            TokenType.UTINYINT,
    154        }

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

    156    class Generator(generator.Generator):
    157        JOIN_HINTS = False
    158        TABLE_HINTS = False
    159        LIMIT_FETCH = "LIMIT"
    160        STRUCT_DELIMITER = ("(", ")")
    161
    162        TRANSFORMS = {
    163            **generator.Generator.TRANSFORMS,
    164            exp.ApproxDistinct: approx_count_distinct_sql,
    165            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    166            if isinstance(seq_get(e.expressions, 0), exp.Select)
    167            else rename_func("LIST_VALUE")(self, e),
    168            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    169            exp.ArraySort: _array_sort_sql,
    170            exp.ArraySum: rename_func("LIST_SUM"),
    171            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
    172            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
    173            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
    174            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
    175            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    176            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    177            exp.DayOfYear: rename_func("DAYOFYEAR"),
    178            exp.DataType: _datatype_sql,
    179            exp.DateAdd: _date_add_sql,
    180            exp.DateDiff: lambda self, e: self.func(
    181                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
    182            ),
    183            exp.DateStrToDate: datestrtodate_sql,
    184            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.dateint_format}) AS INT)",
    185            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.dateint_format}) AS DATE)",
    186            exp.Explode: rename_func("UNNEST"),
    187            exp.JSONExtract: arrow_json_extract_sql,
    188            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    189            exp.JSONBExtract: arrow_json_extract_sql,
    190            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    191            exp.LogicalOr: rename_func("BOOL_OR"),
    192            exp.LogicalAnd: rename_func("BOOL_AND"),
    193            exp.Pivot: no_pivot_sql,
    194            exp.Properties: no_properties_sql,
    195            exp.RegexpExtract: _regexp_extract_sql,
    196            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    197            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    198            exp.SafeDivide: no_safe_divide_sql,
    199            exp.Split: rename_func("STR_SPLIT"),
    200            exp.SortArray: _sort_array_sql,
    201            exp.StrPosition: str_position_sql,
    202            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    203            exp.StrToTime: str_to_time_sql,
    204            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    205            exp.Struct: _struct_sql,
    206            exp.TimestampTrunc: timestamptrunc_sql,
    207            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    208            exp.TimeStrToTime: timestrtotime_sql,
    209            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    210            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    211            exp.TimeToUnix: rename_func("EPOCH"),
    212            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    213            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    214            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    215            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    216            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    217            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    218            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    219        }
    220
    221        TYPE_MAPPING = {
    222            **generator.Generator.TYPE_MAPPING,
    223            exp.DataType.Type.BINARY: "BLOB",
    224            exp.DataType.Type.CHAR: "TEXT",
    225            exp.DataType.Type.FLOAT: "REAL",
    226            exp.DataType.Type.NCHAR: "TEXT",
    227            exp.DataType.Type.NVARCHAR: "TEXT",
    228            exp.DataType.Type.UINT: "UINTEGER",
    229            exp.DataType.Type.VARBINARY: "BLOB",
    230            exp.DataType.Type.VARCHAR: "TEXT",
    231        }
    232
    233        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
    234
    235        PROPERTIES_LOCATION = {
    236            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
    237            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    238        }
    239
    240        def tablesample_sql(
    241            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    242        ) -> str:
    243            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)

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 | str): 'always': always quote, 'safe': quote identifiers if they don't contain an upcase, True defaults to always.
- 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', sep=' AS ') -> str: View Source

    240        def tablesample_sql(
    241            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    242        ) -> str:
    243            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)

##### 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
  columnposition_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
  bytestring_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  except_op
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  inputoutputformat_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
  onconflict_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
  setitem_sql
  set_sql
  pragma_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
  windowspec_sql
  withingroup_sql
  between_sql
  bracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  nextvaluefor_sql
  extract_sql
  trim_sql
  concat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  unique_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_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
  overlaps_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  ilikeany_sql
  is_sql
  like_sql
  likeany_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
  tochar_sql