Skip to content

Optimize remove unused column read from merge tree - #89982

Merged
antaljanosbenjamin merged 21 commits into
masterfrom
optimize-remove-unused-column-read-from-merge-tree
Feb 17, 2026
Merged

antaljanosbenjamin merged 21 commits into
masterfrom
optimize-remove-unused-column-read-from-merge-tree

Conversation

@antaljanosbenjamin

@antaljanosbenjamin antaljanosbenjamin commented Nov 13, 2025 •

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):

The unused columns are removed also from the reading step in case of reading from a MergeTree. It is especially useful when a filter is pushed down into PREWHERE.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Details

Builds on top of #76487, so only the last few (currently 2) commits are relevant. Once the other branch is merged, the diff will become much less.

Version info

  • Merged into: 26.2.1.855

@clickhouse-gh

clickhouse-gh Bot commented Nov 13, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b21d9b7]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 4/6) failure
Check Results failure IGNORED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 13, 2025
@antaljanosbenjamin
antaljanosbenjamin force-pushed the optimize-remove-unused-column-read-from-merge-tree branch 2 times, most recently from 78b88d2 to 7a45907 Compare November 14, 2025 22:54
@antaljanosbenjamin
antaljanosbenjamin force-pushed the optimize-remove-unused-column-read-from-merge-tree branch from a5cdef1 to e95f0a5 Compare November 20, 2025 15:04
antaljanosbenjamin and others added 11 commits November 26, 2025 16:18
I am not sure why, but this problem only pops up when the query single threaded (`max_threads = 1`). This was a bug before, but I added a query as a test which triggered this issue.
@antaljanosbenjamin

Copy link
Copy Markdown
Member Author

There are some test (that are not present in public) changes in private, please check them too.

@antaljanosbenjamin
antaljanosbenjamin marked this pull request as ready for review February 4, 2026 17:34
@serxa serxa self-assigned this Feb 5, 2026
@serxa
serxa requested a review from Copilot February 11, 2026 16:53

Copilot AI 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.

Pull request overview

This PR extends unused-column elimination to the ReadFromMergeTree step (especially beneficial with PREWHERE pushdown), and updates/introduces stateless tests to validate the new behavior including FINAL + special MergeTree columns.

Changes:

  • Implement unused-column removal in ReadFromMergeTree, including interaction with PREWHERE, row-level filters, and FINAL.
  • Invoke unused-column removal after PREWHERE optimization when enabled.
  • Add/adjust stateless tests and references covering MergeTree reads, FINAL special columns, and updated EXPLAIN outputs.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/queries/0_stateless/03709_remove_unused_columns_from_prewhere.sql New test to compare EXPLAIN PLAN with/without unused-column removal for MergeTree reads.
tests/queries/0_stateless/03709_remove_unused_columns_from_prewhere.reference Expected plan output showing reduced ReadFromMergeTree header when enabled.
tests/queries/0_stateless/03709_final_prewhere_special_columns.sql New coverage for FINAL + PREWHERE with special columns across MergeTree variants.
tests/queries/0_stateless/03709_final_prewhere_special_columns.reference Expected results for the FINAL/special-columns test suite.
tests/queries/0_stateless/03445_subcolumns_prewhere_pushdown.reference Updated EXPLAIN reference output due to changed header/positions after optimization.
tests/queries/0_stateless/03324_union_block_structure_mismatch.sql Stabilize test by disabling unused-column removal to preserve intended projection behavior.
tests/queries/0_stateless/03302_analyzer_distributed_filter_push_down.reference Updated EXPLAIN reference output reflecting new action positions/headers.
tests/queries/0_stateless/02514_analyzer_drop_join_on.reference Updated ReadFromMergeTree header in reference after unused-column elimination.
src/Processors/QueryPlan/ReadFromMergeTree.h Add overrides enabling unused-column removal for MergeTree reads.
src/Processors/QueryPlan/ReadFromMergeTree.cpp Implement column pruning logic + FINAL special-column handling hooks.
src/Processors/QueryPlan/Optimizations/optimizeTree.cpp Pass “remove unused columns” setting into optimizePrewhere.
src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp Optionally run unused-column removal after PREWHERE pushdown and enforce header compatibility.
src/Processors/QueryPlan/Optimizations/Optimizations.h Update optimizePrewhere API to accept the remove-unused-columns flag.

Comment on lines +3991 to +3996
if (query_info.isFinal())
columns_to_keep = getColumnsRequiredForMergingFinal(result_sort_description, data.merging_params);

for (const auto & column_name : required_outputs)
columns_to_keep.insert(column_name);

Copilot AI Feb 11, 2026

Copy link

Choose a reason for hiding this comment

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

columns_to_keep includes columns required for FINAL merging, but the resulting new_column_names is built only by filtering the existing all_column_names. If all_column_names does not already contain some FINAL-required special columns (e.g. version, sign, is_deleted, Graphite params), they will not be added here and the FINAL merging pipeline can later fail due to missing inputs. Consider explicitly ensuring that all_column_names is augmented with getColumnsRequiredForMergingFinal(...) (or at least validating they’re present and inserting missing names in a stable order) before constructing new_column_names.

Copilot uses AI. Check for mistakes.
Comment on lines +4045 to +4050
Names new_column_names;
for (const auto & column_name : all_column_names)
{
if (columns_to_keep.contains(column_name))
new_column_names.push_back(column_name);
}

Copilot AI Feb 11, 2026

Copy link

Choose a reason for hiding this comment

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

columns_to_keep includes columns required for FINAL merging, but the resulting new_column_names is built only by filtering the existing all_column_names. If all_column_names does not already contain some FINAL-required special columns (e.g. version, sign, is_deleted, Graphite params), they will not be added here and the FINAL merging pipeline can later fail due to missing inputs. Consider explicitly ensuring that all_column_names is augmented with getColumnsRequiredForMergingFinal(...) (or at least validating they’re present and inserting missing names in a stable order) before constructing new_column_names.

Copilot uses AI. Check for mistakes.
Comment on lines +1644 to +1653
NameSet columns_to_restore;
for (const auto & column : storage_snapshot->metadata->getSortingKey().expression->getRequiredColumnsWithTypes())
sorting_columns.insert(column.name);
restorePrewhereInputs(query_info.row_level_filter.get(), query_info.prewhere_info.get(), sorting_columns);
columns_to_restore.insert(column.name);
if (!data.merging_params.version_column.empty())
columns_to_restore.insert(data.merging_params.version_column);
if (!data.merging_params.sign_column.empty())
columns_to_restore.insert(data.merging_params.sign_column);
if (!data.merging_params.is_deleted_column.empty())
columns_to_restore.insert(data.merging_params.is_deleted_column);
restorePrewhereInputs(query_info.row_level_filter.get(), query_info.prewhere_info.get(), columns_to_restore);

Copilot AI Feb 11, 2026

Copy link

Choose a reason for hiding this comment

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

This duplicates parts of the “special columns required for FINAL merging” logic and is currently incomplete for some merging modes (notably Graphite: graphite_params.*_column_name aren’t restored here). To avoid divergence and missed modes, build columns_to_restore using getColumnsRequiredForMergingFinal(result_sort_description, data.merging_params) (union with sorting key columns if still needed), rather than manually inserting a subset of fields.

Copilot uses AI. Check for mistakes.
@antaljanosbenjamin
antaljanosbenjamin added this pull request to the merge queue Feb 17, 2026
Merged via the queue into master with commit edae50c Feb 17, 2026
129 of 130 checks passed
@antaljanosbenjamin
antaljanosbenjamin deleted the optimize-remove-unused-column-read-from-merge-tree branch February 17, 2026 18:12
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 17, 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.

4 participants