Repository navigation
Fix read-in-order skipping the sort when a value is compared through Variant or Dynamic - #125378
Conversation
|
Workflow [PR], commit [a0080d0] Summary: ✅
AI ReviewSummaryThis PR tightens read-in-order fixed-column detection so Final Verdict✅ No blockers or majors found in the current version. |
diegomestre2
left a comment
There was a problem hiding this comment.
Thanks for the PR, it looks great, I left a few comments.
FixedString is compared through Variant or DynamicVariant or Dynamic
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 19 s compile time in total, 7 of them have a recent master baseline. |
FixedString constant
#123669
Closes: #125178
Closes: #123669
Related: #123677
Related: #125179
The in-order optimizations (
ORDER BY,GROUP BYwithoptimize_aggregation_in_order,LIMIT BY) treat a column compared with a constant byequalsas fixed, and skip sorting or grouping by it. That is wrong whenequalsmatches several values that sort differently. #123677 handled aStringcolumn compared with a plainFixedStringconstant, whichequalspads with zero bytes (s = toFixedString('V0', 3)matches'V0','V0\0','V0\0\0', ...), also inside aTuple. This PR fixes the cases that were still wrong:Stringkey column compared with aFixedStringheld by aVariantorDynamicconstant:s = CAST(toFixedString('V0', 3) AS Dynamic),Dynamic(max_types = 0),JSONExtract(..., 'Variant(FixedString(3), UInt64)'), or such a value inside aTuple:t = tuple(CAST(toFixedString('V0', 3) AS Dynamic)),t = CAST(tuple(toFixedString('V0', 3)) AS Dynamic). These are the constants of04674_key_condition_erased_fixed_string_constant, where Read-in-order skips sorting on a key column compared with a zero-paddedFixedStringconstant #123669 was found.ORDER BY,GROUP BYin order andLIMIT BYreturned wrong results.VariantorDynamicexpression, such as a JSON path, compared with any constant and used inORDER BY. ADynamicvalue can be equal to the constant in several types, which sort differently:j.a = 1matches1stored asInt64and asFloat64, andj.a = toFixedString('V0', 3)matches'V0'and'V0\0'.ORDER BYreturned wrong results.Unchanged: a
VariantorDynamicconstant that holds aString(s = CAST('V0' AS Dynamic)) matches one value, so the column stays fixed (EXPLAINkeepsk ASC, s DESC).equalsMatchesSeveralValues(renamed back fromcomparesStringWithFixedString, because it now also coversVariant/Dynamiccolumns) now looks at the constant's value. It skipsNULL, removesLowCardinalityandNullable, and recurses into the value held by aVariantorDynamicconstant. ADynamicvalue is first inserted into a new column, which moves it out of the shared variant. AVariantorDynamiccolumn side never counts as fixed, so such queries may sort more rows than before.Decimal/DateTime64keys compared with a float constant (#125179) are not covered by this change.05321_read_in_order_string_vs_fixed_string_constantgets the reproducer from #125178 and a query for each case above, withEXPLAINchecks for the constants that keep the column fixed.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of
ORDER BY,GROUP BYwithoptimize_aggregation_in_orderandLIMIT BYwith the in-order optimizations when aStringsorting key column is compared with aFixedStringheld by aVariantorDynamicconstant, for exampleWHERE s = CAST(toFixedString('a', 2) AS Dynamic). Also fixed a wrongORDER BYresult when aVariantorDynamicexpression, such as a JSON path, is compared with a constant, for exampleWHERE j.a = 1overInt64andFloat64values. 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
26.10.1.2243-master(included in26.10and later)