Skip to content

Cherry pick #107988 to 26.4: Fix performance regression for Map subcolumns with PREWHERE - #108380

Merged
robot-clickhouse merged 8 commits into
backport/26.4/107988from
cherrypick/26.4/107988
Jun 24, 2026
Merged

robot-clickhouse merged 8 commits into
backport/26.4/107988from
cherrypick/26.4/107988

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

Avogar and others added 7 commits June 19, 2026 16:03
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]>
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
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Jun 24, 2026
Adapt the Map subcolumn PREWHERE fix to the 26.4 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]>
@robot-clickhouse
robot-clickhouse merged commit 8ffd621 into backport/26.4/107988 Jun 24, 2026
1 check passed
@robot-clickhouse
robot-clickhouse deleted the cherrypick/26.4/107988 branch June 24, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants