Repository navigation
Compare nested NULLs and collated strings in the TopK threshold tracker the way the sort does - #125288
Conversation
…er like the sort `TopKThresholdTrackerGeneric::compareFields` honoured NULLS FIRST/LAST and the collation only for a top-level value and otherwise fell back to `Field::operator<`, which puts a nested NULL first, a nested NaN last and compares nested strings bytewise. The Parquet reader picks a row group's best value with `IColumn::compareAt` and the sort's `nulls_direction`, so the TopK query condition cache verdict of the File engine could record a row group that holds result rows as holding none, and every rerun skipped it. For a sort type with a Nullable or a float anywhere, or with a collation, compare the two values as rows of one column of the sort type with `compareAt` / `compareAtWithCollation` and `nulls_direction`, the comparison `updateTopKBestValue` and `__topKFilter` already use. Other types keep the Field comparison, which already matches the sort, so the per-mark primary key and skip index pruning (which never see these types) pay nothing. A value that does not fit the sort type throws from `insert` instead of comparing as equal. Closes ClickHouse#124788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the empty_column member comment to its invariant (when it is set). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261012-000600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261011-231700 |
|
Workflow [PR], commit [7f48b68] Summary: ✅
AI ReviewSummaryThe PR fixes the Findings❌ Blockers
Missing context / blind spots
Final Verdict❌ Block until nested runtime-typed keys are excluded from every TopK threshold path, either through PR #124984 or an equivalent change. Preserve a focused regression test for that exclusion. The existing inline thread was left unchanged; no duplicate comments were posted. |
| TopKThresholdTrackerGeneric::TopKThresholdTrackerGeneric(const SortColumnDescription & sort_desc_, DataTypePtr data_type_) | ||
| : ITopKThresholdTracker(sort_desc_) | ||
| { | ||
| if (sort_desc.collator || anyInTypeTree(*data_type_, [](const IDataType & node) { return node.isNullable() || isFloat(node); })) |
There was a problem hiding this comment.
This still leaves nested Variant sort keys on the raw Field path. The new guard only switches to column-based comparison for collation / nested Nullable / float, but ColumnVariant::compareAt does not use the same cross-type order as Field::operator<: for example Variant(UInt64, Int64) is canonically ordered by type name (Int64 before UInt64), while Field orders them by Field::Types tag (UInt64 before Int64). Because the TopK optimizer only rejects top-level Variant/Dynamic, a key like Tuple(Variant(UInt64, Int64)) can still reach this tracker with allow_suspicious_types_in_order_by = 1, and then testAndSet / isValueInsideThreshold can disagree with the actual ORDER BY and skip or cache away result-bearing data. Please either route nested runtime-typed keys through the column-based comparator too, or add a deep type-tree eligibility check that keeps them off every TopK path.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units176 translation units recompiled, 1800 s compile time in total, 176 of them have a recent master baseline. |
…-nested-null-order
CI finish ledger - 112d62fEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261012-193011 |
…-nested-null-order # Conflicts: # src/Processors/TopKThresholdTracker.h
|
Merged master to resolve the conflict with #125221 (both PRs changed the |
Backport #125288 to 26.10: Compare nested NULLs and collated strings in the TopK threshold tracker the way the sort does
…in the TopK threshold tracker the way the sort does
Closes: #124788
Related: #122064
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Fixes a master-only regression from #122064 (in no release yet), reported in #124788: a rerun of
SELECT s FROM t ORDER BY s LIMIT 3over aFile(Parquet)table withs Tuple(a UInt64, b Nullable(UInt64))returns(0,0) (1,NULL) (1204,1004)instead of(0,0) (1,5) (1,NULL).The TopK query condition cache drops a row group when the threshold tracker says the group's best value sorts beyond the final threshold. The best value is picked with
IColumn::compareAtand the sort'snulls_direction, butTopKThresholdTrackerGeneric::compareFieldsappliednulls_directionand the collation only at the top level and otherwise usedField::operator<, which puts a nestedNULLfirst, a nested NaN last and compares nested strings bytewise. So(1,5)looked beyond(1,NULL)and the row group holding it was skipped on every rerun.The fix is in the comparator, so every consumer (the
Fileverdict and the same one in #124813,testAndSet, Parquet statistics, primary-key pruning) agrees with the sort. When the sort type has aNullableor a float anywhere, or a collation,compareFieldsinserts both values into a column of the sort type and compares them withcompareAt/compareAtWithCollationandnulls_direction, asupdateTopKBestValueand__topKFilteralready do. Other types keep theFieldcomparison, which already matches the sort.Validation: the new test (
TupleandArraywith a nestedNULL,ASCandDESC NULLS FIRST, and a nestedStringwithCOLLATE 'en') fails on master in all five arms and passes with the fix, 150/150 in repeated runs. Also checked by hand on master and with the fix:LowCardinality(Nullable),Map(K, Nullable(V))and a nestedTuple. No measurable cost on a 50M-rowORDER BY s LIMIT 10over MergeTree and Parquet.Workflow [PR]
Sync PR [sync-upstream/pr/125288]
Version info
26.11.1.6-master(included in26.11and later)