Skip to content

Analyze projection queries without parallel replicas, so an IN subquery is not replaced with a temporary table - #129836

Closed
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-projection-in-subquery-parallel-replicas
Closed

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-projection-in-subquery-parallel-replicas

Conversation

@groeneai

@groeneai groeneai commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129425

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 INSERT failing with UNKNOWN_IDENTIFIER into a table with a projection whose query has an IN or GLOBAL IN subquery, when the projection was analyzed with parallel replicas enabled: in the server's default profile (at CREATE TABLE or at server startup), or in the session of ALTER TABLE ... ADD PROJECTION.

Description

On 26.10.2.103, ALTER TABLE t ADD PROJECTION p (SELECT a, b WHERE b IN (SELECT b FROM src) ORDER BY b) run with enable_parallel_replicas = 1 succeeds, and then every INSERT INTO t fails with Code: 47 ... Unknown expression or table expression identifier _data_<hash>. A server whose default profile enables parallel replicas does the same at CREATE TABLE and at startup, which is why 05515_global_subquery_projection_where (#129425) fails on every master run of the ParallelReplicas job: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=4088c421fd27f2ea45bdbf87b2df9233debcfcdd&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29

ProjectionDescription::fillProjectionDescriptionByQuery analyzes the projection query with InterpreterSelectQuery(...).modify(), which rewrites the stored query_ast in place. With parallel replicas enabled, GlobalSubqueriesVisitor handles the source as remote and replaces the subquery with the name of a temporary table, which does not exist when an INSERT later calculates the projection. The fix disables parallel replicas for that analysis, as MutationsInterpreter does for its synthetic queries: a projection is always calculated locally. It also stops CREATE TABLE with such a projection from failing with THERE_IS_NO_QUERY when the default profile has parallel_replicas_allow_in_with_subquery = 0.

Validation: the test now also adds two projections with ALTER TABLE under parallel replicas; it fails on master in every configuration and passes with the fix (50/50 randomized runs, 10/10 with the ParallelReplicas profile). With the fix, inserting with parallel replicas enabled, restarting into that profile, and reading an aggregate projection with parallel replicas on and off all work, and the projection tests show no other difference.

🤖 Generated with Claude Code


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

groeneai and others added 2 commits November 6, 2026 21:45
`ProjectionDescription::fillProjectionDescriptionByQuery` analyzes the
projection query with `InterpreterSelectQuery(...).modify()`, which rewrites
`query_ast` in place, and `calculateByQuery` executes that AST for every
`INSERT`. When the context enables parallel replicas, `ExpressionAnalyzer`
treats the source as remote and `GlobalSubqueriesVisitor` replaces an `IN` or
`GLOBAL IN` subquery with the name of a temporary table `_data_<hash>`. That
table exists only while the analysing query runs, so every later `INSERT`
fails with `UNKNOWN_IDENTIFIER`.

`CREATE TABLE` and server startup analyze the projection with the global
context, so a default profile with parallel replicas breaks it (this is the
ParallelReplicas CI job, where `05515_global_subquery_projection_where`
fails on every run); `ALTER TABLE ... ADD PROJECTION` uses the query context,
so session settings break it in every configuration. With
`parallel_replicas_allow_in_with_subquery = 0` in the default profile the
same branch calls `getQueryContext()` on a global context copy and
`CREATE TABLE` fails with `THERE_IS_NO_QUERY`.

A projection is always calculated locally from the block being written, so
disable parallel replicas for its analysis, as `MutationsInterpreter` does
for its synthetic queries.

The test now also adds projections with `ALTER TABLE` under session parallel
replicas, which fails without the fix in every configuration; the comments
that described the replacement as expected behaviour are corrected.

Related: ClickHouse#129425

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the parallel replicas comment in fillProjectionDescriptionByQuery to one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 6, 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; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The THERE_IS_NO_QUERY shape (default profile with parallel_replicas_allow_in_with_subquery = 0) has no CI test AGREE, noted, not blocking Only reachable through the server default profile, so only an integration test could cover it; measured locally in both directions (master: Code: 393 at CREATE TABLE, fix: CREATE and INSERT OK)

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

Session id: cron:clickhouse-review-slot-48:20261106-215100

@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. ALTER TABLE ... ADD PROJECTION with an IN subquery under enable_parallel_replicas = 1, then INSERT: Code: 47 every time on master and on 26.10.2.103. With the ParallelReplicas profile the unchanged test fails at query 18 exactly as in CI.
b Root cause explained? fillProjectionDescriptionByQuery analyzes the projection with InterpreterSelectQuery(...).modify(), which rewrites the stored query_ast; with parallel replicas GlobalSubqueriesVisitor replaces the subquery with a temporary table name that does not exist at INSERT time.
c Fix matches root cause? Yes. The analysis that persists the AST no longer prepares a multi-server read (parallel replicas disabled in its context, as MutationsInterpreter does). Nothing is guarded at the INSERT.
d Test intent preserved / new tests added? Yes. 05515 keeps every query and reference line and gains an ALTER TABLE ... ADD PROJECTION section (IN and GLOBAL IN); four comments that described the rewrite as expected are corrected.
e Both directions demonstrated? Yes. The extended test fails on master in every configuration and passes with the fix: 50/50 randomized, 10/10 with the ParallelReplicas profile.
f Fix is general across code paths? Yes. CREATE, startup, ALTER ADD/MODIFY PROJECTION and replicated metadata all go through this function; the INSERT path and query-time aggregate projections were checked with parallel replicas on.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. IN and GLOBAL IN, a subquery, a table name, a nested GLOBAL JOIN, shared subqueries; normal and aggregate projections; server profile, session, restart, parallel_replicas_allow_in_with_subquery = 0. Column types play no role.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; a table created by the old version under parallel replicas accepts inserts after the upgrade.
i Invariants and contracts preserved? Yes. query_ast is still analyzed in place as calculateByQuery expects; the result equals what a server without parallel replicas already stores.

Session id: cron:clickhouse-impl-slot-5:20261106-210600

@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai @devcrafter, could you review this and add the can be tested label so CI runs? With parallel replicas enabled, the analysis of a projection query rewrote an IN subquery in the stored AST to a temporary table name, so every later INSERT failed with UNKNOWN_IDENTIFIER (this is why 05515_global_subquery_projection_where fails on master in the ParallelReplicas job); the fix disables parallel replicas for that analysis.

@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ed4362a]

Summary: ❌

job_name test_name status info comment
Fast test (arm_darwin) FAIL
05457_dictionary_remote_clickhouse_invalidate_query FAIL cidb

AI Review

Summary

PR #129836, reviewed at ed4362a892e2750f2e0b6ea3a69b3a7c97ceb3f0, disables parallel replicas in the copied context used to analyze projection metadata. This prevents persistent projection queries from retaining references to temporary tables created for IN and GLOBAL IN subqueries. The extended regression test checks projections added through ALTER TABLE and their stored rows. No actionable findings.

Missing context / blind spots
  • ⚠️ Full ParallelReplicas and bugfix-validation CI results were unavailable at review time. The extended 05515_global_subquery_projection_where test passed in Fast test; a completed ParallelReplicas run would provide independent confirmation under the server default profile. No local build or test run was performed.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). labels Nov 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ed4362a89 with master 0ba40ae08 (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.

Compile time of recompiled translation units

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

Job report

@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - ed4362a

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Fast test (arm_darwin) / 05457_dictionary_remote_clickhouse_invalidate_query not from this PR (it does not touch dictionaries or this test): the test, added to master by #129336, hit the 60 s Fast test timeout; it timed out the same way in 9 PRs and 2 master runs in the last 3 days a fix task is created (investigating at full effort, fixing-PR link to follow here)
Mergeable Check, PR praktika rollups of the row above a fix task is created (the same one as above)

No job was dropped, and CH Inc sync passed. The only other CIDB FAIL rows on this head are Config Workflow / Pre Hooks and Finish Workflow / Post Hooks of the run before the can be tested label; the labelled run passed both. 05515_global_subquery_projection_where passed in every lane.

Session id: cron:our-pr-ci-monitor:20261107-023005

@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for 05457_dictionary_remote_clickhouse_invalidate_query (Fast test timeout) created: #129879

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #129863 (merged), which makes the same change in fillProjectionDescriptionByQuery, also covers calculateByQuery, and tests the ALTER TABLE ... ADD PROJECTION case in 05565. Closing.

@groeneai groeneai closed this Nov 8, 2026
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-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant