Repository navigation
Description
Company or project name
ClickHouse CI monitoring (@groeneai).
Describe the unexpected behaviour
Aggregation and DISTINCT key a floating-point value on its bit pattern, but a LowCardinality dictionary canonicalizes NaN payloads when the value is inserted (ColumnUnique::uniqueInsert). The same data therefore reports a different number of distinct values depending only on whether the column is wrapped in LowCardinality.
Signed zero goes the other way: neither type normalizes it, so -0.0 and +0.0 are two group keys although -0.0 = 0.0 is true.
Which ClickHouse versions are affected?
master 26.9.1.1 (debug build of c108e273b6087). The mechanism is long-standing (ColumnUnique::uniqueInsert canonicalizes NaN, nothing canonicalizes signed zero), so earlier versions are expected to behave the same.
How to reproduce
clickhouse-local, allow_suspicious_low_cardinality_types = 1 (needed for a float LowCardinality column), no other non-default settings. Two distinct NaN payloads:
SET allow_suspicious_low_cardinality_types = 1;
CREATE TABLE t (k LowCardinality(Float64)) ENGINE = Memory;
CREATE TABLE f (k Float64) ENGINE = Memory;
INSERT INTO t VALUES (reinterpretAsFloat64(toUInt64(9221120237041090561))), (reinterpretAsFloat64(toUInt64(9221120237041090562)));
INSERT INTO f VALUES (reinterpretAsFloat64(toUInt64(9221120237041090561))), (reinterpretAsFloat64(toUInt64(9221120237041090562)));
SELECT count() FROM (SELECT DISTINCT k FROM t); -- 1
SELECT count() FROM (SELECT DISTINCT k FROM f); -- 2
SELECT count() FROM (SELECT k FROM t GROUP BY k); -- 1
SELECT count() FROM (SELECT k FROM f GROUP BY k); -- 2
SELECT uniqExact(k) FROM t; -- 1
SELECT uniqExact(k) FROM f; -- 2
SELECT groupArray(hex(reinterpretAsUInt64(k))) FROM t;
-- ['7FF8000000000000','7FF8000000000000'] the dictionary canonicalized both payloads
SELECT groupArray(hex(reinterpretAsUInt64(k))) FROM f;
-- ['7FF8000000000001','7FF8000000000002']uniq and uniqCombined agree with uniqExact on both types (1 vs 2).
Signed zero, the same fixture with (-0.0), (0.0): DISTINCT, GROUP BY and uniqExact return 2 for both types, and SELECT k FROM ... GROUP BY k returns both 0 and -0, while SELECT -0.0 = 0.0 is 1.
Expected behavior
The two types should agree on how many distinct values the same data holds. Which side should move is a design question:
- treating all NaN payloads as one value (what
LowCardinalitydoes today) is consistent with howcompareAtandORDER BYalready treat them; - keeping payload identity (what
Float64does today) is whatSELECT kreturns for a plain column.
For the signed-zero half, canonicalizing the dictionary in ColumnUnique is not the answer: it would drop the sign of zero from SELECT k, which a plain Float64 column preserves.
Error message and/or stacktrace
None. Wrong counts, no error.
Related issues and pull requests
- Not fixed by Fix wrong row order for a multi-column ORDER BY over a LowCardinality float key #116870, which makes the two sorting paths of
ColumnLowCardinalitydecide row equality by value instead of by dictionary index. Aggregation andDISTINCTreach a different equality mechanism, untouched by that PR. - Correlated subquery equivalence substitution loses rows for Array/Tuple/Map with float elements: the stitch join compares bitwise, so -0.0 stops matching 0.0 #117334 is the same family for plain floats through a different mechanism.
WHERE k IN (0.0)matching one of two signed-zero rows whileWHERE k = 0.0matches both is reachable for plainFloat64too. - Requested in review of Fix wrong row order for a multi-column ORDER BY over a LowCardinality float key #116870 by @ Avogar: Fix wrong row order for a multi-column ORDER BY over a LowCardinality float key #116870 (review)
Activity