Describe the bug
I have a simple table with two columns date (used for ordering and partitioning) and hits. I have created a materialized view to compute total hits per day using an AggregatingMergeTree, and a plain view to easily query / display the data from the AggregatingMergeTree (see below). The rows in the view look correct.
However, sum(hits) on the view is lower than the expected result, as if the last row was ignored. Its value does not appear in groupArray(hits), whereas an unexpected 0 is present.
How to reproduce
Tested on 19.3.4 (Debian) and 18.16.1 (Docker).
TCP (native client).
TCP server running on port 10000.
CREATE TABLE statements for all tables involvedCREATE TABLE test_base
(
date Date,
hits UInt32
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(date)
ORDER BY date;
CREATE MATERIALIZED VIEW test_aggr
(
date Date,
hits_state AggregateFunction(sum, UInt32)
)
ENGINE = AggregatingMergeTree()
PARTITION BY toYYYYMM(date)
ORDER BY date AS
SELECT
date,
sumState(hits) AS hits_state
FROM test_base
GROUP BY date;
CREATE VIEW test_view
(
date Date,
hits UInt32
) AS
SELECT
date,
sumMerge(hits_state) AS hits
FROM test_aggr
GROUP BY date;
INSERT INTO test_base FORMAT JSONEachRow
{"date":"2018-09-26","hits":1}
{"date":"2018-09-26","hits":2}
{"date":"2018-09-27","hits":4}
{"date":"2018-09-28","hits":8};
The query
SELECT *
FROM test_view;
returns as expected
ββββββββdateββ¬βhitsββ
β 2018-09-26 β 3 β
β 2018-09-27 β 4 β
β 2018-09-28 β 8 β
ββββββββββββββ΄βββββββ
SELECT
count(),
sum(hits),
groupArray(hits)
FROM test_view;
Returns:
ββcount()ββ¬βsum(hits)ββ¬βgroupArray(hits)ββ
β 3 β 7 β [3,0,4] β
βββββββββββ΄ββββββββββββ΄βββββββββββββββββββ
Expected behavior
I would have expected:
ββcount()ββ¬βsum(hits)ββ¬βgroupArray(hits)ββ
β 3 β 15 β [3,4,8] β
βββββββββββ΄ββββββββββββ΄βββββββββββββββββββ
Why is the last value (8) missing? Where does the 0 come from in groupArray(hits)?
Seems UInt64 treated as UInt32
CREATE VIEW test_view AS
SELECT date, sumMerge(hits_state) AS hits FROM test_aggr GROUP BY date;
DESCRIBE TABLE test_view
ββnameββ¬βtypeββββ¬βdefault_typeββ¬βdefault_expressionββ¬βcomment_expressionββ¬βcodec_expressionββ
β date β Date β β β β β
β hits β UInt64 β β β β β
ββββββββ΄βββββββββ΄βββββββββββββββ΄βββββββββββββββββββββ΄βββββββββββββββββββββ΄βββββββββββββββββββ
SELECT count(), sum(hits), groupArray(hits) FROM test_view;
ββcount()ββ¬βsum(hits)ββ¬βgroupArray(hits)ββ
β 3 β 15 β [3,4,8] β
βββββββββββ΄ββββββββββββ΄βββββββββββββββββββ
CREATE VIEW test_view
( date Date, hits UInt32
) AS
SELECT
date,
cast(sumMerge(hits_state) as UInt32) AS hits
FROM test_aggr
GROUP BY date;
SELECT count(), sum(hits), groupArray(hits) FROM test_view;
ββcount()ββ¬βsum(hits)ββ¬βgroupArray(hits)ββ
β 3 β 15 β [3,4,8] β
βββββββββββ΄ββββββββββββ΄βββββββββββββββββββ
ClickHouse server version 19.3.5
create view testv(a UInt32) as select number a from numbers(10);
select groupArray(a) from testv;
ββgroupArray(a)ββββββββββ
β [0,0,1,0,2,0,3,0,4,0] β
βββββββββββββββββββββββββ
Original issue was fixed.
And now we have error message instead:
milovidov-Pro-P30 :) create view testv(a UInt32) as select number a from numbers(10);
CREATE VIEW testv
(
`a` UInt32
) AS
SELECT number AS a
FROM numbers(10)
Ok.
0 rows in set. Elapsed: 0.016 sec.
milovidov-Pro-P30 :) select groupArray(a) from testv;
SELECT groupArray(a)
FROM testv
β Progress: 0.00 rows, 0.00 B (0.00 rows/s., 0.00 B/s.) Received exception from server (version 20.2.1):
Code: 368. DB::Exception: Received from localhost:9000. DB::Exception: Bad cast from type DB::ColumnVector<unsigned long> to DB::ColumnVector<unsigned int>.
We need to add type conversions for Views.
100% fixed.