Skip to content

DISTINCT / GROUP BY / uniqExact count NaN payloads differently for LowCardinality(Float64) than for Float64, because the dictionary canonicalizes them on insert #119503

Open

Description

@groeneai

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 LowCardinality does today) is consistent with how compareAt and ORDER BY already treat them;
  • keeping payload identity (what Float64 does today) is what SELECT k returns 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

Activity

  1. added
    comp-datatype-wrapper
    on Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-datatype-wrapper

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions