Skip to content

Commit

Permalink
Fix(bigquery): MOD edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed May 9, 2024
1 parent 1bc0ce5 commit 5cfb29c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ class Generator(generator.Generator):
exp.IntDiv: rename_func("DIV"),
exp.JSONFormat: rename_func("TO_JSON_STRING"),
exp.Max: max_or_greatest,
exp.Mod: lambda self, e: self.func("MOD", e.this.unnest(), e.expression.unnest()),
exp.MD5: lambda self, e: self.func("TO_HEX", self.func("MD5", e.this)),
exp.MD5Digest: rename_func("MD5"),
exp.Min: min_or_least,
Expand Down Expand Up @@ -801,6 +800,15 @@ class Generator(generator.Generator):
"within",
}

def mod_sql(self, expression: exp.Mod) -> str:
this = expression.this
expr = expression.expression
return self.func(
"MOD",
this.unnest() if isinstance(this, exp.Paren) else this,
expr.unnest() if isinstance(expr, exp.Paren) else expr,
)

def column_parts(self, expression: exp.Column) -> str:
if expression.meta.get("quoted_column"):
# If a column reference is of the form `dataset.table`.name, we need
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,7 @@ def test_mod(self):
with self.subTest(f"Testing BigQuery roundtrip of modulo operation: {sql}"):
self.validate_identity(sql)

self.validate_identity("SELECT MOD((SELECT 1), 2)")
self.validate_identity(
"MOD((a + 1), b)",
"MOD(a + 1, b)",
Expand Down

0 comments on commit 5cfb29c

Please sign in to comment.