Skip to content

NULL in a Variant column wrongly fails IN set under transform_null_in #123745

Open

Description

@clickgapai

Describe what's wrong

  • v IN set and nullIn(v, set) return 0 for a NULL Variant value even though the set key is Nullable and contains NULL.
  • Trigger: transform_null_in = 1, or nullIn/notNullIn, or an ENGINE = Set table, with a Variant probe column against a Nullable set key.
  • A Nullable column with the same NULL value matches correctly in the same query, so only the Variant case is wrong.
  • Expected 1 for v IN set and 0 for v NOT IN set on the NULL row, but actual is 0 and 1 respectively.

Root cause: Set::execute (Set.cpp:557) detects the probe's own NULLs by typeid_cast-ing to ColumnNullable, which returns nullptr for a ColumnVariant since it keeps NULL in its discriminator; every such NULL is then treated as invented by the cast and marked did_not_fit (Set.cpp:563), with original_null_map built as all zeros (Set.cpp:565-568).

Analysis details (evidence, affected locations, impact)

Why we believe this is a bug: FunctionIn::executeImpl ([src/Functions/in.cpp:189](https://github.com/ClickHouse/ClickHouse/blob/3c4346064a7a/src/Functions/in.cpp#L189)) passes the Variant column to Set::execute unchanged. The probe type differs from the key type, so the new lenient branch runs ([src/Interpreters/Set.cpp:535](https://github.com/ClickHouse/ClickHouse/blob/3c4346064a7a/src/Interpreters/Set.cpp#L535)). That branch tells a source NULL from a cast-invented NULL with typeid_cast<const ColumnNullable *> (Set.cpp:557), which returns nullptr for a ColumnVariant. Every NULL is then counted as 'did not fit' (Set.cpp:563), and the key column is rebuilt with an all-zero null map (Set.cpp:565-568).

Affected locations:

Impact: Silent wrong result in one query: WHERE v IN set / nullIn(v, set) drops rows whose Variant key is NULL, and NOT IN keeps them. The same NULL in a Nullable column of the same query matches. It needs a Variant left operand and a Nullable set key that holds NULL, reached through transform_null_in = 1, nullIn/notNullIn, or an ENGINE = Set table. So far this is only in master (26.10); #117606 (where this was found; not confirmed as the cause) has no backport label.

Does it reproduce on most recent release?

Confirmed on master (5d1eb5c770b1); not checked on a release.

How to reproduce

Use a t_probe table with a Variant(String, UInt64) column containing NULL against a t_set table (ENGINE = Set) with a Nullable(UInt64) key containing NULL, run with transform_null_in = 1.

▶ Run on ClickHouse Fiddle

-- Under `transform_null_in`, a NULL `Variant` probe matches the NULL of a `Nullable` set key, the same as a `Nullable` probe.

DROP TABLE IF EXISTS t_set_05320;
DROP TABLE IF EXISTS t_probe_05320;

CREATE TABLE t_set_05320 (k Nullable(UInt64)) ENGINE = Set;
INSERT INTO t_set_05320 VALUES (1), (NULL);

CREATE TABLE t_probe_05320 (id UInt8, v Variant(String, UInt64), n Nullable(Int64)) ENGINE = Memory;
INSERT INTO t_probe_05320 VALUES (1, NULL, NULL), (2, 1, 1), (3, 'abc', -1);

SELECT id, v IN t_set_05320, n IN t_set_05320 FROM t_probe_05320 ORDER BY id SETTINGS transform_null_in = 1;
SELECT id, v IN (SELECT CAST(NULL, 'Nullable(String)') UNION ALL SELECT 'abc') FROM t_probe_05320 ORDER BY id SETTINGS transform_null_in = 1;

DROP TABLE t_set_05320;
DROP TABLE t_probe_05320;

Verified: this block reproduces on current master (5d1eb5c770b1) when pasted as is.

Expected behavior

A NULL Variant probe value should match a NULL in a Nullable set key, the same as a NULL Nullable probe value.

Expected output of the reproducer above:

1	1	1
2	1	1
3	0	0
1	1
2	0
3	1

Error message and/or stacktrace

The NULL Variant value is classified as a non-member, so IN returns 0 and NOT IN returns 1 for that row.

Actual output of the reproducer above on master (5d1eb5c770b1):

1	0	1
2	1	1
3	0	0
1	0
2	0
3	1
Suggested fix

Read the probe's own nullness from the column, not from a ColumnNullable cast. For example, when canContainNull(*column_to_cast.type) holds and the column is not a ColumnNullable, build the source mask with column_to_cast.column->isNullAt(row). Use that mask both for did_not_fit and for original_null_map. Trade-off: one virtual isNullAt per row, and only on the non-ColumnNullable path. Natural home for the regression case: next to the LowCardinality(Nullable(...)) probe section of 05179_table_backed_set_out_of_range.sql.

Additional context

Open risks:

  • The literal-list path already disagrees: a Variant NULL in IN (NULL, 1) gives 0 on 26.5 and master. The maintainer has to pick one answer for literal lists, subquery sets and table sets.

Found during automated review of PR #117606; whether that PR introduced it could not be established, so nobody is tagged. Severity P2 · Finding h_pr117606_001

Activity

  1. added
    comp-query-execution
    on Oct 3, 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

    ai_p2comp-query-execution

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions