Skip to content

Commit

Permalink
feat(snowflake)!: Transpile support for bitor/bit_or snowflake functi…
Browse files Browse the repository at this point in the history
…on (#4486)

* feat(snowflake): Transpile support for bitor/bit_or

* feat(snowflake): Transpile support for bitor/bit_or

* feat(snowflake): Transpile support for bitor/bit_or

* chore: moved test cases of bitor to the test_snowflake

* feat(snowflake): Added support for bitnow

* Revert "feat(snowflake): Added support for bitnow"

This reverts commit 85551c9.

* feat(snowflake): Added support for bitor extra parameters

* feat(snowflake): remove unnecessary test case

* chore(snowflake): code refactoring

---------

Co-authored-by: ranjanankur314 <[email protected]>
  • Loading branch information
ankur334 and ranjanankur314 authored Dec 10, 2024
1 parent 2655d7c commit 1d3c9aa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sqlglot/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ def _builder(args: t.List) -> E:
return _builder


def _build_bitor(args: t.List) -> exp.BitwiseOr | exp.Anonymous:
if len(args) == 3:
return exp.Anonymous(this="BITOR", expressions=args)

return binary_from_function(exp.BitwiseOr)(args)


# https://docs.snowflake.com/en/sql-reference/functions/div0
def _build_if_from_div0(args: t.List) -> exp.If:
lhs = exp._wrap(seq_get(args, 0), exp.Binary)
Expand Down Expand Up @@ -393,6 +400,8 @@ class Parser(parser.Parser):
),
"BITXOR": binary_from_function(exp.BitwiseXor),
"BIT_XOR": binary_from_function(exp.BitwiseXor),
"BITOR": _build_bitor,
"BIT_OR": _build_bitor,
"BOOLXOR": binary_from_function(exp.Xor),
"DATE": _build_datetime("DATE", exp.DataType.Type.DATE),
"DATE_TRUNC": _date_trunc_to_time,
Expand Down Expand Up @@ -869,6 +878,7 @@ class Generator(generator.Generator):
"CONVERT_TIMEZONE", e.args.get("zone"), e.this
),
exp.BitwiseXor: rename_func("BITXOR"),
exp.BitwiseOr: rename_func("BITOR"),
exp.Create: transforms.preprocess([_flatten_structured_types_unless_iceberg]),
exp.DateAdd: date_delta_sql("DATEADD"),
exp.DateDiff: date_delta_sql("DATEDIFF"),
Expand Down
6 changes: 6 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,12 @@ def test_snowflake(self):
"snowflake": "EDITDISTANCE(col1, col2, 3)",
},
)
self.validate_identity("SELECT BITOR(a, b) FROM table")

self.validate_identity("SELECT BIT_OR(a, b) FROM table", "SELECT BITOR(a, b) FROM table")

# Test BITOR with three arguments, padding on the left
self.validate_identity("SELECT BITOR(a, b, 'LEFT') FROM table_name")

def test_null_treatment(self):
self.validate_all(
Expand Down

0 comments on commit 1d3c9aa

Please sign in to comment.