Repository navigation
Fix wrong ORDER BY with read-in-order when a String key column is compared with a FixedString constant - #123677
Conversation
`appendFixedColumnsFromFilterExpression` treated every `column = constant`
as fixing the column to one value, so read-in-order skipped sorting on it.
`equals` compares a `String` with a `FixedString` zero-padded, so
`s = toFixedString('V0', 3)` matches `'V0'`, `'V0\0'` and `'V0\0\0'`, which
sort differently, and `ORDER BY k, s DESC` returned them in the wrong order.
Skip such a condition when the column is a `String` (also under `Nullable`
and `LowCardinality`) and the constant is a `FixedString`. A `FixedString`
column compared with a `String` constant still counts as fixed, since all
its values have the same length and at most one of them matches.
Closes: ClickHouse#123669
|
Workflow [PR], commit [6adda56] Summary: ✅
AI ReviewSummaryThis PR narrows Final Verdict✅ No new findings. |
Merge upstream commit 756552d, including the `no-parallel-replicas` exclusion for `05218_timeseries_alp_codec`. Preserve the existing read-in-order fix and its regression test unchanged. Related: ClickHouse#123677 Related: ClickHouse#123763 CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123677&sha=20d3846994236af48dd6c0d7de94c83ba66be82a&name_0=PR Co-authored-by: Copilot <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline. |
NIKTONIKTO717
left a comment
There was a problem hiding this comment.
First of all, thank you @aschoube for a great PR. I'd like to add a couple of words.
In general PR as it is right now, is fixing subset of potential queries with wrong results. I think contracts you provide in PR title/description hold and it closes the direct form. However, I don't think it closes the issue #123669 . That issue mentions test 04674_key_condition_erased_fixed_string_constant which includes cases with Variant(FixedString(3)) or Dynamic holding FixedString(3) which are not covered by this PR. Therefore I'd suggest 2 options, feel free to pick one of them:
- Relax "Closes: " to "Related: ", address comments and then I'll suggest to merge it as it is right now. Then I'll continue with a separate PR addressing other.
- Implement support for
Variant/Dynamicas well. I did some draft and I think it should be manageable to cover following cases: read_in_order_string_vs_fixed_string_cases.sql . In case you pick this path, please ping me and I'll be happy to review it :) .
… check Rename the check to `comparesStringWithFixedString`, use `removeLowCardinalityAndNullable` and recurse into `Tuple` elements, which `equals` compares element by element. Test `Nullable` and `LowCardinality` `FixedString` constants and a `Tuple(String)` key column. Co-authored-by: Copilot <[email protected]>
…ple` cases Co-authored-by: Copilot <[email protected]>
|
@NIKTONIKTO717 I went with option 1: the description now has |
…ixedstring-constant
…ixedstring-constant
…ixedstring-constant
|
Merged master to pick up the fix for |
|
@NIKTONIKTO717 The test is flaky, can we do something about this ? |
4908081
Related: #123669
Related: #94017
Related: #117238
Read-in-order skipped sorting on a
Stringkey column compared with aFixedStringconstant, and returned rows in the wrong order:appendFixedColumnsFromFilterExpressiontreats the non-constant argument of everycolumn = constantas fixed to a single value, and read-in-order then drops the fixed key column from the sort. That holds only whenequalsmeans the values are byte-identical.equalscompares aStringwith aFixedStringzero-padded, sos = toFixedString('V0', 3)holds for'V0','V0\0'and'V0\0\0'. These three values sort differently, sosis not fixed, and the step that is skipped is exactly the one that orders them. WithLIMIT 1the query returns5630instead of56300000.Change
equalsno longer marks the column as fixed when the column is aStringand the constant is aFixedString, also underNullableandLowCardinality. Read-in-order then uses the key prefix beforesand sortssas usual.Tuplevalues are compared element by element withequals, so the same applies inside aTuple:t = tuple(toFixedString('V0', 3))holds fortuple('V0'),tuple('V0\0')andtuple('V0\0\0'). The check recurses intoTupleelements and does not marktas fixed if anyStringelement is compared with aFixedStringelement.ArrayandMapvalues are cast to a common type and compared byte by byte, so they are not affected.The check is deliberately narrow. A
FixedStringcolumn compared with aStringconstant (fs = 'abc', the usual way to filter aFixedStringkey) still counts as fixed. All values of aFixedString(N)column have the same length, so at most one of them equals the padded constant. AStringcolumn compared with aStringconstant is a byte comparison and is not affected either. For both shapesEXPLAINstill shows the fullk ASC, s DESCprefix, and the test checks it.Test
05321_read_in_order_string_vs_fixed_string_constantcoversString,Nullable(String)andLowCardinality(String)key columns with aFixedStringconstant, aStringkey column withNullable(FixedString)andLowCardinality(FixedString)constants, aTuple(String)key column with aTuple(FixedString)constant, theLIMIT 1case, and thePrefix sort descriptionfromEXPLAINfor three shapes:StringvsFixedString(nowk ASC),StringvsStringandFixedStringvsString(both stillk ASC, s DESC). On an unfixed master build theStringandLowCardinality(String)results come back in ascending order,LIMIT 1returns the wrong row andEXPLAINshowsk ASC, s DESC. With the fix every result matchesoptimize_read_in_order = 0.Locally the test passes in every run under randomized settings, except the runs where the randomizer enabled parallel replicas: my single local server does not define the
parallel_replicascluster (CLUSTER_DOESNT_EXIST), which CI has. The 263 existing stateless tests matchingread_in_order,in_order,fixed_stringorfixedstringshow no wrong-result diff; the ones that do not pass locally need clusters, datasets, system log tables or Python modules that my local server does not have.VariantandDynamiccolumns holding aFixedString, which04674_key_condition_erased_fixed_string_constantfrom the issue also covers, are not handled here.Benchmark
The change only affects query planning. The one shape it changes now sorts the rows that match the condition, which is what makes the result correct, so the cost is bounded by the number of matching rows.
clickhouse local, local release build, i5-1135G7. The table has 50M rows,ORDER BY (k, s), one part, and every seventh value has a trailing\0. Median of 11 interleaved runs, including about 70 ms of process startup.masteris an unfixed build.WHERE s = toFixedString('12345', 6) ORDER BY k, s DESC(the fixed shape)LIMIT 1WHERE s = '12345' ORDER BY k, s DESC(StringvsString)FixedString(8)column,WHERE s = '12345' ORDER BY k, s DESCWHERE k = 3 AND s = toFixedString('1', 2) ORDER BY k, s DESCThe
LIMIT 1row is the largest difference: the matching rows are sorted before the limit instead of being read in order. The two shapes that stay fixed are unchanged. On the same data,masterreturns the first four rows of the fixed shape as['3132333435', '3132333435', '3132333435', '3132333435'], while this PR returns['313233343500', '313233343500', '313233343500', '3132333435'], which is the requestedDESCorder.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a wrong
ORDER BYresult withoptimize_read_in_orderwhen aStringkey column is compared with aFixedStringconstant, for exampleWHERE s = toFixedString('a', 2). The comparison matches several values that differ only in trailing zero bytes, but the optimization treated the column as having one value and skipped sorting on it.Workflow [PR]
Sync PR [sync-upstream/pr/123677]
Version info
26.10.1.2116-master(included in26.10and later)