From 9e4b825f631089f3106e1eb4f3ecff75fadbddbc Mon Sep 17 00:00:00 2001 From: Alex Holyoke Date: Sun, 24 Mar 2024 11:14:53 -0400 Subject: [PATCH] fix: Implement modulus operator --- sqlalchemy_bigquery/base.py | 3 +++ tests/unit/test_select.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/sqlalchemy_bigquery/base.py b/sqlalchemy_bigquery/base.py index f4266f13..44844326 100644 --- a/sqlalchemy_bigquery/base.py +++ b/sqlalchemy_bigquery/base.py @@ -579,6 +579,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): diff --git a/tests/unit/test_select.py b/tests/unit/test_select.py index ee5e01cb..f52be120 100644 --- a/tests/unit/test_select.py +++ b/tests/unit/test_select.py @@ -486,3 +486,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