Repository navigation
Fix set index on an expression with a Nullable operand - #113244
Conversation
`MergeTreeIndexConditionSet` matched a query subexpression to an index key column by name only. A `Nullable` operand folding to the same name made it substitute the granule column and drop the operand, while the reused function still declared a `Nullable` return type, raising `LOGICAL_ERROR`. Require the types to match and fall back to `UNKNOWN_FIELD` otherwise. Closes: #113234 Related: #113233 Co-Authored-By: Claude Opus 5 <[email protected]>
|
Workflow [PR], commit [4e72aa8] Summary: ✅
AI ReviewSummaryThis PR teaches Final Verdict✅ No blocking issues found in the current diff. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 28/31 (90.32%) · Uncovered code |
Pin `use_skip_indexes = 1` and check `EXPLAIN indexes = 1` so the test proves it exercised the skip index path instead of inferring it from the row counts. Co-Authored-By: Claude Opus 5 <[email protected]>
A scalar subquery is typed `Nullable` because it may return no rows, so parameterising the indexed expression from a lookup table hits the same collision with no `Nullable` in the schema or the query text. Also shorten the comment in `atomFromDAG`. Co-Authored-By: Claude Opus 5 <[email protected]>
A bare `(SELECT 19)` is typed `Nullable` for the same reason and reaches the same collision, so the table and its two setup statements were dead weight. Co-Authored-By: Claude Opus 5 <[email protected]>
Re-resolve the atom's function against the granule-side argument types and bind the input to the granule column's own type, instead of reusing the query-side `IFunctionBase` and its stale return type. Functions that cannot be re-resolved still fall back to `UNKNOWN_FIELD`, as does an existing INPUT, which cannot be re-typed. Co-Authored-By: Claude Opus 5 <[email protected]>
Without `secondary_indices_enable_bulk_filtering` the condition runs through `mayBeTrueOnGranule`, which evaluates the hyperrectangle check before the same actions. That path fails on its own without the fix. Co-Authored-By: Claude Opus 5 <[email protected]>
Wrapping the granule input in `toNullable` keeps the atom's already-resolved `IFunctionBase` valid, so `atomFromDAG` no longer re-resolves the function from `FunctionFactory` and no longer catches the exception that can raise. Granule counts are unchanged: the `Nullable` variants prune 5/50, same as the plain query. Co-Authored-By: Claude Opus 5 <[email protected]>
Comment-only: the generated code is unchanged. Co-Authored-By: Claude Opus 5 <[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. Compile time of recompiled translation units7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline. |
The `Nullable` shapes asserted only `count()`, so an atom degrading to `UNKNOWN_FIELD` would keep the test green while the index stopped pruning. Assert `Granules: 5/50` for the plain and `Nullable` shapes in both bulk filtering regimes, and pin the settings that move pruning out of index analysis. Co-Authored-By: Claude Opus 5 <[email protected]>
It was the one `Nullable` shape asserting only `count()`. `nullIf(19, 0)` folds to a constant before index analysis, so it renders as `modulo(t, 19)`, matches the key column and prunes 5/50 like the plain expression. Co-Authored-By: Claude Opus 5 <[email protected]>
…able-operand-type`
ReproducerCREATE TABLE t_113234 (t UInt32, INDEX t_set t % 19 TYPE set(4) GRANULARITY 1)
ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 2;
INSERT INTO t_113234 SELECT number FROM numbers(100);
SELECT count() FROM t_113234 WHERE t % toNullable(19) = 16;
-- expected 5; on 25.3..26.7:
-- Code: 49. DB::Exception: Unexpected return type from equals. Expected Nullable(UInt8). Got UInt8.
-- ... while executing 'FUNCTION equals(modulo(t, 19) :: 0, 16 :: 1) -> equals(modulo(t, 19), 16) Nullable(UInt8)' (LOGICAL_ERROR)
-- Same failure for t % nullIf(19, 0) = 16 and t % (SELECT toNullable(19)) = 16.Results:
Backport the fix to CC component owner: @shankar-iyer @rschu1ze Analysis metadata
Introducing change: not identified
|
|
Backporting this PR to fix stress test in backport branches |
Backport #113244 to 26.6: Fix set index on an expression with a Nullable operand
Backport #113244 to 26.3: Fix set index on an expression with a Nullable operand
Backport #113244 to 26.7: Fix set index on an expression with a Nullable operand
…th a Nullable operand
…th a Nullable operand
…th a Nullable operand
STOR: set index on an expression that the query repeats with a Nullable operand (e.g. a scalar subquery) -> exc LOGICAL_ERROR. Effect in the production build a394e92: wrong/failing (the upstream test fails on it and passes on official 26.8.8.8; evidence/fix_ci_20260925/upstream_repro/residual/113244.PROD263.txt). Upstream patch unchanged, with its test(s): 04700_set_index_nullable_operand_in_expression. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A
SELECTfiltered on an expression that matches asetskip index raisesLOGICAL_ERROR:SELECT 1000is Nullable(UInt16), while the underlying column type of the index is plain UInt32, so when MergeTreeIndexConditionSet rewrites the predicate it loses the Nullable type and just saves that it is a UInt. However the equals function still expects a Nullable, and this mismatch throws an error inExpressionActions.cpp:executeAction.============================================
No
Nullableappears anywhere in the schema or the query. A scalar subquery is typedNullablebecause it may return no rows, and that is enough. In practice this shows up when the divisor or bucket size comes from a lookup or settings table instead of a literal; the typing is identical. Explicit wrappers reach the same failure:nullIf(1000, 0),toNullable(1000).MergeTreeIndexConditionSetmatched a query subexpression to an index key column by name only. Names are computed from constant-folded arguments and never include types, sointDiv(id, (SELECT 1000))renders exactly like the index expressionintDiv(id, 1000)while carryingNullable(UInt32). The whole subtree was then replaced by the granule column while the enclosingequalswas re-added with its already-resolvedIFunctionBase, keeping the return type it was resolved with.ExpressionActions::executebinds inputs by name without checking types, so the granule supplied a non-Nullablecolumn and the declared return type no longer matched what execution produced.atomFromDAGnow binds the key column input to the type the granule block holds and restores the query-side type on top of it withtoNullable, so the enclosing atom keeps theIFunctionBaseit was resolved with. The atom function is not re-resolved fromFunctionFactory, so there is no resolution failure to catch. Achassertpins the postcondition that the node handed back carries the type the query side expects.toNullablechanges the type and not the data: the granule column's null map is all zeros, soequalsproduces no NULL, theifNullthattraverseDAGadds for aNullableatom substitutes nothing, and the mask is bit-for-bit what the non-Nullableatom produces. Pruning is therefore unchanged rather than merely correct —EXPLAIN indexes = 1reports the same 5/50 granules fort % nullIf(19, 0) = 16,t % toNullable(19) = 16,t % (SELECT 19) = 16as for the plaint % 19 = 16.Two cases fall back to
UNKNOWN_FIELD, which leaves the granule unpruned and sends the query through the regular filter path. A key column that is already anINPUTof the filter DAG, which cannot be re-typed, because a second input under the same name would be left unbound whereExpressionActions::executemaps each name to one block column. And a type difference that is not aNullablethe query side added:toNullableover the granule type has to reproduce the query-side type exactly, since dropping aNullablethe granule holds would substitute values for its NULLs.The regression test covers both granule filtering paths, since
secondary_indices_enable_bulk_filteringselects betweengetPossibleGranulesandmayBeTrueOnGranule, and each reaches the same actions independently. An index whose expression is itselfNullableis unaffected in either direction, because the types agree and nothing is re-typed; such an index prunes conservatively both before and after this change, because the granule genuinely stores NULLs and each maps toUNKNOWN_FIELD.Reproduces on the official release
26.8.1.727. In builds with assertions enabled it aborts the server, which is how it surfaced in CI — 14 occurrences in 90 days across unrelated pull requests and every build configuration, throughtests/queries/0_stateless/01786_explain_merge_tree.shonce the AST fuzzer wraps the modulus in aNullableexpression.Closes: #113233
Closes: #113234
Related: #113233
Related: #89802
Related: #111830
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
LOGICAL_ERRORwhen reading a table with asetskip index defined on an expression and the query repeats that expression with aNullableoperand, for exampleWHERE intDiv(id, (SELECT 1000)) = 3overINDEX b_set intDiv(id, 1000) TYPE set(100). A scalar subquery is enough to trigger it, since it is typedNullable.Workflow [PR]
Sync PR [sync-upstream/pr/113244]
Version info
26.8.1.1673(included in26.8and later)26.7.7.50,26.6.5.75,26.3.33.13