Repository navigation
Cherry pick #107988 to 26.5: Fix performance regression for Map subcolumns with PREWHERE - #108381
Merged
Merged
Conversation
After introducing bucketed Map serialization and Map subcolumn support (`SerializationMapKeyValue`, `SerializationMapSize`), queries using `optimize_functions_to_subcolumns` with Map columns (e.g. `tags['key']` rewritten to `tags.key_key`) suffered a severe performance regression when PREWHERE was enabled. Root cause: `tryBuildPrewhereSteps` grouped PREWHERE conditions by their logical column names (`required_columns`). Since each Map subcolumn has a distinct name (e.g. `tags.key_k0`, `tags.key_k1`), each one was placed into a separate PREWHERE step with its own reader, causing redundant decompression and deserialization of the same underlying Map data. Fix: - Group PREWHERE conditions by physical storage column name (`required_storage_columns`) instead of logical subcolumn name. Resolve names via `ColumnsDescription::tryGetColumnOrSubcolumn` and `getNameInStorage`. This ensures all subcolumns of the same Map column share a single PREWHERE step and reader. - Remove the `path.empty()` guard in `ISerialization` substreams cache functions so that `SerializationMapKeyValue` can cache the deserialized Map nested column at the top level, allowing multiple key-value subcolumns to share the data within one `readRows` call instead of each re-reading the same range. - Revert the optimized sparse offsets cache in `SerializationSparse` to use the standard `addElementToSubstreamsCache`/`getElementFromSubstreamsCache` API, which is now correct with the `path.empty()` guard removed. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…reads When `optimize_functions_to_subcolumns` rewrites `tags['k0']` to `tags.key_k0`, each Map subcolumn gets a distinct logical column name. The WHERE optimizer's `move_condition` grouped conditions by logical column names (`table_columns`), so conditions on different Map subcolumns were moved independently and could end up non-adjacent in the PREWHERE list. This caused `tryBuildPrewhereSteps` to place them into separate read steps, each redundantly decompressing the same underlying Map data. Fix: resolve column names to their physical storage names via `ColumnsDescription::tryGetColumnOrSubcolumn` and `getNameInStorage`, then group by `table_storage_columns` in `move_condition`. This ensures all conditions on subcolumns of the same Map column are moved together. Also remove the non-functional test `04330_prewhere_map_subcolumns_grouping` (returned the same result on master) and add a performance test case with interleaved Map and non-Map WHERE conditions. Co-Authored-By: Claude Opus 4.6 <[email protected]>
When conditions in explicit PREWHERE are interleaved (e.g. Map subcolumn checks separated by a condition on another column), `tryBuildPrewhereSteps` previously only grouped adjacent conditions with matching storage columns. Non-adjacent conditions on the same Map column would end up in separate read steps, causing redundant deserialization. Replace adjacent-only grouping with a linear scan over existing groups so that any condition matching an earlier group's `required_storage_columns` is appended to that group regardless of position in the condition list. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Non-adjacent grouping could reorder predicates past intentional guards, e.g. `PREWHERE tags['safe'] != '' AND value > 0 AND toUInt64(tags['unsafe']) > 0` would pull the throwing conversion before the `value > 0` filter. Revert to adjacent-only grouping in `tryBuildPrewhereSteps`. This is safe because `MergeTreeWhereOptimizer` already groups subcolumns of the same storage column together, so they arrive adjacent for the WHERE-to-PREWHERE path. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…prewhere-regression # Conflicts: # src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp
…e-regression Fix performance regression for Map subcolumns with PREWHERE
Adapt the Map subcolumn PREWHERE fix to the 26.5 branch code structure: - `MergeTreeWhereOptimizer`: add `table_storage_columns` field to `Condition` and resolve column names to physical storage names in `analyzeImpl`, then group by storage columns in `move_condition` - `MergeTreeSplitPrewhereIntoReadSteps`: keep new includes for `ColumnsDescription` - `SerializationSparse`: trivial comment/whitespace conflicts Co-Authored-By: Claude Opus 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #107988
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job