Skip to content

Commit a9b883c

Browse files
committed
[UT] Add SQL test for sync-MV CASE WHEN/IF rewrite with widening sum
Covers the planner fix in MaterializedViewRewriter where a sync MV's column-ref substitution was leaving the wrapping IF / CASE WHEN with the original (narrower) declared result type, silently truncating outer sum() values that exceeded that range. Per-(k1, k2) sum(k3) is set up to overflow SMALLINT (sum reaches 100000 for one group), so the test would observe a truncated value in sum2 / sum_if before the fix.
1 parent 7179c87 commit a9b883c

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-- name: test_mv_rewrite_case_when_sum_widening_type
2+
create database db_${uuid0};
3+
-- result:
4+
-- !result
5+
use db_${uuid0};
6+
-- result:
7+
-- !result
8+
CREATE TABLE t1 (
9+
k1 DATE NULL,
10+
k2 INT NULL,
11+
k3 SMALLINT NULL
12+
) DUPLICATE KEY(k1)
13+
DISTRIBUTED BY HASH(k1) BUCKETS 1
14+
PROPERTIES("replication_num" = "1");
15+
-- result:
16+
-- !result
17+
CREATE MATERIALIZED VIEW test_mv1 AS
18+
SELECT k1, k2, sum(k3) AS sum1 FROM t1 GROUP BY k1, k2;
19+
-- result:
20+
-- !result
21+
function: wait_materialized_view_finish()
22+
-- result:
23+
None
24+
-- !result
25+
INSERT INTO t1 VALUES
26+
('2024-01-01', 0, 30000),
27+
('2024-01-01', 0, 30000),
28+
('2024-01-01', 0, 30000),
29+
('2024-01-01', 0, 10000),
30+
('2024-01-01', 1, 5);
31+
-- result:
32+
-- !result
33+
SELECT k1,
34+
sum(k3) AS sum1,
35+
sum(case when k2=0 then k3 else 0 end) AS sum2
36+
FROM t1 GROUP BY k1;
37+
-- result:
38+
2024-01-01 100005 100000
39+
-- !result
40+
SELECT k1,
41+
sum(if(k2=0, k3, 0)) AS sum_if
42+
FROM t1 GROUP BY k1;
43+
-- result:
44+
2024-01-01 100000
45+
-- !result
46+
DROP DATABASE db_${uuid0};
47+
-- result:
48+
-- !result
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- name: test_mv_rewrite_case_when_sum_widening_type
2+
-- Regression test for sync-MV rewrite of `sum(case when ... then col else 0 end)`
3+
-- when the MV stores `sum(col)` and the aggregator widens the column's type.
4+
-- Before the fix the rewritten plan kept the inner CASE WHEN / IF declared
5+
-- as the original (narrower) column type while the THEN branch was already
6+
-- the widened MV column, so any group whose sum exceeded the original type's
7+
-- range would be silently truncated at the outer SUM.
8+
create database db_${uuid0};
9+
use db_${uuid0};
10+
11+
CREATE TABLE t1 (
12+
k1 DATE NULL,
13+
k2 INT NULL,
14+
k3 SMALLINT NULL
15+
) DUPLICATE KEY(k1)
16+
DISTRIBUTED BY HASH(k1) BUCKETS 1
17+
PROPERTIES("replication_num" = "1");
18+
19+
CREATE MATERIALIZED VIEW test_mv1 AS
20+
SELECT k1, k2, sum(k3) AS sum1 FROM t1 GROUP BY k1, k2;
21+
function: wait_materialized_view_finish()
22+
23+
-- Per-(k1, k2) sum(k3) deliberately overflows SMALLINT (max 32767):
24+
-- for ('2024-01-01', k2=0) the sum is 30000+30000+30000+10000 = 100000.
25+
INSERT INTO t1 VALUES
26+
('2024-01-01', 0, 30000),
27+
('2024-01-01', 0, 30000),
28+
('2024-01-01', 0, 30000),
29+
('2024-01-01', 0, 10000),
30+
('2024-01-01', 1, 5);
31+
32+
-- The query rewrites `case when k2=0 then k3 else 0` against test_mv1's
33+
-- mv_sum_k3 column. After the fix, the rewritten IF/CASE WHEN result type
34+
-- is BIGINT (matching mv_sum_k3) so the outer sum is not truncated.
35+
SELECT k1,
36+
sum(k3) AS sum1,
37+
sum(case when k2=0 then k3 else 0 end) AS sum2
38+
FROM t1 GROUP BY k1;
39+
40+
-- Same shape, expressed with IF instead of CASE WHEN.
41+
SELECT k1,
42+
sum(if(k2=0, k3, 0)) AS sum_if
43+
FROM t1 GROUP BY k1;
44+
45+
DROP DATABASE db_${uuid0};

0 commit comments

Comments
 (0)