3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is the DISTINCT modifier on aggregate functions.
14 # $Id: distinctagg.test,v 1.3 2009/02/09 13:19:28 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix distinctagg
21 do_test distinctagg-1.1 {
23 CREATE TABLE t1(a,b,c);
24 INSERT INTO t1 VALUES(1,2,3);
25 INSERT INTO t1 VALUES(1,3,4);
26 INSERT INTO t1 VALUES(1,3,5);
27 SELECT count(distinct a),
33 do_test distinctagg-1.2 {
35 SELECT b, count(distinct c) FROM t1 GROUP BY b
38 do_test distinctagg-1.3 {
40 INSERT INTO t1 SELECT a+1, b+3, c+5 FROM t1;
41 INSERT INTO t1 SELECT a+2, b+6, c+10 FROM t1;
42 INSERT INTO t1 SELECT a+4, b+12, c+20 FROM t1;
43 SELECT count(*), count(distinct a), count(distinct b) FROM t1
46 do_test distinctagg-1.4 {
48 SELECT a, count(distinct c) FROM t1 GROUP BY a ORDER BY a
50 } {1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3}
52 do_test distinctagg-2.1 {
54 SELECT count(distinct) FROM t1;
56 } {1 {DISTINCT aggregates must have exactly one argument}}
57 do_test distinctagg-2.2 {
59 SELECT group_concat(distinct a,b) FROM t1;
61 } {1 {DISTINCT aggregates must have exactly one argument}}
63 #--------------------------------------------------------------------------
66 CREATE TABLE t1(a, b, c);
67 CREATE TABLE t2(d, e, f);
69 INSERT INTO t1 VALUES (1, 1, 1);
70 INSERT INTO t1 VALUES (2, 2, 2);
71 INSERT INTO t1 VALUES (3, 3, 3);
72 INSERT INTO t1 VALUES (4, 1, 4);
73 INSERT INTO t1 VALUES (5, 2, 1);
74 INSERT INTO t1 VALUES (5, 3, 2);
75 INSERT INTO t1 VALUES (4, 1, 3);
76 INSERT INTO t1 VALUES (3, 2, 4);
77 INSERT INTO t1 VALUES (2, 3, 1);
78 INSERT INTO t1 VALUES (1, 1, 2);
80 INSERT INTO t2 VALUES('a', 'a', 'a');
81 INSERT INTO t2 VALUES('b', 'b', 'b');
82 INSERT INTO t2 VALUES('c', 'c', 'c');
84 CREATE INDEX t1a ON t1(a);
85 CREATE INDEX t1bc ON t1(b, c);
88 foreach {tn use_eph sql res} {
89 1 0 "SELECT count(DISTINCT a) FROM t1" 5
90 2 0 "SELECT count(DISTINCT b) FROM t1" 3
91 3 1 "SELECT count(DISTINCT c) FROM t1" 4
92 4 0 "SELECT count(DISTINCT c) FROM t1 WHERE b=3" 3
93 5 0 "SELECT count(DISTINCT rowid) FROM t1" 10
94 6 0 "SELECT count(DISTINCT a) FROM t1, t2" 5
95 7 0 "SELECT count(DISTINCT a) FROM t2, t1" 5
96 8 1 "SELECT count(DISTINCT a+b) FROM t1, t2, t2, t2" 6
97 9 0 "SELECT count(DISTINCT c) FROM t1 WHERE c=2" 1
98 10 1 "SELECT count(DISTINCT t1.rowid) FROM t1, t2" 10
101 set prg [db eval "EXPLAIN $sql"]
102 set idx [lsearch $prg OpenEphemeral]
106 do_execsql_test 3.$tn.2 $sql $res
109 do_execsql_test 3.10 {
110 SELECT a, count(DISTINCT b) FROM t1 GROUP BY a;
115 #--------------------------------------------------------------------------
117 do_execsql_test 3.0 {
118 CREATE TABLE t1(a, b, c);
119 CREATE INDEX t1a ON t1(a);
120 CREATE INDEX t1bc ON t1(b, c);
122 INSERT INTO t1 VALUES(1, 'A', 1);
123 INSERT INTO t1 VALUES(1, 'A', 1);
124 INSERT INTO t1 VALUES(2, 'A', 2);
125 INSERT INTO t1 VALUES(2, 'A', 2);
126 INSERT INTO t1 VALUES(1, 'B', 1);
127 INSERT INTO t1 VALUES(2, 'B', 2);
128 INSERT INTO t1 VALUES(3, 'B', 3);
129 INSERT INTO t1 VALUES(NULL, 'B', NULL);
130 INSERT INTO t1 VALUES(NULL, 'C', NULL);
131 INSERT INTO t1 VALUES('d', 'D', 'd');
133 CREATE TABLE t2(d, e, f);
134 CREATE INDEX t2def ON t2(d, e, f);
136 INSERT INTO t2 VALUES(1, 1, 'a');
137 INSERT INTO t2 VALUES(1, 1, 'a');
138 INSERT INTO t2 VALUES(1, 2, 'a');
139 INSERT INTO t2 VALUES(1, 2, 'a');
140 INSERT INTO t2 VALUES(1, 2, 'b');
141 INSERT INTO t2 VALUES(1, 3, 'b');
142 INSERT INTO t2 VALUES(1, 3, 'a');
143 INSERT INTO t2 VALUES(1, 3, 'b');
144 INSERT INTO t2 VALUES(2, 3, 'x');
145 INSERT INTO t2 VALUES(2, 3, 'y');
146 INSERT INTO t2 VALUES(2, 3, 'z');
148 CREATE TABLE t3(x, y, z);
149 INSERT INTO t3 VALUES(1,1,1);
150 INSERT INTO t3 VALUES(2,2,2);
153 foreach {tn use_eph sql res} {
154 1 0 "SELECT count(DISTINCT c) FROM t1 GROUP BY b" {2 3 0 1}
155 2 1 "SELECT count(DISTINCT a) FROM t1 GROUP BY b" {2 3 0 1}
156 3 1 "SELECT count(DISTINCT a) FROM t1 GROUP BY b+c" {0 1 1 1 1}
158 4 0 "SELECT count(DISTINCT f) FROM t2 GROUP BY d, e" {1 2 2 3}
159 5 1 "SELECT count(DISTINCT f) FROM t2 GROUP BY d" {2 3}
160 6 0 "SELECT count(DISTINCT f) FROM t2 WHERE d IS 1 GROUP BY e" {1 2 2}
163 set prg [db eval "EXPLAIN $sql"]
164 set idx [lsearch $prg OpenEphemeral]
168 do_execsql_test 4.$tn.2 $sql $res
172 set t3root [db one {SELECT rootpage FROM sqlite_schema WHERE name='t3'}]
173 foreach {tn use_t3 sql res} {
174 1 1 "SELECT count(*) FROM t3" 2
175 2 0 "SELECT count(*) FROM t1" 10
176 2 1 "SELECT count(DISTINCT a) FROM t1, t3" 4
177 3 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3" 4
178 4 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 WHERE t3.x=1" 4
179 5 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 WHERE t3.x=0" 0
180 6 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 ON (t3.x=0)" 4
181 7 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3" 2
182 8 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 WHERE t3.x=1" 1
183 9 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 WHERE t3.x=0" 0
184 10 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 ON (t3.x=0)" 0
189 db eval "EXPLAIN $sql" a {
190 if {$a(opcode)=="OpenRead" && $a(p2)==$t3root} {set bUse 1}
195 do_execsql_test 5.$tn.2 $sql $res
198 #-------------------------------------------------------------------------
200 do_execsql_test 6.0 {
201 CREATE TABLE t1(a, b);
202 CREATE TABLE t2(c, d);
203 INSERT INTO t1 VALUES(123,456);
204 INSERT INTO t2 VALUES(123,456);
206 do_execsql_test 6.1 {
207 SELECT count(DISTINCT c) FROM t1 LEFT JOIN t2;
210 do_execsql_test 7.0 {
211 CREATE TABLE v1 ( v2 UNIQUE, v3 AS( TYPEOF ( NULL ) ) UNIQUE );
212 SELECT COUNT ( DISTINCT TRUE ) FROM v1 GROUP BY likelihood ( v3 , 0.100000 );