~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
#
2
# Various tests for SUM(DISTINCT ...)
3
#
4
--disable_warnings
5
DROP TABLE IF EXISTS t1, t2;
6
--enable_warnings
7
8
CREATE TABLE t1 (
9
  id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, 
10
  gender CHAR(1),
11
  name VARCHAR(20)
12
);
13
14
# According to ANSI SQL, SUM(DISTINCT ...) should return NULL for empty
15
# record set
16
17
SELECT SUM(DISTINCT LENGTH(name)) s1 FROM t1;
18
19
# According to ANSI SQL, SUM(DISTINCT ...) should return NULL for records sets
20
# entirely consisting of NULLs
21
22
INSERT INTO t1 (gender, name) VALUES (NULL, NULL);
23
INSERT INTO t1 (gender, name) VALUES (NULL, NULL);
24
INSERT INTO t1 (gender, name) VALUES (NULL, NULL);
25
26
SELECT SUM(DISTINCT LENGTH(name)) s1 FROM t1;
27
28
29
# Filling table with t1
30
31
INSERT INTO t1 (gender, name) VALUES ('F', 'Helen'), ('F', 'Anastasia'),
32
('F', 'Katherine'), ('F', 'Margo'), ('F', 'Magdalene'), ('F', 'Mary');
33
34
CREATE TABLE t2 SELECT name FROM t1;
35
36
SELECT (SELECT SUM(DISTINCT LENGTH(name)) FROM t1) FROM t2;
37
38
DROP TABLE t2;
39
40
INSERT INTO t1 (gender, name) VALUES ('F', 'Eva'), ('F', 'Sofia'),
41
('F', 'Sara'), ('F', 'Golda'), ('F', 'Toba'), ('F', 'Victory'),
42
('F', 'Faina'), ('F', 'Miriam'), ('F', 'Beki'), ('F', 'America'),
43
('F', 'Susan'), ('F', 'Glory'), ('F', 'Priscilla'), ('F', 'Rosmary'),
44
('F', 'Rose'), ('F', 'Margareth'), ('F', 'Elizabeth'), ('F', 'Meredith'),
45
('F', 'Julie'), ('F', 'Xenia'), ('F', 'Zena'), ('F', 'Olga'),
46
('F', 'Brunhilda'), ('F', 'Nataly'), ('F', 'Lara'), ('F', 'Svetlana'),
47
('F', 'Grethem'), ('F', 'Irene');
48
49
SELECT
50
  SUM(DISTINCT LENGTH(name)) s1,
51
  SUM(DISTINCT SUBSTRING(NAME, 1, 3)) s2,
52
  SUM(DISTINCT LENGTH(SUBSTRING(name, 1, 4))) s3
53
FROM t1;
54
55
SELECT
56
  SUM(DISTINCT LENGTH(g1.name)) s1,
57
  SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
58
  SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
2141.4.2 by Andrew Hutchings
Implicit joins of the form "SELECT * FROM t1, t2" without WHERE or ON now error.
59
FROM t1 g1 CROSS JOIN t1 g2 CROSS JOIN t1 g3;
1 by brian
clean slate
60
61
SELECT
1487.1.1 by Brian Aker
There is room for improvement around this. We should be using rows as well
62
  SQL_BIG_RESULT
1 by brian
clean slate
63
  SUM(DISTINCT LENGTH(g1.name)) s1,
64
  SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
65
  SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
2141.4.2 by Andrew Hutchings
Implicit joins of the form "SELECT * FROM t1, t2" without WHERE or ON now error.
66
FROM t1 g1 CROSS JOIN t1 g2 CROSS JOIN t1 g3 GROUP BY LENGTH(SUBSTRING(g3.name, 5, 10));
1 by brian
clean slate
67
68
# here we explicitly request summing through temporary table (so
69
# Item_sum_sum_distinct::copy_or_same() is called)
70
71
SELECT SQL_BUFFER_RESULT
72
  SUM(DISTINCT LENGTH(name)) s1,
73
  SUM(DISTINCT SUBSTRING(NAME, 1, 3)) s2,
74
  SUM(DISTINCT LENGTH(SUBSTRING(name, 1, 4))) s3
75
FROM t1;
76
1487.1.1 by Brian Aker
There is room for improvement around this. We should be using rows as well
77
SELECT SQL_BIG_RESULT
1 by brian
clean slate
78
  SUM(DISTINCT LENGTH(g1.name)) s1,
79
  SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
80
  SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
2141.4.2 by Andrew Hutchings
Implicit joins of the form "SELECT * FROM t1, t2" without WHERE or ON now error.
81
FROM t1 g1 CROSS JOIN t1 g2 CROSS JOIN t1 g3 GROUP BY LENGTH(SUBSTRING(g3.name, 5, 10));
1 by brian
clean slate
82
83
# this test demonstrates that strings are automatically converted to numbers
84
# before summing
85
86
SET @l=1;
87
UPDATE t1 SET name=CONCAT(name, @l:=@l+1);
88
89
SELECT SUM(DISTINCT RIGHT(name, 1)) FROM t1;
90
91
# this is a test case for ordinary t1 
92
93
SELECT SUM(DISTINCT id) FROM t1;
94
SELECT SUM(DISTINCT id % 11) FROM t1;
95
96
DROP TABLE t1;