Edit on GitHub

sqlglot.dialects.duckdb

  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    # https://duckdb.org/docs/sql/introduction.html#creating-a-new-table
 92    RESOLVES_IDENTIFIERS_AS_UPPERCASE = None
 93
 94    class Tokenizer(tokens.Tokenizer):
 95        KEYWORDS = {
 96            **tokens.Tokenizer.KEYWORDS,
 97            "~": TokenType.RLIKE,
 98            ":=": TokenType.EQ,
 99            "//": TokenType.DIV,
100            "ATTACH": TokenType.COMMAND,
101            "BINARY": TokenType.VARBINARY,
102            "BPCHAR": TokenType.TEXT,
103            "BITSTRING": TokenType.BIT,
104            "CHAR": TokenType.TEXT,
105            "CHARACTER VARYING": TokenType.TEXT,
106            "EXCLUDE": TokenType.EXCEPT,
107            "INT1": TokenType.TINYINT,
108            "LOGICAL": TokenType.BOOLEAN,
109            "NUMERIC": TokenType.DOUBLE,
110            "PIVOT_WIDER": TokenType.PIVOT,
111            "SIGNED": TokenType.INT,
112            "STRING": TokenType.VARCHAR,
113            "UBIGINT": TokenType.UBIGINT,
114            "UINTEGER": TokenType.UINT,
115            "USMALLINT": TokenType.USMALLINT,
116            "UTINYINT": TokenType.UTINYINT,
117        }
118
119    class Parser(parser.Parser):
120        CONCAT_NULL_OUTPUTS_STRING = True
121
122        FUNCTIONS = {
123            **parser.Parser.FUNCTIONS,
124            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
125            "ARRAY_SORT": exp.SortArray.from_arg_list,
126            "ARRAY_REVERSE_SORT": _sort_array_reverse,
127            "DATEDIFF": _parse_date_diff,
128            "DATE_DIFF": _parse_date_diff,
129            "EPOCH": exp.TimeToUnix.from_arg_list,
130            "EPOCH_MS": lambda args: exp.UnixToTime(
131                this=exp.Div(this=seq_get(args, 0), expression=exp.Literal.number(1000))
132            ),
133            "LIST_REVERSE_SORT": _sort_array_reverse,
134            "LIST_SORT": exp.SortArray.from_arg_list,
135            "LIST_VALUE": exp.Array.from_arg_list,
136            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
137            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
138            "STRING_SPLIT": exp.Split.from_arg_list,
139            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
140            "STRING_TO_ARRAY": exp.Split.from_arg_list,
141            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
142            "STRUCT_PACK": exp.Struct.from_arg_list,
143            "STR_SPLIT": exp.Split.from_arg_list,
144            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
145            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
146            "UNNEST": exp.Explode.from_arg_list,
147        }
148
149        TYPE_TOKENS = {
150            *parser.Parser.TYPE_TOKENS,
151            TokenType.UBIGINT,
152            TokenType.UINT,
153            TokenType.USMALLINT,
154            TokenType.UTINYINT,
155        }
156
157        def _pivot_column_names(self, aggregations: t.List[exp.Expression]) -> t.List[str]:
158            if len(aggregations) == 1:
159                return super()._pivot_column_names(aggregations)
160            return pivot_column_names(aggregations, dialect="duckdb")
161
162    class Generator(generator.Generator):
163        JOIN_HINTS = False
164        TABLE_HINTS = False
165        LIMIT_FETCH = "LIMIT"
166        STRUCT_DELIMITER = ("(", ")")
167        RENAME_TABLE_WITH_DB = False
168
169        TRANSFORMS = {
170            **generator.Generator.TRANSFORMS,
171            exp.ApproxDistinct: approx_count_distinct_sql,
172            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
173            if e.expressions and e.expressions[0].find(exp.Select)
174            else rename_func("LIST_VALUE")(self, e),
175            exp.ArraySize: rename_func("ARRAY_LENGTH"),
176            exp.ArraySort: _array_sort_sql,
177            exp.ArraySum: rename_func("LIST_SUM"),
178            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
179            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
180            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
181            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
182            exp.DayOfMonth: rename_func("DAYOFMONTH"),
183            exp.DayOfWeek: rename_func("DAYOFWEEK"),
184            exp.DayOfYear: rename_func("DAYOFYEAR"),
185            exp.DataType: _datatype_sql,
186            exp.DateAdd: _date_delta_sql,
187            exp.DateSub: _date_delta_sql,
188            exp.DateDiff: lambda self, e: self.func(
189                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
190            ),
191            exp.DateStrToDate: datestrtodate_sql,
192            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
193            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
194            exp.Explode: rename_func("UNNEST"),
195            exp.IntDiv: lambda self, e: self.binary(e, "//"),
196            exp.JSONExtract: arrow_json_extract_sql,
197            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
198            exp.JSONBExtract: arrow_json_extract_sql,
199            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
200            exp.LogicalOr: rename_func("BOOL_OR"),
201            exp.LogicalAnd: rename_func("BOOL_AND"),
202            exp.Properties: no_properties_sql,
203            exp.RegexpExtract: _regexp_extract_sql,
204            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
205            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
206            exp.SafeDivide: no_safe_divide_sql,
207            exp.Split: rename_func("STR_SPLIT"),
208            exp.SortArray: _sort_array_sql,
209            exp.StrPosition: str_position_sql,
210            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
211            exp.StrToTime: str_to_time_sql,
212            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
213            exp.Struct: _struct_sql,
214            exp.TimestampTrunc: timestamptrunc_sql,
215            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
216            exp.TimeStrToTime: timestrtotime_sql,
217            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
218            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
219            exp.TimeToUnix: rename_func("EPOCH"),
220            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
221            exp.TsOrDsAdd: _ts_or_ds_add_sql,
222            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
223            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
224            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
225            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
226            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
227        }
228
229        TYPE_MAPPING = {
230            **generator.Generator.TYPE_MAPPING,
231            exp.DataType.Type.BINARY: "BLOB",
232            exp.DataType.Type.CHAR: "TEXT",
233            exp.DataType.Type.FLOAT: "REAL",
234            exp.DataType.Type.NCHAR: "TEXT",
235            exp.DataType.Type.NVARCHAR: "TEXT",
236            exp.DataType.Type.UINT: "UINTEGER",
237            exp.DataType.Type.VARBINARY: "BLOB",
238            exp.DataType.Type.VARCHAR: "TEXT",
239        }
240
241        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
242
243        UNWRAPPED_INTERVAL_VALUES = (exp.Column, exp.Literal, exp.Paren)
244
245        PROPERTIES_LOCATION = {
246            **generator.Generator.PROPERTIES_LOCATION,
247            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
248        }
249
250        def interval_sql(self, expression: exp.Interval) -> str:
251            multiplier: t.Optional[int] = None
252            unit = expression.text("unit").lower()
253
254            if unit.startswith("week"):
255                multiplier = 7
256            if unit.startswith("quarter"):
257                multiplier = 90
258
259            if multiplier:
260                return f"({multiplier} * {super().interval_sql(exp.Interval(this=expression.this, unit=exp.var('day')))})"
261
262            return super().interval_sql(expression)
263
264        def tablesample_sql(
265            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep: str = " AS "
266        ) -> str:
267            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)
class DuckDB(sqlglot.dialects.dialect.Dialect):
 89class DuckDB(Dialect):
 90    NULL_ORDERING = "nulls_are_last"
 91
 92    # https://duckdb.org/docs/sql/introduction.html#creating-a-new-table
 93    RESOLVES_IDENTIFIERS_AS_UPPERCASE = None
 94
 95    class Tokenizer(tokens.Tokenizer):
 96        KEYWORDS = {
 97            **tokens.Tokenizer.KEYWORDS,
 98            "~": TokenType.RLIKE,
 99            ":=": TokenType.EQ,
100            "//": TokenType.DIV,
101            "ATTACH": TokenType.COMMAND,
102            "BINARY": TokenType.VARBINARY,
103            "BPCHAR": TokenType.TEXT,
104            "BITSTRING": TokenType.BIT,
105            "CHAR": TokenType.TEXT,
106            "CHARACTER VARYING": TokenType.TEXT,
107            "EXCLUDE": TokenType.EXCEPT,
108            "INT1": TokenType.TINYINT,
109            "LOGICAL": TokenType.BOOLEAN,
110            "NUMERIC": TokenType.DOUBLE,
111            "PIVOT_WIDER": TokenType.PIVOT,
112            "SIGNED": TokenType.INT,
113            "STRING": TokenType.VARCHAR,
114            "UBIGINT": TokenType.UBIGINT,
115            "UINTEGER": TokenType.UINT,
116            "USMALLINT": TokenType.USMALLINT,
117            "UTINYINT": TokenType.UTINYINT,
118        }
119
120    class Parser(parser.Parser):
121        CONCAT_NULL_OUTPUTS_STRING = True
122
123        FUNCTIONS = {
124            **parser.Parser.FUNCTIONS,
125            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
126            "ARRAY_SORT": exp.SortArray.from_arg_list,
127            "ARRAY_REVERSE_SORT": _sort_array_reverse,
128            "DATEDIFF": _parse_date_diff,
129            "DATE_DIFF": _parse_date_diff,
130            "EPOCH": exp.TimeToUnix.from_arg_list,
131            "EPOCH_MS": lambda args: exp.UnixToTime(
132                this=exp.Div(this=seq_get(args, 0), expression=exp.Literal.number(1000))
133            ),
134            "LIST_REVERSE_SORT": _sort_array_reverse,
135            "LIST_SORT": exp.SortArray.from_arg_list,
136            "LIST_VALUE": exp.Array.from_arg_list,
137            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
138            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
139            "STRING_SPLIT": exp.Split.from_arg_list,
140            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
141            "STRING_TO_ARRAY": exp.Split.from_arg_list,
142            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
143            "STRUCT_PACK": exp.Struct.from_arg_list,
144            "STR_SPLIT": exp.Split.from_arg_list,
145            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
146            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
147            "UNNEST": exp.Explode.from_arg_list,
148        }
149
150        TYPE_TOKENS = {
151            *parser.Parser.TYPE_TOKENS,
152            TokenType.UBIGINT,
153            TokenType.UINT,
154            TokenType.USMALLINT,
155            TokenType.UTINYINT,
156        }
157
158        def _pivot_column_names(self, aggregations: t.List[exp.Expression]) -> t.List[str]:
159            if len(aggregations) == 1:
160                return super()._pivot_column_names(aggregations)
161            return pivot_column_names(aggregations, dialect="duckdb")
162
163    class Generator(generator.Generator):
164        JOIN_HINTS = False
165        TABLE_HINTS = False
166        LIMIT_FETCH = "LIMIT"
167        STRUCT_DELIMITER = ("(", ")")
168        RENAME_TABLE_WITH_DB = False
169
170        TRANSFORMS = {
171            **generator.Generator.TRANSFORMS,
172            exp.ApproxDistinct: approx_count_distinct_sql,
173            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
174            if e.expressions and e.expressions[0].find(exp.Select)
175            else rename_func("LIST_VALUE")(self, e),
176            exp.ArraySize: rename_func("ARRAY_LENGTH"),
177            exp.ArraySort: _array_sort_sql,
178            exp.ArraySum: rename_func("LIST_SUM"),
179            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
180            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
181            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
182            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
183            exp.DayOfMonth: rename_func("DAYOFMONTH"),
184            exp.DayOfWeek: rename_func("DAYOFWEEK"),
185            exp.DayOfYear: rename_func("DAYOFYEAR"),
186            exp.DataType: _datatype_sql,
187            exp.DateAdd: _date_delta_sql,
188            exp.DateSub: _date_delta_sql,
189            exp.DateDiff: lambda self, e: self.func(
190                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
191            ),
192            exp.DateStrToDate: datestrtodate_sql,
193            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
194            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
195            exp.Explode: rename_func("UNNEST"),
196            exp.IntDiv: lambda self, e: self.binary(e, "//"),
197            exp.JSONExtract: arrow_json_extract_sql,
198            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
199            exp.JSONBExtract: arrow_json_extract_sql,
200            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
201            exp.LogicalOr: rename_func("BOOL_OR"),
202            exp.LogicalAnd: rename_func("BOOL_AND"),
203            exp.Properties: no_properties_sql,
204            exp.RegexpExtract: _regexp_extract_sql,
205            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
206            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
207            exp.SafeDivide: no_safe_divide_sql,
208            exp.Split: rename_func("STR_SPLIT"),
209            exp.SortArray: _sort_array_sql,
210            exp.StrPosition: str_position_sql,
211            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
212            exp.StrToTime: str_to_time_sql,
213            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
214            exp.Struct: _struct_sql,
215            exp.TimestampTrunc: timestamptrunc_sql,
216            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
217            exp.TimeStrToTime: timestrtotime_sql,
218            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
219            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
220            exp.TimeToUnix: rename_func("EPOCH"),
221            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
222            exp.TsOrDsAdd: _ts_or_ds_add_sql,
223            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
224            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
225            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
226            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
227            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
228        }
229
230        TYPE_MAPPING = {
231            **generator.Generator.TYPE_MAPPING,
232            exp.DataType.Type.BINARY: "BLOB",
233            exp.DataType.Type.CHAR: "TEXT",
234            exp.DataType.Type.FLOAT: "REAL",
235            exp.DataType.Type.NCHAR: "TEXT",
236            exp.DataType.Type.NVARCHAR: "TEXT",
237            exp.DataType.Type.UINT: "UINTEGER",
238            exp.DataType.Type.VARBINARY: "BLOB",
239            exp.DataType.Type.VARCHAR: "TEXT",
240        }
241
242        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
243
244        UNWRAPPED_INTERVAL_VALUES = (exp.Column, exp.Literal, exp.Paren)
245
246        PROPERTIES_LOCATION = {
247            **generator.Generator.PROPERTIES_LOCATION,
248            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
249        }
250
251        def interval_sql(self, expression: exp.Interval) -> str:
252            multiplier: t.Optional[int] = None
253            unit = expression.text("unit").lower()
254
255            if unit.startswith("week"):
256                multiplier = 7
257            if unit.startswith("quarter"):
258                multiplier = 90
259
260            if multiplier:
261                return f"({multiplier} * {super().interval_sql(exp.Interval(this=expression.this, unit=exp.var('day')))})"
262
263            return super().interval_sql(expression)
264
265        def tablesample_sql(
266            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep: str = " AS "
267        ) -> str:
268            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)
class DuckDB.Tokenizer(sqlglot.tokens.Tokenizer):
 95    class Tokenizer(tokens.Tokenizer):
 96        KEYWORDS = {
 97            **tokens.Tokenizer.KEYWORDS,
 98            "~": TokenType.RLIKE,
 99            ":=": TokenType.EQ,
100            "//": TokenType.DIV,
101            "ATTACH": TokenType.COMMAND,
102            "BINARY": TokenType.VARBINARY,
103            "BPCHAR": TokenType.TEXT,
104            "BITSTRING": TokenType.BIT,
105            "CHAR": TokenType.TEXT,
106            "CHARACTER VARYING": TokenType.TEXT,
107            "EXCLUDE": TokenType.EXCEPT,
108            "INT1": TokenType.TINYINT,
109            "LOGICAL": TokenType.BOOLEAN,
110            "NUMERIC": TokenType.DOUBLE,
111            "PIVOT_WIDER": TokenType.PIVOT,
112            "SIGNED": TokenType.INT,
113            "STRING": TokenType.VARCHAR,
114            "UBIGINT": TokenType.UBIGINT,
115            "UINTEGER": TokenType.UINT,
116            "USMALLINT": TokenType.USMALLINT,
117            "UTINYINT": TokenType.UTINYINT,
118        }
class DuckDB.Parser(sqlglot.parser.Parser):
120    class Parser(parser.Parser):
121        CONCAT_NULL_OUTPUTS_STRING = True
122
123        FUNCTIONS = {
124            **parser.Parser.FUNCTIONS,
125            "ARRAY_LENGTH": exp.ArraySize.from_arg_list,
126            "ARRAY_SORT": exp.SortArray.from_arg_list,
127            "ARRAY_REVERSE_SORT": _sort_array_reverse,
128            "DATEDIFF": _parse_date_diff,
129            "DATE_DIFF": _parse_date_diff,
130            "EPOCH": exp.TimeToUnix.from_arg_list,
131            "EPOCH_MS": lambda args: exp.UnixToTime(
132                this=exp.Div(this=seq_get(args, 0), expression=exp.Literal.number(1000))
133            ),
134            "LIST_REVERSE_SORT": _sort_array_reverse,
135            "LIST_SORT": exp.SortArray.from_arg_list,
136            "LIST_VALUE": exp.Array.from_arg_list,
137            "REGEXP_MATCHES": exp.RegexpLike.from_arg_list,
138            "STRFTIME": format_time_lambda(exp.TimeToStr, "duckdb"),
139            "STRING_SPLIT": exp.Split.from_arg_list,
140            "STRING_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
141            "STRING_TO_ARRAY": exp.Split.from_arg_list,
142            "STRPTIME": format_time_lambda(exp.StrToTime, "duckdb"),
143            "STRUCT_PACK": exp.Struct.from_arg_list,
144            "STR_SPLIT": exp.Split.from_arg_list,
145            "STR_SPLIT_REGEX": exp.RegexpSplit.from_arg_list,
146            "TO_TIMESTAMP": exp.UnixToTime.from_arg_list,
147            "UNNEST": exp.Explode.from_arg_list,
148        }
149
150        TYPE_TOKENS = {
151            *parser.Parser.TYPE_TOKENS,
152            TokenType.UBIGINT,
153            TokenType.UINT,
154            TokenType.USMALLINT,
155            TokenType.UTINYINT,
156        }
157
158        def _pivot_column_names(self, aggregations: t.List[exp.Expression]) -> t.List[str]:
159            if len(aggregations) == 1:
160                return super()._pivot_column_names(aggregations)
161            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
class DuckDB.Generator(sqlglot.generator.Generator):
163    class Generator(generator.Generator):
164        JOIN_HINTS = False
165        TABLE_HINTS = False
166        LIMIT_FETCH = "LIMIT"
167        STRUCT_DELIMITER = ("(", ")")
168        RENAME_TABLE_WITH_DB = False
169
170        TRANSFORMS = {
171            **generator.Generator.TRANSFORMS,
172            exp.ApproxDistinct: approx_count_distinct_sql,
173            exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0])
174            if e.expressions and e.expressions[0].find(exp.Select)
175            else rename_func("LIST_VALUE")(self, e),
176            exp.ArraySize: rename_func("ARRAY_LENGTH"),
177            exp.ArraySort: _array_sort_sql,
178            exp.ArraySum: rename_func("LIST_SUM"),
179            exp.CommentColumnConstraint: no_comment_column_constraint_sql,
180            exp.CurrentDate: lambda self, e: "CURRENT_DATE",
181            exp.CurrentTime: lambda self, e: "CURRENT_TIME",
182            exp.CurrentTimestamp: lambda self, e: "CURRENT_TIMESTAMP",
183            exp.DayOfMonth: rename_func("DAYOFMONTH"),
184            exp.DayOfWeek: rename_func("DAYOFWEEK"),
185            exp.DayOfYear: rename_func("DAYOFYEAR"),
186            exp.DataType: _datatype_sql,
187            exp.DateAdd: _date_delta_sql,
188            exp.DateSub: _date_delta_sql,
189            exp.DateDiff: lambda self, e: self.func(
190                "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
191            ),
192            exp.DateStrToDate: datestrtodate_sql,
193            exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
194            exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
195            exp.Explode: rename_func("UNNEST"),
196            exp.IntDiv: lambda self, e: self.binary(e, "//"),
197            exp.JSONExtract: arrow_json_extract_sql,
198            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
199            exp.JSONBExtract: arrow_json_extract_sql,
200            exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
201            exp.LogicalOr: rename_func("BOOL_OR"),
202            exp.LogicalAnd: rename_func("BOOL_AND"),
203            exp.Properties: no_properties_sql,
204            exp.RegexpExtract: _regexp_extract_sql,
205            exp.RegexpLike: rename_func("REGEXP_MATCHES"),
206            exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"),
207            exp.SafeDivide: no_safe_divide_sql,
208            exp.Split: rename_func("STR_SPLIT"),
209            exp.SortArray: _sort_array_sql,
210            exp.StrPosition: str_position_sql,
211            exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
212            exp.StrToTime: str_to_time_sql,
213            exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
214            exp.Struct: _struct_sql,
215            exp.TimestampTrunc: timestamptrunc_sql,
216            exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
217            exp.TimeStrToTime: timestrtotime_sql,
218            exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
219            exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
220            exp.TimeToUnix: rename_func("EPOCH"),
221            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
222            exp.TsOrDsAdd: _ts_or_ds_add_sql,
223            exp.TsOrDsToDate: ts_or_ds_to_date_sql("duckdb"),
224            exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
225            exp.UnixToTime: rename_func("TO_TIMESTAMP"),
226            exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
227            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
228        }
229
230        TYPE_MAPPING = {
231            **generator.Generator.TYPE_MAPPING,
232            exp.DataType.Type.BINARY: "BLOB",
233            exp.DataType.Type.CHAR: "TEXT",
234            exp.DataType.Type.FLOAT: "REAL",
235            exp.DataType.Type.NCHAR: "TEXT",
236            exp.DataType.Type.NVARCHAR: "TEXT",
237            exp.DataType.Type.UINT: "UINTEGER",
238            exp.DataType.Type.VARBINARY: "BLOB",
239            exp.DataType.Type.VARCHAR: "TEXT",
240        }
241
242        STAR_MAPPING = {**generator.Generator.STAR_MAPPING, "except": "EXCLUDE"}
243
244        UNWRAPPED_INTERVAL_VALUES = (exp.Column, exp.Literal, exp.Paren)
245
246        PROPERTIES_LOCATION = {
247            **generator.Generator.PROPERTIES_LOCATION,
248            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
249        }
250
251        def interval_sql(self, expression: exp.Interval) -> str:
252            multiplier: t.Optional[int] = None
253            unit = expression.text("unit").lower()
254
255            if unit.startswith("week"):
256                multiplier = 7
257            if unit.startswith("quarter"):
258                multiplier = 90
259
260            if multiplier:
261                return f"({multiplier} * {super().interval_sql(exp.Interval(this=expression.this, unit=exp.var('day')))})"
262
263            return super().interval_sql(expression)
264
265        def tablesample_sql(
266            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep: str = " AS "
267        ) -> str:
268            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:
251        def interval_sql(self, expression: exp.Interval) -> str:
252            multiplier: t.Optional[int] = None
253            unit = expression.text("unit").lower()
254
255            if unit.startswith("week"):
256                multiplier = 7
257            if unit.startswith("quarter"):
258                multiplier = 90
259
260            if multiplier:
261                return f"({multiplier} * {super().interval_sql(exp.Interval(this=expression.this, unit=exp.var('day')))})"
262
263            return super().interval_sql(expression)
def tablesample_sql( self, expression: sqlglot.expressions.TableSample, seed_prefix: str = 'SEED', sep: str = ' AS ') -> str:
265        def tablesample_sql(
266            self, expression: exp.TableSample, seed_prefix: str = "SEED", sep: str = " AS "
267        ) -> str:
268            return super().tablesample_sql(expression, seed_prefix="REPEATABLE", sep=sep)
@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool:
247    @classmethod
248    def can_identify(cls, text: str, identify: str | bool = "safe") -> bool:
249        """Checks if text can be identified given an identify option.
250
251        Args:
252            text: The text to check.
253            identify:
254                "always" or `True`: Always returns true.
255                "safe": True if the identifier is case-insensitive.
256
257        Returns:
258            Whether or not the given text can be identified.
259        """
260        if identify is True or identify == "always":
261            return True
262
263        if identify == "safe":
264            return not cls.case_sensitive(text)
265
266        return False

Checks if text can be identified given an identify option.

Arguments:
  • text: The text to check.
  • identify: "always" or True: Always returns true. "safe": True if the identifier is case-insensitive.
Returns:

Whether or not the given text can be identified.

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