Skip to content

Push down TimescaleDB time_bucket(), first(), and last() - #473

Open
rdurb wants to merge 2 commits into
ClickHouse:mainfrom
rdurb:pushdown-timescaledb
Open

rdurb wants to merge 2 commits into
ClickHouse:mainfrom
rdurb:pushdown-timescaledb

Conversation

@rdurb

@rdurb rdurb commented Nov 18, 2026

Copy link
Copy Markdown

Fixes #471.

This PR pushes down TimescaleDB time_bucket(), first(), and last(). It builds on #472 and includes its commit, so only the second commit is new here. I will rebase when #472 merges.

EXPLAIN (VERBOSE, COSTS OFF)
SELECT time_bucket('1 hour', ts) AS bucket, device,
       first(val, ts), last(val, ts), avg(val)
FROM metrics GROUP BY bucket, device;
 Foreign Scan
   Output: (time_bucket('01:00:00'::interval, ts)), device, (first(val, ts)), (last(val, ts)), (avg(val))
   Relations: Aggregate on (metrics)
   Remote SQL: SELECT toStartOfInterval(toDateTime64(ts, 6), INTERVAL 3600000000 MICROSECOND), device, if(count(ts) > 0, argMin(tuple(val), ts).1, NULL), if(count(ts) > 0, argMax(tuple(val), ts).1, NULL), avg(val) FROM repro.metrics GROUP BY (toStartOfInterval(toDateTime64(ts, 6), INTERVAL 3600000000 MICROSECOND)), device

What changed

  • src/custom_types.c: a timescaledb branch in chfdw_check_for_custom_function(). It maps time_bucket(interval, timestamptz), with or without an origin, and the first and last aggregates.
  • src/shipable.c: time_bucket has the same conditions as date_bin. The stride must be a constant, positive interval without months, and an origin must be a finite constant.
  • src/deparse.c: time_bucket uses the date_bin SQL from Push down date_bin() to toStartOfInterval #472. Without an origin, buckets align to Monday, 2000-01-03 UTC, as in TimescaleDB. first and last have their own function, deparseFirstLast().
  • test/sql/timescaledb.sql, docs, and a CHANGELOG entry.

Why first and last are not a plain argMin and argMax

  • argMin and argMax ignore 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.
  • On ClickHouse 23.3, they return 0 for a group in which every time is NULL. TimescaleDB returns NULL. OrNull does not accept a tuple on that version, so the SQL counts the times.

Not included

  • The time_bucket variants for timestamp, 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_bucket it uses 13 strides, with and without an origin. For first and last it uses NULL values, NULL times, FILTER, and a query with no rows. On PostgreSQL 17 and ClickHouse 26.10, all tests pass except settings, which also fails for me on main. The new test also passes on ClickHouse 23.3.

The test needs timescaledb in shared_preload_libraries. Without it the test skips, and timescaledb_1.out is 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Push down TimescaleDB time_bucket(), first(), and last()

1 participant