ClickHouse: seven SQL shapes planned that ClickHouse rejects or answers differently (lag/lead, CUME_DIST, EXTRACT(DOW), correlated EXISTS, CAST(NULL), date_trunc type, alias in HAVING)
Describe the bug
Running a benchmark of 111 analytical queries through the Python SDK (wrenai 0.15.0) against ClickHouse 25.3, with DuckDB as the reference, found seven SQL shapes that the ClickHouse planning emits in a form ClickHouse either rejects or answers differently from other engines. 14 of the 111 queries were affected.
In every case below, the input is MDL SQL against models. The "planned" SQL is the outer statement of WrenEngine.dry_plan(...) with data_source=clickhouse, with the per-model CTEs left out. Each one reproduces by running that planned SQL directly on ClickHouse, so none of it comes from client-side handling.
| # | MDL SQL | Planned for ClickHouse | ClickHouse result |
|---|---|---|---|
| 1 | LAG(x) OVER (ORDER BY d), LEAD(x, 1) OVER (...) |
lag(x) OVER (...), lead(x, 1) OVER (...) |
Code 63: Aggregate function with name 'lag' does not exist |
| 2 | CUME_DIST() OVER (ORDER BY x) |
passed through verbatim | Code 63: no such function in 25.3 (PERCENT_RANK works) |
| 3 | EXTRACT(DOW FROM d) |
EXTRACT(DOW FROM d) |
syntax error |
| 4 | correlated EXISTS / NOT EXISTS |
passed through, not decorrelated | not supported by ClickHouse 25.3 |
| 5 | CAST(NULL AS DOUBLE) |
CAST(NULL AS Float64) |
rejected: a NULL needs Nullable(Float64) |
| 6 | date_trunc('month', d) |
dateTrunc('MONTH', d) |
runs, but returns Date for day/week/month/quarter/year where other engines return a timestamp (2026-07-01 vs 2026-07-01T00:00:00) |
| 7 | SELECT SUM(c) AS c ... HAVING SUM(c) > 0 |
unchanged | Code 184 ILLEGAL_AGGREGATION: ClickHouse resolves the alias c inside HAVING, so it reads SUM(SUM(c)) |
To Reproduce
Any MDL with a ClickHouse data source. For example, with a model impressions(event_date DATE, ...):
from wren import WrenEngine, DataSource
engine = WrenEngine(manifest_str=..., data_source=DataSource.clickhouse, connection_info=...)
engine.dry_plan("""
SELECT event_date, impressions,
LAG(impressions) OVER (ORDER BY event_date) AS prev_day
FROM (SELECT event_date, COUNT(*) AS impressions FROM impressions GROUP BY event_date) d
""")
# -> ... lag(impressions) OVER (ORDER BY event_date) AS prev_day ... (ClickHouse: code 63)
engine.dry_plan("SELECT EXTRACT(DOW FROM event_date) AS dow, COUNT(*) FROM impressions GROUP BY 1")
# -> SELECT EXTRACT(DOW FROM event_date) ... (ClickHouse: syntax error)
engine.dry_plan("""
SELECT p.tier, COUNT(*) FROM publishers p
WHERE EXISTS (SELECT 1 FROM fraud_signals f WHERE f.publisher_id = p.publisher_id AND f.blocked)
GROUP BY p.tier
""")
# -> ... WHERE EXISTS(SELECT 1 FROM fraud_signals AS f WHERE f.publisher_id = p.publisher_id ...)
# (ClickHouse 25.3: correlated subqueries are not supported)
engine.dry_plan("SELECT dsp_id, SUM(clicks) AS clicks FROM daily_campaign_spend GROUP BY dsp_id HAVING SUM(clicks) > 0")
# -> unchanged (ClickHouse: code 184)Expected behavior
The planned ClickHouse SQL returns what the same MDL query returns on other engines. Here are equivalents we verified on ClickHouse 25.3 against DuckDB, in case they are useful:
lagInFrame(toNullable(x)) OVER (ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), and the same withleadInFrame. The explicit frame matters, because the default frame ends at the current row, soleadInFramewould see nothing ahead.toNullablekeeps the first/last rowNULLrather than the type default0.count(*) OVER (PARTITION BY p ORDER BY x) / count(*) OVER (PARTITION BY p). The default frame of an ordered window includes peers, which is CUME_DIST's numerator.toDayOfWeek(d) % 7, which gives Sunday = 0.toDayOfWeek(d)alone starts at Monday = 1 and would be off by one rather than an error.ISODOWmaps totoDayOfWeek(d).- For the single-table, equality-correlated shape:
(k IS NOT NULL AND k IN (SELECT inner_k FROM t WHERE <local filters> AND inner_k IS NOT NULL)).NOT EXISTSis theNOTof that expression, which keeps aNULLkey unmatched, asNOT EXISTSrequires. CAST(NULL AS Nullable(Float64)).toDateTime(dateTrunc('MONTH', d), 'UTC')for the Date-returning units.- Qualify the shadowed column inside the aggregate (
HAVING SUM(daily_campaign_spend.clicks) > 0), because a qualified name never resolves to an alias.- The server setting
prefer_column_name_to_alias = 1fixesHAVING, but it breaksSELECT SUM(c) AS c ... ORDER BY cwith code 215, so it is not a substitute.
- The server setting
A related ClickHouse default that is not a planning bug, but is easy to trip over: with join_use_nulls = 0 (the server default), an unmatched LEFT JOIN column is ''/0 instead of NULL, so COALESCE(right.col, 'x') never fires. It may be worth setting join_use_nulls = 1 in the ClickHouse connector's default settings.
Wren AI Information
- Version:
wrenai0.15.0 (Python SDK). sqlglot 30.19.0, clickhouse-connect 1.9.0 - ClickHouse: 25.3.14.14 (official build)
Additional context
Found with a deterministic benchmark: the same generated dataset is loaded into ClickHouse and DuckDB, and every query's result is compared cell by cell. The 97 other queries (joins, aggregations, windows, CTEs, filters, top-N, funnels) matched exactly. Happy to provide more repros for any of the above.
Source: Canner/WrenAI