diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-09-07 11:39:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-09-07 11:39:48 +0000 |
commit | f73e9af131151f1e058446361c35b05c4c90bf10 (patch) | |
tree | ed425b89f12d3f5e4709290bdc03d876f365bc97 /sqlglot/dialects/duckdb.py | |
parent | Releasing debian version 17.12.0-1. (diff) | |
download | sqlglot-f73e9af131151f1e058446361c35b05c4c90bf10.tar.xz sqlglot-f73e9af131151f1e058446361c35b05c4c90bf10.zip |
Merging upstream version 18.2.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/duckdb.py')
-rw-r--r-- | sqlglot/dialects/duckdb.py | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/sqlglot/dialects/duckdb.py b/sqlglot/dialects/duckdb.py index 8253b52..684e35e 100644 --- a/sqlglot/dialects/duckdb.py +++ b/sqlglot/dialects/duckdb.py @@ -13,6 +13,7 @@ from sqlglot.dialects.dialect import ( datestrtodate_sql, encode_decode_sql, format_time_lambda, + inline_array_sql, no_comment_column_constraint_sql, no_properties_sql, no_safe_divide_sql, @@ -30,13 +31,13 @@ from sqlglot.helper import seq_get from sqlglot.tokens import TokenType -def _ts_or_ds_add_sql(self: generator.Generator, expression: exp.TsOrDsAdd) -> str: +def _ts_or_ds_add_sql(self: DuckDB.Generator, expression: exp.TsOrDsAdd) -> str: this = self.sql(expression, "this") unit = self.sql(expression, "unit").strip("'") or "DAY" return f"CAST({this} AS DATE) + {self.sql(exp.Interval(this=expression.expression.copy(), unit=unit))}" -def _date_delta_sql(self: generator.Generator, expression: exp.DateAdd | exp.DateSub) -> str: +def _date_delta_sql(self: DuckDB.Generator, expression: exp.DateAdd | exp.DateSub) -> str: this = self.sql(expression, "this") unit = self.sql(expression, "unit").strip("'") or "DAY" op = "+" if isinstance(expression, exp.DateAdd) else "-" @@ -44,7 +45,7 @@ def _date_delta_sql(self: generator.Generator, expression: exp.DateAdd | exp.Dat # BigQuery -> DuckDB conversion for the DATE function -def _date_sql(self: generator.Generator, expression: exp.Date) -> str: +def _date_sql(self: DuckDB.Generator, expression: exp.Date) -> str: result = f"CAST({self.sql(expression, 'this')} AS DATE)" zone = self.sql(expression, "zone") @@ -58,13 +59,13 @@ def _date_sql(self: generator.Generator, expression: exp.Date) -> str: return result -def _array_sort_sql(self: generator.Generator, expression: exp.ArraySort) -> str: +def _array_sort_sql(self: DuckDB.Generator, expression: exp.ArraySort) -> str: if expression.expression: self.unsupported("DUCKDB ARRAY_SORT does not support a comparator") return f"ARRAY_SORT({self.sql(expression, 'this')})" -def _sort_array_sql(self: generator.Generator, expression: exp.SortArray) -> str: +def _sort_array_sql(self: DuckDB.Generator, expression: exp.SortArray) -> str: this = self.sql(expression, "this") if expression.args.get("asc") == exp.false(): return f"ARRAY_REVERSE_SORT({this})" @@ -79,14 +80,14 @@ def _parse_date_diff(args: t.List) -> exp.Expression: return exp.DateDiff(this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0)) -def _struct_sql(self: generator.Generator, expression: exp.Struct) -> str: +def _struct_sql(self: DuckDB.Generator, expression: exp.Struct) -> str: args = [ f"'{e.name or e.this.name}': {self.sql(e, 'expression')}" for e in expression.expressions ] return f"{{{', '.join(args)}}}" -def _datatype_sql(self: generator.Generator, expression: exp.DataType) -> str: +def _datatype_sql(self: DuckDB.Generator, expression: exp.DataType) -> str: if expression.is_type("array"): return f"{self.expressions(expression, flat=True)}[]" @@ -97,7 +98,7 @@ def _datatype_sql(self: generator.Generator, expression: exp.DataType) -> str: return self.datatype_sql(expression) -def _json_format_sql(self: generator.Generator, expression: exp.JSONFormat) -> str: +def _json_format_sql(self: DuckDB.Generator, expression: exp.JSONFormat) -> str: sql = self.func("TO_JSON", expression.this, expression.args.get("options")) return f"CAST({sql} AS TEXT)" @@ -134,6 +135,7 @@ class DuckDB(Dialect): class Parser(parser.Parser): CONCAT_NULL_OUTPUTS_STRING = True + SUPPORTS_USER_DEFINED_TYPES = False BITWISE = { **parser.Parser.BITWISE, @@ -183,18 +185,12 @@ class DuckDB(Dialect): ), } - TYPE_TOKENS = { - *parser.Parser.TYPE_TOKENS, - TokenType.UBIGINT, - TokenType.UINT, - TokenType.USMALLINT, - TokenType.UTINYINT, - } - def _parse_types( - self, check_func: bool = False, schema: bool = False + self, check_func: bool = False, schema: bool = False, allow_identifiers: bool = True ) -> t.Optional[exp.Expression]: - this = super()._parse_types(check_func=check_func, schema=schema) + this = super()._parse_types( + check_func=check_func, schema=schema, allow_identifiers=allow_identifiers + ) # DuckDB treats NUMERIC and DECIMAL without precision as DECIMAL(18, 3) # See: https://duckdb.org/docs/sql/data_types/numeric @@ -207,6 +203,9 @@ class DuckDB(Dialect): return this + def _parse_struct_types(self) -> t.Optional[exp.Expression]: + return self._parse_field_def() + def _pivot_column_names(self, aggregations: t.List[exp.Expression]) -> t.List[str]: if len(aggregations) == 1: return super()._pivot_column_names(aggregations) @@ -219,13 +218,14 @@ class DuckDB(Dialect): LIMIT_FETCH = "LIMIT" STRUCT_DELIMITER = ("(", ")") RENAME_TABLE_WITH_DB = False + NVL2_SUPPORTED = False TRANSFORMS = { **generator.Generator.TRANSFORMS, exp.ApproxDistinct: approx_count_distinct_sql, exp.Array: lambda self, e: self.func("ARRAY", e.expressions[0]) if e.expressions and e.expressions[0].find(exp.Select) - else rename_func("LIST_VALUE")(self, e), + else inline_array_sql(self, e), exp.ArraySize: rename_func("ARRAY_LENGTH"), exp.ArraySort: _array_sort_sql, exp.ArraySum: rename_func("LIST_SUM"), |