287 lines
10 KiB
Plaintext
287 lines
10 KiB
Plaintext
--disable_query_log
|
|
set @@session.explicit_defaults_for_timestamp=off;
|
|
--enable_query_log
|
|
#owner: zhanyue.zzy
|
|
#owner group: sql1
|
|
#tags: optimizer, group_by
|
|
#description:
|
|
|
|
--enable_abort_on_error
|
|
## simple test of all group functions
|
|
##
|
|
#
|
|
--disable_warnings
|
|
drop table if exists t1,t2;
|
|
--enable_warnings
|
|
#
|
|
##set @sav_dpi= @@div_precision_increment;
|
|
##set div_precision_increment= 5;
|
|
##show variables like 'div_precision_increment';
|
|
##create table t1 (grp int, a bigint unsigned, c char(10) not null);
|
|
#--real_sleep 1
|
|
create table t1 (pk int primary key, grp int, a int, c char(10) not null);
|
|
#--real_sleep 1
|
|
|
|
insert into t1 values (1, 1,1,'a');
|
|
insert into t1 values (2, 2,2,'b');
|
|
insert into t1 values (3, 2,3,'c');
|
|
insert into t1 values (4, 3,4,'E');
|
|
insert into t1 values (5, 3,5,'C');
|
|
insert into t1 values (6, 3,6,'D');
|
|
#
|
|
## Test of MySQL field extension with and without matching records.
|
|
##### Note: The two following statements may fail if the execution plan
|
|
##### or optimizer is changed. The result for column c is undefined.
|
|
select a,c,sum(a) from t1 group by a order by a;
|
|
select a,c,sum(a) from t1 where a > 10 group by a order by a;
|
|
select sum(a) from t1 where a > 10;
|
|
#select a from t1 order by rand(10);
|
|
#select distinct a from t1 order by rand(10);
|
|
select count(distinct a),count(distinct grp) from t1;
|
|
insert into t1 values (7, null,null,'');
|
|
select count(distinct a),count(distinct grp) from t1;
|
|
#
|
|
#select sum(all a),count(all a),avg(all a),std(all a),variance(all a),bit_or(all a),bit_and(all a),min(all a),max(all a),min(all c),max(all c) from t1;
|
|
#select grp, sum(a),count(a),avg(a),std(a),variance(a),bit_or(a),bit_and(a),min(a),max(a),min(c),max(c) from t1 group by grp;
|
|
#--disable_warnings
|
|
#select grp, sum(a)+count(a)+avg(a)+std(a)+variance(a)+bit_or(a)+bit_and(a)+min(a)+max(a)+min(c)+max(c) as sum from t1 group by grp;
|
|
#--enable_warnings
|
|
#
|
|
#--real_sleep 1
|
|
create table t2 (pk int primary key, grp int, a int, c char(10));
|
|
#--real_sleep 1
|
|
#insert into t2
|
|
select grp,max(a)+max(grp),max(c) from t1 group by grp order by grp;
|
|
#
|
|
## REPLACE ... SELECT doesn't yet work with PS
|
|
#replace into t2 select grp, a, c from t1 limit 2,1;
|
|
select * from t2;
|
|
#
|
|
drop table t1,t2;
|
|
#
|
|
##
|
|
## Problem with std()
|
|
##
|
|
#
|
|
--disable_warnings
|
|
drop table if exists t1;
|
|
--enable_warnings
|
|
CREATE TABLE t1 (id int ,value1 decimal(10,2),c int primary key);
|
|
INSERT INTO t1 VALUES (1,0.00,1),(1,1.00,2), (1,2.00,3), (2,10.00,4), (2,11.00,5), (2,12.00,6);
|
|
CREATE TABLE t2 (id int primary key,name char(20));
|
|
INSERT INTO t2 VALUES (1,'Set One'),(2,'Set Two');
|
|
#select id, avg(value1), std(value1), variance(value1) from t1 group by id;
|
|
--sorted_result
|
|
select id, avg(value1) from t1 group by id;
|
|
#select name, avg(value1), std(value1), variance(value1) from t1, t2 where t1.id = t2.id group by t1.id;
|
|
--sorted_result
|
|
select name, avg(value1) from t1, t2 where t1.id = t2.id group by t1.id;
|
|
drop table t1,t2;
|
|
#
|
|
##
|
|
## Test of bug in left join & avg
|
|
##
|
|
#
|
|
#--real_sleep 1
|
|
create table t1 (pk int primary key, id int not null);
|
|
#--real_sleep 1
|
|
create table t2 (pk int primary key, id int not null,rating int null);
|
|
#--real_sleep 1
|
|
insert into t1 values(1,1),(2,2),(3,3);
|
|
insert into t2 values(1, 1, 3),(2, 2, NULL),(3, 2, NULL),(4, 3, 2),(5, 3, NULL);
|
|
select t1.id, avg(rating) from t1 left join t2 on ( t1.id = t2.id ) group by t1.id order by t1.id;
|
|
## Test different types with avg()
|
|
#select sql_small_result t2.id, avg(rating) from t2 group by t2.id;
|
|
#select sql_big_result t2.id, avg(rating) from t2 group by t2.id;
|
|
#select sql_small_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
|
|
#select sql_big_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
|
|
drop table t1,t2;
|
|
#
|
|
##
|
|
## test of count
|
|
##
|
|
#--real_sleep 1
|
|
create table t1 (a int primary key, c char(10), b char(128));
|
|
#--real_sleep 1
|
|
INSERT INTO t1 VALUES (1,'1','1');
|
|
INSERT INTO t1 VALUES (2,'2','2');
|
|
INSERT INTO t1 VALUES (4,'4','4');
|
|
#
|
|
select count(*) from t1;
|
|
select count(*) from t1 where a = 1;
|
|
select count(*) from t1 where a = 100;
|
|
select count(*) from t1 where a >= 10;
|
|
select count(a) from t1 where a = 1;
|
|
select count(a) from t1 where a = 100;
|
|
select count(a) from t1 where a >= 10;
|
|
select count(b) from t1 where b >= 2;
|
|
select count(b) from t1 where b >= 10;
|
|
select count(c) from t1 where c = 10;
|
|
drop table t1;
|
|
#
|
|
##
|
|
## Test of bug in COUNT(i)*(i+0)
|
|
##
|
|
#
|
|
#--real_sleep 1
|
|
CREATE TABLE t1 (d datetime default now(), i int primary key);
|
|
#--real_sleep 1
|
|
INSERT INTO t1(i) VALUES (1);
|
|
--sorted_result
|
|
SELECT COUNT(i), i, COUNT(i)*i FROM t1 GROUP BY i;
|
|
--sorted_result
|
|
SELECT COUNT(i), (i+0), COUNT(i)*(i+0) FROM t1 GROUP BY i;
|
|
DROP TABLE t1;
|
|
#
|
|
##
|
|
## Another SUM() problem with 3.23.2
|
|
##
|
|
#
|
|
#--real_sleep 1
|
|
create table t1 (
|
|
pk int primary key,
|
|
num float,
|
|
my_user char(20)
|
|
);
|
|
#--real_sleep 1
|
|
# remeber to remove comments
|
|
insert into t1 values (1, 10.3,'nem'),(2, 20.53,'monty'),(3, 30.23,'sinisa');
|
|
insert into t1 values (4, 30.13,'nem'),(5, 20.98,'monty'),(6, 10.45,'sinisa');
|
|
insert into t1 values (7, 5.2,'nem'),(8, 8.64,'monty'),(9, 11.12,'sinisa');
|
|
select sum(num) from t1;
|
|
select sum(num) from t1 group by my_user order by my_user;
|
|
drop table t1;
|
|
#
|
|
##
|
|
## Test problem with MIN() optimization in case of null values
|
|
##
|
|
#
|
|
#--real_sleep 1
|
|
--disable_warnings
|
|
drop table if exists t1,t2,t3;
|
|
--enable_warnings
|
|
create table t1 (pk int primary key, a1 int, a2 char(3));
|
|
#--real_sleep 1
|
|
insert into t1 values(1, 10,'aaa'), (2, 10,null), (3, 10,'bbb'), (4, 20,'zzz');
|
|
#--real_sleep 1
|
|
create table t2(pk int primary key, a1 char(3), a2 int, a3 float);
|
|
create table t3(pk int primary key, a1 char(3), a2 int, a3 float);
|
|
#--real_sleep 1
|
|
select * from t1;
|
|
## The following returned NULL in 4.0.10
|
|
select min(a2) from t1;
|
|
|
|
select max(t1.a1), max(t2.a2) from t1, t2;
|
|
#select max(t1.a1) from t1, t2;
|
|
#select max(t2.a2), max(t1.a1) from t1, t2;
|
|
|
|
#
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
#explain select min(a2) from t1;
|
|
--enable_result_log
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
#explain select max(t1.a1), max(t2.a2) from t1, t2;
|
|
--enable_result_log
|
|
#
|
|
insert into t2 values(1, 'AAA', 10, 0.5);
|
|
insert into t2 values(2, 'BBB', 20, 1.0);
|
|
#select t1.a1, t1.a2, t2.a1, t2.a2 from t1,t2;
|
|
#
|
|
#select max(t1.a1), max(t2.a1) from t1, t2 where t2.a2=9;
|
|
#select max(t2.a1), max(t1.a1) from t1, t2 where t2.a2=9;
|
|
#there is a bug
|
|
--error 1054
|
|
select t1.a1, t1.a2, t2.a1, t2.a2 from t1 left outer join t3 on t1.a1=10;
|
|
select max(t1.a2) from t1 left outer join t2 on t1.a1=10;
|
|
#select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=20;
|
|
#select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=10;
|
|
select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and 1=0 where t2.a1='AAA';
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
explain select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and 1=0 where t2.a1='AAA';
|
|
--enable_result_log
|
|
select max(t2.a1) from t1 left outer join t2 on t1.a2=t2.a1 where t2.a1='AAA';
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
explain select max(t2.a1) from t1 left outer join t2 on t1.a2=t2.a1 where t2.a1='AAA';
|
|
--enable_result_log
|
|
select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk where t2.a1='AAA' and 1=0;
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
explain select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk where t2.a1='AAA' and 1=0;
|
|
--enable_result_log
|
|
select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.a1=10;
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
explain select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.a1=10;
|
|
--enable_result_log
|
|
select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.pk=10;
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
explain select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.pk=10;
|
|
--enable_result_log
|
|
select * from t1 left join t2 on t1.pk=t2.pk where t2.a2 > 1 and t2.a2 < 10;
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
explain select * from t1 left join t2 on t1.pk=t2.pk where t2.a2 > 1 and t2.a2 < 10;
|
|
--enable_result_log
|
|
select * from t1 left join t2 on t1.pk=t2.pk and t2.a2 > 1 and t2.a2 < 10 order by t1.pk;
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
explain select * from t1 left join t2 on t1.pk=t2.pk and t2.a2 > 1 and t2.a2 < 10;
|
|
--enable_result_log
|
|
|
|
drop table t1,t2;
|
|
#
|
|
##
|
|
## Test of group function and NULL values
|
|
##
|
|
#
|
|
#--real_sleep 1
|
|
CREATE TABLE t1 (pk int primary key, a int, b int);
|
|
#--real_sleep 1
|
|
#select count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1;
|
|
select count(b), sum(b), avg(b), min(b), max(b) from t1;
|
|
select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
|
|
insert into t1 values (1, 1,null);
|
|
select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
|
|
insert into t1 values (2, 1,null);
|
|
insert into t1 values (3, 2,null);
|
|
select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
|
|
#select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
|
|
insert into t1 values (4, 2,1);
|
|
select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
|
|
#select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
|
|
insert into t1 values (5, 3,1);
|
|
select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
|
|
#select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b), bit_xor(b) from t1 group by a;
|
|
--replace_regex /Plan signature: [0-9]*/Plan signature/
|
|
--disable_result_log
|
|
#explain extended select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b), bit_xor(b) from t1 group by a;
|
|
--enable_result_log
|
|
drop table t1;
|
|
#
|
|
##
|
|
## Bug #1972: test for bit_and(), bit_or() and negative values
|
|
##
|
|
#--real_sleep 1
|
|
create table t1 (col int primary key,col2 int);
|
|
#--real_sleep 1
|
|
insert into t1(col) values (-1), (-2), (-3);
|
|
#select bit_and(col), bit_or(col) from t1;
|
|
#select SQL_BIG_RESULT bit_and(col), bit_or(col) from t1 group by col;
|
|
drop table t1;
|
|
#
|
|
##
|
|
## Bug #3376: avg() and an empty table
|
|
##
|
|
#
|
|
#--real_sleep 1
|
|
create table t1 (a int primary key,b int);
|
|
#--real_sleep 1
|
|
select avg(2) from t1;
|
|
drop table t1;
|
|
#
|