Skip to content

Commit c4b3d52

Browse files
committed
Fix crash in comparison constant folding with Variant NULL and LowCardinality
When use_variant_default_implementation_for_comparisons is disabled, comparing a LowCardinality column with a Variant NULL constant crashes in getConstantResultForNonConstArguments. The function detects a constant NULL argument and calls result_type->createColumnConst(1, Null()), but the result type is LowCardinality(UInt8) (non-nullable), causing ColumnUnique::uniqueInsert to fail with "ColumnUnique can't contain null values". The Variant type can hold NULL without being Nullable, so getNullPresense does not detect it as nullable, and the result type is not wrapped in Nullable. When the Variant adaptor is bypassed (via the setting), constant folding attempts to create a NULL constant of a non-nullable type. Fix: check isNullableOrLowCardinalityNullable(result_type) before creating the NULL constant. If the result type cannot hold NULL, skip constant folding and let the runtime execution path handle it.
1 parent aef8cc3 commit c4b3d52

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎src/Functions/FunctionsComparison.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,14 @@ class FunctionComparison : public IFunction
15241524
continue;
15251525

15261526
if (column->isNullAt(0))
1527+
{
1528+
/// If the result type cannot hold NULL values (e.g. LowCardinality(UInt8) when
1529+
/// comparing with a Variant column that contains NULL but is not Nullable itself),
1530+
/// don't constant-fold — let the runtime execution path handle NULL correctly.
1531+
if (!isNullableOrLowCardinalityNullable(result_type))
1532+
return nullptr;
15271533
return result_type->createColumnConst(1, Null());
1534+
}
15281535
}
15291536

15301537
return nullptr;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\N
2+
\N
3+
\N
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Tags: no-random-settings
2+
3+
-- Regression test for crash in getConstantResultForNonConstArguments when comparing
4+
-- a LowCardinality column with a Variant NULL constant.
5+
-- When use_variant_default_implementation_for_comparisons is disabled, the Variant
6+
-- adaptor is bypassed and getConstantResultForNonConstArguments tries to insert NULL
7+
-- into a non-nullable LowCardinality(UInt8) result column, causing:
8+
-- "Logical error: ColumnUnique can't contain null values"
9+
-- The fix skips constant folding when the result type cannot hold NULL.
10+
11+
SET allow_suspicious_variant_types = 1;
12+
SET use_variant_default_implementation_for_comparisons = 0;
13+
14+
DROP TABLE IF EXISTS t_lc_variant_null;
15+
CREATE TABLE t_lc_variant_null (x LowCardinality(String)) ENGINE = MergeTree ORDER BY x;
16+
INSERT INTO t_lc_variant_null VALUES ('a'), ('b'), ('c');
17+
18+
-- These should not crash. Without the Variant adaptor, the comparison types are
19+
-- incompatible (String vs Variant), so a proper error is expected — not a crash.
20+
SELECT greaterOrEquals(CAST(NULL, 'Variant(Date, Date32)'), x) FROM t_lc_variant_null; -- { serverError NO_COMMON_TYPE }
21+
SELECT greater(CAST(NULL, 'Variant(UInt32, Int64)'), x) FROM t_lc_variant_null; -- { serverError NO_COMMON_TYPE }
22+
SELECT equals(x, CAST(NULL, 'Variant(UInt32, Int64)')) FROM t_lc_variant_null; -- { serverError NO_COMMON_TYPE }
23+
24+
-- Verify normal path (Variant adaptor enabled) still works correctly — returns NULL
25+
SET use_variant_default_implementation_for_comparisons = 1;
26+
SELECT greaterOrEquals(CAST(NULL, 'Variant(Date, Date32)'), x) FROM t_lc_variant_null;
27+
28+
DROP TABLE t_lc_variant_null;

0 commit comments

Comments
 (0)