Skip to content

Fix read-in-order skipping the sort when a value is compared through Variant or Dynamic - #125378

Merged
NIKTONIKTO717 merged 4 commits into
masterfrom
fix-dynamic-variable-string-order-by
Oct 12, 2026
Merged

NIKTONIKTO717 merged 4 commits into
masterfrom
fix-dynamic-variable-string-order-by

Conversation

@NIKTONIKTO717

@NIKTONIKTO717 NIKTONIKTO717 commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Closes: #125178
Closes: #123669
Related: #123677
Related: #125179

The in-order optimizations (ORDER BY, GROUP BY with optimize_aggregation_in_order, LIMIT BY) treat a column compared with a constant by equals as fixed, and skip sorting or grouping by it. That is wrong when equals matches several values that sort differently. #123677 handled a String column compared with a plain FixedString constant, which equals pads with zero bytes (s = toFixedString('V0', 3) matches 'V0', 'V0\0', 'V0\0\0', ...), also inside a Tuple. This PR fixes the cases that were still wrong:

  1. A String key column compared with a FixedString held by a Variant or Dynamic constant: s = CAST(toFixedString('V0', 3) AS Dynamic), Dynamic(max_types = 0), JSONExtract(..., 'Variant(FixedString(3), UInt64)'), or such a value inside a Tuple: t = tuple(CAST(toFixedString('V0', 3) AS Dynamic)), t = CAST(tuple(toFixedString('V0', 3)) AS Dynamic). These are the constants of 04674_key_condition_erased_fixed_string_constant, where Read-in-order skips sorting on a key column compared with a zero-padded FixedString constant #123669 was found. ORDER BY, GROUP BY in order and LIMIT BY returned wrong results.
  2. A Variant or Dynamic expression, such as a JSON path, compared with any constant and used in ORDER BY. A Dynamic value can be equal to the constant in several types, which sort differently: j.a = 1 matches 1 stored as Int64 and as Float64, and j.a = toFixedString('V0', 3) matches 'V0' and 'V0\0'. ORDER BY returned wrong results.

Unchanged: a Variant or Dynamic constant that holds a String (s = CAST('V0' AS Dynamic)) matches one value, so the column stays fixed (EXPLAIN keeps k ASC, s DESC).

equalsMatchesSeveralValues (renamed back from comparesStringWithFixedString, because it now also covers Variant/Dynamic columns) now looks at the constant's value. It skips NULL, removes LowCardinality and Nullable, and recurses into the value held by a Variant or Dynamic constant. A Dynamic value is first inserted into a new column, which moves it out of the shared variant. A Variant or Dynamic column side never counts as fixed, so such queries may sort more rows than before. Decimal/DateTime64 keys compared with a float constant (#125179) are not covered by this change.

05321_read_in_order_string_vs_fixed_string_constant gets the reproducer from #125178 and a query for each case above, with EXPLAIN checks for the constants that keep the column fixed.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed wrong results of ORDER BY, GROUP BY with optimize_aggregation_in_order and LIMIT BY with the in-order optimizations when a String sorting key column is compared with a FixedString held by a Variant or Dynamic constant, for example WHERE s = CAST(toFixedString('a', 2) AS Dynamic). Also fixed a wrong ORDER BY result when a Variant or Dynamic expression, such as a JSON path, is compared with a constant, for example WHERE j.a = 1 over Int64 and Float64 values. Such a comparison matches several values that sort differently, but the optimization treated them as one value and skipped sorting by them.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125378]

Version info

  • Merged into: 26.10.1.2243-master (included in 26.10 and later)

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a0080d0]

Summary: ✅


AI Review

Summary

This PR tightens read-in-order fixed-column detection so equals no longer treats a column as fixed when a FixedString is hidden inside a Variant or Dynamic constant, and it conservatively stops treating Variant/Dynamic expressions on the column side as fixed because comparison-function equality can match multiple values that still sort differently. I reviewed the current diff, the current code, and all existing PR discussion threads, and I did not find a remaining issue that rises to the level of an inline review comment.

Final Verdict

✅ No blockers or majors found in the current version.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 12, 2026

@diegomestre2 diegomestre2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, it looks great, I left a few comments.

@diegomestre2 diegomestre2 self-assigned this Oct 12, 2026
@NIKTONIKTO717 NIKTONIKTO717 changed the title Fix read-in-order skipping the sort when a FixedString is compared through Variant or Dynamic Fix read-in-order skipping the sort when a value is compared through Variant or Dynamic Oct 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a0080d0ab with master 360bb0de6 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 19 s compile time in total, 7 of them have a recent master baseline.

Job report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants