File: count_distinct.test

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (269 lines) | stat: -rw-r--r-- 8,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#
# Problem with count(distinct)
#

--disable_warnings
drop table if exists t1,t2,t3;
--enable_warnings

create table t1 (libname varchar(21) not null, city text, primary key (libname));
create table t2 (isbn varchar(21) not null, author text, title text, primary key (isbn));
create table t3 (isbn varchar(21) not null, libname varchar(21) not null, quantity int ,primary key (isbn,libname));
insert into t2 values ('001','Daffy','A duck''s life');
insert into t2 values ('002','Bugs','A rabbit\'s life');
insert into t2 values ('003','Cowboy','Life on the range');
insert into t2 values ('000','Anonymous','Wanna buy this book?');
insert into t2 values ('004','Best Seller','One Heckuva book');
insert into t2 values ('005','EveryoneBuys','This very book');
insert into t2 values ('006','San Fran','It is a san fran lifestyle');
insert into t2 values ('007','BerkAuthor','Cool.Berkley.the.book');
insert into t3 values('000','New York Public Libra','1');
insert into t3 values('001','New York Public Libra','2');
insert into t3 values('002','New York Public Libra','3');
insert into t3 values('003','New York Public Libra','4');
insert into t3 values('004','New York Public Libra','5');
insert into t3 values('005','New York Public Libra','6');
insert into t3 values('006','San Fransisco Public','5');
insert into t3 values('007','Berkeley Public1','3');
insert into t3 values('007','Berkeley Public2','3');
insert into t3 values('001','NYC Lib','8');
insert into t1 values ('New York Public Libra','New York');
insert into t1 values ('San Fransisco Public','San Fran');
insert into t1 values ('Berkeley Public1','Berkeley');
insert into t1 values ('Berkeley Public2','Berkeley');
insert into t1 values ('NYC Lib','New York');
--source include/turn_off_only_full_group_by.inc

--sorted_result
select t2.isbn,city,t1.libname,count(t1.libname) as a from t3 left join t1 on t3.libname=t1.libname left join t2 on t3.isbn=t2.isbn group by city,t1.libname;
--sorted_result
select t2.isbn,city,t1.libname,count(distinct t1.libname) as a from t3 left join t1 on t3.libname=t1.libname left join t2 on t3.isbn=t2.isbn group by city having count(distinct t1.libname) > 1;
--sorted_result
select t2.isbn,city,t1.libname,count(distinct t1.libname) as a from t3 left join t1 on t3.libname=t1.libname left join t2 on t3.isbn=t2.isbn group by city having count(distinct concat(t1.libname,'a')) > 1;

select t2.isbn,city,@bar:=t1.libname,count(distinct t1.libname) as a
       from t3 left join t1 on t3.libname=t1.libname left join t2
       on t3.isbn=t2.isbn group by city having count(distinct
       t1.libname) > 1;

#
# Wrong result, see bug#49872
# 
--skip_if_hypergraph  # Different result, no more or less wrong.
SELECT @bar;

select t2.isbn,city,concat(@bar:=t1.libname),count(distinct t1.libname) as a
       from t3 left join t1 on t3.libname=t1.libname left join t2
       on t3.isbn=t2.isbn group by city having count(distinct
       t1.libname) > 1;
#
# Wrong result, see bug#49872
# 
--skip_if_hypergraph  # Different result, no more or less wrong.
SELECT @bar;

--source include/restore_sql_mode_after_turn_off_only_full_group_by.inc
drop table t1, t2, t3;

#
# Problem with LEFT JOIN
#

create table t1 (f1 int);
insert into t1 values (1);
create table t2 (f1 int,f2 int);
select t1.f1,count(distinct t2.f2),count(distinct 1,NULL) from t1 left join t2 on t1.f1=t2.f1 group by t1.f1;
drop table t1,t2;


#
# Empty tables
#
create table t1 (f int);
select count(distinct f) from t1;
drop table t1;

# End of 4.1 tests

#
# Bug #6515
#

create table t1 (a char(3), b char(20), primary key (a, b));
insert into t1 values ('ABW', 'Dutch'), ('ABW', 'English');
select count(distinct a) from t1 group by b;
drop table t1;

#
# Bug #9593 "The combination of COUNT, DISTINCT and CONCAT 
#            seems to lock the server"
# Bug appears only on Windows system
#

create table t1 (f1 int, f2 int);
insert into t1 values (0,1),(1,2);
select count(distinct if(f1,3,f2)) from t1;
drop table t1;

#
# Bug #51980 "mysqld service crashes with a simple COUNT(DISTINCT) query 
#             over a view"
#

create table t1 (i int);
insert into t1 values (0), (1);
create view v1 as select * from t1;
select count(distinct i) from v1;
drop table t1;
drop view v1;

--echo #
--echo # Bug#11760197: INCORRECT RESULTS WITH COUNT DISTINCT AND BIG_TABLES
--echo #

CREATE TABLE t1 (a INT PRIMARY KEY);
INSERT INTO t1 VALUES (1),(2);
SET @big_tables_save= @@big_tables;
SET SESSION big_tables=0;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = 1;
SET SESSION big_tables=1;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = 1;
DROP TABLE  t1;

CREATE TABLE t1 (a BLOB ,PRIMARY KEY(a(10)));
INSERT INTO t1 VALUES (1),(2);
SET SESSION big_tables=0;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = 1;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = '1';
SET SESSION big_tables=1;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = 1;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = '1';
DROP TABLE  t1;

CREATE TABLE t1 (a CHAR PRIMARY KEY);
INSERT INTO t1 VALUES (1),(2);
SET SESSION big_tables=0;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = 1;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = '1';
SET SESSION big_tables=1;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = 1;
SELECT COUNT(DISTINCT t1.a) FROM t1,t1 t2 WHERE t1.a = '1';
SET SESSION big_tables= @big_tables_save;
DROP TABLE  t1;

--echo #
--echo # BUG#17607155: ASSERTION FAILED: 
--echo #               ((ITEM_RESULT_FIELD*)ITEM)->RESULT_FIELD
--echo #

CREATE TABLE t (a INT)ENGINE=INNODB;
INSERT t VALUES(1);
SELECT '' FROM t GROUP BY 1 WITH ROLLUP HAVING GROUP_CONCAT('');
DROP TABLE t;

--echo #
--echo # BUG#17957913: ASSERTION `((ITEM_RESULT_FIELD*)ITEM)->RESULT_FIELD'
--echo #               FAILS IN CREATE_TMP_FIELD
--echo #

CREATE TABLE t1 ( i INT );
INSERT INTO t1 VALUES (1),(2);
SELECT GROUP_CONCAT(i,'foo') AS f1 FROM t1 GROUP BY 'a' WITH ROLLUP;
DROP TABLE t1;

--echo #
--echo # Bug #17555462: SELECT DISTINCT OF VIEW WITH CONSTANT STRING
--echo #                RETURNS WRONG RESULT
--echo #

CREATE TABLE t (x char(3));
INSERT INTO t VALUES ('foo'), ('bar'); 
CREATE VIEW v AS SELECT 'x' AS x FROM t;
--sorted_result
SELECT DISTINCT x FROM v;

DROP TABLE t;
DROP VIEW v;

--echo #
--echo # Bug #17634335 SELECT DISTINCT...GROUP BY RETURNS WRONG
--echo #               RESULTS IN SOME CASES
--echo #

CREATE TABLE t1 (
a int(11) DEFAULT NULL,
b int(11) DEFAULT NULL,
c int(11) DEFAULT NULL
) ENGINE=InnoDB;

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

--source include/turn_off_only_full_group_by.inc
SET @optimizer_switch_saved= @@optimizer_switch;
SET @@optimizer_switch="derived_merge=off";

--sorted_result
SELECT DISTINCT a, b
FROM (SELECT * FROM t1 ORDER BY a, b) AS f
GROUP BY a,c;
--sorted_result
SELECT DISTINCT a, b, 0
FROM (SELECT * FROM t1 ORDER BY a, b) AS f
GROUP BY a,c;
SET @@optimizer_switch=@optimizer_switch_saved;
--source include/restore_sql_mode_after_turn_off_only_full_group_by.inc

DROP TABLE t1;

--echo #
--echo # Bug #18853696 WRONG RESULT FOR COUNT DISTINCT OF RESULT 
--echo #               OF JOINED DERIVED TABLE
--echo #

CREATE TABLE t1 (
pk INT NOT NULL,
col_varchar_key VARCHAR(1),
col_varchar_nokey VARCHAR(1),
PRIMARY KEY (pk),
KEY col_varchar_key (col_varchar_key)
);

INSERT INTO t1 VALUES (10,'x','x');
INSERT INTO t1 VALUES (11,'d','d');
INSERT INTO t1 VALUES (12,'r','r');
INSERT INTO t1 VALUES (13,'f','f');
INSERT INTO t1 VALUES (14,'y','y');

CREATE TABLE t2 (
pk INT NOT NULL,
col_int_nokey INT,
col_varchar_key VARCHAR(1),
PRIMARY KEY (pk),
KEY col_varchar_key (col_varchar_key)
);

INSERT INTO t2 VALUES (1,7,'x');

SELECT COUNT( DISTINCT table1.col_varchar_key )
FROM t2 AS table1
JOIN (SELECT * FROM t1) AS table2
ON ( table2.pk = table1.col_int_nokey )
WHERE table1.pk = 1;

SELECT COUNT( DISTINCT table1.col_varchar_key )
FROM t2 AS table1
JOIN t1 AS table2
ON ( table2.pk = table1.col_int_nokey )
WHERE table1.pk = 1;

SELECT COUNT( table1.col_varchar_key )
FROM t2 AS table1
JOIN (SELECT * FROM t1) AS table2
ON ( table2.pk = table1.col_int_nokey )
WHERE table1.pk = 1;

DROP TABLE t1,t2;

--echo # End of test for Bug#18853696