Skip to content

Cherry pick #121065 to 26.8: Keep detecting an always-false equals chain when comparison pruning is off - #121727

Merged
robot-clickhouse merged 4 commits into
backport/26.8/121065from
cherrypick/26.8/121065
Sep 23, 2026
Merged

robot-clickhouse merged 4 commits into
backport/26.8/121065from
cherrypick/26.8/121065

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #121065

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 3 commits September 19, 2026 13:18
…s off

`optimize_redundant_comparisons` (new in 26.7, #99736) gates the whole of
`addComparisonFilter` behind an early return at the top of the function: with
the setting off, every operand is parked in `opaque_filters` unanalysed, so the
function never returns ALWAYS_FALSE, `found_conflict` is never set, and the AND
is never collapsed to a constant false.

Detecting that two `equals` on the same expression carry different values is
older than that setting. `v26.6.1.1193-stable` calls
`tryOptimizeAndEqualsNotEqualsChain` with no setting gate at all, and declares
no `optimize_redundant_comparisons` (grep -c = 0); #99736 deleted that function
and folded its contradiction check into the new, gated analysis. Since the
setting is recorded in the 26.7 block of SettingsChangesHistory.cpp, every
`compatibility` below 26.7 now removes a fold that 25.12 genuinely performed,
and a query whose WHERE is always false executes its full plan. On the reported
reproducer that is a LEFT JOIN onto an `argMax ... GROUP BY` subquery over 2.2M
rows: 2,200,000 rows read where 0 are needed, 51 ms -> 3641 ms on the
customer's data.

So detect that one contradiction regardless of the setting, and leave pruning,
boundary folding and strengthening (all genuinely introduced with it) gated.
Neutralizing the history entry instead would be wrong: v26.7.1.1315-stable both
ships the setting and records it in its own newest block, so the placement is
correct, and that route also leaves an explicit `optimize_redundant_comparisons
= 0` regressed.

Why comparing against a single stored representative is equivalent to a full
pairwise scan, which is what keeps the disabled path linear in the operands of
the AND: the only question asked is whether two `equals` on this expression
carry different values, every `equals` is compared against the representative,
and `found_conflict` is only ever set, never cleared. The representative is
recorded as a position rather than a pointer or iterator because
`opaque_filters.push_back` reallocates; in disabled mode that vector only ever
grows, since no erase path reaches it.

The new block sits below every existing keep-opaque guard (nullable result,
non-lossless conversion, NaN constant), so the disabled path is a strict subset
of the enabled one and inherits any guard added above it, and it reaches the
collapse through the single existing `found_conflict` site, so that site's
non-convertible-operand veto still keeps a TYPE_MISMATCH the unoptimized query
would raise from being suppressed. For EQUALS vs EQUALS
`compareComparisonFilters` returns only PRUNE_RIGHT or ALWAYS_FALSE, never a
STRENGTHEN_*, so nothing is mutated on this path.

Three narrowings are deliberate. A duplicate `a = 1 AND a = 1` is still not
deduplicated with pruning off, although 26.6 dropped it: that is pure
evaluation cost with no result change. A non-convertible constant (`Int32 a = 3
AND a = 3.5`) still does not fold, because reusing the enabled path's
lossless-conversion requirement is what keeps 26.6's wrong result for `Float64 a
= 1 AND a = 1.0` away. And a contradiction `optimize_and_compare_chain` derives
now folds the AND with pruning off, which is what its appended plain conjunct is
for; 26.6 never derived one.

Twelve reference lines change across the three 04032* files, every one an
`optimize_redundant_comparisons = 0` case holding two `equals` on the same
expression with different, losslessly convertible values.

Closes: #121007
Closes: #121012

Co-Authored-By: Claude Opus 5 <[email protected]>
Drop the block comment restating the setting split, cut the new field doc and the addComparisonFilter, method and header doc clauses to one line each.
…n-without-pruning

Keep detecting an always-false equals chain when comparison pruning is off
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 23, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

This arm is blocked on #121750, and it clears itself once that lands

The conflict is one hunk in src/Analyzer/Passes/LogicalExpressionOptimizerPass.cpp and it is not a real
divergence. #121065 removes the if (!enable_pruning) early return at the top of addComparisonFilter;
the block that sits right after it on master, the isNullableOrLowCardinalityNullable(...) guard added by
#111535, does not exist on 26.8. The two blocks have byte-identical bodies, so git aligns them and reports
a content conflict.

#111535 is now being backported to 26.8 as #121750 (mergeable, CI running), so _retry_cherrypick clears
this arm by itself once #121750 merges. I checked that end state locally instead of assuming it, on a debug
build with 26.8's own clang-22 pin:

Merge order matters, and in one direction only. With #121750 first, the retried cherry-pick is clean. With
this arm hand-resolved first, #121750 then conflicts on the same file (measured both ways with
git merge-tree). A hand resolution here would also have to pick between dropping #111535's guard, which
leaves the !raw_type->isNullable() logical error in place on 26.8 (SELECT tuple((materialize(toNullable(NULL)) = 1) AND (assumeNotNull(materialize(toNullable(NULL))) = 2)) SETTINGS optimize_and_compare_chain = 1 still
aborts a debug server built from the current 26.8 tip), and pulling part of #111535 in without its test.
So I am not pushing one. If #121750 is dropped for any reason, I have the #121065-only resolution built and
tested here and can put it on my fork on request.

@robot-clickhouse

Copy link
Copy Markdown
Member

The 26.8 branch moved since this cherry-pick was created (now at 144994b). Re-tried the merge against it and it applies cleanly, so the bot is proceeding without manual resolution.

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

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants