diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 05:35:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 05:35:55 +0000 |
commit | fe979e8421c04c038353a0a2d07d81779516186a (patch) | |
tree | efb70a52261e5cf4862a7eb69e1d7cd16356fcba /sqlglot/dialects/presto.py | |
parent | Releasing debian version 23.13.7-1. (diff) | |
download | sqlglot-fe979e8421c04c038353a0a2d07d81779516186a.tar.xz sqlglot-fe979e8421c04c038353a0a2d07d81779516186a.zip |
Merging upstream version 23.16.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/presto.py')
-rw-r--r-- | sqlglot/dialects/presto.py | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/sqlglot/dialects/presto.py b/sqlglot/dialects/presto.py index e2d8924..1285281 100644 --- a/sqlglot/dialects/presto.py +++ b/sqlglot/dialects/presto.py @@ -32,6 +32,7 @@ from sqlglot.dialects.hive import Hive from sqlglot.dialects.mysql import MySQL from sqlglot.helper import apply_index_offset, seq_get from sqlglot.tokens import TokenType +from sqlglot.transforms import unqualify_columns def _explode_to_unnest_sql(self: Presto.Generator, expression: exp.Lateral) -> str: @@ -276,11 +277,13 @@ class Presto(Dialect): this=seq_get(args, 0), substr=seq_get(args, 1), instance=seq_get(args, 2) ), "TO_CHAR": _build_to_char, - "TO_HEX": exp.Hex.from_arg_list, "TO_UNIXTIME": exp.TimeToUnix.from_arg_list, "TO_UTF8": lambda args: exp.Encode( this=seq_get(args, 0), charset=exp.Literal.string("utf-8") ), + "MD5": exp.MD5Digest.from_arg_list, + "SHA256": lambda args: exp.SHA2(this=seq_get(args, 0), length=exp.Literal.number(256)), + "SHA512": lambda args: exp.SHA2(this=seq_get(args, 0), length=exp.Literal.number(512)), } FUNCTION_PARSERS = parser.Parser.FUNCTION_PARSERS.copy() @@ -300,6 +303,7 @@ class Presto(Dialect): LIKE_PROPERTY_INSIDE_SCHEMA = True MULTI_ARG_DISTINCT = False SUPPORTS_TO_NUMBER = False + HEX_FUNC = "TO_HEX" PROPERTIES_LOCATION = { **generator.Generator.PROPERTIES_LOCATION, @@ -381,7 +385,6 @@ class Presto(Dialect): exp.GroupConcat: lambda self, e: self.func( "ARRAY_JOIN", self.func("ARRAY_AGG", e.this), e.args.get("separator") ), - exp.Hex: rename_func("TO_HEX"), exp.If: if_sql(), exp.ILike: no_ilike_sql, exp.Initcap: _initcap_sql, @@ -417,7 +420,7 @@ class Presto(Dialect): exp.StructExtract: struct_extract_sql, exp.Table: transforms.preprocess([_unnest_sequence]), exp.Timestamp: no_timestamp_sql, - exp.TimestampTrunc: timestamptrunc_sql, + exp.TimestampTrunc: timestamptrunc_sql(), exp.TimeStrToDate: timestrtotime_sql, exp.TimeStrToTime: timestrtotime_sql, exp.TimeStrToUnix: lambda self, e: self.func( @@ -444,6 +447,14 @@ class Presto(Dialect): [transforms.remove_within_group_for_percentiles] ), exp.Xor: bool_xor_sql, + exp.MD5: lambda self, e: self.func( + "LOWER", self.func("TO_HEX", self.func("MD5", self.sql(e, "this"))) + ), + exp.MD5Digest: rename_func("MD5"), + exp.SHA: rename_func("SHA1"), + exp.SHA2: lambda self, e: self.func( + "SHA256" if e.text("length") == "256" else "SHA512", e.this + ), } RESERVED_KEYWORDS = { @@ -615,3 +626,25 @@ class Presto(Dialect): if kind == "VIEW" and schema.expressions: expression.this.set("expressions", None) return super().create_sql(expression) + + def delete_sql(self, expression: exp.Delete) -> str: + """ + Presto only supports DELETE FROM for a single table without an alias, so we need + to remove the unnecessary parts. If the original DELETE statement contains more + than one table to be deleted, we can't safely map it 1-1 to a Presto statement. + """ + tables = expression.args.get("tables") or [expression.this] + if len(tables) > 1: + return super().delete_sql(expression) + + table = tables[0] + expression.set("this", table) + expression.set("tables", None) + + if isinstance(table, exp.Table): + table_alias = table.args.get("alias") + if table_alias: + table_alias.pop() + expression = t.cast(exp.Delete, expression.transform(unqualify_columns)) + + return super().delete_sql(expression) |