Skip to content

fix: support ClickHouse ORDER BY WITH FILL and INTERPOLATE (#2467) - #2469

Merged
manticore-projects merged 1 commit into
JSQLParser:masterfrom
fudianchn:fix/clickhouse-order-by-with-fill-2467
Aug 15, 2026
Merged

fix: support ClickHouse ORDER BY WITH FILL and INTERPOLATE (#2467)#2469
manticore-projects merged 1 commit into
JSQLParser:masterfrom
fudianchn:fix/clickhouse-order-by-with-fill-2467

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

Support ClickHouse's ORDER BY ... WITH FILL modifier and the query-level INTERPOLATE clause, following the ClickHouse grammar: per element [WITH FILL] [FROM expr] [TO expr] [STEP expr] [STALENESS expr] (the step may be an INTERVAL literal), and after the ORDER BY list an optional INTERPOLATE [(col [AS expr], ...)] where the bare form fills all allowed columns.

Why

SELECT d, v FROM t ORDER BY d WITH FILL FROM toDate('2024-01-01') TO toDate('2024-02-01') STEP INTERVAL 1 DAY INTERPOLATE (v AS v + 1) currently fails with a ParseException on WITH. WITH FILL is the standard ClickHouse construct for gap filling in time series, so statements using it cannot be parsed at all.

How

  • OrderByElement carries a new WithFill holder (its presence marks the element as filled; from/to/step/staleness are optional expressions). This mirrors ClickHouse, where the bounds belong to the order by element.
  • Select (shared by PlainSelect, ParenthesedSelect and SetOperationList) carries the List<InterpolateElement> as a sibling of orderByElements, mirroring ClickHouse, where INTERPOLATE is a query-level attribute. The existing union hoisting for orderByElements is extended so SELECT ... UNION SELECT ... ORDER BY x WITH FILL INTERPOLATE (...) lands on the SetOperationList.
  • Rendering in both toString() and the deparser (bounds rendered through the expression visitor), and OrderByValidator/SelectValidator walk the new expressions.
  • FILL, STEP, STALENESS and INTERPOLATE are declared as non-reserved keywords (inside the NonReservedWord range), so they remain usable as plain column names (SELECT fill, step FROM t ORDER BY interpolate still parses).

Root cause

The grammar had no production for the WITH FILL modifier family and no INTERPOLATE clause; the element-level bounds and the query-level column list are modeled after the ClickHouse reference parser (bounds only parsed after WITH FILL, in fixed FROM/TO/STEP/STALENESS order).

Testing

  • ClickHouseTest: 9 new tests covering the issue statement, bare WITH FILL, FROM/TO/STEP, STEP INTERVAL plus STALENESS, DESC/NULLS LAST combinations, INTERPOLATE variants (multiple items, item without AS, bare INTERPOLATE), a UNION with hoisting, a subquery, and column names using the new keywords.
  • ./gradlew test: 4793 tests, 0 failures (26 skipped, pre-existing).

Performance

gradle jmh, JSQLParserBenchmark.parseSQLStatements on performance.sql, version=latest, 10 forks × 10 iterations (100 samples) on a 32-core host:

build ms/op
master 3.689 ± 0.019
branch 3.690 ± 0.020

The difference is within the confidence intervals: no regression.

Notes

Fixes #2467

…r#2467)

Parse the ClickHouse WITH FILL modifier on ORDER BY elements together
with the query-level INTERPOLATE clause, matching the upstream grammar:
per element [WITH FILL] [FROM e] [TO e] [STEP e] [STALENESS e] (the step
may be an INTERVAL literal), and after the ORDER BY list an optional
INTERPOLATE [(col [AS expr], ...)] where the bare form fills all allowed
columns.

OrderByElement carries a new WithFill holder and Select (shared by
PlainSelect and SetOperationList, with union hoisting) carries the
interpolate column list. Rendering goes through both toString and the
deparser, and the validators walk the new expressions. The keywords
FILL, STEP, STALENESS and INTERPOLATE are declared non-reserved, so they
remain usable as plain column names.

Signed-off-by: 付典 <fudianchn@gmail.com>
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you much!

@manticore-projects
manticore-projects merged commit 406a4d4 into JSQLParser:master Aug 15, 2026
7 checks passed
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.

[BUG] JSQLParser 5.4-SNAPSHOT : ClickHouse : ORDER BY ... WITH FILL / INTERPOLATE not supported

2 participants