Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement modulus operator #1048

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlalchemy_bigquery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ def visit_regexp_match_op_binary(self, binary, operator, **kw):
def visit_not_regexp_match_op_binary(self, binary, operator, **kw):
return "NOT %s" % self.visit_regexp_match_op_binary(binary, operator, **kw)

def visit_mod_binary(self, binary, operator, **kw):
return f"MOD({self.process(binary.left, **kw)}, {self.process(binary.right, **kw)})"


class BigQueryTypeCompiler(GenericTypeCompiler):
def visit_INTEGER(self, type_, **kw):
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,16 @@ def test_visit_not_regexp_match_op_binary(faux_conn):
expected = "NOT REGEXP_CONTAINS(`table`.`foo`, %(foo_1:STRING)s)"

assert result == expected


def test_visit_mod_binary(faux_conn):
table = setup_table(
faux_conn,
"table",
sqlalchemy.Column("foo", sqlalchemy.Integer),
)
sql_statement = table.c.foo % 2
result = sql_statement.compile(faux_conn).string
expected = "MOD(`table`.`foo`, %(foo_1:INT64)s)"

assert result == expected