Skip to content

Introduce Query-Level Storage Virtual Columns - #110186

Merged
Michicosun merged 30 commits into
masterfrom
query_level_virtual_columns
Aug 5, 2026
Merged

Michicosun merged 30 commits into
masterfrom
query_level_virtual_columns

Conversation

@Michicosun

@Michicosun Michicosun commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

This patch is a part of #106169. This patch allows changing storage metadata inside the query scope depending on table expression modifiers.

For example, for streaming queries, there will be additional syntax for watermarks:

from table stream watermark for event_time as event_time - interval 10 seconds

This query will introduce 2 additional virtual columns to the reading plan from such a table: _watermark and _time_attribute.

Version info

  • Merged into: 26.8.1.816 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [edf5dc5]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, parallel, 2/3) FAIL
Server died FAIL cidb
Logical error: Bad cast from type A to B (STID: 4350-6700) FAIL cidb

AI Review

Summary

This PR moves modifier-dependent metadata onto the per-query StorageSnapshot, which fixes the direct local analyzer path for query-scoped virtual columns. The remaining problem is that two other paths still violate the same contract: serialized/remote plans still drop STREAM-specific modifiers, and lazy-loaded ordinary tables still reject or strip STREAM through the proxy layer. Request changes.

Findings

⚠️ Majors

  • [src/Processors/QueryPlan/ReadFromTableStep.cpp:32-55, src/Processors/QueryPlan/ReadFromTableFunctionStep.cpp:35-55, src/Processors/QueryPlan/resolveStorages.cpp:205-245] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/110186#discussion_r3567184757] The new contract is “table-expression modifiers may change the storage header”, but the serialized-plan path still persists only FINAL / SAMPLE, and the remote/merge() rebuild in resolveStorages only reattaches those same modifiers. The new snapshot = snapshot->clone(...) line does not save this branch, because once we go through InterpreterSelectQueryAnalyzer the rebuilt read sees only the AST reconstructed below. As soon as _watermark / _time_attribute become semantically required, a serialized or remote read will resolve the plain header on the worker and diverge from the initiator.
    Suggested fix: serialize the full TableExpressionModifiers (including STREAM settings) in ReadFromTableStep / ReadFromTableFunctionStep, and rebuild the ASTTableExpression with those settings in resolveStorages.

  • [src/Storages/StorageProxy.h:21-25, src/Storages/StorageProxy.h:44-58, src/Storages/StorageTableProxy.h:47-52] StorageTableProxy is supposed to be transparent for lazy-loaded ordinary tables, but the proxy layer still does not forward supportsStreaming(). That makes QueryAnalyzer::validateTableExpressionModifiers reject FROM t STREAM while the table is still proxied, even if the real nested MergeTree / ReplicatedMergeTree supports streaming. Even after forwarding the capability, StorageProxy::getStorageSnapshot and getQueryProcessingStage currently rebuild snapshots from the nested metadata and replace the caller’s virtuals, so modifier-added columns would still disappear on the first proxied read.
    Suggested fix: forward supportsStreaming() through StorageProxy, and preserve any caller-supplied virtuals when rebuilding the nested snapshot / processing-stage snapshot instead of overwriting them with nested_metadata->virtuals.

Tests
  • ⚠️ Add a regression test for a lazy-loaded ordinary table (lazy_load_tables = 1) proving SELECT ... FROM t STREAM works both before and after the table is materialized.
  • ⚠️ Add a query-plan round-trip test (serialize_query_plan or a distributed read path) that exercises modifier-dependent headers and proves the worker still sees the extra virtual columns.
Final Verdict

Request changes.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.40% 86.50% +0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 55/57 (96.49%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 12, 2026
Comment thread src/Storages/StorageMerge.cpp Resolved
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
Comment thread src/Storages/IStorage.h Resolved
Comment thread src/Storages/StorageTableProxy.h Outdated
/// Forwarding here keeps metadata observers in sync with the nested storage.
StorageMetadataHandle getInMemoryMetadataPtr(ContextPtr context_, bool bypass_metadata_cache) const override
/// NOLINTNEXTLINE(google-default-arguments)
StorageMetadataHandle getInMemoryMetadataPtr(ContextPtr context_, bool bypass_metadata_cache, std::optional<TableExpressionModifiers> modifiers) const override

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.

This still blocks the motivating STREAM case on lazy-loaded ordinary tables. DatabaseOrdinary exposes unloaded tables through StorageTableProxy, but StorageProxy never overrides supportsStreaming, so QueryAnalyzer::validateTableExpressionModifiers sees the proxy and throws before the nested MergeTree / ReplicatedMergeTree is loaded.

The new metadata-forwarding override here will not make FROM t STREAM work through table proxies unless the capability check is delegated as well. After that, the proxy snapshot / read paths also need the modifier-aware wiring from the other comment, otherwise the resolved header and the actual read will still disagree.

@KochetovNicolai KochetovNicolai self-assigned this Jul 14, 2026
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.40% 86.50% +0.10%
Functions 91.80% 91.90% +0.10%
Branches 78.50% 78.60% +0.10%

Changed lines: Changed C/C++ lines covered: 280/395 (70.89%) · Uncovered code

Full report · Diff report

Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
@Michicosun
Michicosun added this pull request to the merge queue Aug 5, 2026
Merged via the queue into master with commit 8309f0a Aug 5, 2026
178 of 180 checks passed
@Michicosun
Michicosun deleted the query_level_virtual_columns branch August 5, 2026 10:51
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 5, 2026
pull Bot pushed a commit to Haofei/ClickHouse that referenced this pull request Aug 27, 2026
…ofiles kill-safe

The `LLVM Coverage` gate compares two totals merged from 21 per-shard
`.profdata` artifacts that are `optional=True`, and nothing checked that
either side had all of them. A missing shard on the PR side fails an innocent
PR ("Coverage degraded" by 0.5 pp on a 102-line PR); a missing shard on a
master run writes a low baseline into the CI DB, so later real regressions
pass invisibly. Observed twice in 60 days, both times the same shard:
`Integration tests (amd_llvm_coverage, 4/8)` merged only 20 of 21 profiles
while every check was green.

Examples:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110186&sha=0b33f54bc8089ea3560397c589e93dc6aec92d47&name_0=PR&name_1=LLVM%20Coverage
(ClickHouse#110186)
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105844&sha=66107bb44ab45a490e839225eb096e46204e5e86&name_0=PR&name_1=LLVM%20Coverage
(ClickHouse#105844)

The invariant: a verdict comes only from two complete measurements.

- Every coverage producer (FT, IT, unit) names its profile after its own
  artifact (`<artifact>.profdata`), so the filename carries the shard
  identity. Producer merges use `--failure-mode=any` and skip zero-length
  `.profraw` inputs: a shard with invalid data is absent, never silently
  short. Artifacts stay `optional=True`, so a missing profile never reddens
  a green test job.
- The `LLVM Coverage` job checks the expected set against what arrived and
  merges an explicit file list (no `*.profdata` glob). On a missing or
  invalid shard it reports `SKIPPED` with the shard name and withholds every
  comparative output: no verdict, no GitHub comment, no `llvm_coverage.info`,
  no CI DB row. Baseline completeness then needs no metadata: the diff script
  already selects the first master ancestor that has an `.info`, and after
  this change such an `.info` exists only for complete runs.
- Under coverage the IT sequential phase runs regardless of how many parallel
  tests failed (`MAX_FAILS_BEFORE_DROP`), so a green shard cannot silently
  publish a profile short by the whole sequential phase.
- Root cause of the recurring shard 4/8 loss: a `SIGKILL` during the
  exit-time profile write leaves a half-written `.profraw` whose header makes
  `llvm-profdata` abort with "LLVM ERROR: out of memory"
  (llvm/llvm-project#50970). Coverage builds now
  compile with `-mllvm -runtime-counter-relocation` and run with `%c`
  (continuous mode): counters are memory-mapped into the file, which is valid
  at every instant, so kills lose nothing and corrupt nothing.

Related: ClickHouse#112832

Co-Authored-By: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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