diff options
Diffstat (limited to 'sqlglot/dialects/duckdb.py')
-rw-r--r-- | sqlglot/dialects/duckdb.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/sqlglot/dialects/duckdb.py b/sqlglot/dialects/duckdb.py index 093a01c..d7e5a43 100644 --- a/sqlglot/dialects/duckdb.py +++ b/sqlglot/dialects/duckdb.py @@ -8,6 +8,7 @@ from sqlglot.dialects.dialect import ( approx_count_distinct_sql, arrow_json_extract_scalar_sql, arrow_json_extract_sql, + date_trunc_to_time, datestrtodate_sql, format_time_lambda, no_comment_column_constraint_sql, @@ -38,6 +39,21 @@ def _date_delta_sql(self: generator.Generator, expression: exp.DateAdd | exp.Dat return f"{this} {op} {self.sql(exp.Interval(this=expression.expression, unit=unit))}" +# BigQuery -> DuckDB conversion for the DATE function +def _date_sql(self: generator.Generator, expression: exp.Date) -> str: + result = f"CAST({self.sql(expression, 'this')} AS DATE)" + zone = self.sql(expression, "zone") + + if zone: + date_str = self.func("STRFTIME", result, "'%d/%m/%Y'") + date_str = f"{date_str} || ' ' || {zone}" + + # This will create a TIMESTAMP with time zone information + result = self.func("STRPTIME", date_str, "'%d/%m/%Y %Z'") + + return result + + def _array_sort_sql(self: generator.Generator, expression: exp.ArraySort) -> str: if expression.expression: self.unsupported("DUCKDB ARRAY_SORT does not support a comparator") @@ -131,6 +147,8 @@ class DuckDB(Dialect): "ARRAY_REVERSE_SORT": _sort_array_reverse, "DATEDIFF": _parse_date_diff, "DATE_DIFF": _parse_date_diff, + "DATE_TRUNC": date_trunc_to_time, + "DATETRUNC": date_trunc_to_time, "EPOCH": exp.TimeToUnix.from_arg_list, "EPOCH_MS": lambda args: exp.UnixToTime( this=exp.Div(this=seq_get(args, 0), expression=exp.Literal.number(1000)) @@ -167,6 +185,7 @@ class DuckDB(Dialect): class Generator(generator.Generator): JOIN_HINTS = False TABLE_HINTS = False + QUERY_HINTS = False LIMIT_FETCH = "LIMIT" STRUCT_DELIMITER = ("(", ")") RENAME_TABLE_WITH_DB = False @@ -188,7 +207,9 @@ class DuckDB(Dialect): exp.DayOfWeek: rename_func("DAYOFWEEK"), exp.DayOfYear: rename_func("DAYOFYEAR"), exp.DataType: _datatype_sql, + exp.Date: _date_sql, exp.DateAdd: _date_delta_sql, + exp.DateFromParts: rename_func("MAKE_DATE"), exp.DateSub: _date_delta_sql, exp.DateDiff: lambda self, e: self.func( "DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this |