Skip to content

Check what if I remove materialize from StorageView::read - #105846

Merged
KochetovNicolai merged 10 commits into
masterfrom
check-what-if-remove-materialize-in-view
Jun 19, 2026
Merged

KochetovNicolai merged 10 commits into
masterfrom
check-what-if-remove-materialize-in-view

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented May 26, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Remove the Materialize constants after VIEW subquery step in StorageView::readImpl. Now, constants are kept in plan and could be used by optimizations without checking for materialize.

Version info

  • Merged into: 26.6.1.1034

@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [58820ba]

Summary: ✅


AI Review

Summary

This PR removes analyzer-side materialization after VIEW reads and adds source_constants plumbing so constants observed from the source keep flowing through planner action chains. The main code path now looks internally consistent, and the earlier window-function concern is fixed in the current code. I am not ready to approve because the changed invariant still lacks a focused regression test.

Tests
  • ⚠️ [src/Storages/StorageView.cpp:382] The analyzer path can now return constant columns from a VIEW and conversion explicitly avoids materializing them, but the PR only updates existing reference outputs. Existing coverage exercises some distributed constant-view results and plan shapes, but it still does not prove the key contract: a VIEW producing constant or derived columns keeps correct query results while filter simplification / key-condition pushdown works without a materialize wrapper. Please add a focused stateless test with enable_analyzer=1 that validates both the query result and EXPLAIN indexes for that case.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: add the focused regression test described above.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 26, 2026
/// Because method 'getSampleBlockForColumns' is used to obtain a structure of result in InterpreterSelectQuery.
ActionsDAG materializing_actions(query_plan.getCurrentHeader()->getColumnsWithTypeAndName());
materializing_actions.addMaterializingOutputActions(/*materialize_sparse=*/ true);
/// It's expected that the columns read from storage are not constant.

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 change drops materialize for the analyzer path, but the PR does not add a focused regression test proving the contract it is trying to fix (issue #78166): constant aliases from a VIEW should still allow filter simplification / key-condition pushdown, and VIEW reads should remain stable when constant columns flow through this branch.

Please add a stateless test with enable_analyzer=1 that validates both query result and EXPLAIN indexes for a VIEW producing constant or derived columns.

…EW subquery`

The previous commit drops the `materialize` step that `StorageView::read`
used to insert after the view subquery. All five tests below only
embed the `EXPLAIN` plan in their output, so the changes are purely
cosmetic:

- `Materialize constants after VIEW subquery` no longer appears in the
  merged `Expression`/`Filter` step descriptions.
- For `03302_analyzer_distributed_filter_push_down` the merged filter
  DAG no longer wraps `n` with `materialize(...)`. Without that wrapper,
  the (Change column names → ... → Project names) rename cycles the
  analyzer inserts at every scope boundary collapse into a chain of
  pure `ALIAS n ↔ __table1.n` round-trips. These aliases never did
  anything semantically - `materialize` was just keeping them wired up.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@KochetovNicolai
KochetovNicolai marked this pull request as ready for review May 27, 2026 11:18
@novikd novikd self-assigned this May 27, 2026
KochetovNicolai and others added 2 commits June 12, 2026 17:17
Pass duplicate_const_columns = false when building expression-analysis
chain DAGs: buildActionsDAGFromExpressionNode (WHERE/HAVING/QUALIFY,
projection, LIMIT BY) and the before_window, before_sort, interpolate
and project-names DAGs. A constant observed in the source header (e.g.
a VIEW subquery returning constants after the materialization step was
removed) no longer gets a free-standing COLUMN twin in chain DAGs, so
the INPUT stays reachable and the column keeps flowing through the
stream like an ordinary column. Stream structure then stays identical
to the structure computed during analysis (where such columns are not
constant), which distributed processing relies on when comparing
independently computed headers.

Fixes THERE_IS_NO_COLUMN / NOT_FOUND_COLUMN_IN_BLOCK in
`01244_optimize_distributed_group_by_sharding_key` and
`01952_optimize_distributed_group_by_sharding_key` for queries like
`select distinct on (k1) k2 from remote('127.{1,2}', view(select 1 k1, 2 k2, 3 v), cityHash64(k1, k2))`.

Update the `01952` reference: shard-local plans now get an anonymous
converting Expression step that materializes observed constants to
match the initiator's expected header.

Known follow-ups:
- The non-duplicating ActionsDAG constructor strips constant values
  from INPUT nodes (addInput(name, type) instead of addInput(column)),
  which makes addConstantIfNecessary treat them as materialized inputs
  and re-bind window-function arguments to fresh constants. This breaks
  `03916_window_functions_group_by_use_nulls` and
  `04068_window_function_type_mismatch_with_rollup_and_group_by_use_nulls`
  with group_by_use_nulls = 1 (a ROLLUP key argument binds to the
  constant instead of the nullable key column). The fix is to keep the
  value on the INPUT, which in turn requires disabling the
  constant-folding rewrite in ActionsChainStep finalize so folded
  ALIAS/FUNCTION nodes do not orphan value-carrying inputs.
- Several EXPLAIN-based tests still need reference updates / triage for
  plan-shape-only diffs (extra anonymous converting Expression steps).

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105846&sha=91d9fefa0080113957d05e135b94cc134f76535d&name_0=PR&name_1=Fast+test
PR: #105846

Co-Authored-By: Claude Fable 5 <[email protected]>
KochetovNicolai and others added 3 commits June 17, 2026 13:35
After removing the `Materialize constants after VIEW subquery` step, a
constant column returned by `Storage::read` (e.g. a view subquery
`select 1 k1, 2 k2`) could be folded into a free-standing `COLUMN` node
and dropped from the stream at a distributed stage boundary, while the
initiator computes the expected per-shard header from the query tree
where the column is ordinary. The mismatch surfaced as
`THERE_IS_NO_COLUMN` / `NOT_FOUND_COLUMN_IN_BLOCK`, e.g. for
`select distinct on (k1) k2 from remote('127.{1,2}', view(select 1 k1, 2 k2, 3 v), cityHash64(k1, k2))`.

Collect the constants the storage actually returns into
`JoinTreeQueryPlan::source_constants` (in `buildQueryPlanForTableExpression`,
merged across joins) and keep those `INPUT` nodes from being orphaned by
constant folding while the expression chain is finalized: `ActionsDAG::project`
and `ActionsChainStep::finalizeInputAndOutputColumns` forward them as
`used_inputs` to `removeUnusedActions`, so the column keeps propagating as a
required input and stays in the stream to match the initiator's expectation.

`ALIAS` columns are excluded from `source_constants`: a constant `ALIAS`
(e.g. `y Array(String) ALIAS ['qwqw']`) is declared on the table but not
stored, so it is re-creatable from the expression that is propagated to the
shard via the query AST (`ReplaseAliasColumnsVisitor`) and may be dropped.
Query-tree literals (e.g. the `1` in `HAVING cnt = 1`) are likewise not in
`source_constants` and stay foldable; keeping them would add inputs absent
from the actual stream block. This keeps `03371_constant_alias_columns`
working while fixing the view-constant case.

Fixes `01244_optimize_distributed_group_by_sharding_key` and
`01952_optimize_distributed_group_by_sharding_key`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105846&sha=91d9fefa0080113957d05e135b94cc134f76535d&name_0=PR&name_1=Fast+test
PR: #105846

Co-Authored-By: Claude Fable 5 <[email protected]>
Shorten the explanatory comments added for `source_constants` keeping to
2-3 lines each. No code changes.

Co-Authored-By: Claude Fable 5 <[email protected]>
`source_constants` is empty for the common case (no constant columns
returned by the storage), so skip iterating the DAG inputs entirely.

Co-Authored-By: Claude Fable 5 <[email protected]>
…e-materialize-in-view

# Conflicts:
#	tests/queries/0_stateless/03302_analyzer_distributed_filter_push_down.reference
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 99/100 (99.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 4 line(s) · Uncovered code

Full report · Diff report

@KochetovNicolai
KochetovNicolai added this pull request to the merge queue Jun 19, 2026
Merged via the queue into master with commit 91149fc Jun 19, 2026
326 of 328 checks passed
@KochetovNicolai
KochetovNicolai deleted the check-what-if-remove-materialize-in-view branch June 19, 2026 13:20
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 19, 2026
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