Skip to content

Commit 07df830

Browse files
fix(sqlalchemy-bigquery): compile func.now() to CURRENT_TIMESTAMP (#18994)
Fixes #15762 BigQuery has no `NOW()` function, but `func.now()` compiled to `now()` with this dialect, so queries and `server_default=func.now()` columns failed in BigQuery. This adds `visit_now_func` to `BigQueryCompiler` so `func.now()` compiles to `CURRENT_TIMESTAMP`. That is what `func.current_timestamp()` already compiles to here, and it matches how the SQLite, MSSQL and Oracle dialects handle `now()`. Added `test_now_compiles_to_current_timestamp` in `tests/unit/test_compiler.py`, which fails on `main` (`SELECT now() AS ts`) and passes with this change. The package's unit tests pass locally, and the noxfile's ruff 0.14.14 checks, flake8 and black report nothing on the changed files. - [x] Make sure to open an issue as a bug/issue before writing your code (#15762) - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease Co-authored-by: abhishekacharya200 <abhishekacharya200@users.noreply.github.com>
1 parent a952b6b commit 07df830

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

‎packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,10 @@ def visit_not_regexp_match_op_binary(self, binary, operator, **kw):
588588
def visit_mod_binary(self, binary, operator, **kw):
589589
return f"MOD({self.process(binary.left, **kw)}, {self.process(binary.right, **kw)})"
590590

591+
def visit_now_func(self, fn, **kw):
592+
# BigQuery has no NOW() function, use CURRENT_TIMESTAMP instead.
593+
return "CURRENT_TIMESTAMP"
594+
591595
def visit_try_cast(self, cast: elements.Cast, **kwargs: Any) -> str:
592596
"""Compile a ``SAFE_CAST`` / ``try_cast`` expression for BigQuery."""
593597
return "SAFE_CAST(%s AS %s)" % (

‎packages/sqlalchemy-bigquery/tests/unit/test_compiler.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,13 @@ def test_float_literal_pyformat_bindparam():
491491
assert "%(param_1:FLOAT64)s" in str(compiled)
492492

493493

494+
def test_now_compiles_to_current_timestamp(faux_conn):
495+
# BigQuery has no NOW() function.
496+
stmt = sqlalchemy.select(sqlalchemy.func.now().label("ts"))
497+
found_sql = stmt.compile(faux_conn).string
498+
assert found_sql == "SELECT CURRENT_TIMESTAMP AS `ts`"
499+
500+
494501
SAFE_CAST_TYPE_CASES = [
495502
pytest.param(sqlalchemy.Integer, "INT64", id="integer_class"),
496503
pytest.param(sqlalchemy.Integer(), "INT64", id="integer_instance"),

0 commit comments

Comments
 (0)