Repository navigation
Conversation
Push down `date_bin(interval, timestamptz, timestamptz)` when the stride is a constant, positive interval without months and the origin is a finite constant. All other calls evaluate in Postgres, as does `date_bin` on `timestamp` values. Deparse with microsecond intervals on `DateTime64(6)`. ClickHouse intervals of an hour or longer align to local midnight, and intervals of a second or longer return `DateTime`, which cannot hold a time before 1970. Microsecond intervals align to the Unix epoch, so shift the value by the offset of the origin from the epoch.
Push down `time_bucket(interval, timestamptz)`, with or without an origin, with the same `toStartOfInterval` SQL as `date_bin()`. Without an origin, buckets align to Monday, 2000-01-03 UTC, as in TimescaleDB. Push down `first(value, time)` and `last(value, time)` to `argMin` and `argMax`. Wrap the value in a tuple, because ClickHouse ignores rows with a NULL value where TimescaleDB returns the NULL. Count the times to return NULL when no row has a time. The other `time_bucket` variants evaluate in Postgres.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #471.
This PR pushes down TimescaleDB
time_bucket(),first(), andlast(). It builds on #472 and includes its commit, so only the second commit is new here. I will rebase when #472 merges.What changed
src/custom_types.c: atimescaledbbranch inchfdw_check_for_custom_function(). It mapstime_bucket(interval, timestamptz), with or without an origin, and thefirstandlastaggregates.src/shipable.c:time_buckethas the same conditions asdate_bin. The stride must be a constant, positive interval without months, and an origin must be a finite constant.src/deparse.c:time_bucketuses thedate_binSQL from Push down date_bin() to toStartOfInterval #472. Without an origin, buckets align to Monday, 2000-01-03 UTC, as in TimescaleDB.firstandlasthave their own function,deparseFirstLast().test/sql/timescaledb.sql, docs, and a CHANGELOG entry.Why
firstandlastare not a plainargMinandargMaxargMinandargMaxignore a row with a NULL value. TimescaleDB returns the NULL when it is on the first or last row. A tuple around the value keeps the NULL.OrNulldoes not accept a tuple on that version, so the SQL counts the times.Not included
time_bucketvariants fortimestamp,date, integer, and UUID values, and the variants with an offset or a time zone. They stay in Postgres.time_bucket_gapfill.Testing
The test compares the pushed-down results with TimescaleDB 2.32 on a local copy of the rows. For
time_bucketit uses 13 strides, with and without an origin. Forfirstandlastit uses NULL values, NULL times,FILTER, and a query with no rows. On PostgreSQL 17 and ClickHouse 26.10, all tests pass exceptsettings, which also fails for me onmain. The new test also passes on ClickHouse 23.3.The test needs
timescaledbinshared_preload_libraries. Without it the test skips, andtimescaledb_1.outis the expected output for that case. CI does not install TimescaleDB, so the test will skip there. Tell me if you want it added to the workflow.