Repository navigation
Fix Logical error 'Arguments of plus have incorrect data types' for Array(LowCardinality(...)) sort keys - #103701
Conversation
… `Array(LowCardinality(...))` sort keys
When a `MergeTree` table had `Array(LowCardinality(...))` in its sort key
and a WHERE clause invoked `plus` / `minus` between that column and an
`Array` constant of a different element type, the server raised
`LOGICAL_ERROR` from `FunctionBinaryArithmetic::executeImpl2`:
Logical error: 'Arguments of 'plus' have incorrect data types:
'...' of type 'Int16', '...' of type 'LowCardinality(Float64)''.
`KeyCondition::getMonotonicityForRange` builds synthetic `Array(...)`
constant columns and invokes `Base::executeImpl` directly, bypassing the
framework's `LowCardinality` default implementation. The pre-existing
`removeLowCardinality` call only strips a top-level `LowCardinality`
wrapper; an inner one inside `Array` survives, and the recursive
`executeArraysImpl` -> `executeImpl` chain eventually reaches the inner
numeric dispatch (`castBothTypes` / `castTypeToEither`) with
`LowCardinality(...)` element types, which the dispatch does not
recognize.
Fix: use `recursiveRemoveLowCardinality` (which traverses `Array`,
`Tuple`, `Map`, `Nullable`, etc.) when normalising the synthetic types
in `getMonotonicityForRange`, so the elements are stripped before
`Base::executeImpl` is invoked.
Closes ClickHouse#103691.
Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pre-PR validation gateSession id: a) Deterministic repro? Yes. Pre-fix, the exact 4-statement script from #103691 ( b) Root cause explained? Yes. c) Fix matches root cause? Yes. The fix swaps d) Test intent preserved? / New tests added? New regression test added: e) Both directions demonstrated? Yes.
f) Fix is general, not a narrow patch?
|
|
cc @rschu1ze @Avogar @vitlibar — could you review this? It fixes a logical error thrown by |
|
Workflow [PR], commit [085234c] Summary: ✅ AI ReviewSummaryThis PR fixes a real correctness issue in monotonicity evaluation for arithmetic on sort keys containing ClickHouse Rules
Final VerdictStatus: ✅ Approve |
| @@ -3228,7 +3228,9 @@ class FunctionBinaryArithmeticWithConstants : public FunctionBinaryArithmetic<Op | |||
|
|
|||
| /// This is a bit dangerous to call Base::executeImpl cause it ignores `use Default Implementation For XXX` flags. | |||
| /// It was possible to check monotonicity for nullable right type which result to exception. | |||
There was a problem hiding this comment.
Small typo in the comment: "which result to exception" should be "which results in an exception".
There was a problem hiding this comment.
Fixed in 085234c. Also backticked Base::executeImpl for consistency with the surrounding code comments.
Address review feedback from clickhouse-gh[bot]: - 'which result to exception' -> 'which results in an exception' - Backtick `Base::executeImpl` for consistency with surrounding comments Comment-only change, no behavior change.
There was a problem hiding this comment.
Pull request overview
This PR fixes a logical error thrown during key-condition monotonicity checks when plus/minus is used with Array(LowCardinality(...)) in a MergeTree sort key and an Array(...) constant of a different element type. The fix ensures nested LowCardinality wrappers are stripped before calling the inner arithmetic execution path that doesn’t recognize LowCardinality types.
Changes:
- Use
recursiveRemoveLowCardinality(instead ofremoveLowCardinality) when normalizing argument/return types inFunctionBinaryArithmeticWithConstants::getMonotonicityForRangeforplus/minus. - Add a stateless regression test covering both
const +/- variableandvariable +/- constmonotonicity branches forArray(LowCardinality(Float64)).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Functions/FunctionBinaryArithmetic.h |
Recursively strips nested LowCardinality wrappers before calling Base::executeImpl in plus/minus monotonicity checks. |
tests/queries/0_stateless/04141_array_lowcard_arithmetic_monotonicity.sql |
Adds regression test reproducing the Array(LowCardinality(...)) + array-constant mismatch scenario for plus and minus. |
tests/queries/0_stateless/04141_array_lowcard_arithmetic_monotonicity.reference |
Adds expected output for the new stateless regression test. |
LLVM Coverage Report
Changed lines: 100.00% (15/15) · Uncovered code |
|
Hi — this PR may need backporting to Affected code: Why: If this should be backported, consider adding |
When a
MergeTreetable hasArray(LowCardinality(...))in its sort key and a WHERE clause invokesplus/minusbetween that column and anArrayconstant of a different element type, the server raises a logical error fromFunctionBinaryArithmetic::executeImpl2:Reproduction (provided by @vitlibar in #103691):
Root cause
KeyCondition::getMonotonicityForRangefor the sort key builds syntheticArray(...)constant columns and callsBase::executeImpldirectly. This bypasses the framework'sLowCardinalitydefault implementation (which would otherwise callconvertLowCardinalityColumnsToFull->recursiveRemoveLowCardinality).The pre-existing
removeLowCardinalitycalls strip only the top-levelLowCardinalitywrapper; an inner one nested insideArraysurvives. WhenexecuteImpl2entersexecuteArraysImpl, it stripsArrayand recurses on the inner element types. The recursiveexecuteImplthen dispatches viacastBothTypes/castTypeToEither, which does not recognizeDataTypeLowCardinality. Thevalidflag staysfalseand theLOGICAL_ERRORis thrown.Fix
Use
recursiveRemoveLowCardinality(which traversesArray,Tuple,Map,Nullable, ...) when normalising the synthetic types ingetMonotonicityForRange, soArray(LowCardinality(Float64))becomesArray(Float64)beforeBase::executeImplis invoked. The numeric dispatch then succeeds normally.Both branches that call
Base::executeImplare covered:const + variable(lines 3217-3253) andvariable + const(lines 3255-3277). The fix applies toplusandminus. Themultiply/dividebranches in the same method only useret_typefor aWhichDataType::isUInt8/...check (no recursiveexecuteImplcall), so they are not affected by this bug and are unchanged.Verification
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix logical error
Arguments of 'plus' have incorrect data typesthrown byFunctionBinaryArithmetic::executeImpl2when aMergeTreetable hadArray(LowCardinality(...))in its sort key and aWHEREclause usedplus/minusbetween that column and anArrayconstant of a different element type.KeyCondition::getMonotonicityForRangenow stripsLowCardinalityrecursively before invoking the inner numeric dispatch.Documentation entry for user-facing changes
Closes #103691.
Version info
26.5.1.172