Repository navigation
Do not evaluate timestamp on rows that a preceding condition excludes - #132120
Conversation
`timestamp(expr[, expr_time])` parses its arguments row by row and throws
`CANNOT_PARSE_DATETIME` (or `ATTEMPT_TO_READ_AFTER_EOF` for an empty time) on
a value it cannot parse, but `isSuitableForShortCircuitArgumentsExecution`
returned `false`. Every mechanism that keeps a function behind the condition
in front of it reads that predicate: `findLazyExecutedNodes` (`if`, `multiIf`,
`and`, `or`), the PREWHERE read-step splitter, the PREWHERE condition order in
`MergeTreeWhereOptimizer`, and the `sum(if(...))` -> `sumIf` rewrite. So with
default settings these failed although the condition excludes the only
unparsable row:
SELECT if(s != 'x', timestamp(s), NULL) FROM (SELECT arrayJoin(['x', '2025-01-01']) AS s);
SELECT payload FROM u WHERE s != 'invaliddatetime' AND timestamp(s) > '2020-01-01';
The String-parsing siblings (`parseDateTime`, `parseTimeDelta`, `makeDate`,
`toDateTime64` and the other String conversions) already return `true`,
which is why `toDateTime64(s, 6)` works in exactly the same queries.
Return `true`, and also describe `canThrowIfKnown` as `true`, the way
`repeat` does: today `canThrow` falls back to the same predicate, so this
changes nothing by itself, but keeps the answer right when that fallback
becomes conservative (the TODO in `IFunctionAdaptors.h`).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the canThrowIfKnown comment in timestamp.cpp to the function's property. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding), plus my own cold review (0 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261116-195400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261116-192800 |
|
Workflow [PR], commit [a1729f8] Summary: ✅
AI ReviewSummary
Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline. |
cec9a0c
|
@groeneai @PedroTadim — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
Since ClickHouse#132120 `timestamp` no longer stops the split of an AND chain in `FilterStep`, so in `WHERE timestamp(d, t) > x AND d = c` the equality is filtered first and `optimize_constant_columns_after_filter` passes `d` to `timestamp` as a constant. Master throws ILLEGAL_COLUMN for both argument positions; this PR returns the rows. The queries pin the four settings that path needs (switching any one of them off makes master pass), since `query_plan_merge_filters` is randomized by the runner. Closes ClickHouse#133331 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #129178
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
timestampthrowingCANNOT_PARSE_DATETIMEfor rows that a preceding condition excludes, inif,multiIf,AND/OR,PREWHERE(also aWHEREmoved toPREWHERE) andsum(if(...)).timestampis now evaluated lazily behind such conditions, liketoDateTime64.Description
On 26.10.3.7 (also 26.3, 26.8 and master),
SELECT if(s != 'x', timestamp(s), NULL) FROM (SELECT arrayJoin(['x', '2025-01-01']) AS s)fails withCANNOT_PARSE_DATETIME, although the condition excludes the only row that cannot be parsed. With default settings the same happens withmultiIf,AND/OR, aWHEREmoved toPREWHERE, an explicitPREWHERE,sum(if(...)), and for the second argument.toDateTime64(s, 6)works in all of these.Root cause.
timestampparses every row and throws on a value it cannot parse, but returnedfalsefromisSuitableForShortCircuitArgumentsExecution. Lazy execution ofif/multiIf/and/or, the PREWHERE read-step split, the PREWHERE condition order and thesumIfrewrite all read this flag, so they rantimestampon every row. The String-parsing siblings (parseDateTime,makeDate, thetoDateTime64conversions) returntrue.Change. Return
true, and also describecanThrowIfKnownastrue, asrepeatdoes. Expressions withtimestampbehind such a condition now run lazily, as withtoDateTime64.Other functions also report
falsewhile throwing on row values (for exampletoModifiedJulianDay,IPv4StringToNum,toBool); there is no can-throw oracle in the tree yet (see the TODO oncanThrowinIFunctionAdaptors.h), so each needs the same change. This PR only fixestimestamp.Tests. New
05814_timestamp_short_circuitfails on master in each of its 12 guarded cases and passed 50 randomized runs with the fix. Found while answering a review comment on #129178.Workflow [PR]
Sync PR [sync-upstream/pr/132120]
Version info
26.12.1.29-master(included in26.12and later)