Skip to content

Calculate a projection without resolving its positional arguments again - #126185

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:projection-positional-arguments-calculating-context
Oct 17, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:projection-positional-arguments-calculating-context

Conversation

@groeneai

@groeneai groeneai commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118375

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 an INSERT failing with NOT_FOUND_COLUMN_IN_BLOCK when enable_positional_arguments_for_projections was enabled for it and a projection of the table had an integer constant in GROUP BY, like GROUP BY 'a', 1.

Description

On 26.9.19.9, with PROJECTION p (SELECT 'a' GROUP BY 'a', 1) created under enable_positional_arguments_for_projections = 0, INSERT INTO t SETTINGS enable_positional_arguments_for_projections = 1 VALUES (2) is rejected with Not found subcolumn 1 in block. There are only columns: 'a'. (NOT_FOUND_COLUMN_IN_BLOCK). On master, since #118375 added the check, it is a LOGICAL_ERROR, found by BuzzHouse: CI report (Projection p1 calculated the columns CAST('1970-01-01 00:00:00', 'DateTime') while its metadata describes the columns 1).

Creating a projection analyzes its query in place: with the setting on, the positional arguments are replaced in the stored query, and with it off, 1 stays a constant key. Calculating the projection then resolved positional arguments again by the setting of the INSERT, merge or mutation, so the calculated block could differ from the projection's columns.

calculateByQuery now always analyzes the stored query with positional arguments disabled.

Re-attaching a table still rebuilds its projections with the server's value of the setting (#104430); this PR does not change that.

Validation: the new test aborts the server on the unfixed binary and passes with the fix, 50 randomized runs and the DatabaseReplicated, s3 storage and parallel replicas setups pass, and 326 projection and positional-argument stateless tests show no new failures.


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

Version info

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

Building a projection's metadata analyzes its query in place: with
`enable_positional_arguments_for_projections` on, the positional arguments
are replaced in the stored query, and with it off, `1` stays a constant
`GROUP BY` key. `calculateByQuery` then resolved positional arguments again
by the setting of the INSERT, merge or mutation that calculates the
projection, so an integer literal could be re-read as a position:
`GROUP BY 'a', 1` built off and calculated on reads `1` as `'a'`, and
`SELECT 2, 1 GROUP BY 1` built on (stored key `1`) reads it as `2`.

Released versions reject such an INSERT with NOT_FOUND_COLUMN_IN_BLOCK;
master, since the metadata check of ClickHouse#118375, throws a LOGICAL_ERROR
(found by BuzzHouse, STID 4238-495c).

Analyze the stored query with positional arguments disabled.

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

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

Three review rounds (own review + an independent model, engine codex), final head 6179a9ec.

Finding Verdict
❌ Calculating with the creation-time value of the setting resolves positional arguments a second time, because building the metadata already replaces them in the stored query AGREE, fixed: the calculation always disables positional arguments
❌ The creation-time value is not durable across a reload AGREE on the contract: the description makes no such claim; reload is #104430, unchanged
⚠️ The test did not tell the designs apart AGREE, fixed: a projection whose positional argument resolves to an integer literal
⚠️ The forced projection read is not forced when optimize_use_projections is randomized to 0 AGREE, fixed: pinned
⚠️ A control insert could mask an insert that wrote no projection part AGREE, fixed: merges stopped, every active part must have its projection part
⚠️ Projection matching on read also resolves positional arguments of the stored query DISAGREE: probed with constant keys that survive the build, built both ways; the projection is read and results equal optimize_use_projections = 0
💡 setPositionalArgumentsAlreadyResolved(false) is dead once positional arguments are disabled AGREE, fixed: removed

Final round: no new findings from the independent model.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A table created with enable_positional_arguments_for_projections = 0 and PROJECTION p1 (SELECT '1970-01-01 00:00:00'::DateTime GROUP BY '1970-01-01 00:00:00'::DateTime, 1), then INSERT ... SETTINGS enable_positional_arguments_for_projections = 1 VALUES (2): the unfixed debug build aborts on every run with the CI message; 26.9.19.9 and 26.8.18.2 reject it with NOT_FOUND_COLUMN_IN_BLOCK.
b Root cause explained? Creating the projection analyzes its query in place: with the setting on, the positional arguments are replaced in the stored query, and with it off, 1 stays a constant key. calculateByQuery then resolved positional arguments again by the setting of the INSERT/merge/mutation context, so an off-built GROUP BY 'a', 1 calculated with it on reads 1 as 'a', and an on-built SELECT 2, 1 GROUP BY 1 (stored key 1) reads it as 2. Either way the calculated block has columns the metadata does not describe.
c Fix matches root cause? Yes. The stored query is already resolved, so calculateByQuery analyzes it with positional arguments disabled and the calculated block matches the metadata by construction, in both directions. No guard, no setting change, no check weakened.
d Test intent preserved / new tests added? New 05401_projection_positional_arguments_calculating_context.sql covers a projection built with the setting off and inserted with it on, one built on and inserted off, and one built on whose positional argument resolves to an integer literal, inserted both ways. With merges stopped, it checks that every active part of each table has its projection part. The existing positional-projection tests (05182, 05318, 03758, 03651) are unchanged and green.
e Both directions demonstrated? Yes. The new test aborts the server on the unfixed binary (Projection p calculated the columns 'a' while its metadata describes the columns 1) and passes on the fix binary, both Build-ID verified. The integer-literal case on its own also aborts the unfixed binary when inserted with the setting on.
f Fix is general across code paths? Yes. INSERT, merge, mutation, MATERIALIZE PROJECTION, async and remote INSERT and projection indexes all go through calculateByQuery, the only place that analyzes the stored query to calculate it. The minmax-count projection never reaches it. Projection matching on read was probed with constant keys that survive the build, for projections built both ways: results equal optimize_use_projections = 0, so it is unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? String and DateTime constant keys, both setting directions, INSERT VALUES and INSERT SELECT. 50 randomized runs, plus DatabaseReplicated, s3 storage and parallel replicas setups, all green.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default, on-disk format, stored definition or ProjectionDescription field changes. Only the calculating query's value of the setting is no longer consulted, and that value either changed nothing or made the calculation fail.
i Invariants and contracts preserved? A projection's calculated block has the columns its sample block was built with, on every calculation path, because the stored query is analyzed with no further resolution. The empty-block early return and the FetchColumns fast path do not analyze GROUP BY. ProjectionDescription and operator== are unchanged.

Session id: cron:clickhouse-impl-slot-40:20261016-022400

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai @CurtizJ, could you review this? Building a projection's metadata already replaces the positional arguments in its stored query, but calculateByQuery resolved them again by the INSERT/merge context's enable_positional_arguments_for_projections, so the calculated block could differ from the metadata (a LOGICAL_ERROR on master since #118375); it now always calculates with positional arguments disabled.

@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6179a9e]

Summary: ✅


AI Review

Summary

The PR prevents calculateByQuery from resolving positional arguments again after projection metadata has already resolved them, keeping calculated columns consistent with that metadata regardless of the writing context's setting. No actionable findings at 6179a9eceddb1ff9252079a00a756b424688dced.

Missing context / blind spots
  • ⚠️ Dedicated CI bugfix validation did not complete: Finish Workflow reports validation jobs dropped following a configuration failure. Rerunning those jobs would close this CI gap. The new regression and existing positional-projection tests passed Fast test; the author also documents successful before/after regression validation.
Final Verdict

Status: ✅ Approve. No code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). labels Oct 16, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 16, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 17, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 17, 2026
Merged via the queue into ClickHouse:master with commit 05719a5 Oct 17, 2026
524 of 528 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 17, 2026
alexey-milovidov pushed a commit that referenced this pull request Nov 22, 2026
The only conflict is in `ProjectionDescription::calculateByQuery`: master
also has the `enable_parallel_replicas` line of #129863 and the positional
arguments change of #126185 there, neither of which is on 26.10. Keep the
26.10 code and add only the `rewrite_in_to_join` line of #130143.

Also take the three lines of #129006 that materialize constant columns of
the calculated projection block. On 26.10 a projection column such as
`exists(SELECT ...)` is calculated as a `ColumnConst`, and once #130143
gives it the right name, writing the projection part fails with
`Bad cast from type DB::ColumnConst to DB::ColumnVector<char8_t>`
(the `t_subquery_normal` case of 05569). The rest of #129006 is not needed
on 26.10: its trigger, `optimize_constant_columns_after_filter`, does not
exist there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants