Repository navigation
Optimize remove unused column read from merge tree - #89982
antaljanosbenjamin merged 21 commits into
Conversation
|
Workflow [PR], commit [b21d9b7] Summary: ❌
|
78b88d2 to
7a45907
Compare
This causes issues in handling the outputs of ActionsDAG. Will be addressed later once the removal of unused columns will be done based on positions.
a5cdef1 to
e95f0a5
Compare
…sed-column-read-from-merge-tree
…sed-column-read-from-merge-tree
…sed-column-read-from-merge-tree
…sed-column-read-from-merge-tree
…sed-column-read-from-merge-tree
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.
|
There are some test (that are not present in public) changes in private, please check them too. |
There was a problem hiding this comment.
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 withPREWHERE, row-level filters, andFINAL. - Invoke unused-column removal after
PREWHEREoptimization when enabled. - Add/adjust stateless tests and references covering MergeTree reads,
FINALspecial columns, and updatedEXPLAINoutputs.
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. |
| 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); | ||
|
|
There was a problem hiding this comment.
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.
| 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); | ||
| } |
There was a problem hiding this comment.
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.
| 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); |
There was a problem hiding this comment.
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.
Co-authored-by: Sergei Trifonov <[email protected]>
Changelog category (leave one):
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
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
26.2.1.855