2
# Various tests for SUM(DISTINCT ...)
5
--source include/big_test.inc
7
DROP TABLE IF EXISTS t1, t2;
11
# Test the case when distinct values doesn't fit in memory and
12
# filesort is used (see uniques.cc:merge_walk)
15
CREATE TABLE t1 (id INTEGER);
16
CREATE TABLE t2 (id INTEGER);
18
INSERT INTO t1 (id) VALUES (1), (1), (1),(1);
19
INSERT INTO t1 (id) SELECT id FROM t1; /* 8 */
20
INSERT INTO t1 (id) SELECT id FROM t1; /* 12 */
21
INSERT INTO t1 (id) SELECT id FROM t1; /* 16 */
22
INSERT INTO t1 (id) SELECT id FROM t1; /* 20 */
23
INSERT INTO t1 (id) SELECT id FROM t1; /* 24 */
24
INSERT INTO t1 SELECT id+1 FROM t1;
25
INSERT INTO t1 SELECT id+2 FROM t1;
26
INSERT INTO t1 SELECT id+4 FROM t1;
27
INSERT INTO t1 SELECT id+8 FROM t1;
28
INSERT INTO t1 SELECT id+16 FROM t1;
29
INSERT INTO t1 SELECT id+32 FROM t1;
30
INSERT INTO t1 SELECT id+64 FROM t1;
31
INSERT INTO t1 SELECT id+128 FROM t1;
32
INSERT INTO t1 SELECT id+256 FROM t1;
33
INSERT INTO t1 SELECT id+512 FROM t1;
35
# Just test that AVG(DISTINCT) is there
36
SELECT AVG(DISTINCT id) FROM t1 GROUP BY id % 13;
37
SELECT SUM(DISTINCT id)/COUNT(DISTINCT id) FROM t1 GROUP BY id % 13;
39
INSERT INTO t1 SELECT id+1024 FROM t1;
40
INSERT INTO t1 SELECT id+2048 FROM t1;
41
INSERT INTO t1 SELECT id+4096 FROM t1;
42
INSERT INTO t1 SELECT id+8192 FROM t1;
43
INSERT INTO t2 SELECT id FROM t1 ORDER BY id*rand();
45
# SELECT '++++++++++++++++++++++++++++++++++++++++++++++++++';
47
SELECT SUM(DISTINCT id) sm FROM t1;
48
SELECT SUM(DISTINCT id) sm FROM t2;
49
SELECT SUM(DISTINCT id) sm FROM t1 group by id % 13;
51
# this limit for max_heap_table_size is set to force testing the case, when
52
# all distinct sum values can not fit in memory and must be stored in a
55
SET max_heap_table_size=16384;
57
# to check that max_heap_table_size was actually set (hard limit for minimum
58
# max_heap_table_size is set in mysqld.cc):
60
SHOW variables LIKE 'max_heap_table_size';
62
SELECT SUM(DISTINCT id) sm FROM t1;
63
SELECT SUM(DISTINCT id) sm FROM t2;
64
SELECT SUM(DISTINCT id) sm FROM t1 GROUP BY id % 13;