Skip to content

Commit 7d485c7

Browse files
Feat(duckdb): Add transpilation support for the negative integer args for BITNOT (#6490)
* feat(duckdb): Add transpilation support for the negative integer args for BITNOT * Update sqlglot/dialects/duckdb.py --------- Co-authored-by: Vaggelis Danias <daniasevangelos@gmail.com>
1 parent 2cc67cd commit 7d485c7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

sqlglot/dialects/duckdb.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,3 +1660,12 @@ def jsonextractscalar_sql(self, expression: exp.JSONExtractScalar) -> str:
16601660
this=rename_func("JSON_VALUE")(self, expression), expression="'$'"
16611661
)
16621662
return _arrow_json_extract_sql(self, expression)
1663+
1664+
def bitwisenot_sql(self, expression: exp.BitwiseNot) -> str:
1665+
this = expression.this
1666+
1667+
# Wrap in parentheses to prevent parsing issues such as "SELECT ~-1"
1668+
if isinstance(this, exp.Neg):
1669+
this = exp.Paren(this=this)
1670+
1671+
return f"~{self.sql(this)}"

tests/dialects/test_snowflake.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,13 @@ def test_snowflake(self):
14371437
)
14381438
self.validate_identity("SELECT BITNOT(a)")
14391439
self.validate_identity("SELECT BIT_NOT(a)", "SELECT BITNOT(a)")
1440+
self.validate_all(
1441+
"SELECT BITNOT(-1)",
1442+
write={
1443+
"duckdb": "SELECT ~(-1)",
1444+
"snowflake": "SELECT BITNOT(-1)",
1445+
},
1446+
)
14401447
self.validate_identity("SELECT BITAND(a, b)")
14411448
self.validate_identity("SELECT BITAND(a, b, 'LEFT')")
14421449
self.validate_identity("SELECT BIT_AND(a, b)", "SELECT BITAND(a, b)")

0 commit comments

Comments
 (0)