Skip to content

Commit d19b8a7

Browse files
partial fix of binary issues
1 parent 7d69859 commit d19b8a7

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

sqlglot/dialects/duckdb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,9 @@ 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):
774+
if _is_binary(this) or isinstance(this, exp.HexString):
775775
original_type = this.to if isinstance(this, exp.Cast) else exp.DataType.build("BLOB")
776-
expression.set("this", _cast_to_bit(this))
776+
expression.set("this", exp.cast(this, exp.DataType.Type.BIT))
777777
elif expression.args.get("requires_int128"):
778778
this.replace(exp.cast(this, exp.DataType.Type.INT128))
779779

sqlglot/dialects/snowflake.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ def _builder(args: t.List) -> B | exp.Anonymous:
168168
# Snowflake specifies INT128 for bitwise shifts
169169
if expr_type in (exp.BitwiseLeftShift, exp.BitwiseRightShift):
170170
result.set("requires_int128", True)
171-
# Mark HexStrings as integers for proper rendering in target dialects
172-
for hexstr in result.find_all(exp.HexString):
173-
hexstr.set("is_integer", True)
174171

175172
return result
176173

tests/dialects/test_snowflake.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,8 +1721,8 @@ def test_snowflake(self):
17211721
self.validate_all(
17221722
"SELECT BITSHIFTLEFT(X'FF', 4)",
17231723
write={
1724-
"snowflake": "SELECT BITSHIFTLEFT(255, 4)",
1725-
"duckdb": "SELECT CAST(255 AS INT128) << 4",
1724+
"snowflake": "SELECT BITSHIFTLEFT(x'FF', 4)",
1725+
"duckdb": "SELECT CAST(CAST(UNHEX('FF') AS BIT) << 4 AS BLOB)",
17261726
},
17271727
)
17281728
self.validate_all(
@@ -1735,8 +1735,22 @@ def test_snowflake(self):
17351735
self.validate_all(
17361736
"SELECT BITSHIFTRIGHT(X'FF', 4)",
17371737
write={
1738-
"snowflake": "SELECT BITSHIFTRIGHT(255, 4)",
1739-
"duckdb": "SELECT CAST(255 AS INT128) >> 4",
1738+
"snowflake": "SELECT BITSHIFTRIGHT(x'FF', 4)",
1739+
"duckdb": "SELECT CAST(CAST(UNHEX('FF') AS BIT) >> 4 AS BLOB)",
1740+
},
1741+
)
1742+
self.validate_all(
1743+
"SELECT BITSHIFTLEFT(X'002A'::BINARY, 1)",
1744+
write={
1745+
"snowflake": "SELECT BITSHIFTLEFT(CAST(x'002A' AS BINARY), 1)",
1746+
"duckdb": "SELECT CAST(CAST(UNHEX('002A') AS BIT) << 1 AS BLOB)",
1747+
},
1748+
)
1749+
self.validate_all(
1750+
"SELECT BITSHIFTRIGHT(X'002A'::BINARY, 1)",
1751+
write={
1752+
"snowflake": "SELECT BITSHIFTRIGHT(CAST(x'002A' AS BINARY), 1)",
1753+
"duckdb": "SELECT CAST(CAST(UNHEX('002A') AS BIT) >> 1 AS BLOB)",
17401754
},
17411755
)
17421756

0 commit comments

Comments
 (0)