sqlglot.dialects.duckdb API documentation

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_properties_sql,
     15    no_safe_divide_sql,
     16    pivot_column_names,
     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_delta_sql(self: generator.Generator, expression: exp.DateAdd | exp.DateSub) -> str:
     35    this = self.sql(expression, "this")
     36    unit = self.sql(expression, "unit").strip("'") or "DAY"
     37    op = "+" if isinstance(expression, exp.DateAdd) else "-"
     38    return f"{this} {op} {self.sql(exp.Interval(this=expression.expression, unit=unit))}"
     39
     40
     41def _array_sort_sql(self: generator.Generator, expression: exp.ArraySort) -> str:
     42    if expression.expression:
     43        self.unsupported("DUCKDB ARRAY_SORT does not support a comparator")
     44    return f"ARRAY_SORT({self.sql(expression, 'this')})"
     45
     46
     47def _sort_array_sql(self: generator.Generator, expression: exp.SortArray) -> str:
     48    this = self.sql(expression, "this")
     49    if expression.args.get("asc") == exp.false():
     50        return f"ARRAY_REVERSE_SORT({this})"
     51    return f"ARRAY_SORT({this})"
     52
     53
     54def _sort_array_reverse(args: t.List) -> exp.Expression:
     55    return exp.SortArray(this=seq_get(args, 0), asc=exp.false())
     56
     57
     58def _parse_date_diff(args: t.List) -> exp.Expression:
     59    return exp.DateDiff(this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0))
     60
     61
     62def _struct_sql(self: generator.Generator, expression: exp.Struct) -> str:
     63    args = [
     64        f"'{e.name or e.this.name}': {self.sql(e, 'expression')}" for e in expression.expressions
     65    ]
     66    return f"{{{', '.join(args)}}}"
     67
     68
     69def _datatype_sql(self: generator.Generator, expression: exp.DataType) -> str:
     70    if expression.is_type("array"):
     71        return f"{self.expressions(expression, flat=True)}[]"
     72    return self.datatype_sql(expression)
     73
     74
     75def _regexp_extract_sql(self: generator.Generator, expression: exp.RegexpExtract) -> str:
     76    bad_args = list(filter(expression.args.get, ("position", "occurrence")))
     77    if bad_args:
     78        self.unsupported(f"REGEXP_EXTRACT does not support arg(s) {bad_args}")
     79
     80    return self.func(
     81        "REGEXP_EXTRACT",
     82        expression.args.get("this"),
     83        expression.args.get("expression"),
     84        expression.args.get("group"),
     85    )
     86
     87
     88class DuckDB(Dialect):
     89    NULL_ORDERING = "nulls_are_last"
     90
     91    class Tokenizer(tokens.Tokenizer):
     92        KEYWORDS = {
     93            **tokens.Tokenizer.KEYWORDS,
     94            "~": TokenType.RLIKE,
     95            ":=": TokenType.EQ,
     96            "//": TokenType.DIV,
     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            "PIVOT_WIDER": TokenType.PIVOT,
    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        CONCAT_NULL_OUTPUTS_STRING = True
    118
    119        FUNCTIONS = {
    120            **parser.Parser.FUNCTIONS,
    121            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
    122            "ARRAY_SORT": exp.SortArray.from_arg_list,
    123            "ARRAY_REVERSE_SORT": _sort_array_reverse,
    124            "DATEDIFF": _parse_date_diff,
    125            "DATE_DIFF": _parse_date_diff,
    126            "EPOCH": exp.TimeToUnix.from_arg_list,
    127            "EPOCH_MS": lambda args: exp.UnixToTime(
    128                this=exp.Div(this=seq_get(args, 0), expression=exp.Literal.number(1000))
    129            ),
    130            "LIST_REVERSE_SORT": _sort_array_reverse,
    131            "LIST_SORT": exp.SortArray.from_arg_list,
    132            "LIST_VALUE": exp.Array.from_arg_list,
    133            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
    134            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
    135            "STRING_SPLIT": exp.Split.from_arg_list,
    136            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    137            "STRING_TO_ARRAY": exp.Split.from_arg_list,
    138            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
    139            "STRUCT_PACK": exp.Struct.from_arg_list,
    140            "STR_SPLIT": exp.Split.from_arg_list,
    141            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
    142            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
    143            "UNNEST": exp.Explode.from_arg_list,
    144        }
    145
    146        TYPE_TOKENS = {
    147            *parser.Parser.TYPE_TOKENS,
    148            TokenType.UBIGINT,
    149            TokenType.UINT,
    150            TokenType.USMALLINT,
    151            TokenType.UTINYINT,
    152        }
    153
    154        def _pivot_column_names(self, aggregations: t.List[exp.Expression]) -> t.List[str]:
    155            if len(aggregations) == 1:
    156                return super()._pivot_column_names(aggregations)
    157            return pivot_column_names(aggregations, dialect="duckdb")
    158
    159    class Generator(generator.Generator):
    160        JOIN_HINTS = False
    161        TABLE_HINTS = False
    162        LIMIT_FETCH = "LIMIT"
    163        STRUCT_DELIMITER = ("(", ")")
    164        RENAME_TABLE_WITH_DB = False
    165
    166        TRANSFORMS = {
    167            **generator.Generator.TRANSFORMS,
    168            exp.ApproxDistinct: approx_count_distinct_sql,
    169            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    170            if isinstance(seq_get(e.expressions, 0), exp.Select)
    171            else rename_func("LIST_VALUE")(self, e),
    172            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    173            exp.ArraySort: _array_sort_sql,
    174            exp.ArraySum: rename_func("LIST_SUM"),
    175            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
    176            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
    177            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
    178            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
    179            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    180            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    181            exp.DayOfYear: rename_func("DAYOFYEAR"),
    182            exp.DataType: _datatype_sql,
    183            exp.DateAdd: _date_delta_sql,
    184            exp.DateSub: _date_delta_sql,
    185            exp.DateDiff: lambda self, e: self.func(
    186                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
    187            ),
    188            exp.DateStrToDate: datestrtodate_sql,
    189            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
    190            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
    191            exp.Explode: rename_func("UNNEST"),
    192            exp.IntDiv: lambda self, e: self.binary(e, "//"),
    193            exp.JSONExtract: arrow_json_extract_sql,
    194            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    195            exp.JSONBExtract: arrow_json_extract_sql,
    196            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    197            exp.LogicalOr: rename_func("BOOL_OR"),
    198            exp.LogicalAnd: rename_func("BOOL_AND"),
    199            exp.Properties: no_properties_sql,
    200            exp.RegexpExtract: _regexp_extract_sql,
    201            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    202            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    203            exp.SafeDivide: no_safe_divide_sql,
    204            exp.Split: rename_func("STR_SPLIT"),
    205            exp.SortArray: _sort_array_sql,
    206            exp.StrPosition: str_position_sql,
    207            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    208            exp.StrToTime: str_to_time_sql,
    209            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    210            exp.Struct: _struct_sql,
    211            exp.TimestampTrunc: timestamptrunc_sql,
    212            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    213            exp.TimeStrToTime: timestrtotime_sql,
    214            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    215            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    216            exp.TimeToUnix: rename_func("EPOCH"),
    217            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    218            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    219            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    220            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    221            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    222            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    223            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    224        }
    225
    226        TYPE_MAPPING = {
    227            **generator.Generator.TYPE_MAPPING,
    228            exp.DataType.Type.BINARY: "BLOB",
    229            exp.DataType.Type.CHAR: "TEXT",
    230            exp.DataType.Type.FLOAT: "REAL",
    231            exp.DataType.Type.NCHAR: "TEXT",
    232            exp.DataType.Type.NVARCHAR: "TEXT",
    233            exp.DataType.Type.UINT: "UINTEGER",
    234            exp.DataType.Type.VARBINARY: "BLOB",
    235            exp.DataType.Type.VARCHAR: "TEXT",
    236        }
    237
    238        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
    239
    240        UNWRAPPED_INTERVAL_VALUES = (exp.Column, exp.Literal, exp.Paren)
    241
    242        PROPERTIES_LOCATION = {
    243            **generator.Generator.PROPERTIES_LOCATION,
    244            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    245        }
    246
    247        def interval_sql(self, expression: exp.Interval) -> str:
    248            multiplier: t.Optional[int] = None
    249            unit = expression.text("unit").lower()
    250
    251            if unit.startswith("week"):
    252                multiplier = 7
    253            if unit.startswith("quarter"):
    254                multiplier = 90
    255
    256            if multiplier:
    257                return f"({multiplier} * {super().interval_sql(exp.Interval(this=expression.this, unit=exp.var('day')))})"
    258
    259            return super().interval_sql(expression)
    260
    261        def tablesample_sql(
    262            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep: str = " AS "
    263        ) -> str:
    264            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)

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

     89class DuckDB(Dialect):
     90    NULL_ORDERING = "nulls_are_last"
     91
     92    class Tokenizer(tokens.Tokenizer):
     93        KEYWORDS = {
     94            **tokens.Tokenizer.KEYWORDS,
     95            "~": TokenType.RLIKE,
     96            ":=": TokenType.EQ,
     97            "//": TokenType.DIV,
     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            "PIVOT_WIDER": TokenType.PIVOT,
    109            "SIGNED": TokenType.INT,
    110            "STRING": TokenType.VARCHAR,
    111            "UBIGINT": TokenType.UBIGINT,
    112            "UINTEGER": TokenType.UINT,
    113            "USMALLINT": TokenType.USMALLINT,
    114            "UTINYINT": TokenType.UTINYINT,
    115        }
    116
    117    class Parser(parser.Parser):
    118        CONCAT_NULL_OUTPUTS_STRING = True
    119
    120        FUNCTIONS = {
    121            **parser.Parser.FUNCTIONS,
    122            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
    123            "ARRAY_SORT": exp.SortArray.from_arg_list,
    124            "ARRAY_REVERSE_SORT": _sort_array_reverse,
    125            "DATEDIFF": _parse_date_diff,
    126            "DATE_DIFF": _parse_date_diff,
    127            "EPOCH": exp.TimeToUnix.from_arg_list,
    128            "EPOCH_MS": lambda args: exp.UnixToTime(
    129                this=exp.Div(this=seq_get(args, 0), expression=exp.Literal.number(1000))
    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        def _pivot_column_names(self, aggregations: t.List[exp.Expression]) -> t.List[str]:
    156            if len(aggregations) == 1:
    157                return super()._pivot_column_names(aggregations)
    158            return pivot_column_names(aggregations, dialect="duckdb")
    159
    160    class Generator(generator.Generator):
    161        JOIN_HINTS = False
    162        TABLE_HINTS = False
    163        LIMIT_FETCH = "LIMIT"
    164        STRUCT_DELIMITER = ("(", ")")
    165        RENAME_TABLE_WITH_DB = False
    166
    167        TRANSFORMS = {
    168            **generator.Generator.TRANSFORMS,
    169            exp.ApproxDistinct: approx_count_distinct_sql,
    170            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
    171            if isinstance(seq_get(e.expressions, 0), exp.Select)
    172            else rename_func("LIST_VALUE")(self, e),
    173            exp.ArraySize: rename_func("ARRAY_LENGTH"),
    174            exp.ArraySort: _array_sort_sql,
    175            exp.ArraySum: rename_func("LIST_SUM"),
    176            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
    177            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
    178            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
    179            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
    180            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    181            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    182            exp.DayOfYear: rename_func("DAYOFYEAR"),
    183            exp.DataType: _datatype_sql,
    184            exp.DateAdd: _date_delta_sql,
    185            exp.DateSub: _date_delta_sql,
    186            exp.DateDiff: lambda self, e: self.func(
    187                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
    188            ),
    189            exp.DateStrToDate: datestrtodate_sql,
    190            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
    191            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
    192            exp.Explode: rename_func("UNNEST"),
    193            exp.IntDiv: lambda self, e: self.binary(e, "//"),
    194            exp.JSONExtract: arrow_json_extract_sql,
    195            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    196            exp.JSONBExtract: arrow_json_extract_sql,
    197            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
    198            exp.LogicalOr: rename_func("BOOL_OR"),
    199            exp.LogicalAnd: rename_func("BOOL_AND"),
    200            exp.Properties: no_properties_sql,
    201            exp.RegexpExtract: _regexp_extract_sql,
    202            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
    203            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
    204            exp.SafeDivide: no_safe_divide_sql,
    205            exp.Split: rename_func("STR_SPLIT"),
    206            exp.SortArray: _sort_array_sql,
    207            exp.StrPosition: str_position_sql,
    208            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
    209            exp.StrToTime: str_to_time_sql,
    210            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
    211            exp.Struct: _struct_sql,
    212            exp.TimestampTrunc: timestamptrunc_sql,
    213            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
    214            exp.TimeStrToTime: timestrtotime_sql,
    215            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
    216            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    217            exp.TimeToUnix: rename_func("EPOCH"),
    218            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
    219            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    220            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
    221            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
    222            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
    223            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
    224            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    225        }
    226
    227        TYPE_MAPPING = {
    228            **generator.Generator.TYPE_MAPPING,
    229            exp.DataType.Type.BINARY: "BLOB",
    230            exp.DataType.Type.CHAR: "TEXT",
    231            exp.DataType.Type.FLOAT: "REAL",
    232            exp.DataType.Type.NCHAR: "TEXT",
    233            exp.DataType.Type.NVARCHAR: "TEXT",
    234            exp.DataType.Type.UINT: "UINTEGER",
    235            exp.DataType.Type.VARBINARY: "BLOB",
    236            exp.DataType.Type.VARCHAR: "TEXT",
    237        }
    238
    239        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
    240
    241        UNWRAPPED_INTERVAL_VALUES = (exp.Column, exp.Literal, exp.Paren)
    242
    243        PROPERTIES_LOCATION = {
    244            **generator.Generator.PROPERTIES_LOCATION,
    245            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    246        }
    247
    248        def interval_sql(self, expression: exp.Interval) -> str:
    249            multiplier: t.Optional[int] = None
    250            unit = expression.text("unit").lower()
    251
    252            if unit.startswith("week"):
    253                multiplier = 7
    254            if unit.startswith("quarter"):
    255                multiplier = 90
    256
    257            if multiplier:
    258                return f"({multiplier} * {super().interval_sql(exp.Interval(this=expression.this, unit=exp.var('day')))})"
    259
    260            return super().interval_sql(expression)
    261
    262        def tablesample_sql(
    263            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep: str = " AS "
    264        ) -> str:
    265            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

     92    class Tokenizer(tokens.Tokenizer):
     93        KEYWORDS = {
     94            **tokens.Tokenizer.KEYWORDS,
     95            "~": TokenType.RLIKE,
     96            ":=": TokenType.EQ,
     97            "//": TokenType.DIV,
     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            "PIVOT_WIDER": TokenType.PIVOT,
    109            "SIGNED": TokenType.INT,
    110            "STRING": TokenType.VARCHAR,
    111            "UBIGINT": TokenType.UBIGINT,
    112            "UINTEGER": TokenType.UINT,
    113            "USMALLINT": TokenType.USMALLINT,
    114            "UTINYINT": TokenType.UTINYINT,
    115        }

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize
  peek

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

    117    class Parser(parser.Parser):
    118        CONCAT_NULL_OUTPUTS_STRING = True
    119
    120        FUNCTIONS = {
    121            **parser.Parser.FUNCTIONS,
    122            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
    123            "ARRAY_SORT": exp.SortArray.from_arg_list,
    124            "ARRAY_REVERSE_SORT": _sort_array_reverse,
    125            "DATEDIFF": _parse_date_diff,
    126            "DATE_DIFF": _parse_date_diff,
    127            "EPOCH": exp.TimeToUnix.from_arg_list,
    128            "EPOCH_MS": lambda args: exp.UnixToTime(
    129                this=exp.Div(this=seq_get(args, 0), expression=exp.Literal.number(1000))
    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        def _pivot_column_names(self, aggregations: t.List[exp.Expression]) -> t.List[str]:
    156            if len(aggregations) == 1:
    157                return super()._pivot_column_names(aggregations)
    158            return pivot_column_names(aggregations, dialect="duckdb")

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

###### Arguments:

- error_level: The desired error level. Default: ErrorLevel.IMMEDIATE
- error_message_context: Determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 100
- 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

##### 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

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

Generator converts a given syntax tree to the corresponding SQL string.

###### Arguments:

- pretty: Whether or not to format the produced SQL string. Default: False.
- identify: Determines when an identifier should be quoted. Possible values are: False (default): Never quote, except in cases where it's mandatory by the dialect. True or 'always': Always quote. 'safe': Only quote identifiers that are case insensitive.
- normalize: Whether or not to normalize identifiers to lowercase. Default: False.
- pad: Determines the pad size in a formatted string. Default: 2.
- indent: Determines the indentation size in a formatted string. Default: 2.
- normalize_functions: Whether or not to normalize all function names. Possible values are: "upper" or True (default): Convert names to uppercase. "lower": Convert names to lowercase. False: Disables function name normalization.
- unsupported_level: Determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- max_unsupported: 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: Determines whether or not the comma is leading or trailing in select expressions. This is only relevant when generating in pretty mode. 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 interval_sql(self, expression: sqlglot.expressions.Interval) -> str: View Source

    248        def interval_sql(self, expression: exp.Interval) -> str:
    249            multiplier: t.Optional[int] = None
    250            unit = expression.text("unit").lower()
    251
    252            if unit.startswith("week"):
    253                multiplier = 7
    254            if unit.startswith("quarter"):
    255                multiplier = 90
    256
    257            if multiplier:
    258                return f"({multiplier} * {super().interval_sql(exp.Interval(this=expression.this, unit=exp.var('day')))})"
    259
    260            return super().interval_sql(expression)

def tablesample_sql( self, expression: sqlglot.expressions.TableSample, seed_prefix: str = 'SEED', sep: str = ' AS ') -> str: View Source

    262        def tablesample_sql(
    263            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep: str = " AS "
    264        ) -> str:
    265            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
  createable_sql
  create_sql
  clone_sql
  describe_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  bytestring_sql
  rawstring_sql
  datatypesize_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
  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
  offset_limit_modifiers
  after_having_modifiers
  after_limit_modifiers
  select_sql
  schema_sql
  schema_columns_sql
  star_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
  safeconcat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_sql
  openjsoncolumndef_sql
  openjson_sql
  in_sql
  in_unnest_op
  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
  mergetreettlaction_sql
  mergetreettl_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
  safedpipe_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
  dictproperty_sql
  dictrange_sql
  dictsubproperty_sql
  oncluster_sql