Repository navigation
Derive optimize_and_compare_chain ranges through a DateTime constant compared with DateTime64 - #123796
Conversation
… DateTime64 Since ClickHouse#110621, `optimize_and_compare_chain` joins a comparison into a transitive chain only when both sides share one `ComparisonOrderDomain`, and `TimePoint` is keyed by scale (`DateTime` 0, `DateTime64(s)` s). A `DateTime` constant compared with a `DateTime64(6)` column therefore has no domain and the edge is skipped. In the query of ClickHouse#123706 (`t1.c3 < e1 AND t1.c3 >= t2.c3 AND t1.c3 <= t2.c3 + INTERVAL 48 HOUR`, `e1` a `DateTime`) `t2.c3 < e1` is no longer derived and t2 is read in full: 26.7 prunes it to 4/29 parts, 26.8 and master read 29/29. The comparison itself widens such a constant exactly to the column's scale. Do the same once, in the step that already retypes String constants (`tryNormalizeConstStringEdge`, now `tryNormalizeConstEdge`): a `DateTime` or lower-scale `DateTime64` constant compared with a `DateTime64` expression is converted with `tryConvertFieldToTypeExact`, which returns Null on overflow or loss. The edge then joins the expression's own `TimePoint(s)` domain. Every derived comparison is between equal `DateTime64` scales, so it cannot rescale or throw, and the inferred-domain check holds. A higher-scale or overflowing constant stays out of the chain as before. `Date` constants are not included, their conversion to a time point depends on the time zone. Closes ClickHouse#123706 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Within one source the derived bound is added as `indexHint`, which never executes, and `values()` has no index, so those counts printed 1 whatever the widened constant was. A one-row `MergeTree` table ordered by the hinted column makes a shifted or truncated bound prune the row (count 0). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge the isLowerScaleTimePoint comment into one line and condense the tryNormalizeConstEdge comment to three lines. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (2 rounds): 0 Gate B findings; 1 test-robustness item and 2 nits from my own review, all resolved
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261004-004600 |
|
Workflow [PR], commit [d7148fd] Summary: ✅
AI ReviewSummaryThis PR restores Final Verdict
LLVM Coverage ReportMeasured on commit d7148fd.
Changed lines: Changed C/C++ lines covered: 30/30 (100.00%) · Uncovered code |
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, 13 s compile time in total, 7 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR changes one rewrite in the analyzer: optimize_and_compare_chain now also handles comparisons where a DateTime or lower-scale DateTime64 constant meets a DateTime64 expression. It only runs at query-planning time, and only for such comparisons. None of the three flagged ClickBench queries (Q18 at +12.2%, Q33 at +5.1%, Q34 at +8.9%) has a filter like that; all are heavy GROUP BY queries, so the rewrite is never triggered. All three stay inside master's normal range, and two ClickBench PR runs were scheduled on the same machine, which fits the similar CPU rise across these unrelated queries. Keeping them as not_sure rather than calling them PR effects matches the diff, and there is no performance concern for this change. clickbenchFlagged queries (3 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
@yariks5s, both backports (#124065 for 26.8, #124066 for 26.9) fail to compile because
- converted = tryConvertFieldToTypeExact(constant_node->getValue(), *other_type, constant_type.get());
+ converted = tryConvertFieldToType(constant_node->getValue(), *other_type, constant_type.get(), {}, /*strict=*/true);This gives the same result as master.
Backporting #121078 instead would not apply cleanly: it edits the I worked this out by reading the code and did not build the branches. |
…his branch tryConvertFieldToTypeExact comes from #121078, which is not on this branch. At this call site only a widening conversion (DateTime or lower-scale DateTime64 to DateTime64) reaches it, which is exact or overflows to Null, so the strict tryConvertFieldToType gives the same result. In 05330, the "implied condition is not duplicated" case compares `a` with a DateTime64 constant instead of a DateTime one: on this branch the existing `a < <DateTime>` conjunct hits TYPE_MISMATCH in tryConvertToColumnType (#117903, fixed on master by #121078), so it is not seen as implying the derived bound. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…his branch tryConvertFieldToTypeExact comes from #121078, which is not on this branch. At this call site only a widening conversion (DateTime or lower-scale DateTime64 to DateTime64) reaches it, which is exact or overflows to Null, so the strict tryConvertFieldToType gives the same result. In 05330, the "implied condition is not duplicated" case compares `a` with a DateTime64 constant instead of a DateTime one: on this branch the existing `a < <DateTime>` conjunct hits TYPE_MISMATCH in tryConvertToColumnType (#117903, fixed on master by #121078), so it is not seen as implying the derived bound. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #123796 to 26.8: Derive `optimize_and_compare_chain` ranges through a `DateTime` constant compared with `DateTime64`
Backport #123796 to 26.9: Derive `optimize_and_compare_chain` ranges through a `DateTime` constant compared with `DateTime64`
…n` ranges through a `DateTime` constant compared with `DateTime64`
Closes: #123706
Related: #110621
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
optimize_and_compare_chainagain derives transitive conditions through aDateTimeor lower-scaleDateTime64constant compared with aDateTime64expression, as it did before 26.8. For example,t1.ts < toDateTime('2026-09-16 00:00:00') AND t1.ts >= t2.tsaddst2.ts < toDateTime('2026-09-16 00:00:00'), so the other side of a join can be pruned by its partition and primary key instead of being read in full. Closes #123706.Description
Since #110621,
optimize_and_compare_chainchains two comparisons only when both have the same order domain, and theDateTime64domain is keyed by scale. ADateTimeconstant (scale 0) compared with aDateTime64(6)column has no domain, so in the query of #123706 (t1.c3 < e1 AND t1.c3 >= t2.c3 AND t1.c3 <= t2.c3 + INTERVAL 48 HOUR,e1aDateTime) the edge is skipped andt2.c3 < e1is no longer derived.The comparison itself widens such a constant exactly to the column's scale. This PR does that once, before the chain is built, in the step that already retypes
Stringconstants: aDateTimeor lower-scaleDateTime64constant compared with aDateTime64expression is converted withtryConvertFieldToTypeExact. A higher-scale or overflowing constant stays out of the chain, so every derived comparison is between equal scales and cannot throw.Dateconstants are not included (their conversion depends on the time zone);Time64andDecimalconstants have the same scale-keyed exclusion and are left as they are.Validation on the issue's synthetic data and settings: t2 Min-Max
Parts: 29/29->4/29,SelectedRows20,998,859 -> 4,319,564, same result. With FINAL this needsdefer_partition_pruning_after_final = 0(the issue'scompatibility = '25.4'); without FINAL it applies at defaults. The new test fails on master on 4 arms and passes with the fix.Workflow [PR]
Sync PR [sync-upstream/pr/123796]
Version info
26.10.1.1597-master(included in26.10and later)26.9.18.4,26.8.26.4