Skip to content

Commit

Permalink
planner: import more tests about rollup expand (#55024)
Browse files Browse the repository at this point in the history
close #42631
  • Loading branch information
AilinKid authored Jul 30, 2024
1 parent 199bf90 commit 5d5de41
Show file tree
Hide file tree
Showing 2 changed files with 311 additions and 0 deletions.
191 changes: 191 additions & 0 deletions tests/integrationtest/r/executor/expand.result
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,194 @@ NULL 7785 519.0000
:Computer: 6900 1380.0000
:Phone: 10 10.0000
:TV: 600 120.0000

drop table t1,t2;
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES(100);
CREATE TABLE t2 (i int);
INSERT INTO t2 VALUES (100),(200);

SELECT i, COUNT(*) FROM t1 GROUP BY i WITH ROLLUP;
i COUNT(*)
NULL 1
100 1

SELECT t1.i, t2.i, COUNT(*) FROM t1,t2 GROUP BY t1.i,t2.i WITH ROLLUP;
i i COUNT(*)
NULL NULL 2
100 NULL 2
100 100 1
100 200 1

DROP TABLE t1,t2;
CREATE TABLE user_day(
user_id INT NOT NULL,
date DATE NOT NULL,
UNIQUE INDEX user_date (user_id, date)
);
INSERT INTO user_day VALUES
(1, '2004-06-06' ),
(1, '2004-06-07' ),
(2, '2004-06-06' );

SELECT
d.date AS day,
COUNT(d.user_id) as sample,
COUNT(next_day.user_id) AS not_cancelled
FROM user_day d
LEFT JOIN user_day next_day
ON next_day.user_id=d.user_id AND
next_day.date= DATE_ADD( d.date, interval 1 day )
GROUP BY day;
day sample not_cancelled
2004-06-06 2 1
2004-06-07 1 0

SELECT
d.date AS day,
COUNT(d.user_id) as sample,
COUNT(next_day.user_id) AS not_cancelled
FROM user_day d
LEFT JOIN user_day next_day
ON next_day.user_id=d.user_id AND
next_day.date= DATE_ADD( d.date, interval 1 day )
GROUP BY day
WITH ROLLUP;
day sample not_cancelled
NULL 3 1
2004-06-06 2 1
2004-06-07 1 0

DROP TABLE user_day;
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES
(1,4),
(2,2), (2,2),
(4,1), (4,1), (4,1), (4,1),
(2,1), (2,1);

SELECT SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)
14
4
4
6

SELECT DISTINCT SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b)
14
4
6

SELECT SUM(b), COUNT(DISTINCT b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b) COUNT(DISTINCT b)
14 3
4 1
4 1
6 2

SELECT DISTINCT SUM(b), COUNT(DISTINCT b) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b) COUNT(DISTINCT b)
14 3
4 1
6 2

SELECT SUM(b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b) COUNT(*)
14 9
4 1
4 4
6 4

SELECT DISTINCT SUM(b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b) COUNT(*)
14 9
4 1
4 4
6 4

SELECT SUM(b), COUNT(DISTINCT b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
SUM(b) COUNT(DISTINCT b) COUNT(*)
14 3 9
4 1 1
4 1 4
6 2 4

SELECT DISTINCT SUM(b), COUNT(DISTINCT b), COUNT(*) FROM t1
GROUP BY a WITH ROLLUP;
SUM(b) COUNT(DISTINCT b) COUNT(*)
14 3 9
4 1 1
4 1 4
6 2 4

SELECT a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
a SUM(b)
NULL 14
1 4
1 4
2 2
2 4
2 6
4 4
4 4

SELECT DISTINCT a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
a SUM(b)
NULL 14
1 4
2 2
2 4
2 6
4 4

SELECT b, a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
b a SUM(b)
NULL NULL 14
NULL 1 4
NULL 2 6
NULL 4 4
1 2 2
1 4 4
2 2 4
4 1 4

SELECT DISTINCT b,a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
b a SUM(b)
NULL NULL 14
NULL 1 4
NULL 2 6
NULL 4 4
1 2 2
1 4 4
2 2 4
4 1 4

ALTER TABLE t1 ADD COLUMN c INT;

SELECT a,b,SUM(c) FROM t1 GROUP BY a,b,c WITH ROLLUP;
a b SUM(c)
NULL NULL NULL
1 NULL NULL
1 4 NULL
1 4 NULL
2 NULL NULL
2 1 NULL
2 1 NULL
2 2 NULL
2 2 NULL
4 NULL NULL
4 1 NULL
4 1 NULL

SELECT distinct a,b,SUM(c) FROM t1 GROUP BY a,b,c WITH ROLLUP;
a b SUM(c)
NULL NULL NULL
1 NULL NULL
1 4 NULL
2 NULL NULL
2 1 NULL
2 2 NULL
4 NULL NULL
4 1 NULL
DROP TABLE t1;
120 changes: 120 additions & 0 deletions tests/integrationtest/t/executor/expand.test
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,123 @@ GROUP BY product, country_id, year HAVING country_id is NULL;
--sorted_result
SELECT CONCAT(':',product,':'), SUM(profit), AVG(profit) FROM t1
GROUP BY product WITH ROLLUP;

#
# Test bug with const tables
#
--echo
drop table t1,t2;
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES(100);
CREATE TABLE t2 (i int);
INSERT INTO t2 VALUES (100),(200);

--echo
--sorted_result
SELECT i, COUNT(*) FROM t1 GROUP BY i WITH ROLLUP;

--echo
--sorted_result
SELECT t1.i, t2.i, COUNT(*) FROM t1,t2 GROUP BY t1.i,t2.i WITH ROLLUP;

#bug #4767: ROLLUP with LEFT JOIN
--echo
DROP TABLE t1,t2;
CREATE TABLE user_day(
user_id INT NOT NULL,
date DATE NOT NULL,
UNIQUE INDEX user_date (user_id, date)
);

INSERT INTO user_day VALUES
(1, '2004-06-06' ),
(1, '2004-06-07' ),
(2, '2004-06-06' );

--echo
--sorted_result
SELECT
d.date AS day,
COUNT(d.user_id) as sample,
COUNT(next_day.user_id) AS not_cancelled
FROM user_day d
LEFT JOIN user_day next_day
ON next_day.user_id=d.user_id AND
next_day.date= DATE_ADD( d.date, interval 1 day )
GROUP BY day;

--echo
--sorted_result
SELECT
d.date AS day,
COUNT(d.user_id) as sample,
COUNT(next_day.user_id) AS not_cancelled
FROM user_day d
LEFT JOIN user_day next_day
ON next_day.user_id=d.user_id AND
next_day.date= DATE_ADD( d.date, interval 1 day )
GROUP BY day
WITH ROLLUP;

--echo
DROP TABLE user_day;

#
# Tests for bugs #8616, #8615: distinct sum with rollup
#

CREATE TABLE t1 (a int, b int);

INSERT INTO t1 VALUES
(1,4),
(2,2), (2,2),
(4,1), (4,1), (4,1), (4,1),
(2,1), (2,1);

--echo
--sorted_result
SELECT SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT DISTINCT SUM(b) FROM t1 GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT SUM(b), COUNT(DISTINCT b) FROM t1 GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT DISTINCT SUM(b), COUNT(DISTINCT b) FROM t1 GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT SUM(b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT DISTINCT SUM(b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT SUM(b), COUNT(DISTINCT b), COUNT(*) FROM t1 GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT DISTINCT SUM(b), COUNT(DISTINCT b), COUNT(*) FROM t1
GROUP BY a WITH ROLLUP;
--echo
--sorted_result
SELECT a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
--echo
--sorted_result
SELECT DISTINCT a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
--echo
--sorted_result
SELECT b, a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
--echo
--sorted_result
SELECT DISTINCT b,a, SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP;
--echo
ALTER TABLE t1 ADD COLUMN c INT;
--echo
--sorted_result
SELECT a,b,SUM(c) FROM t1 GROUP BY a,b,c WITH ROLLUP;
--echo
--sorted_result
SELECT distinct a,b,SUM(c) FROM t1 GROUP BY a,b,c WITH ROLLUP;

DROP TABLE t1;

0 comments on commit 5d5de41

Please sign in to comment.