Skip to content

Keep a PREWHERE guard on a subcolumn ahead of a throwing condition on its sibling - #131054

Merged
fm4v merged 1 commit into
masterfrom
nik/fix-prewhere-guard-sibling-subcolumn
Nov 12, 2026
Merged

fm4v merged 1 commit into
masterfrom
nik/fix-prewhere-guard-sibling-subcolumn

Conversation

@fm4v

@fm4v fm4v commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Closes: #130086
Caused by: #124553
Related: #111954
Related: #129178

Since #124553, MergeTreeWhereOptimizer::analyzeImpl groups conditions by the exact columns they read instead of by the physical column. In WHERE t.safe != '' AND toUInt64(t.val) > 50, the two conditions on subcolumns of one Tuple (also JSON paths and keys of a with_buckets Map) became separate Conditions, were ordered by size, and the smaller toUInt64(t.val) ran first on the rows the guard rejects, throwing CANNOT_PARSE_TEXT. Before that PR they were one group in the written order, and the may_throw split from #111954 in tryBuildPrewhereSteps filtered before the conversion.

This PR restores the grouping by physical columns only where it matters: when a condition that may throw (the same isSuitableForShortCircuitArgumentsExecution predicate as tryBuildPrewhereSteps) follows a condition over the same physical columns, all conditions over those physical columns are grouped by them. All other conditions keep the exact-column grouping, so the gain of #124553 for #124535 is kept. tryBuildPrewhereSteps still builds exact-column steps from the group.

This is a narrow fix of the regression only. A throwing condition on a different column than its guard (a != '' AND toUInt64(b) > 50) can still be reordered ahead of it, as before #124553; that general case is addressed by #129178.

The new test 05627_prewhere_guard_sibling_subcolumn fails on master (7b5f2bf328f9) for all three cases and passes on the parent of the merge of #124553 (c606f2c4c6f6). #124553 was backported to 26.10, 26.9, 26.8 and 26.3, so this needs the same backports.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed a query like WHERE t.safe != '' AND toUInt64(t.val) > 50 throwing an exception such as CANNOT_PARSE_TEXT when the condition on one subcolumn of a Tuple, JSON or bucketed Map column guards a conversion on another subcolumn of the same column: moving the conditions to PREWHERE no longer evaluates the conversion before the guard.


Workflow [PR]
Sync PR [sync-upstream/pr/131054]

Version info

  • Merged into: 26.11.1.2122-master (included in 26.11 and later)

… its sibling

Since #124553, `MergeTreeWhereOptimizer` groups conditions by the exact columns they read, so in `t.safe != '' AND toUInt64(t.val) > 50` the two conditions on subcolumns of one `Tuple` (or `JSON`, or bucketed `Map`) became separate groups and were reordered by size, and `toUInt64` ran on the rows the guard rejects.

When a condition that may throw follows a condition over the same physical columns, group all conditions over these physical columns by them again, as before that PR. `tryBuildPrewhereSteps` still splits the group into exact-column steps and filters before the throwing one.

Closes: #130086
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [644fd94]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, parallel) FAIL
05137_lazy_load_tables_replace_proxy_after_load FAIL cidb

AI Review

Summary

Restores physical-column grouping when a potentially throwing PREWHERE predicate follows a guard on a sibling subcolumn, preserving their order while retaining separate read steps. Reviewed head 644fd949c65e4c00410dde856b7b44f7acbf4882 and all prior discussion; no actionable findings. The three new regression cases pass locally with the PR binary with multiple read steps enabled and disabled. CI also confirms that the existing subcolumn read-cost regression test passes.

Final Verdict

✅ Approve.

@fm4v
fm4v requested a review from Avogar November 12, 2026 13:17
@Avogar Avogar self-assigned this Nov 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 644fd949c with master 43871b94f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.68, programs/clickhouse-keeper ×0.68 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.6 s 14.4 s +10.6 s (+280%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.8 s +5.6 s (+248%)
Compile time of recompiled translation units

7 translation units recompiled, 15 s compile time in total, 7 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 12, 2026
@fm4v
fm4v added this pull request to the merge queue Nov 12, 2026
Merged via the queue into master with commit e6ebb2a Nov 12, 2026
200 of 203 checks passed
@fm4v
fm4v deleted the nik/fix-prewhere-guard-sibling-subcolumn branch November 12, 2026 19:05
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 12, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 12, 2026
Backport #131054 to 26.8: Keep a PREWHERE guard on a subcolumn ahead of a throwing condition on its sibling
clickhouse-gh Bot added a commit that referenced this pull request Nov 12, 2026
Backport #131054 to 26.10: Keep a PREWHERE guard on a subcolumn ahead of a throwing condition on its sibling
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…umn ahead of a throwing condition on its sibling
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…lumn ahead of a throwing condition on its sibling
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

@fm4v, the 26.8 backport of this PR (#131109) makes the server crash with SIGSEGV in conditionMayThrow, and the open 26.9 backport #131110 does the same. Reproducer, with the analyzer on:

CREATE TABLE t (a UInt64, b String) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t VALUES (1, 'x');
EXPLAIN AST optimize = 1 SELECT a, b FROM t WHERE a = 1 SETTINGS query_plan_optimize_prewhere = 0;
build result
26.8.34.13-lts (before #131109) OK, 0 of 3 runs crash
26.8.37.168-lts (released today) SIGSEGV, 3 of 3 runs
CI build of #131110 (26.9) SIGSEGV
26.9.28.166, 26.10.4.207, master OK

On 26.8 a plain SELECT with enable_analyzer = 0 and query_plan_optimize_prewhere = 0 (or query_plan_enable_optimizations = 0) crashes the same way. 26.8 CI hit it through the AST fuzzer: Stress test (arm_msan).

Cause: on 26.3, 26.8 and 26.9, InterpreterSelectQuery (the old analyzer, and EXPLAIN AST optimize = 1 even with the analyzer on) still calls the AST overload MergeTreeWhereOptimizer::optimize(SelectQueryInfo &, ...) when the query plan does not optimize PREWHERE. That overload builds the RPNBuilderTreeNode from the AST, so getDAGNode() returns nullptr, and conditionMayThrow(infos[i].node.getDAGNode()) dereferences it. Master and 26.10 have no AST overload, so they are not affected. The 26.3 cherry-pick #131108 adds the same call, and 26.3 has the AST overload (code reading, I did not build it).

Which do you prefer for 26.8, 26.9 and 26.3:

Also on 26.8, the backport does not fix 05627_prewhere_guard_sibling_subcolumn under query_plan_merge_filters = 0, because 26.8 lacks #111954: #124553 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport v26.8-must-backport v26.9-must-backport v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PREWHERE reorder can run a throwing conversion before its sibling guard

6 participants