Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions sqlglot/generators/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
unit_to_str,
sequence_sql,
explode_to_unnest_sql,
sha2_digest_sql,
)
from sqlglot.dialects.hive import Hive
from sqlglot.generator import unsupported_args
Expand All @@ -34,6 +33,19 @@
DATE_ADD_OR_SUB = t.Union[exp.DateAdd, exp.TimestampAdd, exp.DateSub]


def _sha2_digest_sql(self: PrestoGenerator, expression: exp.SHA2Digest) -> str:
length = expression.text("length") or "256"
if length not in ("256", "512"):
self.unsupported(f"SHA{length} is not supported in Presto")

this = expression.this
if this.is_type(*exp.DataType.TEXT_TYPES):
# the native digest takes VARBINARY, so a text-typed argument needs encoding
this = exp.Encode(this=this, charset=exp.Literal.string("utf-8"))

return self.func(f"SHA{length}", this)


def _initcap_sql(self: PrestoGenerator, expression: exp.Initcap) -> str:
delimiters = expression.expression
if delimiters and not (
Expand Down Expand Up @@ -384,7 +396,7 @@ class PrestoGenerator(generator.Generator):
exp.MD5Digest: rename_func("MD5"),
exp.SHA: rename_func("SHA1"),
exp.SHA1Digest: rename_func("SHA1"),
exp.SHA2Digest: sha2_digest_sql,
exp.SHA2Digest: _sha2_digest_sql,
exp.Substring: rename_func("SUBSTR"),
}

Expand Down
4 changes: 3 additions & 1 deletion sqlglot/parsers/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ class BigQueryParser(parser.Parser):
"SHA256": lambda args: exp.SHA2Digest(
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)),
"SHA512": lambda args: exp.SHA2Digest(
this=seq_get(args, 0), length=exp.Literal.number(512)
),
"SIMILARITY": exp.AISimilarity.from_arg_list,
"SPLIT": lambda args: exp.Split(
# https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#split
Expand Down
6 changes: 3 additions & 3 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_bigquery(self):
# Presto's SHA512 takes VARBINARY, so annotated string args are wrapped in TO_UTF8
expr = self.parse_one("SELECT SHA512('foo')")
annotated = annotate_types(expr, dialect="bigquery")
self.assertEqual(annotated.sql("presto"), "SELECT LOWER(TO_HEX(SHA512(TO_UTF8('foo'))))")
self.assertEqual(annotated.sql("presto"), "SELECT SHA512(TO_UTF8('foo'))")

self.validate_identity(
"SELECT 'foo' 'bar'",
Expand Down Expand Up @@ -1181,8 +1181,8 @@ def test_bigquery(self):
"clickhouse": "SHA512(x)",
"bigquery": "SHA512(x)",
"spark2": "SHA2(x, 512)",
"presto": "LOWER(TO_HEX(SHA512(x)))",
"trino": "LOWER(TO_HEX(SHA512(x)))",
"presto": "SHA512(x)",
"trino": "SHA512(x)",
},
)
self.validate_all(
Expand Down
Loading