ClickHouse / ClickHouse

ClickHouse® is a real-time analytics DBMS
https://clickhouse.com
Apache License 2.0
37.22k stars 6.86k forks source link

Support Nullable types for arrayDifference function #35965

Open boginski opened 2 years ago

boginski commented 2 years ago

arrayDifference works with nullable and doesn't work with nullable. ClickHouse version: 21.7.5.29

--drop table default.test;

CREATE TABLE test ( first Nullable(Int32), second Int32 ) ENGINE = MergeTree PRIMARY KEY(second) order by second;

INSERT INTO default.test values (1, 2); INSERT INTO default.test values (3, 4);

-- work here (nullable) select groupArray(first) as array_ts, arrayDifference(array_ts) as diff from default.test

-- not work here (also nullable) select groupArray(tuple(first)) as array_tuple_ts, arrayMap(x -> x.1, array_tuple_ts) as array_ts, arrayDifference(array_ts) as diff from default.test

STDERR: Code: 43, e.displayText() = DB::Exception: arrayDifference cannot process values of type Nullable(Int32): While processing groupArray(tuple(first)) AS array_tuple_ts, arrayMap(x -> (x.1), array_tuple_ts) AS array_ts, arrayDifference(array_ts) AS diff (version 21.7.5.29 (official build))

It seems it should either work in both cases or not work in both cases.

-- fix, but not explicitly select groupArray(tuple(first)) as array_tuple_ts, arrayMap(x -> CAST(x.1 as Int32), array_tuple_ts) as array_ts, arrayDifference(array_ts) as diff from default.test

novikd commented 2 years ago

groupArray removes Nullability of the argument type. That's why your first query works. At the same moment, arrayDifference is a higher-order function and can receive a transformation lambda as the first argument. The following query should work too.

SELECT
    groupArray(tuple(first)) AS array_tuple_ts,
    arrayDifference(x -> CAST(x.1 AS Int32), array_tuple_ts) AS diff
FROM default.test
boginski commented 2 years ago

OK, thanks!