diff options
Diffstat (limited to 'sqlglot/dialects')
-rw-r--r-- | sqlglot/dialects/clickhouse.py | 3 | ||||
-rw-r--r-- | sqlglot/dialects/dialect.py | 25 | ||||
-rw-r--r-- | sqlglot/dialects/duckdb.py | 13 | ||||
-rw-r--r-- | sqlglot/dialects/hive.py | 8 | ||||
-rw-r--r-- | sqlglot/dialects/postgres.py | 1 | ||||
-rw-r--r-- | sqlglot/dialects/presto.py | 6 | ||||
-rw-r--r-- | sqlglot/dialects/tableau.py | 5 |
7 files changed, 36 insertions, 25 deletions
diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py index da5c856..f446e6d 100644 --- a/sqlglot/dialects/clickhouse.py +++ b/sqlglot/dialects/clickhouse.py @@ -1,7 +1,6 @@ from sqlglot import exp from sqlglot.dialects.dialect import Dialect, inline_array_sql, var_map_sql from sqlglot.generator import Generator -from sqlglot.helper import csv from sqlglot.parser import Parser, parse_var_map from sqlglot.tokens import Tokenizer, TokenType @@ -66,7 +65,7 @@ class ClickHouse(Dialect): TRANSFORMS = { **Generator.TRANSFORMS, exp.Array: inline_array_sql, - exp.StrPosition: lambda self, e: f"position({csv(self.sql(e, 'this'), self.sql(e, 'substr'), self.sql(e, 'position'))})", + exp.StrPosition: lambda self, e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})", exp.Final: lambda self, e: f"{self.sql(e, 'this')} FINAL", exp.Map: lambda self, e: _lower_func(var_map_sql(self, e)), exp.VarMap: lambda self, e: _lower_func(var_map_sql(self, e)), diff --git a/sqlglot/dialects/dialect.py b/sqlglot/dialects/dialect.py index f7c6cb5..531c72a 100644 --- a/sqlglot/dialects/dialect.py +++ b/sqlglot/dialects/dialect.py @@ -2,7 +2,7 @@ from enum import Enum from sqlglot import exp from sqlglot.generator import Generator -from sqlglot.helper import csv, list_get +from sqlglot.helper import list_get from sqlglot.parser import Parser from sqlglot.time import format_time from sqlglot.tokens import Tokenizer @@ -177,11 +177,11 @@ class Dialect(metaclass=_Dialect): def rename_func(name): def _rename(self, expression): args = ( - self.expressions(expression, flat=True) + expression.expressions if isinstance(expression, exp.Func) and expression.is_var_len_args - else csv(*[self.sql(e) for e in expression.args.values()]) + else expression.args.values() ) - return f"{name}({args})" + return f"{name}({self.format_args(*args)})" return _rename @@ -189,15 +189,11 @@ def rename_func(name): def approx_count_distinct_sql(self, expression): if expression.args.get("accuracy"): self.unsupported("APPROX_COUNT_DISTINCT does not support accuracy") - return f"APPROX_COUNT_DISTINCT({self.sql(expression, 'this')})" + return f"APPROX_COUNT_DISTINCT({self.format_args(expression.this)})" def if_sql(self, expression): - expressions = csv( - self.sql(expression, "this"), - self.sql(expression, "true"), - self.sql(expression, "false"), - ) + expressions = self.format_args(expression.this, expression.args.get("true"), expression.args.get("false")) return f"IF({expressions})" @@ -254,6 +250,11 @@ def no_trycast_sql(self, expression): return self.cast_sql(expression) +def no_properties_sql(self, expression): + self.unsupported("Properties unsupported") + return "" + + def str_position_sql(self, expression): this = self.sql(expression, "this") substr = self.sql(expression, "substr") @@ -275,13 +276,13 @@ def var_map_sql(self, expression): if not isinstance(keys, exp.Array) or not isinstance(values, exp.Array): self.unsupported("Cannot convert array columns into map.") - return f"MAP({self.sql(keys)}, {self.sql(values)})" + return f"MAP({self.format_args(keys, values)})" args = [] for key, value in zip(keys.expressions, values.expressions): args.append(self.sql(key)) args.append(self.sql(value)) - return f"MAP({csv(*args)})" + return f"MAP({self.format_args(*args)})" def format_time_lambda(exp_class, dialect, default=None): diff --git a/sqlglot/dialects/duckdb.py b/sqlglot/dialects/duckdb.py index e09c3dd..f3ff6d3 100644 --- a/sqlglot/dialects/duckdb.py +++ b/sqlglot/dialects/duckdb.py @@ -6,6 +6,7 @@ from sqlglot.dialects.dialect import ( arrow_json_extract_sql, format_time_lambda, no_pivot_sql, + no_properties_sql, no_safe_divide_sql, no_tablesample_sql, rename_func, @@ -68,6 +69,12 @@ def _struct_pack_sql(self, expression): return f"STRUCT_PACK({', '.join(args)})" +def _datatype_sql(self, expression): + if expression.this == exp.DataType.Type.ARRAY: + return f"{self.expressions(expression, flat=True)}[]" + return self.datatype_sql(expression) + + class DuckDB(Dialect): class Tokenizer(Tokenizer): KEYWORDS = { @@ -106,6 +113,8 @@ class DuckDB(Dialect): } class Generator(Generator): + STRUCT_DELIMITER = ("(", ")") + TRANSFORMS = { **Generator.TRANSFORMS, exp.ApproxDistinct: approx_count_distinct_sql, @@ -113,8 +122,9 @@ class DuckDB(Dialect): exp.ArraySize: rename_func("ARRAY_LENGTH"), exp.ArraySort: _array_sort_sql, exp.ArraySum: rename_func("LIST_SUM"), + exp.DataType: _datatype_sql, exp.DateAdd: _date_add, - exp.DateDiff: lambda self, e: f"""DATE_DIFF({self.sql(e, 'unit') or "'day'"}, {self.sql(e, 'expression')}, {self.sql(e, 'this')})""", + exp.DateDiff: lambda self, e: f"""DATE_DIFF({self.format_args(e.args.get("unit") or "'day'", e.expression, e.this)})""", exp.DateStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)", exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.dateint_format}) AS INT)", exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.dateint_format}) AS DATE)", @@ -124,6 +134,7 @@ class DuckDB(Dialect): exp.JSONBExtract: arrow_json_extract_sql, exp.JSONBExtractScalar: arrow_json_extract_scalar_sql, exp.Pivot: no_pivot_sql, + exp.Properties: no_properties_sql, exp.RegexpLike: rename_func("REGEXP_MATCHES"), exp.RegexpSplit: rename_func("STR_SPLIT_REGEX"), exp.SafeDivide: no_safe_divide_sql, diff --git a/sqlglot/dialects/hive.py b/sqlglot/dialects/hive.py index 55d7bcc..8888df8 100644 --- a/sqlglot/dialects/hive.py +++ b/sqlglot/dialects/hive.py @@ -14,7 +14,7 @@ from sqlglot.dialects.dialect import ( var_map_sql, ) from sqlglot.generator import Generator -from sqlglot.helper import csv, list_get +from sqlglot.helper import list_get from sqlglot.parser import Parser, parse_var_map from sqlglot.tokens import Tokenizer @@ -32,7 +32,7 @@ def _property_sql(self, expression): def _str_to_unix(self, expression): - return f"UNIX_TIMESTAMP({csv(self.sql(expression, 'this'), _time_format(self, expression))})" + return f"UNIX_TIMESTAMP({self.format_args(expression.this, _time_format(self, expression))})" def _str_to_date(self, expression): @@ -226,7 +226,7 @@ class Hive(Dialect): exp.SchemaCommentProperty: lambda self, e: self.naked_property(e), exp.SetAgg: rename_func("COLLECT_SET"), exp.Split: lambda self, e: f"SPLIT({self.sql(e, 'this')}, CONCAT('\\\\Q', {self.sql(e, 'expression')}))", - exp.StrPosition: lambda self, e: f"LOCATE({csv(self.sql(e, 'substr'), self.sql(e, 'this'), self.sql(e, 'position'))})", + exp.StrPosition: lambda self, e: f"LOCATE({self.format_args(e.args.get('substr'), e.this, e.args.get('position'))})", exp.StrToDate: _str_to_date, exp.StrToTime: _str_to_time, exp.StrToUnix: _str_to_unix, @@ -241,7 +241,7 @@ class Hive(Dialect): exp.TsOrDsAdd: lambda self, e: f"DATE_ADD({self.sql(e, 'this')}, {self.sql(e, 'expression')})", exp.TsOrDsToDate: _to_date_sql, exp.TryCast: no_trycast_sql, - exp.UnixToStr: lambda self, e: f"FROM_UNIXTIME({csv(self.sql(e, 'this'), _time_format(self, e))})", + exp.UnixToStr: lambda self, e: f"FROM_UNIXTIME({self.format_args(e.this, _time_format(self, e))})", exp.UnixToTime: rename_func("FROM_UNIXTIME"), exp.UnixToTimeStr: rename_func("FROM_UNIXTIME"), exp.PartitionedByProperty: lambda self, e: f"PARTITIONED BY {self.sql(e, 'value')}", diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py index 731e28e..c91ff4b 100644 --- a/sqlglot/dialects/postgres.py +++ b/sqlglot/dialects/postgres.py @@ -167,6 +167,7 @@ class Postgres(Dialect): **Tokenizer.KEYWORDS, "ALWAYS": TokenType.ALWAYS, "BY DEFAULT": TokenType.BY_DEFAULT, + "COMMENT ON": TokenType.COMMENT_ON, "IDENTITY": TokenType.IDENTITY, "GENERATED": TokenType.GENERATED, "DOUBLE PRECISION": TokenType.DOUBLE, diff --git a/sqlglot/dialects/presto.py b/sqlglot/dialects/presto.py index 85647c5..8dfb2fd 100644 --- a/sqlglot/dialects/presto.py +++ b/sqlglot/dialects/presto.py @@ -11,7 +11,7 @@ from sqlglot.dialects.dialect import ( ) from sqlglot.dialects.mysql import MySQL from sqlglot.generator import Generator -from sqlglot.helper import csv, list_get +from sqlglot.helper import list_get from sqlglot.parser import Parser from sqlglot.tokens import Tokenizer, TokenType @@ -26,7 +26,7 @@ def _concat_ws_sql(self, expression): sep, *args = expression.expressions sep = self.sql(sep) if len(args) > 1: - return f"ARRAY_JOIN(ARRAY[{csv(*(self.sql(e) for e in args))}], {sep})" + return f"ARRAY_JOIN(ARRAY[{self.format_args(*args)}], {sep})" return f"ARRAY_JOIN({self.sql(args[0])}, {sep})" @@ -66,7 +66,7 @@ def _no_sort_array(self, expression): comparator = "(a, b) -> CASE WHEN a < b THEN 1 WHEN a > b THEN -1 ELSE 0 END" else: comparator = None - args = csv(self.sql(expression, "this"), comparator) + args = self.format_args(expression.this, comparator) return f"ARRAY_SORT({args})" diff --git a/sqlglot/dialects/tableau.py b/sqlglot/dialects/tableau.py index e571749..45aa041 100644 --- a/sqlglot/dialects/tableau.py +++ b/sqlglot/dialects/tableau.py @@ -1,7 +1,6 @@ from sqlglot import exp from sqlglot.dialects.dialect import Dialect from sqlglot.generator import Generator -from sqlglot.helper import list_get from sqlglot.parser import Parser @@ -16,7 +15,7 @@ def _coalesce_sql(self, expression): def _count_sql(self, expression): this = expression.this if isinstance(this, exp.Distinct): - return f"COUNTD({self.sql(this, 'this')})" + return f"COUNTD({self.expressions(this, flat=True)})" return f"COUNT({self.sql(expression, 'this')})" @@ -33,5 +32,5 @@ class Tableau(Dialect): FUNCTIONS = { **Parser.FUNCTIONS, "IFNULL": exp.Coalesce.from_arg_list, - "COUNTD": lambda args: exp.Count(this=exp.Distinct(this=list_get(args, 0))), + "COUNTD": lambda args: exp.Count(this=exp.Distinct(expressions=args)), } |