Repository navigation
Defer row policies over float sorting key columns after FINAL - #121782
Conversation
|
Workflow [PR], commit [9866864] Summary: ✅
AI ReviewSummaryThis PR fixes the Findings❌ Blockers
Tests
Final Verdict❌ Needs changes before merge: LLVM Coverage ReportMeasured on commit 9866864.
Changed lines: Changed C/C++ lines covered: 50/50 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 46 s compile time in total, 8 of them have a recent master baseline. |
| -- the policy hides the winner (1, -0.0, 2), the stale (1, 0.0, 1) must not reappear | ||
| SELECT '= the stale version stays hidden ='; | ||
| SELECT id, f, v FROM t_rp_float_key FINAL; | ||
| SELECT id, f, v FROM t_rp_float_key FINAL SETTINGS use_skip_indexes_if_final = 1; |
There was a problem hiding this comment.
(found by an agent) This query can't fail. use_skip_indexes_if_final_exact_mode defaults to 1, and its second pass re-expands the pruned ranges, so the result is empty whether or not the float atoms are excluded from index analysis. Please add use_skip_indexes_if_final_exact_mode = 0 here. Checked on 26.10.1.551 with a policy shaped to take the deferral path (deleted = 0 AND toString(f) = '0', set(0) index on toString(f)): empty at exact_mode = 1, stale row 1 0 1 at exact_mode = 0.
Also, NaN is never tested, neither nested like Tuple(Float64, UInt8) or Nullable(Float64) - would be nice to have it for coverage.
| NameSet sortingKeyNamesSafeBeforeFinal(const KeyDescription & sorting_key) | ||
| { | ||
| NameSet names; | ||
| for (size_t i = 0; i < sorting_key.column_names.size(); ++i) | ||
| { | ||
| bool has_float = isFloat(removeLowCardinalityAndNullable(sorting_key.data_types[i])); | ||
| sorting_key.data_types[i]->forEachChild([&](const IDataType & child) | ||
| { | ||
| if (!has_float && WhichDataType(child).isFloat()) | ||
| has_float = true; | ||
| }); | ||
| if (!has_float) | ||
| names.insert(sorting_key.column_names[i]); | ||
| } | ||
| return names; | ||
| } |
There was a problem hiding this comment.
apparently we have the same problem in MergeTreeWhereOptimizer but it's only reachable with no policy at all:
CREATE TABLE t (id UInt64, f Float64, v UInt64, s String)
ENGINE = ReplacingMergeTree(v) ORDER BY (id, f);
INSERT INTO t VALUES (1, 0.0, 1, 'stale');
INSERT INTO t VALUES (1, -0.0, 2, 'winner');
SELECT * FROM t FINAL WHERE toString(f) = '0' SETTINGS optimize_move_to_prewhere_if_final = 1;
-- returns: 1 0 1 stale (correct answer is empty)
not a blocker, but we need to either expand this PR or create a follow-up
There was a problem hiding this comment.
Before starting I checked for overlap: that fix is already written in #121350, which is open. It adds getSortingKeyNamesSafeBeforeFinal in MergeTreeWhereOptimizer.cpp and builds sorting_key_names from it, so a condition over a float or float-containing sorting key column stops being eligible to move under FINAL, and its test adds the 0.0 / -0.0 ReplacingMergeTree case with an EXPLAIN assertion. Master itself is still unguarded: the constructor at src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp:126 builds that set from every sorting key column, and #121782 did not touch the file.
Two gaps still make a separate PR worth having, so I will open one against master unless you would rather not have it:
- Enable
optimize_move_to_prewhere_if_finalby default #121350 also flipsoptimize_move_to_prewhere_if_finaltotrue, so it is not backportable, while the wrong result is reachable on stable releases for anyone who enables the setting explicitly. - Its float block covers plain
Float64only. The NaN payload,Nullable(Float64)andTuple(Float64, UInt8)cases you asked for on the row policy test are not covered on the PREWHERE side.
So: the same guard, no default change, plus the NaN and nested float coverage. If you would rather have all of it land inside #121350, say so and I will drop mine.
There was a problem hiding this comment.
Opened as #122142. Confirmed still live after #121782 merged (that change did not touch MergeTreeWhereOptimizer): your reproducer returns 1 0 1 stale on a build of current master, and the new test covers NaN payloads, Nullable(Float64), Tuple(Float64, UInt8) and Array(Tuple(Float64, UInt8)) on the PREWHERE side.
The guard is character-identical to the one already in open #121350, so whichever lands first the other side is a rename and a dropped hunk, and my offer to close mine and fold the coverage into #121350 stands.
| If the row policy expression depends only on columns in ORDER BY, it will still be applied before FINAL as an optimization, | ||
| since such filtering cannot affect the deduplication result. | ||
| If the row policy expression is deterministic and depends only on non-floating-point columns in ORDER BY, it will still be | ||
| applied before FINAL as an optimization, since such filtering cannot affect the deduplication result. Floating-point columns |
There was a problem hiding this comment.
| applied before FINAL as an optimization, since such filtering cannot affect the deduplication result. Floating-point columns | |
| applied before FINAL as an optimization, since such filtering cannot affect the deduplication result. Floating-point columns or columns containing floating-point (such as Tuple(Float64, …), Array(Float64), Nullable(Float64), etc.) |
There was a problem hiding this comment.
| Note: PREWHERE is also deferred, regardless of this setting, whenever a row policy is deferred by | |
| `apply_row_policy_after_final`, because the row policy must be applied before PREWHERE. That happens when the | |
| policy expression is non-deterministic, or reads a column that is not in ORDER BY, or reads an ORDER BY column | |
| whose type is or contains a floating-point type. | |
| Possible values: | |
| - 0 — PREWHERE is applied before FINAL, unless a deferred row policy defers it too (default). | |
| - 1 — PREWHERE is applied after FINAL. |
…mary key column `buildIndexes` turns `use_skip_indexes_if_final_exact_mode` off when every column of every useful skip index is in the primary key (ClickHouse#93899): rows of one FINAL group have equal key values, so such an index cannot drop the winner of a group while keeping an older version. For floats that premise is false. The FINAL merge treats -0.0 and 0.0, and all NaN payloads, as one key, while a skip index stores the actual values. With rows (1, 0.0, 1) and (1, -0.0, 2) in two parts and `INDEX ix_f f TYPE set(0)` on key column `f`, `FINAL WHERE toString(f) = '0'` drops the granule with the winner and returns the superseded row; SummingMergeTree returns a partial sum. Build the set of primary key columns with `sortingKeyNamesSafeBeforeFinal`, which excludes float-containing columns for the same reason in deferred row policies (ClickHouse#121782). A skip index on a float key column now keeps the exact-mode recovery pass on; for such keys it reads the whole surviving parts, as it already does for an index on a non-key column of these tables. Indexes on non-float key columns keep the shortcut. Closes ClickHouse#122346 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Row policies over floating-point sorting key columns are now applied after
FINAL(withapply_row_policy_after_finalenabled), and such columns are excluded from index analysis of deferred filters. Previously a policy condition distinguishing-0.0from0.0orNaNpayloads could filter out the winning row version before the merge and return stale data.Workflow [PR]
Sync PR [sync-upstream/pr/121782]
Version info
26.10.1.641-master(included in26.10and later)