Skip to content

Cherry pick #124553 to 26.8: Group PREWHERE read steps by exact columns and rewrite m['key'] to a key subcolumn only for bucketed Map - #129572

Merged
robot-clickhouse-ci-1 merged 15 commits into
backport/26.8/124553from
cherrypick/26.8/124553
Nov 10, 2026
Merged

robot-clickhouse-ci-1 merged 15 commits into
backport/26.8/124553from
cherrypick/26.8/124553

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #124553

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.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

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

groeneai and others added 14 commits October 8, 2026 05:07
…n its subcolumn

Since #107988, tryBuildPrewhereSteps groups adjacent PREWHERE conditions
by the storage column they read, so that the keys of one Map, each of
which reads the whole Map, share one read step (#107912). The resolution
was type-blind: `j.kind` and `j`, two JSON paths, a Tuple element and the
whole Tuple, `m.size0` and `m.key_k1`, and keys of two Maps nested in one
Tuple all resolved to one storage column. A cheap subcolumn condition was
then merged with an expensive condition on the rest of the column, which
read the whole column for every row. On master a may-throw `toString(j)`
gets its own step (#111954), but its read-ahead, keyed on the same
comparison, reads `j` in step 0 anyway.

Key each input by the storage column and the substreams cache key of its
subcolumn path instead. A Map key subcolumn drops its trailing
MapKeyValue, so it keys as the Map that owns it (`(m, "")` at the top
level, as before; `(c, "%2Eleft")` for a Map inside a Tuple, so two
sibling Maps no longer share a step). The pair is used rather than a
concatenated string because the path rendering escapes tuple dots.

MergeTreeWhereOptimizer keeps grouping by storage column: that keeps a
guard ahead of a throwing condition on the same column. So with the
expensive condition written first the order is still kept.

Closes #124535

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the clause narrating how the new grouping identity splits steps from the resolveStorageColumnName comment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The performance test runner expands every `{name}` in a query template
with `str.format`, so the JSON literal in the fill query was read as a
`{"kind"}` placeholder and the test failed with `KeyError: '"kind"'`
before running any query. Double the braces, as other JSON perf tests do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…zation

With `basic` serialization, reading the key subcolumn `m.key_<key>` of a
`Map` reads the whole column, so the rewrite gave nothing there, and
#107988 had to group PREWHERE conditions by the storage column to keep
several keys of one `Map` in one read step. That grouping also merged a
cheap subcolumn condition with an expensive condition on the rest of the
same column (#124535). The first version of this pull request worked
around it with a `Map` key special case in `tryBuildPrewhereSteps`,
which put generic PREWHERE code in charge of `Map` serialization details
and was wrong for `with_buckets`.

Gate the rewrite on the new `IStorage::hasBucketedMapSerialization`,
true for a `MergeTree` table if either `map_serialization_version`
setting is `with_buckets`, and forwarded by the storages that pass the
analyzed query to other tables (`Merge` requires all of them). The check
is in `storageAllowsTransformer`, so both passes of
`FunctionToSubcolumnsPass` agree. Explicit key subcolumns still work for
any serialization.

With that, `MergeTreeWhereOptimizer` and `tryBuildPrewhereSteps` group
conditions by their exact column set again, and `tryBuildPrewhereSteps`
no longer needs `ColumnsDescription`. The `may_throw` split and the
read-ahead of a split run stay.

Tests that assert the rewrite now pin both map serialization settings,
which clickhouse-test randomizes. The new `05340` covers the gate, and
`05339` gets the `with_buckets` case from the CI review.

Closes: #124535
Related: #124553

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…re-subcolumn-read-steps

Keep the query condition cache attribution boundary from master in the
PREWHERE step grouping, comparing exact column sets. Drop the
`ColumnsDescription` argument from the new seal-gated
`MergeTreeSelectProcessor` call site.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The test compares the `arrayElement` and key subcolumn forms of
`m['key']`, and the key subcolumn rewrite now applies only to bucketed
Maps.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The new `SubcolumnSupportAnswers::bucketed_map_serialization` field was
missing from the aggregate initialization, which fails the build with
`-Wmissing-field-initializers`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124553&sha=6a84ce82278446b934bb316be58af106c2f4f53b&name_0=PR&name_1=Fast%20test
PR: #124553

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
In 04631 the interleaved Map conditions read the same column `tags`, so
the last step takes it from the first one instead of reading it again,
and only two of the three steps read rows.

In 05339 the WHERE optimizer may put `m['k1'] != ''` before
`length(m) = 3` based on the column sizes, and the key condition passes
every row. Write that case as PREWHERE, which keeps the written order,
since the test is about separate read steps, not about the order.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124553&sha=29003da91f5a35f49a43deb35dedd0055448880c&name_0=PR&name_1=Fast%20test
PR: #124553

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…re-subcolumn-read-steps

Keep the index-aware PREWHERE steps from master (`required_indexes`,
`index_read_tasks`), grouping table columns by exact name instead of by
storage column, and drop the `ColumnsDescription` argument from the new
`tryBuildPrewhereSteps` call.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The WHERE optimizer orders the conditions by the on-disk size of their
columns when it has no selectivity estimate. A repeated character
compresses to a few bytes per row, so the expensive subcolumn could be
ordered first and read for every row. Random values make the on-disk
size reflect the cost of reading a column.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124553&sha=0686a448ce0ba3aa70b6aeecc1aa20a2747a7b17&name_0=PR&name_1=Fast%20test
PR: #124553

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…_prefixes

The `m['key']` -> `m.key_<key>` rewrite now applies only to bucketed
`Map` serialization, while the test pins `basic`. Read the key
subcolumn explicitly, which works for any serialization, so the test
keeps measuring the size estimate of a `basic` key subcolumn.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124553&sha=2f21f04549bfebbd31b1ba4c196b8aa320e9481d&name_0=PR&name_1=Fast%20test
PR: #124553

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…re-subcolumn-read-steps

Keep master's separate groups for expensive conditions and the
`group_of` index in `MergeTreeWhereOptimizer`, grouping conditions by
their exact columns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n-read-steps

Group PREWHERE read steps by exact columns and rewrite `m['key']` to a key subcolumn only for bucketed `Map`
@robot-ch-test-poll3 robot-ch-test-poll3 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 labels Nov 5, 2026
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

The conflicts are all in code that 26.8 does not have: the may_throw split of PREWHERE steps (#111954), index reader columns, read_ahead_prewhere_columns, the seal-gated read path, the SubcolumnSupportAnswers cache (#113367) and the PREWHERE steps of ReadFromMemoryStorageStep. None of them is on its way to 26.8 (#113367 is labelled for it, but its cherry-pick #125099 was closed), so the robot's retry cannot resolve this one. I kept the 26.8 side there and applied only this PR's change: PREWHERE steps are grouped by their exact required_columns, the ColumnsDescription argument goes away at the 26.8 call sites, and m['key'] is not rewritten when hasBucketedMapSerialization is false (checked on the TypeIndex that 26.8 passes, plus the map_serialization_version_for_zero_level_parts declaration that MergeTreeData.cpp lacks on 26.8). The MergeTreeSplitPrewhereIntoReadSteps.cpp change is byte-identical to the 26.9 one. Tests 02346, 04630, 04631, 04935, 05060 and the keyValuePairs part of 05217 do not exist on 26.8; every other file applies as on master.

On the 26.8 tree (clang-22, debug), 05339_prewhere_subcolumn_read_steps and 05340_map_element_to_subcolumn_bucketed_only pass 50 of 50 with random settings; the 8 existing tests whose settings this PR pins pass too. Of the PREWHERE, subcolumn and Map stateless tests, 617 pass locally, and each of the 44 failures is an environment error (no test.hits, remote shards, Keeper, S3 or Iceberg files on this machine). The official 26.8.35.29 fails both new tests.

I cannot push to cherrypick/26.8/124553. The merge is on my fork, its first parent is this PR's head 1a12ca61bb13 and its second is backport/26.8/124553, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git groeneai/resolve-26.8-cherrypick-124553
git push origin FETCH_HEAD:cherrypick/26.8/124553

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Dear @novikd, @Avogar, @groeneai, this cherry-pick PR has not been updated for 3d0h4m2s. Please resolve the conflicts to backport #124553, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

Merge `backport/26.8/124553` and rebuild the change on it. The branch predates master's read step machinery, so instead of the master hunks:
- PREWHERE steps and the WHERE optimizer group conditions by exact column sets again: the storage column grouping of #107988 is reverted (keeping the `expensive` handling of this branch), and the `ColumnsDescription` parameter it added is removed from `tryBuildPrewhereSteps`, `getPrewhereActions` and `MergeTreeSelectProcessor`.
- `m['key']` is rewritten to `m.key_<key>` only if `IStorage::hasBucketedMapSerialization` is true (`MergeTree` with `with_buckets` in either map serialization setting; Proxy, MaterializedView, Alias, Buffer, Merge and MaterializedPostgreSQL forward it).
- Tests: 05339 and 05340 added; the existing tests that assert the rewrite pin `with_buckets`; edits to tests that do not exist on this branch are dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit f3f52d7 into backport/26.8/124553 Nov 10, 2026
2 checks passed
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.8/124553 branch November 10, 2026 19:23
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-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.

6 participants