Repository navigation
Fix wrong results from WHERE p AND LowCardinality(Nullable(int)) constant on MergeTree - #104767
Conversation
…tant on MergeTree When `splitFilterNodeForAllowedInputs` reduced `(non_virtual AND const)` to a single surviving atom whose `result_type` did not match the original `AND` node's type, it synthesized a `notEquals(x, default)` guard so the filter still produced a boolean. It used `removeNullable(result_type)` to choose the default. For a `LowCardinality(Nullable(X))` constant the outer type is `LowCardinality` (not `Nullable`), so `removeNullable` was a no-op; `getDefault` on `LowCardinality(Nullable(X))` falls through to the dictionary type and returns `Null()`. The synthesized guard became `notEquals(x, NULL)` -> `NULL` -> false, and partition pruning then discarded every part before any data was read. Replace `removeNullable` with `removeLowCardinalityAndNullable`, which is `removeNullable(removeLowCardinality(type))`. This strips both wrappers so the default of the actual scalar type is used (`0` for integers), restoring the original `(x > 0) AND truthy_const` semantics. The `Nullable(Nothing)` special case still falls through to the original type's default (`NULL`) so a literal `NULL` continues to prune all rows as before. Closes ClickHouse#104393. Reproducer (returned 0 instead of 499 on master): SET allow_suspicious_low_cardinality_types = 1; CREATE TABLE t (a UInt32) ENGINE = MergeTree ORDER BY a; INSERT INTO t SELECT number FROM numbers(500); SELECT count() FROM t WHERE (a > 0) AND CAST(1 AS LowCardinality(Nullable(UInt8)));
Pre-PR validation gate (TASK.md Phase 4 step 9)
Session: |
|
cc @CurtizJ @KochetovNicolai — could you review this? It fixes a silent wrong-results bug on |
|
Workflow [PR], commit [9835180] Summary: ❌
AI ReviewSummaryThis PR fixes a wrong-results path in Final Verdict
|
LLVM Coverage Report
Changed lines: 100.00% (5/5) · Uncovered code |
|
@groeneai, provide fixes for these two errors in separate PRs: Stateless tests (amd_msan, WasmEdge, parallel, 2/2) FAIL As soon as the fixes are submitted or linked, update the branch here. |
|
Let's move these failures in separate issues, merge this one and I will take care of the issues. |
2cd409d
|
Hi — could this be a backport candidate for Affected code: Why this might apply: The bug was introduced by the #103049 fix (commit 7c78cbd, 2026-04-18) which was backported to 26.4. Branch 26.4 shows Other supported branches ( 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 |
|
I have a similar issue on v26.3.x which seems to be fixed in 26.5: DROP TABLE IF EXISTS t_repro;
CREATE TABLE t_repro (
s String,
n LowCardinality(Nullable(String)),
INDEX idx_n n TYPE set(100) GRANULARITY 2,
INDEX idx_s s TYPE ngrambf_v1(4, 1024, 2, 0) GRANULARITY 3
) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t_repro SELECT 'has-republisher', NULL FROM numbers(100000);
INSERT INTO t_repro SELECT 'no-match', NULL FROM numbers(100);
-- Correct: 100,000 rows match the tag
SELECT count() FROM t_repro WHERE s LIKE '%republisher%';
-- BUG: OR with the nullable column should ALSO return 100,000, but returns 0 on 26.3.x
SELECT count() FROM t_repro WHERE (n = 'republisher' OR s LIKE '%republisher%');
-- WORKAROUND: assumeNotNull() returns the correct 100,000 on all versions
SELECT count() FROM t_repro WHERE (assumeNotNull(n) = 'republisher' OR s LIKE '%republisher%');@Avogar any ideas if a fix could be related to this PR? |
|
@cnkk It's seems like an issue in combining two skip indexes. Open an issue. It could be accidentally fixed by this PR. Anyway your example should be added to test/ci. |
On
MergeTreetables,WHERE p AND <LowCardinality(Nullable(int)) constant>silently returned zero rows. Found by @qoega via SQLancer in #104393.splitFilterNodeForAllowedInputs(src/Storages/VirtualColumnUtils.cpp) splits the predicate at part-pruning time. When one branch of anANDis not evaluable on virtual columns it drops that branch and keeps the surviving constant child. If the child's type differs from the originalANDresult, the function synthesizes anotEquals(x, default)guard so the result is still boolean.It computed the default with
removeNullable(result_type). For aLowCardinality(Nullable(X))constant the outer type isLowCardinality, notNullable, soremoveNullablewas a no-op andgetDefault()then went through the LC dictionary and returnedNull. The synthesized guard becamenotEquals(x, NULL), which evaluates toNULLand is treated as false in filter context — so every part was pruned before any data was read.Replaced with
removeLowCardinalityAndNullable, which strips both wrappers. The default of the underlying scalar type (0for integers) is used instead, restoring the intended(x > 0) AND truthy_constsemantics. The pre-existingNullable(Nothing)special case (for a bareNULLliteral) still falls through to the original type's default, soWHERE x AND NULLcontinues to prune every row.Verified the new test reproducer FAILS on
masterand PASSES on the fix branch (50/50 runs with full setting randomization). Related regression tests (partition_pruning,virtual_column,04074_where_large_int_boolean_predicate,04104_103049_nullable_and_overflow_filter) still pass.Closes #104393.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results from
WHERE p AND <LowCardinality(Nullable(int)) constant>onMergeTreetables. Previously such queries returned zero rows because part pruning derived aNULLdefault from theLowCardinality(Nullable(...))type and synthesized anotEquals(x, NULL)guard, pruning every part.Documentation entry for user-facing changes
Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/
Version info
26.5.1.826