Skip to content

Commit bc2170a

Browse files
fixed hex stuff
1 parent d19b8a7 commit bc2170a

File tree

4 files changed

+11
-17
lines changed

4 files changed

+11
-17
lines changed

sqlglot/dialects/duckdb.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ def _cast_to_boolean(arg: t.Optional[exp.Expression]) -> t.Optional[exp.Expressi
563563

564564

565565
def _is_binary(arg: exp.Expression) -> bool:
566-
return arg.is_type(
566+
return isinstance(arg, exp.HexString) or arg.is_type(
567567
exp.DataType.Type.BINARY,
568568
exp.DataType.Type.VARBINARY,
569569
exp.DataType.Type.BLOB,
@@ -771,7 +771,7 @@ def _bitshift_sql(
771771
this = annotate_types(this, dialect=self.dialect)
772772

773773
# Deal with binary separately, remember the original type, cast back later
774-
if _is_binary(this) or isinstance(this, exp.HexString):
774+
if _is_binary(this):
775775
original_type = this.to if isinstance(this, exp.Cast) else exp.DataType.build("BLOB")
776776
expression.set("this", exp.cast(this, exp.DataType.Type.BIT))
777777
elif expression.args.get("requires_int128"):
@@ -2273,16 +2273,8 @@ def format_sql(self, expression: exp.Format) -> str:
22732273
def hexstring_sql(
22742274
self, expression: exp.HexString, binary_function_repr: t.Optional[str] = None
22752275
) -> str:
2276-
from_hex = super().hexstring_sql(expression, binary_function_repr="FROM_HEX")
2277-
2278-
if expression.args.get("is_integer"):
2279-
return from_hex
2280-
2281-
# `from_hex` has transpiled x'ABCD' (BINARY) to DuckDB's '\xAB\xCD' (BINARY)
2282-
# `to_hex` & CASTing transforms it to "ABCD" (BINARY) to match representation
2283-
to_hex = exp.cast(self.func("TO_HEX", from_hex), exp.DataType.Type.BLOB)
2284-
2285-
return self.sql(to_hex)
2276+
# UNHEX('FF') correctly produces blob \xFF in DuckDB
2277+
return super().hexstring_sql(expression, binary_function_repr="UNHEX")
22862278

22872279
def timestamptrunc_sql(self, expression: exp.TimestampTrunc) -> str:
22882280
unit = unit_to_str(expression)

sqlglot/typing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
for expr_type in {
4343
exp.FromBase32,
4444
exp.FromBase64,
45+
exp.HexString,
46+
exp.Unhex,
4547
}
4648
},
4749
**{

tests/dialects/test_mysql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def test_hexadecimal_literal(self):
482482
"clickhouse": UnsupportedError,
483483
"databricks": "SELECT X'CC'",
484484
"drill": "SELECT 204",
485-
"duckdb": "SELECT CAST(HEX(FROM_HEX('CC')) AS VARBINARY)",
485+
"duckdb": "SELECT UNHEX('CC')",
486486
"hive": "SELECT 204",
487487
"mysql": "SELECT x'CC'",
488488
"oracle": "SELECT 204",
@@ -503,7 +503,7 @@ def test_hexadecimal_literal(self):
503503
"clickhouse": UnsupportedError,
504504
"databricks": "SELECT X'0000CC'",
505505
"drill": "SELECT 204",
506-
"duckdb": "SELECT CAST(HEX(FROM_HEX('0000CC')) AS VARBINARY)",
506+
"duckdb": "SELECT UNHEX('0000CC')",
507507
"hive": "SELECT 204",
508508
"mysql": "SELECT x'0000CC'",
509509
"oracle": "SELECT 204",

tests/dialects/test_snowflake.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,14 +1743,14 @@ def test_snowflake(self):
17431743
"SELECT BITSHIFTLEFT(X'002A'::BINARY, 1)",
17441744
write={
17451745
"snowflake": "SELECT BITSHIFTLEFT(CAST(x'002A' AS BINARY), 1)",
1746-
"duckdb": "SELECT CAST(CAST(UNHEX('002A') AS BIT) << 1 AS BLOB)",
1746+
"duckdb": "SELECT CAST(CAST(CAST(UNHEX('002A') AS BLOB) AS BIT) << 1 AS BLOB)",
17471747
},
17481748
)
17491749
self.validate_all(
17501750
"SELECT BITSHIFTRIGHT(X'002A'::BINARY, 1)",
17511751
write={
17521752
"snowflake": "SELECT BITSHIFTRIGHT(CAST(x'002A' AS BINARY), 1)",
1753-
"duckdb": "SELECT CAST(CAST(UNHEX('002A') AS BIT) >> 1 AS BLOB)",
1753+
"duckdb": "SELECT CAST(CAST(CAST(UNHEX('002A') AS BLOB) AS BIT) >> 1 AS BLOB)",
17541754
},
17551755
)
17561756

@@ -2171,7 +2171,7 @@ def test_snowflake(self):
21712171
"SELECT x'ABCD'",
21722172
write={
21732173
"snowflake": "SELECT x'ABCD'",
2174-
"duckdb": "SELECT CAST(HEX(FROM_HEX('ABCD')) AS VARBINARY)",
2174+
"duckdb": "SELECT UNHEX('ABCD')",
21752175
},
21762176
)
21772177

0 commit comments

Comments
 (0)