diff options
Diffstat (limited to 'sqlglot/dialects/spark2.py')
-rw-r--r-- | sqlglot/dialects/spark2.py | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/sqlglot/dialects/spark2.py b/sqlglot/dialects/spark2.py index e27ba18..e4bb30e 100644 --- a/sqlglot/dialects/spark2.py +++ b/sqlglot/dialects/spark2.py @@ -129,10 +129,20 @@ class Spark2(Hive): "SHIFTRIGHT": binary_from_function(exp.BitwiseRightShift), "STRING": _parse_as_cast("string"), "TIMESTAMP": _parse_as_cast("timestamp"), - "TO_TIMESTAMP": lambda args: _parse_as_cast("timestamp")(args) - if len(args) == 1 - else format_time_lambda(exp.StrToTime, "spark")(args), + "TO_TIMESTAMP": lambda args: ( + _parse_as_cast("timestamp")(args) + if len(args) == 1 + else format_time_lambda(exp.StrToTime, "spark")(args) + ), "TO_UNIX_TIMESTAMP": exp.StrToUnix.from_arg_list, + "TO_UTC_TIMESTAMP": lambda args: exp.FromTimeZone( + this=exp.cast_unless( + seq_get(args, 0) or exp.Var(this=""), + exp.DataType.build("timestamp"), + exp.DataType.build("timestamp"), + ), + zone=seq_get(args, 1), + ), "TRUNC": lambda args: exp.DateTrunc(unit=seq_get(args, 1), this=seq_get(args, 0)), "WEEKOFYEAR": lambda args: exp.WeekOfYear(this=exp.TsOrDsToDate(this=seq_get(args, 0))), } @@ -188,6 +198,7 @@ class Spark2(Hive): exp.DayOfYear: rename_func("DAYOFYEAR"), exp.FileFormatProperty: lambda self, e: f"USING {e.name.upper()}", exp.From: transforms.preprocess([_unalias_pivot]), + exp.FromTimeZone: lambda self, e: f"TO_UTC_TIMESTAMP({self.sql(e, 'this')}, {self.sql(e, 'zone')})", exp.LogicalAnd: rename_func("BOOL_AND"), exp.LogicalOr: rename_func("BOOL_OR"), exp.Map: _map_sql, @@ -255,10 +266,12 @@ class Spark2(Hive): def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str: return super().columndef_sql( expression, - sep=": " - if isinstance(expression.parent, exp.DataType) - and expression.parent.is_type("struct") - else sep, + sep=( + ": " + if isinstance(expression.parent, exp.DataType) + and expression.parent.is_type("struct") + else sep + ), ) class Tokenizer(Hive.Tokenizer): |