Skip to content

Implement/parameterized view explicit schema v2 - #98708

Open
GSokol wants to merge 71 commits into
ClickHouse:masterfrom
GSokol:implement/parameterized_view_explicit_schema_v2
Open

GSokol wants to merge 71 commits into
ClickHouse:masterfrom
GSokol:implement/parameterized_view_explicit_schema_v2

Conversation

@GSokol

@GSokol GSokol commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Experimental Feature

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

Added the experimental setting use_declared_schema_for_parameterized_views. When it is enabled, the column schema declared explicitly in a parameterized view is exposed in SHOW COLUMNS, system.columns and DESCRIBE, and is validated against the actual schema after parameter substitution, throwing TYPE_MISMATCH if they differ. The decision is made when the view is created and stored with its definition, so it is the same on every replica and across restarts.

Closes: #97288

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features). Documentation would be generated from settings.

Note

Medium Risk
Touches view/table-function execution and metadata exposure for parameterized views, including SQL security context handling and new runtime validation that can start throwing TYPE_MISMATCH when enabled.

Overview
Adds an experimental setting use_declared_schema_for_parameterized_views to optionally expose and enforce explicitly declared column schemas on parameterized views.

When enabled, parameterized view invocation now validates the post-substitution schema against the declared columns (throwing TYPE_MISMATCH on mismatch) and allows declared columns to be used for view metadata/SHOW COLUMNS; this required wiring SQL SECURITY context into sample-block inference and passing ContextPtr into StorageView construction. Tests were added for both matching and mismatching schemas, and GitHub hygiene is updated (PR template now asks for docs entry; Dependabot cargo configuration is removed).

Reviewed by Cursor Bugbot for commit 3a2f11e. Bugbot is set up for automated code reviews on this repo. Configure here.


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

@GSokol

GSokol commented Mar 4, 2026

Copy link
Copy Markdown
Contributor Author

@fm4v, reapplied feature, you reverted (with feature flag setting to prevent backward-incompatible changes.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Mar 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [987e2e2]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

The PR adds experimental exposure and enforcement of explicitly declared parameterized-view schemas, persisting the choice across reloads. Reviewed head: 987e2e20d0797b9fae075249a3e248d06e5a4cb5. The previously discussed upgrade compatibility blocker remains; no new inline findings were identified.

Declared-schema consumers traced: both parameterized-view builders validate substituted output; bare and parameterized DESCRIBE expose declared descriptions; system.columns filters metadata by privileges; Alias forwards metadata; Merge rejects parameterized views through alias chains; CREATE TABLE ... AS copies declared columns; column-comment alterations rewrite stored metadata. The unresolved consumer is loading definitions written before this feature.

Findings

❌ Blockers

  • src/Databases/DatabaseOnDisk.cpp:157 — Upgrading opts existing views into schema enforcement even with the experimental setting disabled. [dismissed by author -- https://github.com/Implement/parameterized view explicit schema v2 #98708#discussion_r4057656096]

    Older servers persist explicitly written column lists while ignoring them during parameterized-view execution. The new loader interprets every such list as an opt-in, and both execution builders enforce it independently of the setting. For example, an existing view defined as CREATE VIEW pv (n UInt64, s String) AS SELECT number AS n FROM numbers({p:UInt64}) previously returned rows; after upgrading and loading its metadata, SELECT * FROM pv(p = 1) throws TYPE_MISMATCH, including with use_declared_schema_for_parameterized_views = 0. The compatibility history cannot restore the former behavior.

    The author deliberately leaves a metadata marker to a maintainer decision. The concern remains because existing, previously valid workloads break without enabling the feature—the compatibility problem motivating Regression in parameterized views (After parameters substitution of parameterized view ... the actual schema does not match the defined one) #97288. Persist a versioned opt-in marker, or an equivalent deterministic compatibility mechanism, that distinguishes new opted-in definitions from legacy metadata. The existing thread is left unchanged.

Tests
  • ⚠️ Add a focused upgrade regression that creates a mismatching explicit schema on a pre-feature server, reloads or restores it on the new server with the setting disabled, and verifies continued execution. Also verify that a newly opted-in view retains enforcement after reload under the opposite setting. Current same-version lifecycle tests cover the latter behavior but cannot distinguish legacy metadata. No local tests were run; the fetched Praktika reports list zero failed tests.
Final Verdict

Status: ❌ Block

Preserve legacy parameterized-view behavior during metadata loading and add the upgrade regression. No duplicate inline comment or additional thread reply was posted for the acknowledged compatibility decision.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 4, 2026
@GSokol
GSokol force-pushed the implement/parameterized_view_explicit_schema_v2 branch from 007f335 to d703e10 Compare March 9, 2026 11:50
Comment thread src/Core/Settings.cpp Resolved
@GSokol
GSokol force-pushed the implement/parameterized_view_explicit_schema_v2 branch from d703e10 to 47e813a Compare March 9, 2026 12:29
@GrigoryPervakov GrigoryPervakov self-assigned this Mar 10, 2026
Comment thread src/Core/Settings.cpp Resolved
Comment thread src/Interpreters/Context.cpp Resolved
Comment thread src/Core/SettingsEnums.cpp Resolved
Comment thread src/Core/Settings.cpp Resolved
@GSokol
GSokol requested a review from GrigoryPervakov March 17, 2026 08:54
@GSokol
GSokol force-pushed the implement/parameterized_view_explicit_schema_v2 branch from 751ca2b to c6c72b1 Compare March 24, 2026 08:37
Comment thread src/Storages/StorageView.cpp Resolved
Comment thread src/Interpreters/Context.cpp Resolved
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alexey-milovidov

Copy link
Copy Markdown
Member

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

@clickhouse-gh clickhouse-gh Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submitting stale pending review to allow automated inline comments posting.

Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

Hi @GSokol, this PR has all reviewer feedback addressed and was approved by @GrigoryPervakov with all CI checks green at commit 448446ec. However, master has moved significantly since the last update (2026-04-14) and the PR is now in CONFLICTING state.

There is a small conflict in src/Storages/StorageView.cpp in the namespace Setting extern declarations block: master added allow_experimental_parallel_reading_from_replicas, parallel_replicas_allow_view_over_mergetree, enable_positional_arguments, etc. at the same location where this PR added use_declared_schema_for_parameterized_views.

Could you please merge master into the branch (locally) and resolve the conflict so we can ship this? The fix is to keep all extern declarations from both sides.

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label May 20, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 8ce17c1. Configure here.

Comment thread .github/workflows/auto_releases.yml Resolved
Comment thread .github/workflows/create_release.yml Resolved
GSokol added 2 commits May 25, 2026 11:29
Show the schema of a parameterized view when it's explicitly specified.
Bartok9 pushed a commit to Bartok9/ClickHouse that referenced this pull request Oct 9, 2026
The benchmark looked each row's group up in a constant array,
`groups[number % 4096 + 1]`. About half of its CPU went to that lookup
(`arrayElement` over a constant array copies row by row through a virtual
`insertFrom`), not to the sampling-key code the test measures. On arm some
builds run that loop about twice as slow in every server process: in the
job profiles of the slow runs on ClickHouse#98708, ClickHouse#116376, ClickHouse#117754, ClickHouse#123545 and
ClickHouse#123188 only that frame grew (x1.4 to x2.2) while every other frame stayed
flat, so the query moved 20-27% and the confirmation restart reproduced it.

The per-query tags collector numbers groups 0, 1, 2, ... with 0 reserved for
the empty tag set (`ContextTimeSeriesTagsCollector.h`), so the 4096 groups
the subquery creates are 1..4096 and the query can pass
`number % length(groups) + 1` directly. `length(groups)` keeps the scalar
subquery referenced: without it the analyzer drops the subquery, no group is
registered and the function throws `Group 1 is out of bounds`.

Checked on x86 with an official master build: the same keys over the 20M
rows (`sum` and `groupBitXor` equal, 0 rows differ), 84 -> 52 ms, and the
per-process medians of 24 processes span 79-91 ms before and 52-53 ms after.

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

Copy link
Copy Markdown
Member

🕵 Merged current master and pushed 4bf00b7b275 to the fork. The merge was clean, and the diff against master is unchanged.

  • The only red check was the ARM perf report on timeseries_group_to_sampling_key. It was unrelated to this PR, and its fix (Pass group ids directly in the timeSeriesGroupToSamplingKey benchmark #124809, thanks @groeneai) is now in master, so it should clear on this run.
  • I built the merged tree and ran 03721, 05037, 05055, 05315 and 04238 locally. All passed.
  • One thread is still open on DatabaseOnDisk.cpp: pre-feature metadata that stored an explicit column list. A maintainer needs to decide it, because the fix would add a versioned marker to the stored definition, which changes the metadata format.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Pushed 86e61747a69. The only red check was the settings_changes_history style check: master now reports version 26.11, so the history record of use_declared_schema_for_parameterized_views moved from 26.10 to 26.11. Nothing else changed.

One review thread is still open on purpose (src/Databases/DatabaseOnDisk.cpp, about pre-feature metadata). A maintainer needs to decide whether to add a versioned marker to the stored CREATE VIEW text.

Comment thread src/Interpreters/Context.cpp Resolved
Comment thread src/Storages/StorageView.cpp Resolved
alexey-milovidov and others added 5 commits October 17, 2026 16:44
…match

The `TYPE_MISMATCH` exception from `validateParameterizedViewSchema` reveals the
declared column names and types, but it was thrown in `Context::buildParameterizedViewStorage`
and `Context::executeTableFunction` before the planner checks access to the view.
A user without any grant on the view could read its declared schema from the error.

Now, right before a mismatch is reported, `SHOW COLUMNS` on the declared columns is
checked for the caller; without it the query fails with `ACCESS_DENIED`, whose hint
hides the column names. Matching views and column-level grants are unaffected.

Address review: ClickHouse#98708 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`applyMetadataChangesToCreateQuery` skipped the column list of every parameterized
view, so a successful `ALTER TABLE pv COMMENT COLUMN` on a view with a declared schema
updated only the in-memory metadata and was lost on `DETACH`/`ATTACH` or restart.
Rewrite the column list for parameterized views whose stored definition declares one.

Address review: ClickHouse#98708 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	src/Interpreters/InterpreterCreateQuery.cpp
…list

With `use_declared_schema_for_parameterized_views` disabled, the declared column
list of a parameterized view is dropped from its definition. In a `Replicated`
database the normalized query is what gets enqueued, so the replicas replayed
`ATTACH VIEW pv UUID '...' AS SELECT ...` without a column list, and the stub
`ATTACH` detection rejected it with `BAD_ARGUMENTS`. This made
`05037_parametrize_view_full_attach_schema` fail in DBReplicated runs.

A parameterized view with a `SELECT` is a full definition even without a column
list, so it is no longer treated as a stub `ATTACH`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=98708&sha=c6cb30bc152e73231f9f8fe905a7ad4027ee6840&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%201%2F2%29
ClickHouse#98708

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…view_schema`

`SELECT *` from `system.columns` broke after master added a new column to it.

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

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=98708&sha=c53e6ad66c0d71a7f4d94178f477ed63274cbc61&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

02530_dictionaries_update_field_hashed hit its time limit while waiting for SecondUpdated. It is not related to this PR. Between 2026-10-25 00:05 and 00:57 UTC it failed the same way on five unrelated PRs (#115988, #98708, #127375, #125510, #127759). That window is the ambiguous 02:00–03:00 local hour right before the European DST change at 01:00 UTC. ClickHouseDictionarySource sends the update_field lower bound as a local-time string (ExternalQueryBuilder::composeUpdateCondition). Inside the repeated hour, that string can resolve to the later of the two instants, so rows inserted with now()+5 / now()+10 are never picked up. Most likely this is a real DST bug in update_field incremental updates, not only a flaky test.

The CH Inc sync failure comes from three private jobs (two Stress test (arm_asan_ubsan, ... meta in keeper) jobs and Integration tests (amd_msan, 4/8)). Each ended with ERROR after 25–120 seconds, which looks like an infrastructure problem, not a code failure.

@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm investigating the 02530_dictionaries_update_field_hashed timeout and the update_field lower bound handling in the repeated DST hour; I'll follow up here with a PR (or my findings) shortly.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is in #127765. The update_field lower bound was sent as server-local text; in the second (CET) pass of the repeated hour it resolved to the earlier occurrence, one hour early, so every update re-read Second together with SecondUpdated and the hashed layouts kept the first row per key (flat keeps the last, which is why it passed). The bound is now sent as a Unix timestamp.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged the latest master (b21291d5e8f). The merge had no conflicts. The clickhouse binary builds, and the PR's 7 stateless tests pass locally.

The previous red checks are not related to this PR:

The thread on src/Databases/DatabaseOnDisk.cpp about pre-feature metadata is still open on purpose. A maintainer needs to decide whether to add a versioned marker to the stored CREATE VIEW text.

@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
# Conflicts:
#	src/Core/Settings.cpp
@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

alexey-milovidov and others added 2 commits November 13, 2026 06:15
# Conflicts:
#	src/Interpreters/InterpreterCreateQuery.cpp
…arguments

`master` now hides the arguments of table functions that are not registered
(parameterized views included) in `EXPLAIN QUERY TREE`, failing closed in
`SecretArgumentsRegistry`. The two `EXPLAIN QUERY TREE` outputs in the test now
show `Literal '[HIDDEN]'` instead of `Literal UInt64_3`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=98708&sha=latest&name_0=PR
PR: ClickHouse#98708

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

Copy link
Copy Markdown
Member

🕵 Merged the latest master and pushed c485db66096e.

  • Conflict in InterpreterCreateQuery.cpp. master moved the stub-ATTACH condition into a new isStubAttach helper. I added this PR's exception (!create.isParameterizedView()) to that helper. A full ATTACH VIEW of a parameterized view without a column list now counts as a full definition everywhere the helper is used. That includes the cloud_mode check that rejects full-definition ATTACH.
  • Fast test failure in 03721_parametrize_view_schema. master now hides the arguments of unregistered table functions, parameterized views included, in EXPLAIN QUERY TREE. This is a deliberate fail-closed rule in SecretArgumentsRegistry. I updated the two affected reference lines from Literal UInt64_3 to Literal '[HIDDEN]'.

The clickhouse binary builds. All 8 of the PR's stateless tests pass locally against a server, including 03721_parametrize_view_schema with randomized settings.

The thread on src/Databases/DatabaseOnDisk.cpp about metadata written before this feature still needs a maintainer to decide.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged the latest master with no conflicts and pushed 18400b336187. The clickhouse binary builds, and the PR's .sql stateless tests pass locally. The only red check is still the review-threads gate. It is waiting on the maintainer decision in the open DatabaseOnDisk.cpp thread about metadata written before this feature.

alexey-milovidov and others added 2 commits November 20, 2026 09:45
The current version is now 26.12, so the `settings_changes_history` style
check requires the new setting to be recorded under it.

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

Copy link
Copy Markdown
Member

🕵 Merged the latest master (91b044d1980d) with no conflicts and pushed 987e2e20d079. The clickhouse binary builds, and the PR's 7 stateless tests pass locally against a server.

The private sync PR was conflicting with its master; I merged the new head of this PR and the private master into the sync branch, so CH Inc sync should re-run.

The only remaining blocker is the review-threads gate: the open thread on src/Databases/DatabaseOnDisk.cpp about metadata written before this feature still needs a maintainer decision (accept the behavior for this experimental setting, or request a versioned marker in the stored CREATE VIEW definition).

This branch has not been deployed

No deployments
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-view Logical (non-materialized) views. external Author is not a member of the ClickHouse organization manual approve Manual approve required to run CI pr-experimental Experimental Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression in parameterized views (After parameters substitution of parameterized view ... the actual schema does not match the defined one)

7 participants