Skip to content

Cherry pick #116364 to 26.3: Do not read in order with a prefix longer than the table's sorting key - #119933

Merged
PedroTadim merged 5 commits into
backport/26.3/116364from
cherrypick/26.3/116364
Sep 14, 2026
Merged

PedroTadim merged 5 commits into
backport/26.3/116364from
cherrypick/26.3/116364

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #116364

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 4 commits August 25, 2026 13:26
spreadMarkRangesAmongStreamsWithOrder clones the sorting-key expression list
and calls children.resize(prefix_size) on it. That is written as a truncation
to the leading key columns and behaves as one while the prefix fits, but resize
grows the vector when the prefix is longer, and the element type is ASTPtr, so
the appended children are null. TreeRewriter::normalize then walks that AST with
the Customize* visitors, which dereference every child unconditionally. The same
block also indexes the sorting-key column names and reverse flags over the same
range, so an over-long prefix is two further out-of-bounds reads.

The prefix arrives verbatim from InputOrderInfo::used_prefix_of_sorting_key_size,
which requestReadingInOrder stored without checking it against the sorting key
it will later be applied to. ReadFromMerge::requestReadingInOrder forwards the
parent's prefix to every reader recursivelyApplyToReadingSteps finds, and is the
only caller not already bounded by the receiving reader's own key.

Refuse such a request before any state is mutated, so the caller falls back to
an explicit sort. Refusal is already this function's idiom for a request it
cannot serve (reverse order with FINAL), and all six callers handle it.

The guard newly refuses only requests whose prefix the table's own sorting key
cannot provide. Of those, the ones that reach preliminary merge or per-partition
output dereference a null AST node today; the remainder lose the read-in-order
optimization and fall back to an explicit sort, which is correct but may be
slower. No SQL input is currently known to produce such a request at default
settings, so the causal chain is demonstrated by forcing an over-long prefix at
the push-down site: the resulting stack matches the reported one frame for frame,
including the CustomizeFunctionsData visitor and the ReadFromMerge frames, and
the same query returns rows identical to the optimize_read_in_order = 0 baseline
with this change. Debug builds abort inside intrusive_ptr::operator->; release
and sanitizer builds report the dereference as a segmentation fault.

Found by the AST fuzzer. Its fuzzed queries execute on a context built by
Context::createCopy plus makeQueryContext, which does not install the per-query
metadata cache that otherwise keeps the optimizer's and the reader's
sorting-key reads consistent.

The added test pins that read-in-order is still chosen for a well-formed Merge
query, with a control asserting the check is not vacuous. It does not reach the
guard, since no SQL does.
…erts

The EXPLAIN predicate matches the "Read type:" spelling, which is produced
only when explain_query_plan_default is 'pretty'. Under a 'legacy' profile the
predicate matched nothing and the assertion silently passed on an empty match,
so both EXPLAIN arms now pin the setting.

Stopping merges keeps the two inserted parts distinct, so the preliminary merge
path that consumes the sorting-key prefix stays covered rather than depending on
whether a background merge has run.

The per-setting random-settings clamp line was dead: the no-random-merge-tree-settings
tag already disables merge-tree randomization and the CREATE pins index_granularity
explicitly. Such clamps fail silently when they do not apply, so it is removed
rather than left as decoration.

Co-Authored-By: Claude Fable 5 <[email protected]>
The repository rulebook (AGENTS.md, a symlink to .claude/CLAUDE.md) requires
literal class and function names to be wrapped in inline code blocks. The
comment added with the guard wrote prefix_size and ASTPtr as bare words; the
surrounding file already follows the rule.

Comment-only. Verified by stripping comments from both revisions and comparing
the resulting token streams, which are identical at 194572 bytes; a control
mutating one code token in the guard does change them.
…hin-sorting-key

Do not read in order with a prefix longer than the table's sorting key
@robot-ch-test-poll robot-ch-test-poll 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-critical-bugfix labels Sep 14, 2026
Keep only the sorting-key prefix guard from #116364. The master-only
`widened_over_previous_request` block came along with it, but its consumer
(`resetVirtualRowConversions`) does not exist on 26.3, so it would be an
unused variable.

Adapt the test: `explain_query_plan_default` is new in 26.7, and 26.3 prints
`ReadType: ` unconditionally rather than `Read type: `.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants