Skip to content

Commit 820d664

Browse files
authored
Feat(presto): wrap md5 string arguments in to_utf8 (#3732)
1 parent fb066a6 commit 820d664

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

sqlglot/dialects/presto.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,6 @@ class Generator(generator.Generator):
478478
[transforms.remove_within_group_for_percentiles]
479479
),
480480
exp.Xor: bool_xor_sql,
481-
exp.MD5: lambda self, e: self.func(
482-
"LOWER", self.func("TO_HEX", self.func("MD5", self.sql(e, "this")))
483-
),
484481
exp.MD5Digest: rename_func("MD5"),
485482
exp.SHA: rename_func("SHA1"),
486483
exp.SHA2: sha256_sql,
@@ -547,6 +544,19 @@ class Generator(generator.Generator):
547544
"with",
548545
}
549546

547+
def md5_sql(self, expression: exp.MD5) -> str:
548+
this = expression.this
549+
550+
if not this.type:
551+
from sqlglot.optimizer.annotate_types import annotate_types
552+
553+
this = annotate_types(this)
554+
555+
if this.is_type(*exp.DataType.TEXT_TYPES):
556+
this = exp.Encode(this=this, charset=exp.Literal.string("utf-8"))
557+
558+
return self.func("LOWER", self.func("TO_HEX", self.func("MD5", self.sql(this))))
559+
550560
def strtounix_sql(self, expression: exp.StrToUnix) -> str:
551561
# Since `TO_UNIXTIME` requires a `TIMESTAMP`, we need to parse the argument into one.
552562
# To do this, we first try to `DATE_PARSE` it, but since this can fail when there's a

tests/dialects/test_presto.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,13 @@ def test_unicode_string(self):
581581
)
582582

583583
def test_presto(self):
584+
self.assertEqual(
585+
exp.func("md5", exp.func("concat", exp.cast("x", "text"), exp.Literal.string("s"))).sql(
586+
dialect="presto"
587+
),
588+
"LOWER(TO_HEX(MD5(TO_UTF8(CONCAT(CAST(x AS VARCHAR), CAST('s' AS VARCHAR))))))",
589+
)
590+
584591
with self.assertLogs(helper_logger):
585592
self.validate_all(
586593
"SELECT COALESCE(ELEMENT_AT(MAP_FROM_ENTRIES(ARRAY[(51, '1')]), id), quantity) FROM my_table",

0 commit comments

Comments
 (0)