Skip to content
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
4 changes: 4 additions & 0 deletions packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ def visit_not_regexp_match_op_binary(self, binary, operator, **kw):
def visit_mod_binary(self, binary, operator, **kw):
return f"MOD({self.process(binary.left, **kw)}, {self.process(binary.right, **kw)})"

def visit_now_func(self, fn, **kw):
# BigQuery has no NOW() function, use CURRENT_TIMESTAMP instead.
return "CURRENT_TIMESTAMP"

def visit_try_cast(self, cast: elements.Cast, **kwargs: Any) -> str:
"""Compile a ``SAFE_CAST`` / ``try_cast`` expression for BigQuery."""
return "SAFE_CAST(%s AS %s)" % (
Expand Down
7 changes: 7 additions & 0 deletions packages/sqlalchemy-bigquery/tests/unit/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,13 @@ def test_float_literal_pyformat_bindparam():
assert "%(param_1:FLOAT64)s" in str(compiled)


def test_now_compiles_to_current_timestamp(faux_conn):
# BigQuery has no NOW() function.
stmt = sqlalchemy.select(sqlalchemy.func.now().label("ts"))
found_sql = stmt.compile(faux_conn).string
assert found_sql == "SELECT CURRENT_TIMESTAMP AS `ts`"


SAFE_CAST_TYPE_CASES = [
pytest.param(sqlalchemy.Integer, "INT64", id="integer_class"),
pytest.param(sqlalchemy.Integer(), "INT64", id="integer_instance"),
Expand Down
Loading