-
Notifications
You must be signed in to change notification settings - Fork 64
Compile timestampdiff() to EXTRACT(EPOCH FROM ...) on cockroachdb dialect #301
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
Merged
rafiss
merged 4 commits into
cockroachdb:master
from
viragtripathi:airflow-timestampdiff-compat
Apr 23, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cd6530b
Compile timestampdiff() to EXTRACT(EPOCH FROM ...) on cockroachdb dia…
viragtripathi fdd219d
Resolve timestampdiff unit at compile time even when passed as BindPa…
viragtripathi b391809
Match MySQL TIMESTAMPDIFF integer-truncation semantics; add WEEK
viragtripathi 602a801
Add live-DB tests + tighten compile-time assertions per @rafiss review
viragtripathi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import pytest | ||
| from sqlalchemy import Column | ||
| from sqlalchemy import Integer | ||
| from sqlalchemy import MetaData | ||
| from sqlalchemy import Table | ||
| from sqlalchemy import TIMESTAMP | ||
| from sqlalchemy import select | ||
| from sqlalchemy import text | ||
| from sqlalchemy.dialects.postgresql import dialect as postgresql_dialect | ||
| from sqlalchemy.sql import func | ||
| from sqlalchemy.testing import fixtures | ||
|
|
||
| from sqlalchemy_cockroachdb.psycopg2 import CockroachDBDialect_psycopg2 | ||
| from sqlalchemy_cockroachdb.stmt_compiler import timestampdiff # noqa: F401 registers compiler | ||
|
|
||
|
|
||
| def _events_table(): | ||
| metadata = MetaData() | ||
| return Table( | ||
| "events", | ||
| metadata, | ||
| Column("id", Integer, primary_key=True), | ||
| Column("start_date", TIMESTAMP), | ||
| Column("end_date", TIMESTAMP), | ||
| ) | ||
|
|
||
|
|
||
| def _compile(stmt, dialect): | ||
| return str(stmt.compile(dialect=dialect, compile_kwargs={"literal_binds": True})) | ||
|
|
||
|
|
||
| class TimestampdiffCompilerTest(fixtures.TestBase): | ||
| """Compile-only tests: no live database connection required.""" | ||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _setup(self): | ||
| self.dialect = CockroachDBDialect_psycopg2() | ||
| self.events = _events_table() | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "unit,expected_suffix", | ||
| [ | ||
| ("MICROSECOND", "* 1000000"), | ||
| ("MILLISECOND", "* 1000"), | ||
| ("MINUTE", "/ 60"), | ||
| ("HOUR", "/ 3600"), | ||
| ("DAY", "/ 86400"), | ||
| ], | ||
| ) | ||
| def test_compiles_with_arithmetic_suffix(self, unit, expected_suffix): | ||
| expr = func.timestampdiff(text(unit), self.events.c.start_date, self.events.c.end_date) | ||
| sql = _compile(select(expr), self.dialect) | ||
| assert "EXTRACT(EPOCH FROM" in sql | ||
| assert "AS NUMERIC" in sql | ||
| assert expected_suffix in sql | ||
|
|
||
| def test_seconds_has_no_arithmetic_suffix(self): | ||
| expr = func.timestampdiff(text("SECOND"), self.events.c.start_date, self.events.c.end_date) | ||
| sql = _compile(select(expr), self.dialect) | ||
| assert "EXTRACT(EPOCH FROM" in sql | ||
| assert "AS NUMERIC" in sql | ||
| after_cast = sql.split("AS NUMERIC", 1)[1] | ||
| assert " * " not in after_cast | ||
| assert " / " not in after_cast | ||
|
|
||
| def test_lowercase_unit_accepted(self): | ||
| expr = func.timestampdiff( | ||
| text("microsecond"), self.events.c.start_date, self.events.c.end_date | ||
| ) | ||
| sql = _compile(select(expr), self.dialect) | ||
| assert "EXTRACT(EPOCH FROM" in sql | ||
| assert "* 1000000" in sql | ||
|
|
||
| def test_unknown_unit_rejected(self): | ||
| expr = func.timestampdiff( | ||
| text("FORTNIGHT"), self.events.c.start_date, self.events.c.end_date | ||
| ) | ||
| with pytest.raises(ValueError, match="Unsupported timestampdiff"): | ||
| _compile(select(expr), self.dialect) | ||
|
|
||
| def test_wrong_arity_rejected(self): | ||
| expr = func.timestampdiff(text("SECOND"), self.events.c.start_date) | ||
| with pytest.raises(ValueError, match="3 arguments"): | ||
| _compile(select(expr), self.dialect) | ||
|
|
||
| def test_postgresql_dialect_unaffected(self): | ||
| """The cockroachdb compiler hook must not change rendering for other dialects.""" | ||
| expr = func.timestampdiff(text("SECOND"), self.events.c.start_date, self.events.c.end_date) | ||
| sql = _compile(select(expr), postgresql_dialect()) | ||
| assert "EXTRACT" not in sql | ||
| assert "timestampdiff" in sql.lower() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.