Skip to content

Position based unused column removal - #100586

Merged
antaljanosbenjamin merged 35 commits into
masterfrom
position-based-unused-column-removal
May 27, 2026
Merged

antaljanosbenjamin merged 35 commits into
masterfrom
position-based-unused-column-removal

Conversation

@antaljanosbenjamin

@antaljanosbenjamin antaljanosbenjamin commented Mar 24, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Identify columns by position (instead of by name) when removing unused columns in the query plan. This enables unused-column removal when duplicated column names are present.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.195

@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7d5e40d]


AI Review

Summary

This PR switches unused-column removal from name-based to position-based behavior across query-plan steps (ExpressionStep, FilterStep, JoinStepLogical, ReadFromMergeTree) and adds focused regression coverage for duplicate-name plans and PREWHERE/FINAL interactions. After reviewing the current diff and all prior thread discussions/replies, I did not find unresolved correctness, safety, or high-confidence performance issues that should block merge.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 24, 2026
@antaljanosbenjamin
antaljanosbenjamin marked this pull request as draft March 24, 2026 13:44
@antaljanosbenjamin
antaljanosbenjamin force-pushed the position-based-unused-column-removal branch from 1fa4727 to 4c9e414 Compare March 31, 2026 15:07
Comment thread src/Storages/SelectQueryInfo.cpp Resolved
@nickpresta

Copy link
Copy Markdown

@antaljanosbenjamin Created #105521 specifically for the issue I'm seeing. Thank you!

Block new_input_header{};
/// Build the set of required input header positions: DAG inputs that survived pruning + required pass-throughs.
auto matched_positions = actions_dag.matchInputPositionsToHeader(*input_header).matched;
std::set<size_t> required_input_positions(matched_positions.begin(), matched_positions.end());

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.

ActionsDAG::matchInputNodesToHeader() is building a map absl::flat_hash_map<std::string_view, inline_vector> input_positions and seems to do a name based lookup. Do we risk getting a mixup here when there's two columns in input_header with the same name?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We shouldn't: we collect the columns with same name into a vector and pop one of them on a match. Because we put the input columns into the vector in reversed order (size_t pos = input_nodes.size(); pos != 0; pos--), we match the first input header for the first input with that name, the second column to the second input etc.

@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 91.47% (472/516) | lost baseline coverage: 28 line(s) · Uncovered code

Full report · Diff report

@antaljanosbenjamin
antaljanosbenjamin added this pull request to the merge queue May 27, 2026
Merged via the queue into master with commit 3877d4a May 27, 2026
163 of 165 checks passed
@antaljanosbenjamin
antaljanosbenjamin deleted the position-based-unused-column-removal branch May 27, 2026 15:28
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @antaljanosbenjamin @cv4g — a couple of suggestions to make this PR easier for reviewers and the changelog reader.

Description (currently incomplete):

  • Why: The PR description is template-only with no explanation of the change, motivation, or impact.

  • Suggested addition:

    This PR changes the unused column removal optimization from name-based to position-based column tracking. Previously, ExpressionStep::canRemoveUnusedColumns returned false when duplicate column names existed in inputs or outputs, disabling the optimization. Now it always returns true and uses position indices to correctly track which columns to keep/remove, even when names are duplicated.

    Key changes:

    • New methods matchInputPositionsToHeader, matchInputNodesToHeader, splitOutputPositions in ActionsDAG for position-based column matching
    • removeUnusedColumns implementations in ExpressionStep, FilterStep, JoinStepLogical, and ReadFromMergeTree now use position vectors instead of name sets
    • JoinStepLogical disables optimization when join condition inputs have duplicate names (safeguard for HashJoin compatibility)
    • Removed unused name-based helper functions

Ignore this if the current title/body is intentional.

@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 28, 2026
davenger added a commit that referenced this pull request May 28, 2026
`ReadSettings::http_max_tries` and friends moved into the nested
`http_settings` struct on master (PR #100586's IO refactor). Update
the four call sites that build HTTP read settings for the stateless
worker client.
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 29, 2026
Per @antaljanosbenjamin's suggestion on PR ClickHouse#106104, prevent the same
column from being added twice at the source instead of relaxing the
downstream validator.

The previous commit relaxed `ReadFromMergeTree::removeUnusedColumns`
to tolerate same-pointer duplicates in the filter DAG outputs. That
worked but kept the duplicate around in every downstream consumer.
This change eliminates the duplicate at the producers:

* `Planner/Utils.cpp::buildFilterInfo` (analyzer path) -- when the
  filter expression is one of the inputs (bare-column case), skip the
  redundant passthrough push and set `remove_filter_column = false`
  so `FilterTransform` does not erase the column we want to keep.

* `Interpreters/InterpreterSelectQuery.cpp::generateFilterActions`
  (legacy path) -- same logic: detect the bare-input filter result,
  skip its passthrough push, and set `do_remove_column = false`.

* `Interpreters/ExpressionAnalyzer.cpp` -- drop the unconditional
  `row_policy_info->do_remove_column = true;` override that masked
  the producer-side decision. The producer now sets the correct value
  for both bare-column and computed-column policies.

* `Processors/QueryPlan/ReadFromMergeTree.cpp` -- restore the strict
  name-based duplicate validator. Producer-side dedup makes
  same-pointer duplicates impossible in well-formed DAGs, so the
  original check from PR ClickHouse#100586 is safe again.

Regression test improvements (per bot review): the data set now
includes a row with `c0 = 0` and explicit `count()` / `countIf` queries.
The test now catches both (a) the `LOGICAL_ERROR` reappearing AND
(b) the policy silently not being applied -- the second mode would
let the falsy row through, which a `count() == 3` assertion catches.

The Cause 2 fix in `projectionsCommon.cpp::findInOutputs` (turn
`ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER` into a graceful `nullptr` for
non-`UInt8` filters) is unchanged and independent of the dedup approach.

PR review: ClickHouse#106104
Issue: ClickHouse#106099
@Fgrtue Fgrtue removed their assignment Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

LOGICAL_ERROR header mismatch after PREWHERE pushdown on ReplacingMergeTree FINAL queries

7 participants