Repository navigation
Compute per-row Tuple range mask in hilbertEncode and mortonEncode - #104992
Algunenano merged 4 commits into
Conversation
When the first argument of `hilbertEncode` or `mortonEncode` was a non-constant `Tuple` column (for example produced by `materialize` or by an `ARRAY JOIN` over `Tuple` values), the implementation silently read the range-mask values from row 0 of the tuple and used them to drive the bit-shift for every row, producing wrong results. `function_prop_fuzzer` flagged this as a determinism violation: single-row vs multi-row execution of the same input row produced different `hilbertEncode` values. Sibling `Decode` functions (`hilbertDecode`, `mortonDecode`) already enforce that the first argument is a constant via `FunctionSpaceFillingCurveDecode::getReturnTypeImpl`; the `Encode` side was the asymmetric outlier. Introduce `FunctionSpaceFillingCurveEncode::extractConstantRangeMask` and route both `hilbertEncode` and `mortonEncode` through it. When the first argument has `Tuple` type and is not a constant column (and there is more than one row to process), throw `ILLEGAL_COLUMN`. The check is gated on `input_rows_count > 1` so the legitimate all-const-arguments path (where `useDefaultImplementationForConstants` strips the `ColumnConst` wrapper and leaves a single-row `ColumnTuple`) keeps working unchanged. Add a `04242_space_filling_curve_encode_non_const_mask` regression test covering both encoders and both the simple-mode and constant-mask paths. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Pre-PR validation gate (session (a) Deterministic repro: (b) Root cause: (c) Fix matches root cause: the new (d) Test intent preserved: no existing test is modified or weakened. Verified all 7 existing hilbert/morton tests still pass: (e) Both directions demonstrated: pre-fix → wrong silent results ( (f) Fix is general, not a narrow patch: the helper lives in the shared base class Competing-PR check: the only open PR touching these files is #104948 ( Originally requested by |
|
cc @rschu1ze @alexey-milovidov — could you review this? It closes the asymmetry between |
|
Workflow [PR], commit [311fdf6] Summary: ❌
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
Algunenano
left a comment
There was a problem hiding this comment.
@groeneai This is the wrong approach. You should fix the functions by using the values of each row, not by blocking it
Move the constant-only check on the range-mask `Tuple` argument from `extractConstantRangeMask` (executed per chunk, gated on `input_rows_count > 1`) to `FunctionSpaceFillingCurveEncode::getReturnTypeImpl`, which runs once at query analysis time on the original `ColumnsWithTypeAndName` before `useDefaultImplementationForConstants` strips any `ColumnConst` wrapper. The previous gate was the size of the current pipeline chunk, not a query-level property: with `SET max_block_size = 1`, a non-constant `Tuple` mask was sliced into 1-row chunks and silently produced wrong results (row 0's mask values applied to every row) instead of throwing `ILLEGAL_COLUMN`. Naturally single-row sources (e.g. `FROM numbers(1)`) exhibited the same issue at any `max_block_size`. The new placement mirrors the constant-only contract enforced by `FunctionSpaceFillingCurveDecode::getReturnTypeImpl` and applies uniformly to `hilbertEncode` and `mortonEncode`. `extractConstantRangeMask` is simplified accordingly: it now only extracts the tuple, relying on the upstream invariant established at type resolution. Extends `04242_space_filling_curve_encode_non_const_mask` to cover both the `max_block_size = 1` case and naturally single-row sources. Addresses clickhouse-gh[bot] review on ClickHouse#104992 (comment).
Pre-PR Validation Gate (for commit 12b1a4c —
|
|
@groeneai You keep going in the wrong path. You shouldn't restrict non const arguments, you should fix them |
… rejecting Per @Algunenano's directive on the PR: a non-constant `Tuple` range mask should not be rejected — the functions should accept it and use the mask values of each row. The previous commit on this branch did the opposite (threw `ILLEGAL_COLUMN`); this commit replaces the rejection with per-row mask evaluation. The implementations previously read the range mask from row 0 of the `Tuple` argument and used the same values for every output row. For a `ColumnConst`-wrapped `Tuple` (the common case: a literal or constant- folded expression) this is correct, because all rows share the same mask. For a non-constant multi-row `ColumnTuple` (e.g. produced by `materialize`, `ARRAY JOIN`, or by reading a `Tuple` column from a table) each row has its own mask values, and reading row 0 for every row was a silent wrong-result bug. The bug was block-size-dependent: with `SET max_block_size = 1` each chunk had a single row and row 0 of that chunk was the correct row, so the partial output happened to be right; with the default block size the entire batch used row 0's mask, producing wrong values. `function_prop_fuzzer` flagged this as a determinism violation (single-row vs multi-row execution diverged). Approach: * `FunctionSpaceFillingCurveEncode::extractConstantRangeMask` is replaced with `extractRangeMask`, returning a small `RangeMask` struct that holds the underlying `ColumnTuple` and a flag indicating whether the outer column was a `ColumnConst`. Reading via `RangeMask::read(col_idx, row_idx)` forwards to row 0 for constant masks (correct, single-row underlying tuple) and to `row_idx` for non-constant masks (per-row values). * `FunctionHilbertEncode::executeImpl` and the `EXECUTE` macro in `mortonEncode.cpp` now read each row's ratio inside the encoding loop. The bit-shift / encoder selection now uses the row's own mask values. * Validation of the ratio ranges (0-32 for hilbert, 1-8 for morton) runs once for constant masks and per row for non-constant masks, so an out-of-range value in any row throws `ARGUMENT_OUT_OF_BOUND`. * `FunctionSpaceFillingCurveDecode` still requires a `ColumnConst` mask, because the result tuple size is part of the return type and must be resolved at analysis time. The regression test in `04242_space_filling_curve_encode_non_const_mask` is updated to assert the correct per-row values instead of `ILLEGAL_COLUMN`, including a naturally single-row source and an explicit `SET max_block_size = 1` case to verify block-size independence. Per-row validation is exercised by single-row sources to keep the test output independent of chunk size (a mid-sequence bad row in a multi-row source would stream the preceding rows in 1-row chunks before throwing, which is correct but orthogonal to the property under test). Addresses @Algunenano's review on ClickHouse#104992 (review)
|
@Algunenano Thanks for the redirect — you were right, blocking was the wrong response. Pushed a2dbfe2e7db which replaces the rejection with per-row mask evaluation. What changed
VerificationSET max_block_size = 1; -- and also with the default
SELECT hilbertEncode(a, b) FROM (
SELECT tuple(toUInt64(number)) AS a, toUInt16(3) AS b FROM numbers(3)
);
-- before: 3, 3, 3 (default block) / 3, 6, 12 (max_block_size = 1) ← buggy & block-size-dependent
-- after: 3, 6, 12 in both cases (= 3 << 0, 3 << 1, 3 << 2)Single-row vs multi-row execution of the same logical row now produce The regression test Pre-PR Validation Gate
Session: |
Algunenano
left a comment
There was a problem hiding this comment.
@groeneai Please don't be so verbose with comments. There is no need to say what the code is doing when the code is right there (e.g For constant masks only row 0 carries information, Validate ratios..., the long comment at the start of the test).
Also, please update the PR title and description with your latest changes
Remove explanatory `///` and `/* */` comments from `FunctionSpaceFillingCurve.h`, `hilbertEncode.cpp` and `mortonEncode.cpp` where the code is self-explanatory, and strip the long header comment plus per-section paragraphs from `04242_space_filling_curve_encode_non_const_mask.sql`. Per-row mask evaluation, validation behaviour, and the regression test results are unchanged from a2dbfe2.
|
@Algunenano done — pushed 311fdf6314c8 which:
No code/test logic changes — the encoded values and the per-row validation behaviour stay exactly the same as in a2dbfe2e7db. |
LLVM Coverage Report
Changed lines: 95.56% (43/45) · Uncovered code |
cbbf9e8
|
Hi — could this be a backport candidate for Affected code: Why this might apply: The Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider |
Why
When the first argument of
hilbertEncodeormortonEncodewas a non-constantTuplecolumn (for example produced bymaterializeor by reading aTuplecolumn from a table), the implementation silently read the range-mask values from row 0 of the tuple and used them to drive the bit-shift for every row, producing wrong results.function_prop_fuzzerflagged this as a determinism violation — single-row vs multi-row execution of the same logical input row produced differenthilbertEncodevalues. The result also depended on the chunk size: withSET max_block_size = 1each row was its own chunk so the bug was hidden, while with the default block size the same query returned wrong values.Example with the tuple
tuple(N)and value3:What
Introduce
FunctionSpaceFillingCurveEncode::RangeMaskandextractRangeMask. The struct carries the underlyingColumnTupleplus anis_constflag and exposesread(col_idx, row_idx)that reads row 0 for constant masks and rowrow_idxfor non-constant masks.FunctionHilbertEncode::executeImpland theEXECUTEmacro inmortonEncode.cppnow read each row's ratio inside the encoding loop, and validation runs per row for non-constant masks.FunctionSpaceFillingCurveDecodekeeps its existing strictColumnConstrequirement — the result tuple size is part of the return type and must be resolvable at analysis time.Add
04242_space_filling_curve_encode_non_const_maskregression test covering simple mode, constant masks, per-row non-constant masks, block-size independence (SET max_block_size = 1), and per-row out-of-range validation.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a silent wrong-result bug in
hilbertEncodeandmortonEncodewhere a non-constantTuplefirst argument (range mask) used row 0's values to drive the bit-shift for every row. The functions now compute the per-row mask values, so the result no longer depends on the input being constant or on the block size.Documentation entry for user-facing changes
(10, 6); non-constantTuplemasks are now also supported with per-row semantics.Version info
26.5.1.813