Skip to content

Fix NOT_FOUND_COLUMN_IN_BLOCK when an extract alias with several trailing .*$ is reused by GROUP BY - #124482

Merged
fm4v merged 2 commits into
ClickHouse:masterfrom
groeneai:regexp-rewrite-extract-idempotent-tail-strip
Nov 16, 2026
Merged

fm4v merged 2 commits into
ClickHouse:masterfrom
groeneai:regexp-rewrite-extract-idempotent-tail-strip

Conversation

@groeneai

@groeneai groeneai commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

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 NOT_FOUND_COLUMN_IN_BLOCK for a query whose SELECT alias is an extract call with a pattern ending in more than one .*$ and is reused by GROUP BY together with ORDER BY, HAVING, WHERE or another alias, for example SELECT extract(s, '(1).*$.*$') AS r FROM t GROUP BY r ORDER BY r.

Description

On 26.8.18.2, SELECT extract(s, '(1).*$.*$') AS r FROM t GROUP BY r ORDER BY r fails with Code: 10 NOT_FOUND_COLUMN_IN_BLOCK on default settings. It returns the expected rows with enable_identifier_resolve_cache = 0 or optimize_rewrite_regexp_functions = 0.

With enable_identifier_resolve_cache (default since 26.6) the GROUP BY key and its ORDER BY / HAVING / WHERE references are one shared node, while the projection keeps its own copy. RegexpFunctionRewritePass visits the shared node once per reference, and handleExtract removed one trailing .*$ per visit, so the shared node ended with '(1)' and the projection with '(1).*$', and the key names no longer match (EXPLAIN QUERY TREE shows it).

The change removes every trailing unescaped .*$ in one visit, so a second visit changes nothing (the per-pass shape of #108553). Each removed tail is the transformation the pass already applies; the escape check runs for each tail, and the (?-s) and \Q guards are unchanged.

The new test covers GROUP BY + ORDER BY, WHERE, three tails with HAVING, an alias of an alias, a Nullable column and an escaped second tail (the first five fail on master), and pins the rewritten patterns with EXPLAIN QUERY TREE. 50 randomized runs pass, and 158 existing extract / regexp-rewrite tests give the same result on master and with the fix.

Related: #122404. The same class in other passes is handled separately (#123925, #122512, #122726).


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

Version info

  • Merged into: 26.11.1.2451-master (included in 26.11 and later)
  • Backported to: 26.10.5.41

groeneai and others added 2 commits October 7, 2026 19:09
With `enable_identifier_resolve_cache` the GROUP BY key and its ORDER BY,
HAVING and WHERE references are one shared node, while the projection keeps
its own copy. `RegexpFunctionRewritePass` enters the shared node once per
reference, and `handleExtract` removed one trailing `.*$` per visit, so for
a pattern such as `(1).*$.*$` the shared node ended with `(1)` and the
projection with `(1).*$`. The GROUP BY key then no longer matched the
projection and the query failed with NOT_FOUND_COLUMN_IN_BLOCK.

Remove all trailing unescaped `.*$` in a single visit, so a repeated visit
is a no-op. The escape check is evaluated for each tail; `disablesDotAll`
and `endsInsideQuotedLiteral` are evaluated once on the original pattern,
which is equivalent because removing a trailing `.*$` adds or removes no
`(` and no `\E`. This is the per-pass shape of ClickHouse#108553; cloning at the
cache was refused there.

Related: ClickHouse#122404

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The result-only arms also pass for an implementation that leaves a pattern
with several trailing `.*$` unrewritten. Two `EXPLAIN QUERY TREE` lines, in
the style of 03538, now show that `(abc).*$.*$` becomes `(abc)` and that an
escaped tail in `(abc)\.*$.*$` is kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-self PR origin: self-found defect (impact-bar validated) labels Oct 7, 2026
@groeneai

groeneai commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding) plus my own cold review (0 findings in both rounds).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test asserts only results, so it cannot tell "every trailing .*$ stripped" from "rewrite declined" AGREE, fixed @ eb119c9 added EXPLAIN QUERY TREE lines that pin both rewritten patterns; a "decline two or more tails" variant fails them while the six result arms pass

Round 2 (delta 587df7afdcec7..eb119c9ae6d63, tests only): 0 findings.

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-self-slot-58:20261007-202100

@groeneai

groeneai commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new test fails on master with Code: 10 NOT_FOUND_COLUMN_IN_BLOCK every time, with and without random settings.
b Root cause explained? With the identifier resolve cache the GROUP BY key and its ORDER BY / HAVING / WHERE references are one node; the pass visits it once per reference and handleExtract removed one .*$ per visit, so the key and the projection end with different patterns and names.
c Fix matches root cause? Yes. The rewrite is made idempotent (all trailing .*$ in one visit). No guard, no change to the cache or the traversal.
d Test intent preserved / new tests added? New SQL test: six result arms plus two EXPLAIN QUERY TREE lines that pin the rewritten patterns. The escaped-second-tail arm fails if the escape check runs only once; the first EXPLAIN line fails if patterns with several tails are left unrewritten.
e Both directions demonstrated? Yes. Master: five arms fail with Code 10, the sixth passes. Fix: all six match the cache-off output; 50/50 randomized runs pass.
f Fix is general across code paths? replaceRegexpAll / regexp_replace are already idempotent. Other passes with the same class are separate PRs.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Nullable, LowCardinality, constant and constant-folded patterns, ^.* prefix, (?-s), \Q, escaped middle tail, no capture group, one to four tails, remote(): every result equals the result with the rewrite off.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; results are unchanged.
i Invariants and contracts preserved? Yes. The view stays valid until the new constant is built; the escape check reads only bytes before the cut; a capture group always remains, so the pattern cannot become empty.

Session id: cron:clickhouse-impl-self-slot-57:20261007-195800

@clickhouse-gh

clickhouse-gh Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [eb119c9]

Summary: ✅


AI Review

Summary

This PR makes RegexpFunctionRewritePass::handleExtract idempotent by stripping every trailing unescaped .*$ suffix from a constant extract pattern in one visit instead of one suffix per visit. That matches the stated root cause for the NOT_FOUND_COLUMN_IN_BLOCK alias mismatch when a cached identifier node is revisited through GROUP BY-related references, and the new stateless regression test is focused enough to catch both the functional failure and the exact rewritten pattern shape. I did not find unresolved correctness, safety, or rollout issues in the current patch.

Final Verdict
  • Status: ✅ Approve
  • No changes required from this review.

LLVM Coverage Report

Measured on commit eb119c9.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 7/7 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 7, 2026
@groeneai
groeneai requested review from novikd and vdimir October 7, 2026 20:44
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 7, 2026
@fm4v
fm4v added this pull request to the merge queue Nov 16, 2026
Merged via the queue into ClickHouse:master with commit 0957684 Nov 16, 2026
153 of 154 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 16, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 16, 2026
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 16, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #124482 to 26.10: Fix `NOT_FOUND_COLUMN_IN_BLOCK` when an `extract` alias with several trailing `.*$` is reused by `GROUP BY`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). groeneai-origin-self PR origin: self-found defect (impact-bar validated) 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.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants