Files
tidb/tests/integrationtest/r/executor/aggregate.result

2112 lines
73 KiB
Plaintext

drop table if exists t;
create table t(id int primary key, b varchar(50), c int);
insert into t values(1, '1ff', NULL), (2, '234.02', 1);
select id, sum(b) from t group by id;
id sum(b)
1 1
2 234.02
select sum(b) from t;
sum(b)
235.02
select id, count(c) from t group by id;
id count(c)
1 0
2 1
drop table if exists t;
create table t(id int primary key, b float, c float);
insert into t values(1, 1, 3), (2, 1, 6);
select sum(b/c) from t group by id;
sum(b/c)
0.3333333333333333
0.16666666666666666
drop table if exists t;
create table t(id int primary key, b float, c float, d float);
insert into t values(1, 1, 3, NULL), (2, 1, NULL, 6), (3, NULL, 1, 2), (4, NULL, NULL, 1), (5, NULL, 2, NULL), (6, 3, NULL, NULL), (7, NULL, NULL, NULL), (8, 1, 2 ,3);
select count(distinct b, c, d) from t group by id;
count(distinct b, c, d)
0
0
0
0
0
0
0
1
select approx_count_distinct( b, c, d) from t group by id order by id;
approx_count_distinct( b, c, d)
0
0
0
0
0
0
0
1
drop table if exists t;
create table t(a int primary key, b varchar(10));
insert into t value(1, 11),(3, NULL);
SELECT a, MIN(b), MAX(b) FROM t GROUP BY a;
a MIN(b) MAX(b)
1 11 11
3 NULL NULL
drop table if exists t;
create table t (a int, b int, c int, index idx(a, b, c));
select count(a) from t group by a;
count(a)
select count(a) from t;
count(a)
0
insert t values(0,0,0);
select distinct b from t;
b
0
select count(b) from t group by a;
count(b)
1
insert t values(1,1,1),(3,3,6),(3,2,5),(2,1,4),(1,1,3),(1,1,2);
select count(a) from t where b>0 group by a, b;
count(a)
1
1
1
3
select count(a) from t where b>0 group by a, b order by a;
count(a)
3
1
1
1
select count(a) from t where b>0 group by a, b order by a limit 1;
count(a)
3
drop table if exists t, tt;
create table t(a int primary key, b int, c int);
create table tt(a int primary key, b int, c int);
insert into t values(1, 1, 1), (2, 1, 1);
insert into tt values(1, 2, 1);
select max(a.b), max(b.b) from t a join tt b on a.a = b.a group by a.c;
max(a.b) max(b.b)
1 2
select a, count(b) from (select * from t union all select * from tt) k group by a order by a;
a count(b)
1 2
2 1
set sql_mode = 'ONLY_FULL_GROUP_BY';
drop table if exists s;
create table s(a int);
select count(a) , date_format(a, '%Y-%m-%d') from s group by date_format(a, '%Y-%m-%d');
count(a) date_format(a, '%Y-%m-%d')
select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by date_format(a, '%Y-%m-%d');
count(a) xx
select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by xx;
count(a) xx
set sql_mode = default;
drop table if exists t1;
CREATE TABLE t1 (
a int(11) DEFAULT NULL,
b tinyint(4) NOT NULL,
PRIMARY KEY (b)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE ( b ) (
PARTITION p0 VALUES LESS THAN (10),
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (30),
PARTITION p3 VALUES LESS THAN (40),
PARTITION p4 VALUES LESS THAN (MAXVALUE)
);
insert into t1 values (0, 0), (1, 1), (1, 2), (1, 3), (2, 4), (2, 5), (2, 6), (3, 7), (3, 10), (3, 11), (12, 12), (12, 13), (14, 14), (14, 15), (20, 20), (20, 21), (20, 22), (23, 23), (23, 24), (23, 25), (31, 30), (31, 31), (31, 32), (33, 33), (33, 34), (33, 35), (36, 36), (80, 80), (90, 90), (100, 100);
set @@tidb_opt_agg_push_down = 1;
select /*+ AGG_TO_COP() */ sum(a), sum(b) from t1 where a < 40 group by a;
sum(a) sum(b)
0 0
24 25
28 29
3 6
36 36
6 15
60 63
69 72
9 28
93 93
99 102
set @@tidb_opt_agg_push_down = default;
set sql_mode = 'ONLY_FULL_GROUP_BY';
drop table if exists t;
create table t(a int);
select ((+a+1)) as tmp from t group by tmp;
tmp
set @@tidb_opt_agg_push_down = default;
set sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
drop table if exists t;
create table t (c1 int, c2 int, c3 int);
insert into t values (1,2,3), (2, 3, 1), (3, 1, 2);
select c1 as c2, c3 from t having c2 = 2;
c2 c3
2 1
select c1 as c2, c3 from t group by c2 having c2 = 2;
c2 c3
1 3
select c1 as c2, c3 from t group by c2 having sum(c2) = 2;
c2 c3
1 3
select c1 as c2, c3 from t group by c3 having sum(c2) = 2;
c2 c3
1 3
select c1 as c2, c3 from t group by c3 having sum(0) + c2 = 2;
c2 c3
2 1
select c1 as a from t having c1 = 1;
a
1
select t.c1 from t having c1 = 1;
c1
1
select a.c1 from t as a having c1 = 1;
c1
1
select c1 as a from t group by c3 having sum(a) = 1;
a
1
select c1 as a from t group by c3 having sum(a) + a = 2;
a
1
select a.c1 as c, a.c1 as d from t as a, t as b having c1 = 1 limit 1;
c d
1 1
select sum(c1) as s from t group by c1 having sum(c1) order by s;
s
1
2
3
select sum(c1) - 1 as s from t group by c1 having sum(c1) - 1 order by s;
s
1
2
select 1 from t group by c1 having sum(abs(c2 + c3)) = c1;
1
1
set @@tidb_opt_agg_push_down = default;
drop table if exists t;
create table t(a int primary key, b int);
select min(a), min(a) from t;
min(a) min(a)
NULL NULL
insert into t values(1, -1), (2, -2), (3, 1), (4, NULL);
select max(a) from t;
max(a)
4
select min(b) from t;
min(b)
-2
select max(b*b) from t;
max(b*b)
4
select min(b*b) from t;
min(b*b)
1
select group_concat(b, b) from t group by a;
group_concat(b, b)
NULL
-1-1
-2-2
11
drop table if exists t;
set @@tidb_enable_clustered_index = 1;
create table t (a int, b int, c int, primary key(a, b));
insert into t values (0, 0, 0);
insert into t values (1, 1, 1);
insert into t values (2, 2, 2);
insert into t values (3, 3, 3);
insert into t values (4, 4, 4);
insert into t values (5, 5, 5);
insert into t values (6, 6, 6);
insert into t values (7, 7, 7);
insert into t values (8, 8, 8);
insert into t values (9, 9, 9);
insert into t values (10, 10, 10);
select max(a), min(a+b) from t;
max(a) min(a+b)
10 0
select max(a+b), min(a+b) from t;
max(a+b) min(a+b)
20 0
select min(a), max(a), min(b), max(b) from t;
min(a) max(a) min(b) max(b)
0 10 0 10
set @@tidb_enable_clustered_index = default;
DROP TABLE IF EXISTS T;
CREATE TABLE T(A VARCHAR(10), B VARCHAR(10), C FLOAT);
INSERT INTO T VALUES('0', "val_b", 12.191);
SELECT MAX(CASE B WHEN 'val_b' THEN C ELSE 0 END) val_b FROM T WHERE cast(A as signed) = 0 GROUP BY a;
val_b
12.190999984741211
SELECT MIN(CASE B WHEN 'val_b' THEN C ELSE 0 END) val_b FROM T WHERE cast(A as signed) = 0 GROUP BY a;
val_b
12.190999984741211
drop table if exists t;
create table t (i int);
insert into t values (1), (1), (1),(2),(3),(2),(3),(2),(3);
select i+1 as a, count(i+2), sum(i+3), group_concat(i+4), bit_or(i+5) from t group by i, hex(i+6) order by a;
a count(i+2) sum(i+3) group_concat(i+4) bit_or(i+5)
2 3 12 5,5,5 6
3 3 15 6,6,6 7
4 3 18 7,7,7 8
drop table if exists t;
create table t(a enum('a', 'b'));
insert into t values('a');
select a from t group by a;
a
a
drop table if exists t;
create table t(a datetime, b json, index idx(a));
insert into t values('2019-03-20 21:50:00', '["a", "b", 1]');
insert into t values('2019-03-20 21:50:01', '["a", "b", 1]');
insert into t values('2019-03-20 21:50:02', '["a", "b", 1]');
insert into t values('2019-03-20 21:50:03', '{"k1": "value", "k2": [10, 20]}');
insert into t values('2019-03-20 21:50:04', '{"k1": "value", "k2": [10, 20]}');
insert into t values('2019-03-20 21:50:05', '{"k1": "value", "k2": [10, 20]}');
insert into t values('2019-03-20 21:50:06', '"hello"');
insert into t values('2019-03-20 21:50:07', '"hello"');
insert into t values('2019-03-20 21:50:08', '"hello"');
set @@sql_mode='';
select b from t group by a order by a;
b
["a", "b", 1]
["a", "b", 1]
["a", "b", 1]
{"k1": "value", "k2": [10, 20]}
{"k1": "value", "k2": [10, 20]}
{"k1": "value", "k2": [10, 20]}
"hello"
"hello"
"hello"
select min(b) from t group by a order by a;
min(b)
["a", "b", 1]
["a", "b", 1]
["a", "b", 1]
{"k1": "value", "k2": [10, 20]}
{"k1": "value", "k2": [10, 20]}
{"k1": "value", "k2": [10, 20]}
"hello"
"hello"
"hello"
select max(b) from t group by a order by a;
max(b)
["a", "b", 1]
["a", "b", 1]
["a", "b", 1]
{"k1": "value", "k2": [10, 20]}
{"k1": "value", "k2": [10, 20]}
{"k1": "value", "k2": [10, 20]}
"hello"
"hello"
"hello"
set @@sql_mode=default;
drop table if exists t;
create table t(a char(10), b char(10));
insert into t values('1', '222'), ('12', '22');
select count(distinct a, b) from t;
count(distinct a, b)
2
select approx_count_distinct( a, b) from t;
approx_count_distinct( a, b)
2
drop table if exists t;
create table t(a char(10), b char(10));
insert into t values('1', '222'), ('12', '22');
select group_concat(distinct a, b) from t;
group_concat(distinct a, b)
1222,1222
drop table if exists t, s;
create table t(a int);
create table s(a int, b int);
insert into s values(100292, 508931), (120002, 508932);
insert into t values(508931), (508932);
select (select /*+ stream_agg() */ group_concat(concat(123,'-')) from t where t.a = s.b group by t.a) as t from s;
t
123-
123-
select (select /*+ hash_agg() */ group_concat(concat(123,'-')) from t where t.a = s.b group by t.a) as t from s;
t
123-
123-
CREATE TABLE `t49`(`c0` char(1) DEFAULT '1', `c2` char(1) DEFAULT NULL, UNIQUE KEY `c2` (`c2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
INSERT INTO `t49` VALUES ('0','0'),('0','1');
CREATE TABLE `t0` (`c0` blob DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
INSERT INTO `t0` VALUES (_binary ']'),(_binary '777926278'),(_binary '0.2136404982804636'),(_binary '1901362489'),(_binary '1558203848'),(''),(_binary '1830406335'),(''),(_binary '0'),(NULL),(_binary '601930250'),(_binary '1558203848'),(_binary '-122008948'),(_binary '-2053608489'),(_binary 'hb/vt <7'),(_binary 'RC&2*'),(_binary '1'),(_binary '-1722334316'),(_binary '1830406335'),(_binary '1372126029'),(_binary '882291196'),(NULL),(_binary '-399693596');
CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `v0` (`c0`, `c1`, `c2`) AS SELECT NULL AS `NULL`,`t49`.`c2` AS `c2`,(((CASE _UTF8MB4'I되EkfIO퀶' WHEN NULL THEN `t49`.`c0` WHEN `t49`.`c2` THEN `t0`.`c0` ELSE (CASE `t49`.`c0` WHEN _UTF8MB4'%' THEN 1035293362 ELSE _UTF8MB4',' END) END))<<(`t49`.`c0`)) AS `(((CASE 'I되EkfIO퀶' WHEN NULL THEN t49.c0 WHEN t49.c2 THEN t0.c0 ELSE (CASE t49.c0 WHEN '%' THEN 1035293362 ELSE ',' END ) END ))<<(t49.c0))` FROM (`t0`) JOIN `t49` WHERE TRUE;
SELECT /*+ STREAM_AGG()*/v0.c0 FROM t49, v0 LEFT OUTER JOIN t0 ON ('Iw') GROUP BY true;
c0
NULL
drop table if exists t;
create table t(a json);
insert into t values ('{"id": 1,"score":23}');
insert into t values ('{"id": 2,"score":23}');
insert into t values ('{"id": 1,"score":233}');
insert into t values ('{"id": 2,"score":233}');
insert into t values ('{"id": 3,"score":233}');
set tidb_max_chunk_size = 2;
select max(JSON_EXTRACT(a, '$.score')) as max_score,JSON_EXTRACT(a,'$.id') as id from t group by id order by id;
max_score id
233 1
233 2
233 3
set tidb_max_chunk_size = default;
set tidb_max_chunk_size = 2;
drop table if exists t;
create table t(a int);
insert into t values(null),(null);
insert into t values(0),(2),(2),(4),(8);
select /*+ stream_agg() */ distinct * from t;
a
NULL
0
2
4
8
drop table if exists t;
create table t(a float);
insert into t values(null),(null),(null),(null);
insert into t values(1.1),(1.1);
select /*+ stream_agg() */ distinct * from t;
a
NULL
1.1
drop table if exists t;
create table t(a decimal(5,1));
insert into t values(null),(null),(null);
insert into t values(1.1),(2.2),(2.2);
select /*+ stream_agg() */ distinct * from t;
a
NULL
1.1
2.2
drop table if exists t;
create table t(a datetime);
insert into t values(null);
insert into t values("2019-03-20 21:50:00"),("2019-03-20 21:50:01"), ("2019-03-20 21:50:00");
select /*+ stream_agg() */ distinct * from t;
a
NULL
2019-03-20 21:50:00
2019-03-20 21:50:01
drop table if exists t;
create table t(a json);
insert into t values(null),(null),(null),(null);
select /*+ stream_agg() */ distinct * from t;
a
NULL
drop table if exists t;
create table t(a char);
insert into t values(null),(null),(null),(null);
insert into t values('a'),('b');
select /*+ stream_agg() */ distinct * from t;
a
NULL
a
b
set tidb_max_chunk_size = default;
set tidb_max_chunk_size = 2;
drop table if exists t;
create table t(y year);
insert into t values (2020), (2000), (2050);
select sum(y) from t;
sum(y)
6070
select avg(y) from t;
avg(y)
2023.3333
set tidb_max_chunk_size = default;
drop table if exists t1;
CREATE TABLE t1 (
pk int(11) NOT NULL,
col1 decimal(40,20) DEFAULT NULL
);
INSERT INTO t1 VALUES (2084,0.02040000000000000000),(35324,0.02190000000000000000),(43760,0.00510000000000000000),(46084,0.01400000000000000000),(46312,0.00560000000000000000),(61632,0.02730000000000000000),(94676,0.00660000000000000000),(102244,0.01810000000000000000),(113144,0.02140000000000000000),(157024,0.02750000000000000000),(157144,0.01750000000000000000),(182076,0.02370000000000000000),(188696,0.02330000000000000000),(833,0.00390000000000000000),(6701,0.00230000000000000000),(8533,0.01690000000000000000),(13801,0.01360000000000000000),(20797,0.00680000000000000000),(36677,0.00550000000000000000),(46305,0.01290000000000000000),(76113,0.00430000000000000000),(76753,0.02400000000000000000),(92393,0.01720000000000000000),(111733,0.02690000000000000000),(152757,0.00250000000000000000),(162393,0.02760000000000000000),(167169,0.00440000000000000000),(168097,0.01360000000000000000),(180309,0.01720000000000000000),(19918,0.02620000000000000000),(58674,0.01820000000000000000),(67454,0.01510000000000000000),(70870,0.02880000000000000000),(89614,0.02530000000000000000),(106742,0.00180000000000000000),(107886,0.01580000000000000000),(147506,0.02230000000000000000),(148366,0.01340000000000000000),(167258,0.01860000000000000000),(194438,0.00500000000000000000),(10307,0.02850000000000000000),(14539,0.02210000000000000000),(27703,0.00050000000000000000),(32495,0.00680000000000000000),(39235,0.01450000000000000000),(52379,0.01640000000000000000),(54551,0.01910000000000000000),(85659,0.02330000000000000000),(104483,0.02670000000000000000),(109911,0.02040000000000000000),(114523,0.02110000000000000000),(119495,0.02120000000000000000),(137603,0.01910000000000000000),(154031,0.02580000000000000000);
SELECT count(distinct col1) FROM t1;
count(distinct col1)
48
drop table if exists t;
create table t(a tinyint(1));
insert into t values (-120), (127);
select avg(a) from t group by a;
avg(a)
-120.0000
127.0000
drop table t;
create table t(a smallint(1));
insert into t values (-120), (127);
select avg(a) from t group by a;
avg(a)
-120.0000
127.0000
drop table t;
create table t(a mediumint(1));
insert into t values (-120), (127);
select avg(a) from t group by a;
avg(a)
-120.0000
127.0000
drop table t;
create table t(a int(1));
insert into t values (-120), (127);
select avg(a) from t group by a;
avg(a)
-120.0000
127.0000
drop table t;
create table t(a bigint(1));
insert into t values (-120), (127);
select avg(a) from t group by a;
avg(a)
-120.0000
127.0000
drop table t;
drop table if exists td;
create table td (col_bigint bigint(20), col_smallint smallint(6));
insert into td values (null, 22876);
insert into td values (9220557287087669248, 32767);
insert into td values (28030, 32767);
insert into td values (-3309864251140603904,32767);
insert into td values (4,0);
insert into td values (null,0);
insert into td values (4,-23828);
insert into td values (54720,32767);
insert into td values (0,29815);
insert into td values (10017,-32661);
SELECT AVG( col_bigint / col_smallint) AS field1 FROM td;
field1
25769363061037.62077260
SELECT AVG(col_bigint) OVER (PARTITION BY col_smallint) as field2 FROM td where col_smallint = -23828;
field2
4.0000
drop table td;
drop table if exists t1;
create table t1(col1 time(2) NOT NULL);
insert into t1 values("16:40:20.01");
select col1 from t1 group by col1;
col1
16:40:20.01
drop table if exists t100;
set @@tidb_partition_prune_mode = 'static';
CREATE TABLE t100 (
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
col1 int(10) NOT NULL DEFAULT '0' COMMENT 'test',
money bigint(20) NOT NULL COMMENT 'test',
logtime datetime NOT NULL COMMENT '记录时间',
PRIMARY KEY (ID,logtime)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 COMMENT='test'
PARTITION BY RANGE COLUMNS(logtime) (
PARTITION p20220608 VALUES LESS THAN ("20220609"),
PARTITION p20220609 VALUES LESS THAN ("20220610"),
PARTITION p20220610 VALUES LESS THAN ("20220611"),
PARTITION p20220611 VALUES LESS THAN ("20220612"),
PARTITION p20220612 VALUES LESS THAN ("20220613"),
PARTITION p20220613 VALUES LESS THAN ("20220614"),
PARTITION p20220614 VALUES LESS THAN ("20220615"),
PARTITION p20220615 VALUES LESS THAN ("20220616"),
PARTITION p20220616 VALUES LESS THAN ("20220617"),
PARTITION p20220617 VALUES LESS THAN ("20220618"),
PARTITION p20220618 VALUES LESS THAN ("20220619"),
PARTITION p20220619 VALUES LESS THAN ("20220620"),
PARTITION p20220620 VALUES LESS THAN ("20220621"),
PARTITION p20220621 VALUES LESS THAN ("20220622"),
PARTITION p20220622 VALUES LESS THAN ("20220623"),
PARTITION p20220623 VALUES LESS THAN ("20220624"),
PARTITION p20220624 VALUES LESS THAN ("20220625")
);
insert into t100(col1,money,logtime) values (100,10,'2022-06-09 00:00:00');
insert into t100(col1,money,logtime) values (100,10,'2022-06-10 00:00:00');
SELECT /*+STREAM_AGG()*/ col1,sum(money) FROM t100 WHERE logtime>='2022-06-09 00:00:00' AND col1=100 ;
col1 sum(money)
100 20
SELECT /*+HASH_AGG()*/ col1,sum(money) FROM t100 WHERE logtime>='2022-06-09 00:00:00' AND col1=100 ;
col1 sum(money)
100 20
set @@tidb_partition_prune_mode = default;
drop table if exists t;
create table t(nname char(20));
insert into t values ('2'),(null),('11'),('2'),(null),('2'),(null),('11'),('33');
set @@group_concat_max_len=0;
select group_concat(nname order by 1 separator '#' ) from t;
group_concat(nname order by 1 separator '#' )
11#1
select group_concat(nname order by 1 desc separator '#' ) from t;
group_concat(nname order by 1 desc separator '#' )
33#2
set @@group_concat_max_len=default;
DROP TABLE IF EXISTS customer, orders;
CREATE TABLE `customer` ( `C_CUSTKEY` bigint(20) NOT NULL, `C_NAME` varchar(25) NOT NULL, `C_ADDRESS` varchar(40) NOT NULL, `C_NATIONKEY` bigint(20) NOT NULL, `C_PHONE` char(15) NOT NULL, `C_ACCTBAL` decimal(15,2) NOT NULL, `C_MKTSEGMENT` char(10) NOT NULL, `C_COMMENT` varchar(117) NOT NULL, PRIMARY KEY (`C_CUSTKEY`) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `orders` ( `O_ORDERKEY` bigint(20) NOT NULL, `O_CUSTKEY` bigint(20) NOT NULL, `O_ORDERSTATUS` char(1) NOT NULL, `O_TOTALPRICE` decimal(15,2) NOT NULL, `O_ORDERDATE` date NOT NULL, `O_ORDERPRIORITY` char(15) NOT NULL, `O_CLERK` char(15) NOT NULL, `O_SHIPPRIORITY` bigint(20) NOT NULL, `O_COMMENT` varchar(79) NOT NULL, PRIMARY KEY (`O_ORDERKEY`) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
set tidb_opt_agg_push_down=ON;
explain format='plan_tree' SELECT /*+ hash_join_build(customer) */ c_custkey, count(o_orderkey) as c_count from customer left join orders on c_custkey = o_custkey and o_comment not like '%special%requests%' group by c_custkey;
id task access object operator info
Projection root executor__aggregate.customer.c_custkey, Column
└─HashAgg root group by:executor__aggregate.customer.c_custkey, funcs:count(Column)->Column, funcs:firstrow(executor__aggregate.customer.c_custkey)->executor__aggregate.customer.c_custkey
└─HashJoin root left outer join, left side:TableReader, equal:[eq(executor__aggregate.customer.c_custkey, executor__aggregate.orders.o_custkey)]
├─TableReader(Build) root data:TableFullScan
│ └─TableFullScan cop[tikv] table:customer keep order:false, stats:pseudo
└─HashAgg(Probe) root group by:executor__aggregate.orders.o_custkey, funcs:count(Column)->Column, funcs:firstrow(executor__aggregate.orders.o_custkey)->executor__aggregate.orders.o_custkey
└─TableReader root data:HashAgg
└─HashAgg cop[tikv] group by:executor__aggregate.orders.o_custkey, funcs:count(executor__aggregate.orders.o_orderkey)->Column
└─Selection cop[tikv] not(like(executor__aggregate.orders.o_comment, "%special%requests%", 92))
└─TableFullScan cop[tikv] table:orders keep order:false, stats:pseudo
set tidb_opt_agg_push_down=default;
SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (c1 ENUM('a', '', 'b'));
INSERT INTO t1 (c1) VALUES ('b');
INSERT INTO t1 (c1) VALUES ('');
INSERT INTO t1 (c1) VALUES ('a');
INSERT INTO t1 (c1) VALUES ('');
INSERT INTO t1 (c1) VALUES (0);
select * from t1;
c1
b
a
select c1 + 0 from t1;
c1 + 0
0
1
2
2
3
SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;
c1 + 0 COUNT(c1)
0 1
1 1
2 2
3 1
alter table t1 add index idx(c1);
select c1 + 0 from t1;
c1 + 0
0
1
2
2
3
SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;
c1 + 0 COUNT(c1)
0 1
1 1
2 2
3 1
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (c1 ENUM('a', 'b', 'c'));
INSERT INTO t1 (c1) VALUES ('b');
INSERT INTO t1 (c1) VALUES ('a');
INSERT INTO t1 (c1) VALUES ('b');
INSERT INTO t1 (c1) VALUES ('c');
INSERT INTO t1 (c1) VALUES (0);
select * from t1;
c1
b
a
b
c
SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;
c1 + 0 COUNT(c1)
0 1
1 1
2 2
3 1
SET sql_mode = default;
set @@tidb_hash_join_concurrency=1;
set sql_mode='STRICT_TRANS_TABLES';
drop table if exists t;
create table t (c int, d int);
insert t values (NULL, 1);
insert t values (1, 1);
insert t values (1, 2);
insert t values (1, 3);
insert t values (1, 1);
insert t values (3, 2);
insert t values (4, 3);
select bit_and(c) from t where NULL;
bit_and(c)
18446744073709551615
select bit_or(c) from t where NULL;
bit_or(c)
0
select bit_xor(c) from t where NULL;
bit_xor(c)
0
select approx_count_distinct(c) from t where NULL;
approx_count_distinct(c)
0
select count(*) from t;
count(*)
7
select count(*) from t group by d order by c;
count(*)
3
2
2
select distinct 99 from t group by d having d > 0;
99
99
select count(*) from t having 1 = 0;
count(*)
select c,d from t group by d order by d;
c d
NULL 1
1 2
1 3
select - c, c as d from t group by c having null not between c and avg(distinct d) - d;
- c d
select - c as c from t group by c having t.c > 5;
c
select t1.c from t t1, t t2 group by c having c > 5;
c
select count(*) from (select d, c from t) k where d != 0 group by d order by c;
count(*)
3
2
2
select c as a from t group by d having a < 0;
a
select c as a from t group by d having sum(a) = 2;
a
NULL
select count(distinct c) from t group by d order by c;
count(distinct c)
1
2
2
select approx_count_distinct(c) from t group by d order by c;
approx_count_distinct(c)
1
2
2
select sum(c) as a from t group by d order by a;
a
2
4
5
select sum(c) as a, sum(c+1), sum(c), sum(c+1) from t group by d order by a;
a sum(c+1) sum(c) sum(c+1)
2 4 2 4
4 6 4 6
5 7 5 7
select count(distinct c,d) from t;
count(distinct c,d)
5
select approx_count_distinct(c,d) from t;
approx_count_distinct(c,d)
5
select count(c,d) from t;
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 15 near ",d) from t"
select d*2 as ee, sum(c) from t group by ee order by ee;
ee sum(c)
2 2
4 4
6 5
select sum(distinct c) as a from t group by d order by a;
a
1
4
5
select min(c) as a from t group by d order by a;
a
1
1
1
select max(c) as a from t group by d order by a;
a
1
3
4
select avg(c) as a from t group by d order by a;
a
1.0000
2.0000
2.5000
select c, approx_count_distinct(d) as a from t group by c order by a, c;
c a
NULL 1
3 1
4 1
1 3
select d, d + 1 from t group by d order by d;
d d + 1
1 2
2 3
3 4
select count(*) from t;
count(*)
7
select count(distinct d) from t;
count(distinct d)
3
select approx_count_distinct(d) from t;
approx_count_distinct(d)
3
select count(*) as a from t group by d having sum(c) > 3 order by a;
a
2
2
select max(c) from t group by d having sum(c) > 3 order by avg(c) desc;
max(c)
4
3
select sum(-1) from t a left outer join t b on not null is null;
sum(-1)
-7
select count(*), b.d from t a left join t b on a.c = b.d group by b.d order by b.d;
count(*) d
2 NULL
12 1
2 3
select count(b.d), b.d from t a left join t b on a.c = b.d group by b.d order by b.d;
count(b.d) d
0 NULL
12 1
2 3
select count(b.d), b.d from t b right join t a on a.c = b.d group by b.d order by b.d;
count(b.d) d
0 NULL
12 1
2 3
select count(*), b.d from t b right join t a on a.c = b.d group by b.d order by b.d;
count(*) d
2 NULL
12 1
2 3
select max(case when b.d is null then 10 else b.c end), b.d from t b right join t a on a.c = b.d group by b.d order by b.d;
max(case when b.d is null then 10 else b.c end) d
10 NULL
1 1
4 3
select count(*) from t a , t b;
count(*)
49
select count(*) from t a , t b, t c;
count(*)
343
select count(*) from t a , t b where a.c = b.d;
count(*)
14
select count(a.d), sum(b.c) from t a , t b where a.c = b.d order by a.d;
count(a.d) sum(b.c)
14 13
select count(*) from t a , t b, t c where a.c = b.d and b.d = c.d;
count(*)
40
select count(*), a.c from t a , t b, t c where a.c = b.d and b.d = c.d group by c.d order by a.c;
count(*) c
36 1
4 3
select count(a.c), c.d from t a , t b, t c where a.c = b.d and b.d = c.d group by c.d order by c.d;
count(a.c) d
36 1
4 3
select count(*) from t a , t b where a.c = b.d and a.c + b.d = 2;
count(*)
12
select count(*) from t a join t b having sum(a.c) < 0;
count(*)
select count(*) from t a join t b where a.c < 0;
count(*)
0
select sum(b.c), count(b.d), a.c from t a left join t b on a.c = b.d group by b.d order by b.d;
sum(b.c) count(b.d) c
NULL 0 NULL
8 12 1
5 2 3
select 1-d as d from t having d < 0 order by d desc;
d
-1
-1
-2
-2
select 1-d as d from t having d + 1 < 0 order by d + 1;
d
-2
-2
drop table if exists t;
create table t (keywords varchar(20), type int);
insert into t values('测试', 1), ('test', 2);
select group_concat(keywords) from t group by type order by type;
group_concat(keywords)
测试
test
drop table if exists t;
create table t (c int, d int);
insert t values (1, -1);
insert t values (1, 0);
insert t values (1, 1);
select d, d*d as d from t having d = -1;
d d
select d*d as d from t group by d having d = -1;
d
1
select d, 1-d as d, c as d from t order by d;
d d d
1 0 1
0 1 1
-1 2 1
select d, 1-d as d, c as d from t order by d+1;
d d d
-1 2 1
0 1 1
1 0 1
select d, 1-d as d, c as d from t group by d order by d;
d d d
1 0 1
0 1 1
-1 2 1
select d as d1, t.d as d1, 1-d as d1, c as d1 from t having d1 < 10 order by d;
d1 d1 d1 d1
-1 -1 2 1
0 0 1 1
1 1 0 1
select d*d as d1, c as d1 from t group by d1 order by d1;
d1 d1
0 1
1 1
select d*d as d1, c as d1 from t group by 2;
d1 d1
1 1
select * from t group by 2 order by d;
c d
1 -1
1 0
1 1
select * , sum(d) from t group by 1 order by d;
c d sum(d)
1 -1 0
select sum(d), t.* from t group by 2 order by d;
sum(d) c d
0 1 -1
select d as d, c as d from t group by d + 1 order by t.d;
d d
-1 1
0 1
1 1
select c as d, c as d from t group by d order by d;
d d
1 1
1 1
1 1
select d as d, c as d from t group by d;
Error 1052 (23000): Column 'd' in group statement is ambiguous
select t.d, c as d from t group by d;
Error 1052 (23000): Column 'd' in group statement is ambiguous
select *, c+1 as d from t group by 3 order by d;
c d d
1 -1 2
drop table if exists t1;
create table t1(a float, b int default 3);
insert into t1 (a) values (2), (11), (8);
select min(a), min(case when 1=1 then a else NULL end), min(case when 1!=1 then NULL else a end) from t1 where b=3 group by b;
min(a) min(case when 1=1 then a else NULL end) min(case when 1!=1 then NULL else a end)
2 2 2
drop table if exists t1;
create table t1(a int, index(a));
insert into t1 (a) values (1),(2),(3),(4),(5);
select count(a) from t1 where a < 3;
count(a)
2
drop table if exists t1;
create table t1(a int, b int, index(a));
select sum(b) from (select * from t1) t group by a;
sum(b)
select sum(b) from (select * from t1) t;
sum(b)
NULL
insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4),(3, 5);
select avg(b) from (select * from t1) t group by a order by a;
avg(b)
2.5000
2.0000
4.0000
select sum(b) from (select * from t1) t group by a order by a;
sum(b)
5
2
8
select count(b) from (select * from t1) t group by a order by a;
count(b)
2
1
2
select max(b) from (select * from t1) t group by a order by a;
max(b)
4
2
5
select min(b) from (select * from t1) t group by a order by a;
min(b)
1
2
3
drop table if exists t1;
create table t1(a int, b int, index(a,b));
insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3);
select avg(distinct b) from (select * from t1) t group by a order by a;
avg(distinct b)
2.5000
2.0000
4.0000
select sum(distinct b) from (select * from t1) t group by a order by a;
sum(distinct b)
5
2
8
select count(distinct b) from (select * from t1) t group by a order by a;
count(distinct b)
2
1
2
select approx_count_distinct(b) from (select * from t1) t group by a order by a;
approx_count_distinct(b)
2
1
2
select max(distinct b) from (select * from t1) t group by a order by a;
max(distinct b)
4
2
5
select min(distinct b) from (select * from t1) t group by a order by a;
min(distinct b)
1
2
3
drop table if exists t1;
create table t1(a int, b int, index(b, a));
insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3);
select avg(distinct b) from (select * from t1) t group by a order by a;
avg(distinct b)
2.5000
2.0000
4.0000
select sum(distinct b) from (select * from t1) t group by a order by a;
sum(distinct b)
5
2
8
select count(distinct b) from (select * from t1) t group by a order by a;
count(distinct b)
2
1
2
select max(distinct b) from (select * from t1) t group by a order by a;
max(distinct b)
4
2
5
select min(distinct b) from (select * from t1) t group by a order by a;
min(distinct b)
1
2
3
drop table if exists t;
create table t (id int primary key, ds date);
insert into t (id, ds) values (1, "1991-09-05"),(2,"1991-09-05"), (3, "1991-09-06"),(0,"1991-09-06");
select sum(id), ds from t group by ds order by id;
sum(id) ds
3 1991-09-06
3 1991-09-05
drop table if exists t1;
drop table if exists t2;
create table t1 (col0 int, col1 int);
create table t2 (col0 int, col1 int);
insert into t1 values(83, 0), (26, 0), (43, 81);
insert into t2 values(22, 2), (3, 12), (38, 98);
SELECT COALESCE ( + 1, cor0.col0 ) + - CAST( NULL AS DECIMAL ) FROM t2, t1 AS cor0, t2 AS cor1 GROUP BY cor0.col1;
COALESCE ( + 1, cor0.col0 ) + - CAST( NULL AS DECIMAL )
NULL
NULL
drop table if exists t1;
drop table if exists t2;
create table t1 (c1 int);
create table t2 (c1 int);
insert into t1 values(3), (2);
insert into t2 values(1), (2);
set @@session.tidb_opt_insubq_to_join_and_agg = 0;
select sum(c1 in (select * from t2)) from t1;
sum(c1 in (select * from t2))
1
set @@session.tidb_opt_insubq_to_join_and_agg = 1;
select sum(c1 in (select * from t2)) from t1;
sum(c1 in (select * from t2))
1
select sum(c1) k from (select * from t1 union all select * from t2)t group by c1 * 2 order by k;
k
1
3
4
drop table if exists t;
create table t (a int, b int, c int);
insert into t values(1, 2, 3), (1, 2, 4);
select count(distinct c), count(distinct a,b) from t;
count(distinct c) count(distinct a,b)
2 1
select approx_count_distinct( c), approx_count_distinct( a,b) from t;
approx_count_distinct( c) approx_count_distinct( a,b)
2 1
drop table if exists t;
create table t (a float);
insert into t values(966.36), (363.97), (569.99), (453.33), (376.45), (321.93), (12.12), (45.77), (9.66), (612.17);
select distinct count(distinct a) from t;
count(distinct a)
10
select distinct approx_count_distinct( a) from t;
approx_count_distinct( a)
10
create table idx_agg (a int, b int, index (b));
insert idx_agg values (1, 1), (1, 2), (2, 2);
select sum(a), sum(b) from idx_agg where b > 0 and b < 10;
sum(a) sum(b)
4 5
select 10 from idx_agg group by b;
10
10
10
select 11 from idx_agg group by a;
11
11
11
set @@tidb_init_chunk_size=1;
select group_concat(b) from idx_agg group by b;
group_concat(b)
1
2,2
set @@tidb_init_chunk_size=2;
drop table if exists t;
create table t(a int(11), b decimal(15,2));
insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97);
select a, max(b) from t group by a order by a limit 2;
a max(b)
1 771.64
2 378.49
drop table if exists t;
create table t(a int(11), b char(15));
insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97);
select a, max(b) from t group by a order by a limit 2;
a max(b)
1 771.64
2 378.49
drop table if exists t;
create table t (id int(11) NOT NULL, tags json DEFAULT NULL);
insert into t values (1, '{"i": 1, "n": "n1"}');
insert into t values (2, '{"i": 2, "n": "n2"}');
insert into t values (3, '{"i": 3, "n": "n3"}');
insert into t values (4, '{"i": 4, "n": "n4"}');
insert into t values (5, '{"i": 5, "n": "n5"}');
insert into t values (6, '{"i": 0, "n": "n6"}');
insert into t values (7, '{"i": -1, "n": "n7"}');
select sum(tags->'$.i') from t;
sum(tags->'$.i')
14
select id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95) from t where null;
id count(95) sum(95) avg(95) bit_or(95) bit_and(95) bit_or(95) max(95) min(95) group_concat(95)
NULL 0 NULL NULL 0 18446744073709551615 0 NULL NULL NULL
truncate table t;
drop table if exists s;
create table s(id int);
select t.id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95), approx_count_distinct(95) from t left join s on t.id = s.id;
id count(95) sum(95) avg(95) bit_or(95) bit_and(95) bit_or(95) max(95) min(95) group_concat(95) approx_count_distinct(95)
NULL 0 NULL NULL 0 18446744073709551615 0 NULL NULL NULL 0
insert into t values (1, '{"i": 1, "n": "n1"}');
select t.id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95), approx_count_distinct(95) from t left join s on t.id = s.id;
id count(95) sum(95) avg(95) bit_or(95) bit_and(95) bit_or(95) max(95) min(95) group_concat(95) approx_count_distinct(95)
1 1 95 95.0000 95 95 95 95 95 95 1
set @@tidb_hash_join_concurrency=5;
drop table t;
CREATE TABLE `t` (`a` bit(1) NOT NULL, PRIMARY KEY (`a`));
insert into t value(1), (0);
select a from t group by 1;
a

select max(a) from t group by a order by a;
max(a)

select cast(a as signed) as idx, cast(max(a) as signed), cast(min(a) as signed) from t group by 1 order by idx;
idx cast(max(a) as signed) cast(min(a) as signed)
0 0 0
1 1 1
drop table t;
create table t(a int, b int);
insert into t value(null, null);
select group_concat(a), group_concat(distinct a) from t;
group_concat(a) group_concat(distinct a)
NULL NULL
insert into t value(1, null), (null, 1), (1, 2), (3, 4);
select group_concat(a, b), group_concat(distinct a,b) from t;
group_concat(a, b) group_concat(distinct a,b)
12,34 12,34
set @@session.tidb_opt_distinct_agg_push_down = 0;
select count(distinct a) from t;
count(distinct a)
2
set @@session.tidb_opt_distinct_agg_push_down = 1;
select count(distinct a) from t;
count(distinct a)
2
set @@session.tidb_opt_distinct_agg_push_down = 0;
select approx_count_distinct( a) from t;
approx_count_distinct( a)
2
drop table t;
create table t(a decimal(10, 4));
select 10 from t group by a;
10
insert into t value(0), (-0.9871), (-0.9871);
select 10 from t group by a;
10
10
10
select sum(a) from (select a from t union all select a from t) tmp;
sum(a)
-3.9484
drop table t;
create table t(a tinyint, b smallint, c mediumint, d int, e bigint, f float, g double, h decimal);
insert into t values(1, 2, 3, 4, 5, 6.1, 7.2, 8.3), (1, 3, 4, 5, 6, 7.1, 8.2, 9.3);
select var_pop(b), var_pop(c), var_pop(d), var_pop(e), var_pop(f), var_pop(g), var_pop(h) from t group by a;
var_pop(b) var_pop(c) var_pop(d) var_pop(e) var_pop(f) var_pop(g) var_pop(h)
0.25 0.25 0.25 0.25 0.25 0.25 0.25
insert into t values(2, 3, 4, 5, 6, 7.2, 8.3, 9);
select a, var_pop(b) over w, var_pop(c) over w from t window w as (partition by a);
a var_pop(b) over w var_pop(c) over w
1 0.25 0.25
1 0.25 0.25
2 0 0
delete from t where t.a = 2;
insert into t values(1, 2, 4, 5, 6, 6.1, 7.2, 9);
select a, var_pop(distinct b), var_pop(distinct c), var_pop(distinct d), var_pop(distinct e), var_pop(distinct f), var_pop(distinct g), var_pop(distinct h) from t group by a;
a var_pop(distinct b) var_pop(distinct c) var_pop(distinct d) var_pop(distinct e) var_pop(distinct f) var_pop(distinct g) var_pop(distinct h)
1 0.25 0.25 0.25 0.25 0.25 0.25 0.25
drop table t;
create table t(a int, b bigint, c float, d double, e decimal);
insert into t values(1, 1000, 6.8, 3.45, 8.3), (1, 3998, -3.4, 5.12, 9.3),(1, 288, 9.2, 6.08, 1);
select variance(b), variance(c), variance(d), variance(e) from t group by a;
variance(b) variance(c) variance(d) variance(e)
2584338.6666666665 29.840000178019228 1.1808222222222229 12.666666666666666
insert into t values(1, 255, 6.8, 6.08, 1);
select variance(distinct b), variance(distinct c), variance(distinct d), variance(distinct e) from t group by a;
variance(distinct b) variance(distinct c) variance(distinct d) variance(distinct e)
2364075.6875 29.840000178019228 1.1808222222222229 12.666666666666666
insert into t values(2, 322, 0.8, 2.22, 6);
select a, variance(b) over w from t window w as (partition by a);
a variance(b) over w
1 2364075.6875
1 2364075.6875
1 2364075.6875
1 2364075.6875
2 0
select std_samp(a) from t;
Error 1305 (42000): FUNCTION executor__aggregate.std_samp does not exist
drop table if exists t1;
create table t1 (a int, b int generated always as (-a) virtual, c int generated always as (-a) stored);
insert into t1 (a) values (2), (1), (1), (3), (NULL);
select sum(a) from t1 group by b order by b;
sum(a)
NULL
3
2
2
select sum(a) from t1 group by c order by c;
sum(a)
NULL
3
2
2
select sum(b) from t1 group by a order by a;
sum(b)
NULL
-2
-2
-3
select sum(b) from t1 group by c order by c;
sum(b)
NULL
-3
-2
-2
select sum(c) from t1 group by a order by a;
sum(c)
NULL
-2
-2
-3
select sum(c) from t1 group by b order by b;
sum(c)
NULL
-3
-2
-2
drop table if exists t1;
create table t1 (grp int, a bigint unsigned, c char(10) not null);
insert into t1 values (1,1,"a");
insert into t1 values (2,2,"b");
insert into t1 values (2,3,"c");
insert into t1 values (3,4,"E");
insert into t1 values (3,5,"C");
insert into t1 values (3,6,"D");
select stddev_pop(all a) from t1;
stddev_pop(all a)
1.707825127659933
select stddev_pop(a) from t1 group by grp order by grp;
stddev_pop(a)
0
0.5
0.816496580927726
select sum(a)+count(a)+avg(a)+stddev_pop(a) as sum from t1 group by grp order by grp;
sum
3
10
23.816496580927726
select std(all a) from t1;
std(all a)
1.707825127659933
select std(a) from t1 group by grp order by grp;
std(a)
0
0.5
0.816496580927726
select sum(a)+count(a)+avg(a)+std(a) as sum from t1 group by grp order by grp;
sum
3
10
23.816496580927726
select stddev(all a) from t1;
stddev(all a)
1.707825127659933
select stddev(a) from t1 group by grp order by grp;
stddev(a)
0
0.5
0.816496580927726
select sum(a)+count(a)+avg(a)+stddev(a) as sum from t1 group by grp order by grp;
sum
3
10
23.816496580927726
drop table if exists t1;
CREATE TABLE t1 (a int, b int);
select stddev_pop(b) from t1;
stddev_pop(b)
NULL
select std(b) from t1;
std(b)
NULL
select stddev(b) from t1;
stddev(b)
NULL
insert into t1 values (1,null);
select stddev_pop(b) from t1 group by a order by a;
stddev_pop(b)
NULL
select std(b) from t1 group by a order by a;
std(b)
NULL
select stddev(b) from t1 group by a order by a;
stddev(b)
NULL
insert into t1 values (1,null);
insert into t1 values (2,null);
select stddev_pop(b) from t1 group by a order by a;
stddev_pop(b)
NULL
NULL
select std(b) from t1 group by a order by a;
std(b)
NULL
NULL
select stddev(b) from t1 group by a order by a;
stddev(b)
NULL
NULL
insert into t1 values (2,1);
select stddev_pop(b) from t1 group by a order by a;
stddev_pop(b)
NULL
0
select std(b) from t1 group by a order by a;
std(b)
NULL
0
select stddev(b) from t1 group by a order by a;
stddev(b)
NULL
0
insert into t1 values (3,1);
select stddev_pop(b) from t1 group by a order by a;
stddev_pop(b)
NULL
0
0
select std(b) from t1 group by a order by a;
std(b)
NULL
0
0
select stddev(b) from t1 group by a order by a;
stddev(b)
NULL
0
0
drop table if exists t1;
CREATE TABLE t1 (id int(11),value1 float(10,2));
INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00), (2,13.00);
select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id order by id;
id stddev_pop(value1) var_pop(value1) stddev_samp(value1) var_samp(value1)
1 0.816496580927726 0.6666666666666666 1 1
2 1.118033988749895 1.25 1.2909944487358056 1.6666666666666667
drop table if exists t1;
CREATE TABLE t1 (id int);
insert into t1 values (1),(2);
select stddev_pop(id) from t1;
stddev_pop(id)
0.5
insert into t1 values (1);
select stddev_pop(distinct id) from t1;
stddev_pop(distinct id)
0.5
set @@tidb_hash_join_concurrency=default;
set sql_mode=default;
set @@session.tidb_opt_insubq_to_join_and_agg = default;
set @@tidb_init_chunk_size=default;
set @@session.tidb_opt_distinct_agg_push_down = default;
set sql_mode = 'ONLY_FULL_GROUP_BY';
set @@session.tidb_enable_new_only_full_group_by_check = 'on';
drop table if exists t, x;
create table t(a int not null primary key, b int not null, c int default null, d int not null, unique key I_b_c (b,c), unique key I_b_d (b,d));
create table x(a int not null primary key, b int not null, c int default null, d int not null, unique key I_b_c (b,c), unique key I_b_d (b,d));
select max(a) from t group by d;
max(a)
select max(a), any_value(c) from t group by d;
max(a) any_value(c)
select * from t group by d;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select b-c from t group by b+c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select (b-c)*(b+c), min(a) from t group by b+c, b-c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select b between c and d from t group by b,c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select case b when 1 then c when 2 then d else d end from t group by b,c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c > (select b from t) from t group by b;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c is null from t group by b;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c is true from t group by b;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select (c+b)*d from t group by c,d;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select b in (c,d) from t group by b,c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select b like '%a' from t group by c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c REGEXP '1.*' from t group by b;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select -b from t group by c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select a, max(b) from t;
Error 8123 (HY000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'executor__aggregate.t.a'; this is incompatible with sql_mode=only_full_group_by
select sum(a)+b from t;
Error 8123 (HY000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'executor__aggregate.t.b'; this is incompatible with sql_mode=only_full_group_by
select count(b), c from t;
Error 8123 (HY000): In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'executor__aggregate.t.c'; this is incompatible with sql_mode=only_full_group_by
select count(b), any_value(c) from t;
count(b) any_value(c)
0 NULL
select count(b), any_value(c) + 2 from t;
count(b) any_value(c) + 2
0 NULL
select distinct a, b, count(a) from t;
Error 8123 (HY000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'executor__aggregate.t.a'; this is incompatible with sql_mode=only_full_group_by
select a from t group by a,b,c;
a
select b from t group by b;
b
select b*rand() from t group by b;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select b as e from t group by b;
e
select b+c from t group by b+c;
b+c
select b+c, min(a) from t group by b+c, b-c;
b+c min(a)
select b+c, min(a) from t group by b, c;
b+c min(a)
select b+c from t group by b,c;
b+c
select b+c from (select b, b+rand() as c from t) t group by b;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column '' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select b between c and d from t group by b,c,d;
b between c and d
select case b when 1 then c when 2 then d else d end from t group by b,c,d;
case b when 1 then c when 2 then d else d end
select c > (select b from t) from t group by c;
c > (select b from t)
select exists (select * from t) from t group by d;
exists (select * from t)
select c is null from t group by c;
c is null
select c is true from t group by c;
c is true
select (c+b)*d from t group by c,b,d;
(c+b)*d
select b in (c,d) from t group by b,c,d;
b in (c,d)
select b like '%a' from t group by b;
b like '%a'
select c REGEXP '1.*' from t group by c;
c REGEXP '1.*'
select -b from t group by b;
-b
select max(a+b) from t;
max(a+b)
NULL
select avg(a)+1 from t;
avg(a)+1
NULL
select count(c), 5 from t;
count(c) 5
0 5
select * from t group by a;
a b c d
select * from t group by b,d;
a b c d
select * from t group by b,c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select * from t where c = d group by b, c;
a b c d
select t.*, x.* from t, x where t.a = x.a group by t.a;
a b c d a b c d
select t.*, x.* from t, x where t.b = x.b and t.d = x.d group by t.b, t.d;
a b c d a b c d
select t.*, x.* from t, x where t.b = x.a group by t.b, t.d;
a b c d a b c d
select t.b, x.* from t, x where t.b = x.a group by t.b;
b a b c d
select t.*, x.* from t, x where t.c = x.a group by t.b, t.c;
a b c d a b c d
select t.*, x.* from t inner join x on t.a = x.a group by t.a;
a b c d a b c d
select t.*, x.* from t inner join x on (t.b = x.b and t.d = x.d) group by t.b, x.d;
a b c d a b c d
select t.b, x.* from t inner join x on t.b = x.b group by t.b, x.d;
b a b c d
select t.b, x.* from t left join x on t.b = x.b group by t.b, x.d;
b a b c d
select t.b, x.* from t left join x on x.b = t.b group by t.b, x.d;
b a b c d
select x.b, t.* from t right join x on x.b = t.b group by x.b, t.d;
b a b c d
select x.b, t.* from t right join x on t.b = x.b group by x.b, t.d;
b a b c d
select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d;
Error 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.x.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d;
Error 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.x.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select * from (select * from t) as e group by a;
a b c d
select * from (select * from t) as e group by b,d;
a b c d
select * from (select * from t) as e group by b,c;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.e.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c from t group by c,d order by d;
c
select c from t group by c order by d;
Error 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c from t,x group by t.c;
Error 1052 (23000): Column 'c' in field list is ambiguous
set sql_mode = default;
set @@session.tidb_enable_new_only_full_group_by_check = default;
set sql_mode = 'ONLY_FULL_GROUP_BY';
drop table if exists t1;
create table t1(
id int(11) NOT NULL PRIMARY KEY,
c1 int(11) NOT NULL DEFAULT '0'
);
SELECT c1 FROM t1 GROUP BY c1 ORDER BY c1 ASC;
c1
SELECT ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) AS `c1` FROM `t1` GROUP BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ORDER BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ASC;
c1
SELECT ((floor(((`c1` - 10) / 300)) * 50000) + 0.0) AS `c1` FROM `t1` GROUP BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ORDER BY ((floor(((`c1` - 0.0) / 50000)) * 50000) + 0.0) ASC;
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t1.c1' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
set sql_mode = default;
set sql_mode = 'ONLY_FULL_GROUP_BY';
drop table if exists t;
create table t(a real);
select a from t group by (a);
a
select a from t group by ((a));
a
select a from t group by +a;
a
select a from t group by ((+a));
a
select a from t group by (-a);
Error 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'executor__aggregate.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
set sql_mode = default;
drop table if exists test;
create table test(id int, name int);
insert into test values(1, 10);
insert into test values(1, 20);
insert into test values(1, 30);
insert into test values(2, 20);
insert into test values(3, 200);
insert into test values(3, 500);
select id, group_concat(name) from test group by id order by id;
id group_concat(name)
1 10,20,30
2 20
3 200,500
select id, group_concat(name SEPARATOR ';') from test group by id order by id;
id group_concat(name SEPARATOR ';')
1 10;20;30
2 20
3 200;500
select id, group_concat(name SEPARATOR ',') from test group by id order by id;
id group_concat(name SEPARATOR ',')
1 10,20,30
2 20
3 200,500
select id, group_concat(name SEPARATOR '%') from test group by id order by id;
id group_concat(name SEPARATOR '%')
1 10%20%30
2 20
3 200%500
select id, group_concat(name SEPARATOR '') from test group by id order by id;
id group_concat(name SEPARATOR '')
1 102030
2 20
3 200500
select id, group_concat(name SEPARATOR '123') from test group by id order by id;
id group_concat(name SEPARATOR '123')
1 101232012330
2 20
3 200123500
select group_concat(id ORDER BY name) from (select * from test order by id, name limit 2,2) t;
group_concat(id ORDER BY name)
2,1
select group_concat(id ORDER BY name desc) from (select * from test order by id, name limit 2,2) t;
group_concat(id ORDER BY name desc)
1,2
select group_concat(name ORDER BY id) from (select * from test order by id, name limit 2,2) t;
group_concat(name ORDER BY id)
30,20
select group_concat(name ORDER BY id desc) from (select * from test order by id, name limit 2,2) t;
group_concat(name ORDER BY id desc)
20,30
select group_concat(name ORDER BY name desc SEPARATOR '++') from test;
group_concat(name ORDER BY name desc SEPARATOR '++')
500++200++30++20++20++10
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--1--2--1
select group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(name ORDER BY name desc SEPARATOR '++') group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
500++200++30++20++20++10 3--3--1--1--2--1
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,30,20,10
set session group_concat_max_len=4;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=5;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=6;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=7;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=8;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=9;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=10;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--1
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=11;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--1-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=12;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--1--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=13;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--1--2
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=14;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--1--2-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=15;
select group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test;
group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
3--3--1--1--2--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=4;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=5;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=6;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=7;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=8;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=9;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=10;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1--1
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=11;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1--1-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=12;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1--1--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=13;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1--1--3
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=14;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1--1--3-
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=15;
select group_concat(id ORDER BY name asc, id desc SEPARATOR '--') from test;
group_concat(id ORDER BY name asc, id desc SEPARATOR '--')
1--2--1--1--3--
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=4;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=5;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,2
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=6;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,20
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=7;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=8;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=9;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,3
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=10;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,30
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=11;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,30,
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=12;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,30,2
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=13;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,30,20
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=14;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,30,20,
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=15;
select group_concat(distinct name order by name desc) from test;
group_concat(distinct name order by name desc)
500,200,30,20,1
select @@warning_count;
@@warning_count
1
set session group_concat_max_len=default;
drop table if exists test2;
create table test2(id varchar(20), name varchar(20));
insert into test2 select * from test;
select group_concat(id ORDER BY name) from (select * from test2 order by id, name limit 2,2) t;
group_concat(id ORDER BY name)
2,1
select group_concat(id ORDER BY name desc) from (select * from test2 order by id, name limit 2,2) t;
group_concat(id ORDER BY name desc)
1,2
select group_concat(name ORDER BY id) from (select * from test2 order by id, name limit 2,2) t;
group_concat(name ORDER BY id)
30,20
select group_concat(name ORDER BY id desc) from (select * from test2 order by id, name limit 2,2) t;
group_concat(name ORDER BY id desc)
20,30
select group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from test2;
group_concat(name ORDER BY name desc SEPARATOR '++') group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
500++30++200++20++20++10 3--1--3--1--2--1
select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY 1 desc, id SEPARATOR '++') from test;
1 2 3 4 5 group_concat(name, id ORDER BY 1 desc, id SEPARATOR '++')
1 2 3 4 5 5003++2003++301++201++202++101
select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY 2 desc, name SEPARATOR '++') from test;
1 2 3 4 5 group_concat(name, id ORDER BY 2 desc, name SEPARATOR '++')
1 2 3 4 5 2003++5003++202++101++201++301
select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY 3 desc, name SEPARATOR '++') from test;
Error 1054 (42S22): Unknown column '3' in 'order clause'
prepare s1 from "select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY floor(id/?) desc, name SEPARATOR '++') from test";
set @a=2;
execute s1 using @a;
1 2 3 4 5 group_concat(name, id ORDER BY floor(id/?) desc, name SEPARATOR '++')
1 2 3 4 5 202++2003++5003++101++201++301
prepare s1 from "select 1, 2, 3, 4, 5 , group_concat(name, id ORDER BY ? desc, name SEPARATOR '++') from test";
set @a=2;
execute s1 using @a;
1 2 3 4 5 group_concat(name, id ORDER BY ? desc, name SEPARATOR '++')
1 2 3 4 5 2003++5003++202++101++201++301
set @a=3;
execute s1 using @a;
Error 1054 (42S22): Unknown column '?' in 'order clause'
set @a=3.0;
execute s1 using @a;
1 2 3 4 5 group_concat(name, id ORDER BY ? desc, name SEPARATOR '++')
1 2 3 4 5 101++202++201++301++2003++5003
drop table if exists ptest;
CREATE TABLE ptest (id int,name int) PARTITION BY RANGE ( id ) (PARTITION `p0` VALUES LESS THAN (2), PARTITION `p1` VALUES LESS THAN (11));
insert into ptest select * from test;
set session tidb_opt_distinct_agg_push_down = 0;
set session tidb_opt_agg_push_down = 0;
select /*+ agg_to_cop */ group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from ptest;
group_concat(name ORDER BY name desc SEPARATOR '++') group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
500++200++30++20++20++10 3--3--1--1--2--1
select /*+ agg_to_cop */ group_concat(distinct name order by name desc) from ptest;
group_concat(distinct name order by name desc)
500,200,30,20,10
set session tidb_opt_distinct_agg_push_down = 0;
set session tidb_opt_agg_push_down = 1;
select /*+ agg_to_cop */ group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from ptest;
group_concat(name ORDER BY name desc SEPARATOR '++') group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
500++200++30++20++20++10 3--3--1--1--2--1
select /*+ agg_to_cop */ group_concat(distinct name order by name desc) from ptest;
group_concat(distinct name order by name desc)
500,200,30,20,10
set session tidb_opt_distinct_agg_push_down = 1;
set session tidb_opt_agg_push_down = 0;
select /*+ agg_to_cop */ group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from ptest;
group_concat(name ORDER BY name desc SEPARATOR '++') group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
500++200++30++20++20++10 3--3--1--1--2--1
select /*+ agg_to_cop */ group_concat(distinct name order by name desc) from ptest;
group_concat(distinct name order by name desc)
500,200,30,20,10
set session tidb_opt_distinct_agg_push_down = 1;
set session tidb_opt_agg_push_down = 1;
select /*+ agg_to_cop */ group_concat(name ORDER BY name desc SEPARATOR '++'), group_concat(id ORDER BY name desc, id asc SEPARATOR '--') from ptest;
group_concat(name ORDER BY name desc SEPARATOR '++') group_concat(id ORDER BY name desc, id asc SEPARATOR '--')
500++200++30++20++20++10 3--3--1--1--2--1
select /*+ agg_to_cop */ group_concat(distinct name order by name desc) from ptest;
group_concat(distinct name order by name desc)
500,200,30,20,10
set session tidb_opt_distinct_agg_push_down = default;
set session tidb_opt_agg_push_down = default;
select group_concat(123, null);
group_concat(123, null)
NULL
drop table if exists t1;
create table t1(cid int, sname varchar(100));
insert into t1 values(1, 'Bob'), (1, 'Alice');
insert into t1 values(3, 'Ace');
set @@group_concat_max_len=5;
select group_concat(sname order by sname) from t1 group by cid;
group_concat(sname order by sname)
Alice
Ace
drop table if exists t1;
create table t1(c1 varchar(10));
insert into t1 values('0123456789');
insert into t1 values('12345');
set @@group_concat_max_len=8;
select group_concat(c1 order by c1) from t1 group by c1;
group_concat(c1 order by c1)
01234567
12345
set @@group_concat_max_len=default;
drop table if exists select_distinct_test;
create table select_distinct_test(id int not null default 1, name varchar(255), PRIMARY KEY(id));
insert INTO select_distinct_test VALUES (1, "hello");
insert into select_distinct_test values (2, "hello");
begin;
select distinct name from select_distinct_test;
name
hello
commit;
drop table if exists t;
create table t (i int);
insert into t values (1), (1), (1),(2),(3),(2),(3),(2),(3);
explain format = 'plan_tree' select * from t order by i + 1;
id task access object operator info
Projection root executor__aggregate.t.i
└─Sort root Column
└─Projection root executor__aggregate.t.i, plus(executor__aggregate.t.i, 1)->Column
└─TableReader root data:TableFullScan
└─TableFullScan cop[tikv] table:t keep order:false, stats:pseudo
select * from t order by i + 1;
i
1
1
1
2
2
2
3
3
3
explain format = 'plan_tree' select * from t order by i + 1 limit 2;
id task access object operator info
Projection root executor__aggregate.t.i
└─TopN root Column, offset:0, count:2
└─Projection root executor__aggregate.t.i, plus(executor__aggregate.t.i, 1)->Column
└─TableReader root data:TopN
└─TopN cop[tikv] plus(executor__aggregate.t.i, 1), offset:0, count:2
└─TableFullScan cop[tikv] table:t keep order:false, stats:pseudo
select * from t order by i + 1 limit 2;
i
1
1
select i, i, i from t order by i + 1;
i i i
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
3 3 3
3 3 3
3 3 3
insert into mysql.opt_rule_blacklist value("decorrelate");
drop table if exists test;
create table test (a int);
insert into test value(1);
analyze table test;
select /*+ hash_agg() */ sum(a), (select NULL from test where tt.a = test.a limit 1),(select NULL from test where tt.a = test.a limit 1),(select NULL from test where tt.a = test.a limit 1) from test tt;
sum(a) (select NULL from test where tt.a = test.a limit 1) (select NULL from test where tt.a = test.a limit 1) (select NULL from test where tt.a = test.a limit 1)
1 NULL NULL NULL
explain format = 'plan_tree' select /*+ hash_agg() */ sum(a), (select NULL from test where tt.a = test.a limit 1),(select NULL from test where tt.a = test.a limit 1),(select NULL from test where tt.a = test.a limit 1) from test tt;
id task access object operator info
Projection root Column, Column, Column, Column
└─Apply root CARTESIAN left outer join, left side:Apply
├─Apply(Build) root CARTESIAN left outer join, left side:Apply
│ ├─Apply(Build) root CARTESIAN left outer join, left side:HashAgg
│ │ ├─HashAgg(Build) root funcs:sum(Column)->Column, funcs:firstrow(Column)->executor__aggregate.test.a
│ │ │ └─Projection root cast(executor__aggregate.test.a, decimal(10,0) BINARY)->Column, executor__aggregate.test.a->Column
│ │ │ └─TableReader root data:TableFullScan
│ │ │ └─TableFullScan cop[tikv] table:tt keep order:false
│ │ └─Projection(Probe) root <nil>->Column
│ │ └─Limit root offset:0, count:1
│ │ └─TableReader root data:Limit
│ │ └─Limit cop[tikv] offset:0, count:1
│ │ └─Selection cop[tikv] eq(executor__aggregate.test.a, executor__aggregate.test.a)
│ │ └─TableFullScan cop[tikv] table:test keep order:false
│ └─Projection(Probe) root <nil>->Column
│ └─Limit root offset:0, count:1
│ └─TableReader root data:Limit
│ └─Limit cop[tikv] offset:0, count:1
│ └─Selection cop[tikv] eq(executor__aggregate.test.a, executor__aggregate.test.a)
│ └─TableFullScan cop[tikv] table:test keep order:false
└─Projection(Probe) root <nil>->Column
└─Limit root offset:0, count:1
└─TableReader root data:Limit
└─Limit cop[tikv] offset:0, count:1
└─Selection cop[tikv] eq(executor__aggregate.test.a, executor__aggregate.test.a)
└─TableFullScan cop[tikv] table:test keep order:false
delete from mysql.opt_rule_blacklist where name = "decorrelate";
admin reload opt_rule_blacklist;
select distinct avg(nullif(77.15, PI()));
avg(nullif(77.15, PI()))
77.1500000000000000000000000000000000
Level Code Message
Warning 1265 Data truncated for column '%s' at row %d
drop table if exists t_a;
CREATE TABLE `t_a` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
`a` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'merchant id',
`b` decimal(64,8) NOT NULL DEFAULT '0.00000000' COMMENT 'total amount',
`c` decimal(64,8) NOT NULL DEFAULT '0.00000000' COMMENT 'settlement amount',
`d` decimal(64,8) NOT NULL DEFAULT '0.00000000' COMMENT 'total fee',
`e` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'settle time',
`f` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'create time',
`g` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'is deleted',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='settlement table';
explain format = 'plan_tree' SELECT
MAX(id) AS id,
LEFT(FROM_UNIXTIME(e), 10) AS settle_time,
a,
SUM(b) AS total_amount,
SUM(c) AS settlement_amount,
SUM(d) AS total_fee
FROM `t_a`
WHERE `f` >= 1753632000
AND `f` <= 1753718399
AND `g` = 0
GROUP BY `a`, left(FROM_UNIXTIME(e), 10);
id task access object operator info
Projection root Column, left(cast(from_unixtime(cast(executor__aggregate.t_a.e, decimal(10,0) UNSIGNED BINARY)), var_string(19)), 10)->Column, executor__aggregate.t_a.a, Column, Column, Column
└─HashAgg root group by:Column, Column, funcs:max(Column)->Column, funcs:sum(Column)->Column, funcs:sum(Column)->Column, funcs:sum(Column)->Column, funcs:firstrow(Column)->executor__aggregate.t_a.a, funcs:firstrow(Column)->executor__aggregate.t_a.e
└─Projection root executor__aggregate.t_a.id->Column, executor__aggregate.t_a.b->Column, executor__aggregate.t_a.c->Column, executor__aggregate.t_a.d->Column, executor__aggregate.t_a.a->Column, executor__aggregate.t_a.e->Column, left(cast(from_unixtime(cast(executor__aggregate.t_a.e, decimal(10,0) UNSIGNED BINARY)), var_string(19)), 10)->Column
└─TableReader root data:Selection
└─Selection cop[tikv] eq(executor__aggregate.t_a.g, 0), ge(executor__aggregate.t_a.f, 1753632000), le(executor__aggregate.t_a.f, 1753718399)
└─TableFullScan cop[tikv] table:t_a keep order:false, stats:pseudo
SELECT
MAX(id) AS id,
LEFT(FROM_UNIXTIME(e), 10) AS settle_time,
a,
SUM(b) AS total_amount,
SUM(c) AS settlement_amount,
SUM(d) AS total_fee
FROM `t_a`
WHERE `f` >= 1753632000
AND `f` <= 1753718399
AND `g` = 0
GROUP BY `a`, left(FROM_UNIXTIME(e), 10);
id settle_time a total_amount settlement_amount total_fee