diff --git a/executor/test/aggregate/BUILD.bazel b/executor/test/aggregate/BUILD.bazel index 7f15f28419032..39edd0c6aae96 100644 --- a/executor/test/aggregate/BUILD.bazel +++ b/executor/test/aggregate/BUILD.bazel @@ -9,15 +9,12 @@ go_test( ], data = glob(["testdata/**"]), flaky = True, - shard_count = 39, + shard_count = 9, deps = [ "//config", "//executor/aggregate", "//executor/internal", - "//parser/terror", - "//planner/core", "//session", - "//sessionctx/variable", "//testkit", "//testkit/testdata", "//testkit/testsetup", diff --git a/executor/test/aggregate/aggregate_test.go b/executor/test/aggregate/aggregate_test.go index a3de682004499..253f8bb6cb3f9 100644 --- a/executor/test/aggregate/aggregate_test.go +++ b/executor/test/aggregate/aggregate_test.go @@ -28,480 +28,13 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/executor/aggregate" "github.com/pingcap/tidb/executor/internal" - "github.com/pingcap/tidb/parser/terror" - plannercore "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/util/sqlexec" "github.com/stretchr/testify/require" ) -func TestAggregation(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("set @@tidb_hash_join_concurrency=1") - tk.MustExec("use test") - tk.MustExec("set sql_mode='STRICT_TRANS_TABLES'") // disable only-full-group-by - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (c int, d int)") - tk.MustExec("insert t values (NULL, 1)") - tk.MustExec("insert t values (1, 1)") - tk.MustExec("insert t values (1, 2)") - tk.MustExec("insert t values (1, 3)") - tk.MustExec("insert t values (1, 1)") - tk.MustExec("insert t values (3, 2)") - tk.MustExec("insert t values (4, 3)") - tk.MustQuery("select bit_and(c) from t where NULL").Check(testkit.Rows("18446744073709551615")) - tk.MustQuery("select bit_or(c) from t where NULL").Check(testkit.Rows("0")) - tk.MustQuery("select bit_xor(c) from t where NULL").Check(testkit.Rows("0")) - tk.MustQuery("select approx_count_distinct(c) from t where NULL").Check(testkit.Rows("0")) - result := tk.MustQuery("select count(*) from t") - result.Check(testkit.Rows("7")) - result = tk.MustQuery("select count(*) from t group by d order by c") - result.Check(testkit.Rows("3", "2", "2")) - result = tk.MustQuery("select distinct 99 from t group by d having d > 0") - result.Check(testkit.Rows("99")) - result = tk.MustQuery("select count(*) from t having 1 = 0") - result.Check(testkit.Rows()) - result = tk.MustQuery("select c,d from t group by d order by d") - result.Check(testkit.Rows(" 1", "1 2", "1 3")) - result = tk.MustQuery("select - c, c as d from t group by c having null not between c and avg(distinct d) - d") - result.Check(testkit.Rows()) - result = tk.MustQuery("select - c as c from t group by c having t.c > 5") - result.Check(testkit.Rows()) - result = tk.MustQuery("select t1.c from t t1, t t2 group by c having c > 5") - result.Check(testkit.Rows()) - result = tk.MustQuery("select count(*) from (select d, c from t) k where d != 0 group by d order by c") - result.Check(testkit.Rows("3", "2", "2")) - result = tk.MustQuery("select c as a from t group by d having a < 0") - result.Check(testkit.Rows()) - result = tk.MustQuery("select c as a from t group by d having sum(a) = 2") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select count(distinct c) from t group by d order by c") - result.Check(testkit.Rows("1", "2", "2")) - result = tk.MustQuery("select approx_count_distinct(c) from t group by d order by c") - result.Check(testkit.Rows("1", "2", "2")) - result = tk.MustQuery("select sum(c) as a from t group by d order by a") - result.Check(testkit.Rows("2", "4", "5")) - result = tk.MustQuery("select sum(c) as a, sum(c+1), sum(c), sum(c+1) from t group by d order by a") - result.Check(testkit.Rows("2 4 2 4", "4 6 4 6", "5 7 5 7")) - result = tk.MustQuery("select count(distinct c,d) from t") - result.Check(testkit.Rows("5")) - result = tk.MustQuery("select approx_count_distinct(c,d) from t") - result.Check(testkit.Rows("5")) - err := tk.ExecToErr("select count(c,d) from t") - require.Error(t, err) - result = tk.MustQuery("select d*2 as ee, sum(c) from t group by ee order by ee") - result.Check(testkit.Rows("2 2", "4 4", "6 5")) - result = tk.MustQuery("select sum(distinct c) as a from t group by d order by a") - result.Check(testkit.Rows("1", "4", "5")) - result = tk.MustQuery("select min(c) as a from t group by d order by a") - result.Check(testkit.Rows("1", "1", "1")) - result = tk.MustQuery("select max(c) as a from t group by d order by a") - result.Check(testkit.Rows("1", "3", "4")) - result = tk.MustQuery("select avg(c) as a from t group by d order by a") - result.Check(testkit.Rows("1.0000", "2.0000", "2.5000")) - result = tk.MustQuery("select c, approx_count_distinct(d) as a from t group by c order by a, c") - result.Check(testkit.Rows(" 1", "3 1", "4 1", "1 3")) - result = tk.MustQuery("select d, d + 1 from t group by d order by d") - result.Check(testkit.Rows("1 2", "2 3", "3 4")) - result = tk.MustQuery("select count(*) from t") - result.Check(testkit.Rows("7")) - result = tk.MustQuery("select count(distinct d) from t") - result.Check(testkit.Rows("3")) - result = tk.MustQuery("select approx_count_distinct(d) from t") - result.Check(testkit.Rows("3")) - result = tk.MustQuery("select count(*) as a from t group by d having sum(c) > 3 order by a") - result.Check(testkit.Rows("2", "2")) - result = tk.MustQuery("select max(c) from t group by d having sum(c) > 3 order by avg(c) desc") - result.Check(testkit.Rows("4", "3")) - result = tk.MustQuery("select sum(-1) from t a left outer join t b on not null is null") - result.Check(testkit.Rows("-7")) - result = tk.MustQuery("select count(*), b.d from t a left join t b on a.c = b.d group by b.d order by b.d") - result.Check(testkit.Rows("2 ", "12 1", "2 3")) - result = tk.MustQuery("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") - result.Check(testkit.Rows("0 ", "12 1", "2 3")) - result = tk.MustQuery("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") - result.Check(testkit.Rows("0 ", "12 1", "2 3")) - result = tk.MustQuery("select count(*), b.d from t b right join t a on a.c = b.d group by b.d order by b.d") - result.Check(testkit.Rows("2 ", "12 1", "2 3")) - result = tk.MustQuery("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") - result.Check(testkit.Rows("10 ", "1 1", "4 3")) - result = tk.MustQuery("select count(*) from t a , t b") - result.Check(testkit.Rows("49")) - result = tk.MustQuery("select count(*) from t a , t b, t c") - result.Check(testkit.Rows("343")) - result = tk.MustQuery("select count(*) from t a , t b where a.c = b.d") - result.Check(testkit.Rows("14")) - result = tk.MustQuery("select count(a.d), sum(b.c) from t a , t b where a.c = b.d order by a.d") - result.Check(testkit.Rows("14 13")) - result = tk.MustQuery("select count(*) from t a , t b, t c where a.c = b.d and b.d = c.d") - result.Check(testkit.Rows("40")) - result = tk.MustQuery("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") - result.Check(testkit.Rows("36 1", "4 3")) - result = tk.MustQuery("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") - result.Check(testkit.Rows("36 1", "4 3")) - result = tk.MustQuery("select count(*) from t a , t b where a.c = b.d and a.c + b.d = 2") - result.Check(testkit.Rows("12")) - result = tk.MustQuery("select count(*) from t a join t b having sum(a.c) < 0") - result.Check(testkit.Rows()) - result = tk.MustQuery("select count(*) from t a join t b where a.c < 0") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("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") - result.Check(testkit.Rows(" 0 ", "8 12 1", "5 2 3")) - // This two cases prove that having always resolve name from field list firstly. - result = tk.MustQuery("select 1-d as d from t having d < 0 order by d desc") - result.Check(testkit.Rows("-1", "-1", "-2", "-2")) - result = tk.MustQuery("select 1-d as d from t having d + 1 < 0 order by d + 1") - result.Check(testkit.Rows("-2", "-2")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (keywords varchar(20), type int)") - tk.MustExec("insert into t values('测试', 1), ('test', 2)") - result = tk.MustQuery("select group_concat(keywords) from t group by type order by type") - result.Check(testkit.Rows("测试", "test")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (c int, d int)") - tk.MustExec("insert t values (1, -1)") - tk.MustExec("insert t values (1, 0)") - tk.MustExec("insert t values (1, 1)") - result = tk.MustQuery("select d, d*d as d from t having d = -1") - result.Check(testkit.Rows()) - result = tk.MustQuery("select d*d as d from t group by d having d = -1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select d, 1-d as d, c as d from t order by d") - result.Check(testkit.Rows("1 0 1", "0 1 1", "-1 2 1")) - result = tk.MustQuery("select d, 1-d as d, c as d from t order by d+1") - result.Check(testkit.Rows("-1 2 1", "0 1 1", "1 0 1")) - result = tk.MustQuery("select d, 1-d as d, c as d from t group by d order by d") - result.Check(testkit.Rows("1 0 1", "0 1 1", "-1 2 1")) - result = tk.MustQuery("select d as d1, t.d as d1, 1-d as d1, c as d1 from t having d1 < 10 order by d") - result.Check(testkit.Rows("-1 -1 2 1", "0 0 1 1", "1 1 0 1")) - result = tk.MustQuery("select d*d as d1, c as d1 from t group by d1 order by d1") - result.Check(testkit.Rows("0 1", "1 1")) - result = tk.MustQuery("select d*d as d1, c as d1 from t group by 2") - result.Check(testkit.Rows("1 1")) - result = tk.MustQuery("select * from t group by 2 order by d") - result.Check(testkit.Rows("1 -1", "1 0", "1 1")) - result = tk.MustQuery("select * , sum(d) from t group by 1 order by d") - result.Check(testkit.Rows("1 -1 0")) - result = tk.MustQuery("select sum(d), t.* from t group by 2 order by d") - result.Check(testkit.Rows("0 1 -1")) - result = tk.MustQuery("select d as d, c as d from t group by d + 1 order by t.d") - result.Check(testkit.Rows("-1 1", "0 1", "1 1")) - result = tk.MustQuery("select c as d, c as d from t group by d order by d") - result.Check(testkit.Rows("1 1", "1 1", "1 1")) - err = tk.ExecToErr("select d as d, c as d from t group by d") - require.Error(t, err) - err = tk.ExecToErr("select t.d, c as d from t group by d") - require.Error(t, err) - result = tk.MustQuery("select *, c+1 as d from t group by 3 order by d") - result.Check(testkit.Rows("1 -1 2")) - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(a float, b int default 3)") - tk.MustExec("insert into t1 (a) values (2), (11), (8)") - result = tk.MustQuery("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") - result.Check(testkit.Rows("2 2 2")) - // The following cases use streamed aggregation. - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(a int, index(a))") - tk.MustExec("insert into t1 (a) values (1),(2),(3),(4),(5)") - result = tk.MustQuery("select count(a) from t1 where a < 3") - result.Check(testkit.Rows("2")) - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(a int, b int, index(a))") - result = tk.MustQuery("select sum(b) from (select * from t1) t group by a") - result.Check(testkit.Rows()) - result = tk.MustQuery("select sum(b) from (select * from t1) t") - result.Check(testkit.Rows("")) - tk.MustExec("insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4),(3, 5)") - result = tk.MustQuery("select avg(b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("2.5000", "2.0000", "4.0000")) - result = tk.MustQuery("select sum(b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("5", "2", "8")) - result = tk.MustQuery("select count(b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("2", "1", "2")) - result = tk.MustQuery("select max(b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("4", "2", "5")) - result = tk.MustQuery("select min(b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("1", "2", "3")) - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(a int, b int, index(a,b))") - tk.MustExec("insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3)") - result = tk.MustQuery("select avg(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("2.5000", "2.0000", "4.0000")) - result = tk.MustQuery("select sum(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("5", "2", "8")) - result = tk.MustQuery("select count(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("2", "1", "2")) - result = tk.MustQuery("select approx_count_distinct(b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("2", "1", "2")) - result = tk.MustQuery("select max(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("4", "2", "5")) - result = tk.MustQuery("select min(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("1", "2", "3")) - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(a int, b int, index(b, a))") - tk.MustExec("insert into t1 (a, b) values (1, 1),(2, 2),(3, 3),(1, 4), (1,1),(3, 5), (2,2), (3,5), (3,3)") - result = tk.MustQuery("select avg(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("2.5000", "2.0000", "4.0000")) - result = tk.MustQuery("select sum(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("5", "2", "8")) - result = tk.MustQuery("select count(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("2", "1", "2")) - result = tk.MustQuery("select max(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("4", "2", "5")) - result = tk.MustQuery("select min(distinct b) from (select * from t1) t group by a order by a") - result.Check(testkit.Rows("1", "2", "3")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int primary key, ds date)") - tk.MustExec("insert into t (id, ds) values (1, \"1991-09-05\"),(2,\"1991-09-05\"), (3, \"1991-09-06\"),(0,\"1991-09-06\")") - result = tk.MustQuery("select sum(id), ds from t group by ds order by id") - result.Check(testkit.Rows("3 1991-09-06", "3 1991-09-05")) - tk.MustExec("drop table if exists t1") - tk.MustExec("drop table if exists t2") - tk.MustExec("create table t1 (col0 int, col1 int)") - tk.MustExec("create table t2 (col0 int, col1 int)") - tk.MustExec("insert into t1 values(83, 0), (26, 0), (43, 81)") - tk.MustExec("insert into t2 values(22, 2), (3, 12), (38, 98)") - result = tk.MustQuery("SELECT COALESCE ( + 1, cor0.col0 ) + - CAST( NULL AS DECIMAL ) FROM t2, t1 AS cor0, t2 AS cor1 GROUP BY cor0.col1") - result.Check(testkit.Rows("", "")) - - tk.MustExec("drop table if exists t1") - tk.MustExec("drop table if exists t2") - tk.MustExec("create table t1 (c1 int)") - tk.MustExec("create table t2 (c1 int)") - tk.MustExec("insert into t1 values(3), (2)") - tk.MustExec("insert into t2 values(1), (2)") - tk.MustExec("set @@session.tidb_opt_insubq_to_join_and_agg = 0") - result = tk.MustQuery("select sum(c1 in (select * from t2)) from t1") - result.Check(testkit.Rows("1")) - tk.MustExec("set @@session.tidb_opt_insubq_to_join_and_agg = 1") - result = tk.MustQuery("select sum(c1 in (select * from t2)) from t1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select sum(c1) k from (select * from t1 union all select * from t2)t group by c1 * 2 order by k") - result.Check(testkit.Rows("1", "3", "4")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int)") - tk.MustExec("insert into t values(1, 2, 3), (1, 2, 4)") - result = tk.MustQuery("select count(distinct c), count(distinct a,b) from t") - result.Check(testkit.Rows("2 1")) - result = tk.MustQuery("select approx_count_distinct( c), approx_count_distinct( a,b) from t") - result.Check(testkit.Rows("2 1")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a float)") - tk.MustExec("insert into t values(966.36), (363.97), (569.99), (453.33), (376.45), (321.93), (12.12), (45.77), (9.66), (612.17)") - result = tk.MustQuery("select distinct count(distinct a) from t") - result.Check(testkit.Rows("10")) - result = tk.MustQuery("select distinct approx_count_distinct( a) from t") - result.Check(testkit.Rows("10")) - - tk.MustExec("create table idx_agg (a int, b int, index (b))") - tk.MustExec("insert idx_agg values (1, 1), (1, 2), (2, 2)") - tk.MustQuery("select sum(a), sum(b) from idx_agg where b > 0 and b < 10").Check(testkit.Rows("4 5")) - - // test without any aggregate function - tk.MustQuery("select 10 from idx_agg group by b").Check(testkit.Rows("10", "10")) - tk.MustQuery("select 11 from idx_agg group by a").Check(testkit.Rows("11", "11")) - - tk.MustExec("set @@tidb_init_chunk_size=1;") - tk.MustQuery("select group_concat(b) from idx_agg group by b;").Sort().Check(testkit.Rows("1", "2,2")) - tk.MustExec("set @@tidb_init_chunk_size=2;") - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int(11), b decimal(15,2))") - tk.MustExec("insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97)") - tk.MustQuery("select a, max(b) from t group by a order by a limit 2").Check(testkit.Rows("1 771.64", "2 378.49")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int(11), b char(15))") - tk.MustExec("insert into t values(1,771.64),(2,378.49),(3,920.92),(4,113.97)") - tk.MustQuery("select a, max(b) from t group by a order by a limit 2").Check(testkit.Rows("1 771.64", "2 378.49")) - - // for issue #6014 - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int(11) NOT NULL, tags json DEFAULT NULL)") - tk.MustExec(`insert into t values (1, '{"i": 1, "n": "n1"}')`) - tk.MustExec(`insert into t values (2, '{"i": 2, "n": "n2"}')`) - tk.MustExec(`insert into t values (3, '{"i": 3, "n": "n3"}')`) - tk.MustExec(`insert into t values (4, '{"i": 4, "n": "n4"}')`) - tk.MustExec(`insert into t values (5, '{"i": 5, "n": "n5"}')`) - tk.MustExec(`insert into t values (6, '{"i": 0, "n": "n6"}')`) - tk.MustExec(`insert into t values (7, '{"i": -1, "n": "n7"}')`) - tk.MustQuery("select sum(tags->'$.i') from t").Check(testkit.Rows("14")) - - // test agg with empty input - result = tk.MustQuery("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") - result.Check(testkit.Rows(" 0 0 18446744073709551615 0 ")) - tk.MustExec("truncate table t") - tk.MustExec("create table s(id int)") - result = tk.MustQuery("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") - result.Check(testkit.Rows(" 0 0 18446744073709551615 0 0")) - tk.MustExec(`insert into t values (1, '{"i": 1, "n": "n1"}')`) - result = tk.MustQuery("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") - result.Check(testkit.Rows("1 1 95 95.0000 95 95 95 95 95 95 1")) - tk.MustExec("set @@tidb_hash_join_concurrency=5") - - // test agg bit col - tk.MustExec("drop table t") - tk.MustExec("CREATE TABLE `t` (`a` bit(1) NOT NULL, PRIMARY KEY (`a`))") - tk.MustExec("insert into t value(1), (0)") - tk.MustQuery("select a from t group by 1") - // This result is compatible with MySQL, the readable result is shown in the next case. - result = tk.MustQuery("select max(a) from t group by a order by a") - result.Check(testkit.Rows(string([]byte{0x0}), string([]byte{0x1}))) - result = tk.MustQuery("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") - result.Check(testkit.Rows("0 0 0", "1 1 1")) - - tk.MustExec("drop table t") - tk.MustExec("create table t(a int, b int)") - tk.MustExec("insert into t value(null, null)") - tk.MustQuery("select group_concat(a), group_concat(distinct a) from t").Check(testkit.Rows(" ")) - tk.MustExec("insert into t value(1, null), (null, 1), (1, 2), (3, 4)") - tk.MustQuery("select group_concat(a, b), group_concat(distinct a,b) from t").Check(testkit.Rows("12,34 12,34")) - tk.MustExec("set @@session.tidb_opt_distinct_agg_push_down = 0") - tk.MustQuery("select count(distinct a) from t;").Check(testkit.Rows("2")) - tk.MustExec("set @@session.tidb_opt_distinct_agg_push_down = 1") - tk.MustQuery("select count(distinct a) from t;").Check(testkit.Rows("2")) - tk.MustExec("set @@session.tidb_opt_distinct_agg_push_down = 0") - tk.MustQuery("select approx_count_distinct( a) from t;").Check(testkit.Rows("2")) - - tk.MustExec("drop table t") - tk.MustExec("create table t(a decimal(10, 4))") - tk.MustQuery("select 10 from t group by a").Check(testkit.Rows()) - tk.MustExec("insert into t value(0), (-0.9871), (-0.9871)") - tk.MustQuery("select 10 from t group by a").Check(testkit.Rows("10", "10")) - tk.MustQuery("select sum(a) from (select a from t union all select a from t) tmp").Check(testkit.Rows("-3.9484")) - - tk.MustExec("drop table t") - tk.MustExec("create table t(a tinyint, b smallint, c mediumint, d int, e bigint, f float, g double, h decimal)") - tk.MustExec("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)") - result = tk.MustQuery("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") - result.Check(testkit.Rows("0.25 0.25 0.25 0.25 0.25 0.25 0.25")) - - tk.MustExec("insert into t values(2, 3, 4, 5, 6, 7.2, 8.3, 9)") - result = tk.MustQuery("select a, var_pop(b) over w, var_pop(c) over w from t window w as (partition by a)").Sort() - result.Check(testkit.Rows("1 0.25 0.25", "1 0.25 0.25", "2 0 0")) - - tk.MustExec("delete from t where t.a = 2") - tk.MustExec("insert into t values(1, 2, 4, 5, 6, 6.1, 7.2, 9)") - result = tk.MustQuery("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") - result.Check(testkit.Rows("1 0.25 0.25 0.25 0.25 0.25 0.25 0.25")) - - tk.MustExec("drop table t") - tk.MustExec("create table t(a int, b bigint, c float, d double, e decimal)") - tk.MustExec("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)") - result = tk.MustQuery("select variance(b), variance(c), variance(d), variance(e) from t group by a") - result.Check(testkit.Rows("2584338.6666666665 29.840000178019228 1.1808222222222229 12.666666666666666")) - - tk.MustExec("insert into t values(1, 255, 6.8, 6.08, 1)") - result = tk.MustQuery("select variance(distinct b), variance(distinct c), variance(distinct d), variance(distinct e) from t group by a") - result.Check(testkit.Rows("2364075.6875 29.840000178019228 1.1808222222222229 12.666666666666666")) - - tk.MustExec("insert into t values(2, 322, 0.8, 2.22, 6)") - result = tk.MustQuery("select a, variance(b) over w from t window w as (partition by a)").Sort() - result.Check(testkit.Rows("1 2364075.6875", "1 2364075.6875", "1 2364075.6875", "1 2364075.6875", "2 0")) - - // TODO: Fix this error message. - tk.MustGetErrMsg("select std_samp(a) from t", "[expression:1305]FUNCTION test.std_samp does not exist") - - // For issue #14072: wrong result when using generated column with aggregate statement - tk.MustExec("drop table if exists t1;") - tk.MustExec("create table t1 (a int, b int generated always as (-a) virtual, c int generated always as (-a) stored);") - tk.MustExec("insert into t1 (a) values (2), (1), (1), (3), (NULL);") - tk.MustQuery("select sum(a) from t1 group by b order by b;").Check(testkit.Rows("", "3", "2", "2")) - tk.MustQuery("select sum(a) from t1 group by c order by c;").Check(testkit.Rows("", "3", "2", "2")) - tk.MustQuery("select sum(b) from t1 group by a order by a;").Check(testkit.Rows("", "-2", "-2", "-3")) - tk.MustQuery("select sum(b) from t1 group by c order by c;").Check(testkit.Rows("", "-3", "-2", "-2")) - tk.MustQuery("select sum(c) from t1 group by a order by a;").Check(testkit.Rows("", "-2", "-2", "-3")) - tk.MustQuery("select sum(c) from t1 group by b order by b;").Check(testkit.Rows("", "-3", "-2", "-2")) - - // For stddev_pop()/std()/stddev() function - tk.MustExec("drop table if exists t1;") - tk.MustExec(`create table t1 (grp int, a bigint unsigned, c char(10) not null);`) - tk.MustExec(`insert into t1 values (1,1,"a");`) - tk.MustExec(`insert into t1 values (2,2,"b");`) - tk.MustExec(`insert into t1 values (2,3,"c");`) - tk.MustExec(`insert into t1 values (3,4,"E");`) - tk.MustExec(`insert into t1 values (3,5,"C");`) - tk.MustExec(`insert into t1 values (3,6,"D");`) - tk.MustQuery(`select stddev_pop(all a) from t1;`).Check(testkit.Rows("1.707825127659933")) - tk.MustQuery(`select stddev_pop(a) from t1 group by grp order by grp;`).Check(testkit.Rows("0", "0.5", "0.816496580927726")) - tk.MustQuery(`select sum(a)+count(a)+avg(a)+stddev_pop(a) as sum from t1 group by grp order by grp;`).Check(testkit.Rows("3", "10", "23.816496580927726")) - tk.MustQuery(`select std(all a) from t1;`).Check(testkit.Rows("1.707825127659933")) - tk.MustQuery(`select std(a) from t1 group by grp order by grp;`).Check(testkit.Rows("0", "0.5", "0.816496580927726")) - tk.MustQuery(`select sum(a)+count(a)+avg(a)+std(a) as sum from t1 group by grp order by grp;`).Check(testkit.Rows("3", "10", "23.816496580927726")) - tk.MustQuery(`select stddev(all a) from t1;`).Check(testkit.Rows("1.707825127659933")) - tk.MustQuery(`select stddev(a) from t1 group by grp order by grp;`).Check(testkit.Rows("0", "0.5", "0.816496580927726")) - tk.MustQuery(`select sum(a)+count(a)+avg(a)+stddev(a) as sum from t1 group by grp order by grp;`).Check(testkit.Rows("3", "10", "23.816496580927726")) - // test null - tk.MustExec("drop table if exists t1;") - tk.MustExec("CREATE TABLE t1 (a int, b int);") - tk.MustQuery("select stddev_pop(b) from t1;").Check(testkit.Rows("")) - tk.MustQuery("select std(b) from t1;").Check(testkit.Rows("")) - tk.MustQuery("select stddev(b) from t1;").Check(testkit.Rows("")) - tk.MustExec("insert into t1 values (1,null);") - tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("")) - tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("")) - tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("")) - tk.MustExec("insert into t1 values (1,null);") - tk.MustExec("insert into t1 values (2,null);") - tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("", "")) - tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("", "")) - tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("", "")) - tk.MustExec("insert into t1 values (2,1);") - tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("", "0")) - tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("", "0")) - tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("", "0")) - tk.MustExec("insert into t1 values (3,1);") - tk.MustQuery("select stddev_pop(b) from t1 group by a order by a;").Check(testkit.Rows("", "0", "0")) - tk.MustQuery("select std(b) from t1 group by a order by a;").Check(testkit.Rows("", "0", "0")) - tk.MustQuery("select stddev(b) from t1 group by a order by a;").Check(testkit.Rows("", "0", "0")) - - // For var_samp()/stddev_samp() - tk.MustExec("drop table if exists t1;") - tk.MustExec("CREATE TABLE t1 (id int(11),value1 float(10,2));") - tk.MustExec("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);") - result = tk.MustQuery("select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id order by id;") - result.Check(testkit.Rows("1 0.816496580927726 0.6666666666666666 1 1", "2 1.118033988749895 1.25 1.2909944487358056 1.6666666666666667")) - - // For issue #19676 The result of stddev_pop(distinct xxx) is wrong - tk.MustExec("drop table if exists t1;") - tk.MustExec("CREATE TABLE t1 (id int);") - tk.MustExec("insert into t1 values (1),(2);") - tk.MustQuery("select stddev_pop(id) from t1;").Check(testkit.Rows("0.5")) - tk.MustExec("insert into t1 values (1);") - tk.MustQuery("select stddev_pop(distinct id) from t1;").Check(testkit.Rows("0.5")) -} - -func TestAggPrune(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(id int primary key, b varchar(50), c int)") - tk.MustExec("insert into t values(1, '1ff', NULL), (2, '234.02', 1)") - tk.MustQuery("select id, sum(b) from t group by id").Check(testkit.Rows("1 1", "2 234.02")) - tk.MustQuery("select sum(b) from t").Check(testkit.Rows("235.02")) - tk.MustQuery("select id, count(c) from t group by id").Check(testkit.Rows("1 0", "2 1")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(id int primary key, b float, c float)") - tk.MustExec("insert into t values(1, 1, 3), (2, 1, 6)") - tk.MustQuery("select sum(b/c) from t group by id").Check(testkit.Rows("0.3333333333333333", "0.16666666666666666")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(id int primary key, b float, c float, d float)") - tk.MustExec("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)") - tk.MustQuery("select count(distinct b, c, d) from t group by id").Check(testkit.Rows("0", "0", "0", "0", "0", "0", "0", "1")) - tk.MustQuery("select approx_count_distinct( b, c, d) from t group by id order by id").Check(testkit.Rows("0", "0", "0", "0", "0", "0", "0", "1")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int primary key, b varchar(10))") - tk.MustExec("insert into t value(1, 11),(3, NULL)") - tk.MustQuery("SELECT a, MIN(b), MAX(b) FROM t GROUP BY a").Check(testkit.Rows("1 11 11", "3 ")) -} - func TestGroupConcatAggr(t *testing.T) { var err error // issue #5411 @@ -659,321 +192,6 @@ func TestSelectDistinct(t *testing.T) { tk.MustExec("commit") } -func TestAggPushDown(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int)") - tk.MustExec("alter table t add index idx(a, b, c)") - // test for empty table - tk.MustQuery("select count(a) from t group by a;").Check(testkit.Rows()) - tk.MustQuery("select count(a) from t;").Check(testkit.Rows("0")) - // test for one row - tk.MustExec("insert t values(0,0,0)") - tk.MustQuery("select distinct b from t").Check(testkit.Rows("0")) - tk.MustQuery("select count(b) from t group by a;").Check(testkit.Rows("1")) - // test for rows - tk.MustExec("insert t values(1,1,1),(3,3,6),(3,2,5),(2,1,4),(1,1,3),(1,1,2);") - tk.MustQuery("select count(a) from t where b>0 group by a, b;").Sort().Check(testkit.Rows("1", "1", "1", "3")) - tk.MustQuery("select count(a) from t where b>0 group by a, b order by a;").Check(testkit.Rows("3", "1", "1", "1")) - tk.MustQuery("select count(a) from t where b>0 group by a, b order by a limit 1;").Check(testkit.Rows("3")) - - tk.MustExec("drop table if exists t, tt") - tk.MustExec("create table t(a int primary key, b int, c int)") - tk.MustExec("create table tt(a int primary key, b int, c int)") - tk.MustExec("insert into t values(1, 1, 1), (2, 1, 1)") - tk.MustExec("insert into tt values(1, 2, 1)") - tk.MustQuery("select max(a.b), max(b.b) from t a join tt b on a.a = b.a group by a.c").Check(testkit.Rows("1 2")) - tk.MustQuery("select a, count(b) from (select * from t union all select * from tt) k group by a order by a").Check(testkit.Rows("1 2", "2 1")) -} - -func TestOnlyFullGroupBy(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'") - tk.MustExec("set @@session.tidb_enable_new_only_full_group_by_check = 'on';") - tk.MustExec("drop table if exists t") - tk.MustExec("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))") - tk.MustExec("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))") - - // test AggregateFunc - tk.MustQuery("select max(a) from t group by d") - // for issue #8161: enable `any_value` in aggregation if `ONLY_FULL_GROUP_BY` is set - tk.MustQuery("select max(a), any_value(c) from t group by d;") - // test incompatible with sql_mode = ONLY_FULL_GROUP_BY - err := tk.ExecToErr("select * from t group by d") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select b-c from t group by b+c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select (b-c)*(b+c), min(a) from t group by b+c, b-c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select b between c and d from t group by b,c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select case b when 1 then c when 2 then d else d end from t group by b,c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select c > (select b from t) from t group by b") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select c is null from t group by b") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select c is true from t group by b") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select (c+b)*d from t group by c,d") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select b in (c,d) from t group by b,c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select b like '%a' from t group by c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select c REGEXP '1.*' from t group by b") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select -b from t group by c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select a, max(b) from t") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), "err %v", err) - err = tk.ExecToErr("select sum(a)+b from t") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), "err %v", err) - err = tk.ExecToErr("select count(b), c from t") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), "err %v", err) - tk.MustQuery("select count(b), any_value(c) from t") - tk.MustQuery("select count(b), any_value(c) + 2 from t") - err = tk.ExecToErr("select distinct a, b, count(a) from t") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrMixOfGroupFuncAndFields), "err %v", err) - // test compatible with sql_mode = ONLY_FULL_GROUP_BY - tk.MustQuery("select a from t group by a,b,c") - tk.MustQuery("select b from t group by b") - err = tk.ExecToErr("select b*rand() from t group by b") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - tk.MustQuery("select b as e from t group by b") - tk.MustQuery("select b+c from t group by b+c") - tk.MustQuery("select b+c, min(a) from t group by b+c, b-c") - tk.MustQuery("select b+c, min(a) from t group by b, c") - tk.MustQuery("select b+c from t group by b,c") - err = tk.ExecToErr("select b+c from (select b, b+rand() as c from t) t group by b") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - tk.MustQuery("select b between c and d from t group by b,c,d") - tk.MustQuery("select case b when 1 then c when 2 then d else d end from t group by b,c,d") - tk.MustQuery("select c > (select b from t) from t group by c") - tk.MustQuery("select exists (select * from t) from t group by d;") - tk.MustQuery("select c is null from t group by c") - tk.MustQuery("select c is true from t group by c") - tk.MustQuery("select (c+b)*d from t group by c,b,d") - tk.MustQuery("select b in (c,d) from t group by b,c,d") - tk.MustQuery("select b like '%a' from t group by b") - tk.MustQuery("select c REGEXP '1.*' from t group by c") - tk.MustQuery("select -b from t group by b") - tk.MustQuery("select max(a+b) from t") - tk.MustQuery("select avg(a)+1 from t") - tk.MustQuery("select count(c), 5 from t") - // test functional depend on primary key - tk.MustQuery("select * from t group by a") - // test functional depend on unique not null columns - tk.MustQuery("select * from t group by b,d") - // test functional depend on a unique null column - err = tk.ExecToErr("select * from t group by b,c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - // test functional dependency derived from keys in where condition - tk.MustQuery("select * from t where c = d group by b, c") - tk.MustQuery("select t.*, x.* from t, x where t.a = x.a group by t.a") - tk.MustQuery("select t.*, x.* from t, x where t.b = x.b and t.d = x.d group by t.b, t.d") - tk.MustQuery("select t.*, x.* from t, x where t.b = x.a group by t.b, t.d") - tk.MustQuery("select t.b, x.* from t, x where t.b = x.a group by t.b") - tk.MustQuery("select t.*, x.* from t, x where t.c = x.a group by t.b, t.c") - // test functional dependency derived from keys in join - tk.MustQuery("select t.*, x.* from t inner join x on t.a = x.a group by t.a") - tk.MustQuery("select t.*, x.* from t inner join x on (t.b = x.b and t.d = x.d) group by t.b, x.d") - tk.MustQuery("select t.b, x.* from t inner join x on t.b = x.b group by t.b, x.d") - tk.MustQuery("select t.b, x.* from t left join x on t.b = x.b group by t.b, x.d") - tk.MustQuery("select t.b, x.* from t left join x on x.b = t.b group by t.b, x.d") - tk.MustQuery("select x.b, t.* from t right join x on x.b = t.b group by x.b, t.d") - tk.MustQuery("select x.b, t.* from t right join x on t.b = x.b group by x.b, t.d") - err = tk.ExecToErr("select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - err = tk.ExecToErr("select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - - tk.MustQuery("select * from (select * from t) as e group by a") - tk.MustQuery("select * from (select * from t) as e group by b,d") - err = tk.ExecToErr("select * from (select * from t) as e group by b,c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - - // test order by - tk.MustQuery("select c from t group by c,d order by d") - err = tk.ExecToErr("select c from t group by c order by d") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) - // test ambiguous column - err = tk.ExecToErr("select c from t,x group by t.c") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrAmbiguous), "err %v", err) -} - -func TestIssue16279(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'") - tk.MustExec("drop table if exists s") - tk.MustExec("create table s(a int);") - tk.MustQuery("select count(a) , date_format(a, '%Y-%m-%d') from s group by date_format(a, '%Y-%m-%d');") - tk.MustQuery("select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by date_format(a, '%Y-%m-%d');") - tk.MustQuery("select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by xx") -} - -func TestIssue24676(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'") - tk.MustExec("drop table if exists t1") - tk.MustExec(`create table t1( - id int(11) NOT NULL PRIMARY KEY, - c1 int(11) NOT NULL DEFAULT '0' - )`) - tk.MustQuery("SELECT c1 FROM t1 GROUP BY c1 ORDER BY c1 ASC;") - tk.MustQuery("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;") - err := tk.ExecToErr("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;") - require.Truef(t, terror.ErrorEqual(err, plannercore.ErrFieldNotInGroupBy), "err %v", err) -} - -func TestAggPushDownPartitionTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t1") - tk.MustExec(`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) - )`) - tk.MustExec("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)") - tk.MustExec("set @@tidb_opt_agg_push_down = 1") - tk.MustQuery("select /*+ AGG_TO_COP() */ sum(a), sum(b) from t1 where a < 40 group by a").Sort().Check(testkit.Rows( - "0 0", - "24 25", - "28 29", - "3 6", - "36 36", - "6 15", - "60 63", - "69 72", - "9 28", - "93 93", - "99 102")) -} - -func TestIssue13652(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a real)") - tk.MustQuery("select a from t group by (a)") - tk.MustQuery("select a from t group by ((a))") - tk.MustQuery("select a from t group by +a") - tk.MustQuery("select a from t group by ((+a))") - tk.MustGetErrMsg("select a from t group by (-a)", - "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by") -} - -func TestIssue14947(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int)") - tk.MustQuery("select ((+a+1)) as tmp from t group by tmp") -} - -func TestHaving(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (c1 int, c2 int, c3 int)") - tk.MustExec("insert into t values (1,2,3), (2, 3, 1), (3, 1, 2)") - - tk.MustQuery("select c1 as c2, c3 from t having c2 = 2").Check(testkit.Rows("2 1")) - tk.MustQuery("select c1 as c2, c3 from t group by c2 having c2 = 2;").Check(testkit.Rows("1 3")) - tk.MustQuery("select c1 as c2, c3 from t group by c2 having sum(c2) = 2;").Check(testkit.Rows("1 3")) - tk.MustQuery("select c1 as c2, c3 from t group by c3 having sum(c2) = 2;").Check(testkit.Rows("1 3")) - tk.MustQuery("select c1 as c2, c3 from t group by c3 having sum(0) + c2 = 2;").Check(testkit.Rows("2 1")) - tk.MustQuery("select c1 as a from t having c1 = 1;").Check(testkit.Rows("1")) - tk.MustQuery("select t.c1 from t having c1 = 1;").Check(testkit.Rows("1")) - tk.MustQuery("select a.c1 from t as a having c1 = 1;").Check(testkit.Rows("1")) - tk.MustQuery("select c1 as a from t group by c3 having sum(a) = 1;").Check(testkit.Rows("1")) - tk.MustQuery("select c1 as a from t group by c3 having sum(a) + a = 2;").Check(testkit.Rows("1")) - tk.MustQuery("select a.c1 as c, a.c1 as d from t as a, t as b having c1 = 1 limit 1;").Check(testkit.Rows("1 1")) - - tk.MustQuery("select sum(c1) as s from t group by c1 having sum(c1) order by s").Check(testkit.Rows("1", "2", "3")) - tk.MustQuery("select sum(c1) - 1 as s from t group by c1 having sum(c1) - 1 order by s").Check(testkit.Rows("1", "2")) - tk.MustQuery("select 1 from t group by c1 having sum(abs(c2 + c3)) = c1").Check(testkit.Rows("1")) -} - -func TestAggEliminator(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("create table t(a int primary key, b int)") - tk.MustQuery("select min(a), min(a) from t").Check(testkit.Rows(" ")) - tk.MustExec("insert into t values(1, -1), (2, -2), (3, 1), (4, NULL)") - tk.MustQuery("select max(a) from t").Check(testkit.Rows("4")) - tk.MustQuery("select min(b) from t").Check(testkit.Rows("-2")) - tk.MustQuery("select max(b*b) from t").Check(testkit.Rows("4")) - tk.MustQuery("select min(b*b) from t").Check(testkit.Rows("1")) - tk.MustQuery("select group_concat(b, b) from t group by a").Sort().Check(testkit.Rows("-1-1", "-2-2", "11", "")) -} - -func TestClusterIndexMaxMinEliminator(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.Session().GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeOn - tk.MustExec("create table t (a int, b int, c int, primary key(a, b));") - for i := 0; i < 10+1; i++ { - tk.MustExec("insert into t values (?, ?, ?)", i, i, i) - } - tk.MustQuery("select max(a), min(a+b) from t;").Check(testkit.Rows("10 0")) - tk.MustQuery("select max(a+b), min(a+b) from t;").Check(testkit.Rows("20 0")) - tk.MustQuery("select min(a), max(a), min(b), max(b) from t;").Check(testkit.Rows("0 10 0 10")) -} - -func TestMaxMinFloatScalaFunc(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec(`DROP TABLE IF EXISTS T;`) - tk.MustExec(`CREATE TABLE T(A VARCHAR(10), B VARCHAR(10), C FLOAT);`) - tk.MustExec(`INSERT INTO T VALUES('0', "val_b", 12.191);`) - tk.MustQuery(`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;`).Check(testkit.Rows("12.190999984741211")) - tk.MustQuery(`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;`).Check(testkit.Rows("12.190999984741211")) -} - -func TestBuildProjBelowAgg(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (i int);") - tk.MustExec("insert into t values (1), (1), (1),(2),(3),(2),(3),(2),(3);") - rs := tk.MustQuery("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") - rs.Check(testkit.Rows( - "2 3 12 5,5,5 6", - "3 3 15 6,6,6 7", - "4 3 18 7,7,7 8")) -} - func TestInjectProjBelowTopN(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -994,111 +212,6 @@ func TestInjectProjBelowTopN(t *testing.T) { } } -func TestFirstRowEnum(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec(`use test;`) - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a enum('a', 'b'));`) - tk.MustExec(`insert into t values('a');`) - tk.MustQuery(`select a from t group by a;`).Check(testkit.Rows( - `a`, - )) -} - -func TestAggJSON(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a datetime, b json, index idx(a));`) - tk.MustExec(`insert into t values('2019-03-20 21:50:00', '["a", "b", 1]');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:01', '["a", "b", 1]');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:02', '["a", "b", 1]');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:03', '{"k1": "value", "k2": [10, 20]}');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:04', '{"k1": "value", "k2": [10, 20]}');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:05', '{"k1": "value", "k2": [10, 20]}');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:06', '"hello"');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:07', '"hello"');`) - tk.MustExec(`insert into t values('2019-03-20 21:50:08', '"hello"');`) - tk.MustExec(`set @@sql_mode='';`) - tk.MustQuery(`select b from t group by a order by a;`).Check(testkit.Rows( - `["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"`, - )) - tk.MustQuery(`select min(b) from t group by a order by a;`).Check(testkit.Rows( - `["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"`, - )) - tk.MustQuery(`select max(b) from t group by a order by a;`).Check(testkit.Rows( - `["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"`, - )) -} - -func TestIssue10099(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10), b char(10))") - tk.MustExec("insert into t values('1', '222'), ('12', '22')") - tk.MustQuery("select count(distinct a, b) from t").Check(testkit.Rows("2")) - tk.MustQuery("select approx_count_distinct( a, b) from t").Check(testkit.Rows("2")) -} - -func TestIssue10098(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec("create table t(a char(10), b char(10))") - tk.MustExec("insert into t values('1', '222'), ('12', '22')") - tk.MustQuery("select group_concat(distinct a, b) from t").Check(testkit.Rows("1222,1222")) -} - -func TestIssue10608(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t, s;`) - tk.MustExec("create table t(a int)") - tk.MustExec("create table s(a int, b int)") - tk.MustExec("insert into s values(100292, 508931), (120002, 508932)") - tk.MustExec("insert into t values(508931), (508932)") - tk.MustQuery("select (select /*+ stream_agg() */ group_concat(concat(123,'-')) from t where t.a = s.b group by t.a) as t from s;").Check(testkit.Rows("123-", "123-")) - tk.MustQuery("select (select /*+ hash_agg() */ group_concat(concat(123,'-')) from t where t.a = s.b group by t.a) as t from s;").Check(testkit.Rows("123-", "123-")) - - tk.MustExec("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;") - tk.MustExec("INSERT INTO `t49` VALUES ('0','0'),('0','1');") - tk.MustExec("CREATE TABLE `t0` (`c0` blob DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;") - tk.MustExec("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');") - tk.MustExec("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;") - tk.MustQuery("SELECT /*+ STREAM_AGG()*/v0.c0 FROM t49, v0 LEFT OUTER JOIN t0 ON ('Iw') GROUP BY true;"). - Check(testkit.Rows("")) -} - func TestIssue12759HashAggCalledByApply(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1128,98 +241,6 @@ func TestIssue12759HashAggCalledByApply(t *testing.T) { } } -func TestPR15242ShallowCopy(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a json);`) - tk.MustExec(`insert into t values ('{"id": 1,"score":23}');`) - tk.MustExec(`insert into t values ('{"id": 2,"score":23}');`) - tk.MustExec(`insert into t values ('{"id": 1,"score":233}');`) - tk.MustExec(`insert into t values ('{"id": 2,"score":233}');`) - tk.MustExec(`insert into t values ('{"id": 3,"score":233}');`) - tk.Session().GetSessionVars().MaxChunkSize = 2 - tk.MustQuery(`select max(JSON_EXTRACT(a, '$.score')) as max_score,JSON_EXTRACT(a,'$.id') as id from t group by id order by id;`).Check(testkit.Rows("233 1", "233 2", "233 3")) -} - -func TestIssue15690(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.Session().GetSessionVars().MaxChunkSize = 2 - // check for INT type - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a int);`) - tk.MustExec(`insert into t values(null),(null);`) - tk.MustExec(`insert into t values(0),(2),(2),(4),(8);`) - tk.MustQuery(`select /*+ stream_agg() */ distinct * from t;`).Check(testkit.Rows("", "0", "2", "4", "8")) - require.Equal(t, tk.Session().GetSessionVars().StmtCtx.WarningCount(), uint16(0)) - - // check for FLOAT type - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a float);`) - tk.MustExec(`insert into t values(null),(null),(null),(null);`) - tk.MustExec(`insert into t values(1.1),(1.1);`) - tk.MustQuery(`select /*+ stream_agg() */ distinct * from t;`).Check(testkit.Rows("", "1.1")) - require.Equal(t, tk.Session().GetSessionVars().StmtCtx.WarningCount(), uint16(0)) - // check for DECIMAL type - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a decimal(5,1));`) - tk.MustExec(`insert into t values(null),(null),(null);`) - tk.MustExec(`insert into t values(1.1),(2.2),(2.2);`) - tk.MustQuery(`select /*+ stream_agg() */ distinct * from t;`).Check(testkit.Rows("", "1.1", "2.2")) - require.Equal(t, tk.Session().GetSessionVars().StmtCtx.WarningCount(), uint16(0)) - - // check for DATETIME type - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a datetime);`) - tk.MustExec(`insert into t values(null);`) - tk.MustExec(`insert into t values("2019-03-20 21:50:00"),("2019-03-20 21:50:01"), ("2019-03-20 21:50:00");`) - tk.MustQuery(`select /*+ stream_agg() */ distinct * from t;`).Check(testkit.Rows("", "2019-03-20 21:50:00", "2019-03-20 21:50:01")) - require.Equal(t, tk.Session().GetSessionVars().StmtCtx.WarningCount(), uint16(0)) - - // check for JSON type - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a json);`) - tk.MustExec(`insert into t values(null),(null),(null),(null);`) - tk.MustQuery(`select /*+ stream_agg() */ distinct * from t;`).Check(testkit.Rows("")) - require.Equal(t, tk.Session().GetSessionVars().StmtCtx.WarningCount(), uint16(0)) - - // check for char type - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a char);`) - tk.MustExec(`insert into t values(null),(null),(null),(null);`) - tk.MustExec(`insert into t values('a'),('b');`) - tk.MustQuery(`select /*+ stream_agg() */ distinct * from t;`).Check(testkit.Rows("", "a", "b")) - require.Equal(t, tk.Session().GetSessionVars().StmtCtx.WarningCount(), uint16(0)) -} - -func TestIssue15958(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.Session().GetSessionVars().MaxChunkSize = 2 - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(y year);`) - tk.MustExec(`insert into t values (2020), (2000), (2050);`) - tk.MustQuery(`select sum(y) from t`).Check(testkit.Rows("6070")) - tk.MustQuery(`select avg(y) from t`).Check(testkit.Rows("2023.3333")) -} - -func TestIssue17216(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t1") - tk.MustExec(`CREATE TABLE t1 ( - pk int(11) NOT NULL, - col1 decimal(40,20) DEFAULT NULL - )`) - tk.MustExec(`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);`) - tk.MustQuery("SELECT count(distinct col1) FROM t1").Check(testkit.Rows("48")) -} - func TestHashAggRuntimeStat(t *testing.T) { partialInfo := &aggregate.AggWorkerInfo{ Concurrency: 5, @@ -1399,71 +420,6 @@ func TestIssue20658(t *testing.T) { } } -func TestIssue23277(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists t;") - - tk.MustExec("create table t(a tinyint(1));") - tk.MustExec("insert into t values (-120), (127);") - tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000")) - tk.MustExec("drop table t;") - - tk.MustExec("create table t(a smallint(1));") - tk.MustExec("insert into t values (-120), (127);") - tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000")) - tk.MustExec("drop table t;") - - tk.MustExec("create table t(a mediumint(1));") - tk.MustExec("insert into t values (-120), (127);") - tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000")) - tk.MustExec("drop table t;") - - tk.MustExec("create table t(a int(1));") - tk.MustExec("insert into t values (-120), (127);") - tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000")) - tk.MustExec("drop table t;") - - tk.MustExec("create table t(a bigint(1));") - tk.MustExec("insert into t values (-120), (127);") - tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000")) - tk.MustExec("drop table t;") -} - -func TestAvgDecimal(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists td;") - tk.MustExec("create table td (col_bigint bigint(20), col_smallint smallint(6));") - tk.MustExec("insert into td values (null, 22876);") - tk.MustExec("insert into td values (9220557287087669248, 32767);") - tk.MustExec("insert into td values (28030, 32767);") - tk.MustExec("insert into td values (-3309864251140603904,32767);") - tk.MustExec("insert into td values (4,0);") - tk.MustExec("insert into td values (null,0);") - tk.MustExec("insert into td values (4,-23828);") - tk.MustExec("insert into td values (54720,32767);") - tk.MustExec("insert into td values (0,29815);") - tk.MustExec("insert into td values (10017,-32661);") - tk.MustQuery(" SELECT AVG( col_bigint / col_smallint) AS field1 FROM td;").Sort().Check(testkit.Rows("25769363061037.62077260")) - tk.MustQuery(" SELECT AVG(col_bigint) OVER (PARTITION BY col_smallint) as field2 FROM td where col_smallint = -23828;").Sort().Check(testkit.Rows("4.0000")) - tk.MustExec("drop table td;") -} - -// https://github.com/pingcap/tidb/issues/23314 -func TestIssue23314(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(col1 time(2) NOT NULL)") - tk.MustExec("insert into t1 values(\"16:40:20.01\")") - res := tk.MustQuery("select col1 from t1 group by col1") - res.Check(testkit.Rows("16:40:20.01")) -} - func TestAggInDisk(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1584,135 +540,3 @@ func TestRandomPanicConsume(t *testing.T) { } } } - -func TestIssue35295(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t100") - // This bug only happens on partition prune mode = 'static' - tk.MustExec("set @@tidb_partition_prune_mode = 'static'") - tk.MustExec(`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") - );`) - tk.MustExec("insert into t100(col1,money,logtime) values (100,10,'2022-06-09 00:00:00');") - tk.MustExec("insert into t100(col1,money,logtime) values (100,10,'2022-06-10 00:00:00');") - tk.MustQuery("SELECT /*+STREAM_AGG()*/ col1,sum(money) FROM t100 WHERE logtime>='2022-06-09 00:00:00' AND col1=100 ;").Check(testkit.Rows("100 20")) - tk.MustQuery("SELECT /*+HASH_AGG()*/ col1,sum(money) FROM t100 WHERE logtime>='2022-06-09 00:00:00' AND col1=100 ;").Check(testkit.Rows("100 20")) -} - -// https://github.com/pingcap/tidb/issues/27751 -func TestIssue27751(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table test.t(nname char(20));") - tk.MustExec("insert into test.t values ('2'),(null),('11'),('2'),(null),('2'),(null),('11'),('33');") - tk.MustExec("set @@group_concat_max_len=0;") - tk.MustQuery("select group_concat(nname order by 1 separator '#' ) from t;").Check(testkit.Rows("11#1")) - tk.MustQuery("select group_concat(nname order by 1 desc separator '#' ) from t;").Check(testkit.Rows("33#2")) -} - -func TestIssue44795(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec(`use test`) - tk.MustExec(`DROP TABLE IF EXISTS c`) - - // case from tcph. - tk.MustExec("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") - - tk.MustExec("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") - - tk.MustExec("set tidb_opt_agg_push_down=ON;") - - tk.MustQuery("explain format='brief' 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;").Check(testkit.Rows( - "Projection 8000.00 root test.customer.c_custkey, Column#18", - "└─HashAgg 8000.00 root group by:test.customer.c_custkey, funcs:count(Column#19)->Column#18, funcs:firstrow(test.customer.c_custkey)->test.customer.c_custkey", - " └─HashJoin 10000.00 root left outer join, equal:[eq(test.customer.c_custkey, test.orders.o_custkey)]", - " ├─TableReader(Build) 10000.00 root data:TableFullScan", - " │ └─TableFullScan 10000.00 cop[tikv] table:customer keep order:false, stats:pseudo", - " └─HashAgg(Probe) 6400.00 root group by:test.orders.o_custkey, funcs:count(Column#20)->Column#19, funcs:firstrow(test.orders.o_custkey)->test.orders.o_custkey", - " └─TableReader 6400.00 root data:HashAgg", - " └─HashAgg 6400.00 cop[tikv] group by:test.orders.o_custkey, funcs:count(test.orders.o_orderkey)->Column#20", - " └─Selection 8000.00 cop[tikv] not(like(test.orders.o_comment, \"%special%requests%\", 92))", - " └─TableFullScan 10000.00 cop[tikv] table:orders keep order:false, stats:pseudo")) -} - -func TestIssue26885(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec(`use test`) - tk.MustExec(`SET sql_mode = 'NO_ENGINE_SUBSTITUTION';`) - tk.MustExec(`DROP TABLE IF EXISTS t1;`) - - tk.MustExec("CREATE TABLE t1 (c1 ENUM('a', '', 'b'));") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('b');") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('');") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('a');") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('');") - tk.MustExec("INSERT INTO t1 (c1) VALUES (0);") - tk.MustQuery("select * from t1").Check(testkit.Rows("b", "", "a", "", "")) - tk.MustQuery("select c1 + 0 from t1").Sort().Check(testkit.Rows("0", "1", "2", "2", "3")) - tk.MustQuery("SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;").Check(testkit.Rows("0 1", "1 1", "2 2", "3 1")) - - tk.MustExec("alter table t1 add index idx(c1); ") - tk.MustQuery("select c1 + 0 from t1").Sort().Check(testkit.Rows("0", "1", "2", "2", "3")) - tk.MustQuery("SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;").Check(testkit.Rows("0 1", "1 1", "2 2", "3 1")) - - tk.MustExec(`DROP TABLE IF EXISTS t1;`) - tk.MustExec("CREATE TABLE t1 (c1 ENUM('a', 'b', 'c'));") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('b');") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('a');") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('b');") - tk.MustExec("INSERT INTO t1 (c1) VALUES ('c');") - tk.MustExec("INSERT INTO t1 (c1) VALUES (0);") - tk.MustQuery("select * from t1").Check(testkit.Rows("b", "a", "b", "c", "")) - tk.MustQuery("SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;").Check(testkit.Rows("0 1", "1 1", "2 2", "3 1")) -} diff --git a/tests/integrationtest/r/executor/aggregate.result b/tests/integrationtest/r/executor/aggregate.result new file mode 100644 index 0000000000000..f9847d92c5629 --- /dev/null +++ b/tests/integrationtest/r/executor/aggregate.result @@ -0,0 +1,1555 @@ +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); +alter table t add 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='brief' 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 estRows task access object operator info +Projection 8000.00 root executor__aggregate.customer.c_custkey, Column#18 +└─HashAgg 8000.00 root group by:executor__aggregate.customer.c_custkey, funcs:count(Column#19)->Column#18, funcs:firstrow(executor__aggregate.customer.c_custkey)->executor__aggregate.customer.c_custkey + └─HashJoin 10000.00 root left outer join, equal:[eq(executor__aggregate.customer.c_custkey, executor__aggregate.orders.o_custkey)] + ├─TableReader(Build) 10000.00 root data:TableFullScan + │ └─TableFullScan 10000.00 cop[tikv] table:customer keep order:false, stats:pseudo + └─HashAgg(Probe) 6400.00 root group by:executor__aggregate.orders.o_custkey, funcs:count(Column#20)->Column#19, funcs:firstrow(executor__aggregate.orders.o_custkey)->executor__aggregate.orders.o_custkey + └─TableReader 6400.00 root data:HashAgg + └─HashAgg 6400.00 cop[tikv] group by:executor__aggregate.orders.o_custkey, funcs:count(executor__aggregate.orders.o_orderkey)->Column#20 + └─Selection 8000.00 cop[tikv] not(like(executor__aggregate.orders.o_comment, "%special%requests%", 92)) + └─TableFullScan 10000.00 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; +[parser:1064]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; diff --git a/tests/integrationtest/t/executor/aggregate.test b/tests/integrationtest/t/executor/aggregate.test new file mode 100644 index 0000000000000..f5b08430bdf3b --- /dev/null +++ b/tests/integrationtest/t/executor/aggregate.test @@ -0,0 +1,835 @@ +# TestAggPrune +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; +select sum(b) from t; +select id, count(c) from t group by id; +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; +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; +select approx_count_distinct( b, c, d) from t group by id order by id; +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; + +# TestAggPushDown +drop table if exists t; +create table t (a int, b int, c int); +alter table t add index idx(a, b, c); +select count(a) from t group by a; +select count(a) from t; +insert t values(0,0,0); +select distinct b from t; +select count(b) from t group by a; +insert t values(1,1,1),(3,3,6),(3,2,5),(2,1,4),(1,1,3),(1,1,2); +--sorted_result +select count(a) from t where b>0 group by a, b; +select count(a) from t where b>0 group by a, b order by a; +select count(a) from t where b>0 group by a, b order by a limit 1; +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; +select a, count(b) from (select * from t union all select * from tt) k group by a order by a; + +# TestIssue16279 +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'); +select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by date_format(a, '%Y-%m-%d'); +select count(a) , date_format(a, '%Y-%m-%d') as xx from s group by xx; +set sql_mode = default; + +# TestAggPushDownPartitionTable +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; +--sorted_result +select /*+ AGG_TO_COP() */ sum(a), sum(b) from t1 where a < 40 group by a; +set @@tidb_opt_agg_push_down = default; + +# TestIssue14947 +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; +set @@tidb_opt_agg_push_down = default; + +# TestHaving +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; +select c1 as c2, c3 from t group by c2 having c2 = 2; +select c1 as c2, c3 from t group by c2 having sum(c2) = 2; +select c1 as c2, c3 from t group by c3 having sum(c2) = 2; +select c1 as c2, c3 from t group by c3 having sum(0) + c2 = 2; +select c1 as a from t having c1 = 1; +select t.c1 from t having c1 = 1; +select a.c1 from t as a having c1 = 1; +select c1 as a from t group by c3 having sum(a) = 1; +select c1 as a from t group by c3 having sum(a) + a = 2; +select a.c1 as c, a.c1 as d from t as a, t as b having c1 = 1 limit 1; +select sum(c1) as s from t group by c1 having sum(c1) order by s; +select sum(c1) - 1 as s from t group by c1 having sum(c1) - 1 order by s; +select 1 from t group by c1 having sum(abs(c2 + c3)) = c1; +set @@tidb_opt_agg_push_down = default; + +# TestAggEliminator +drop table if exists t; +create table t(a int primary key, b int); +select min(a), min(a) from t; +insert into t values(1, -1), (2, -2), (3, 1), (4, NULL); +select max(a) from t; +select min(b) from t; +select max(b*b) from t; +select min(b*b) from t; +--sorted_result +select group_concat(b, b) from t group by a; + +# TestClusterIndexMaxMinEliminator +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; +select max(a+b), min(a+b) from t; +select min(a), max(a), min(b), max(b) from t; +set @@tidb_enable_clustered_index = default; + +# TestMaxMinFloatScalaFunc +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; +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; + +# TestBuildProjBelowAgg +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; + +# TestFirstRowEnum +drop table if exists t; +create table t(a enum('a', 'b')); +insert into t values('a'); +select a from t group by a; + +# TestAggJSON +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; +select min(b) from t group by a order by a; +select max(b) from t group by a order by a; +set @@sql_mode=default; + +# TestIssue10099 +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; +select approx_count_distinct( a, b) from t; + +# TestIssue10098 +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; + +# TestIssue10608 +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; +select (select /*+ hash_agg() */ group_concat(concat(123,'-')) from t where t.a = s.b group by t.a) as t from s; +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; + +# TestPR15242ShallowCopy +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; +set tidb_max_chunk_size = default; + +# TestIssue15690 +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); +--enable_warnings +select /*+ stream_agg() */ distinct * from t; +--disable_warnings +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); +--enable_warnings +select /*+ stream_agg() */ distinct * from t; +--disable_warnings +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); +--enable_warnings +select /*+ stream_agg() */ distinct * from t; +--disable_warnings +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"); +--enable_warnings +select /*+ stream_agg() */ distinct * from t; +--disable_warnings +drop table if exists t; +create table t(a json); +insert into t values(null),(null),(null),(null); +--enable_warnings +select /*+ stream_agg() */ distinct * from t; +--disable_warnings +drop table if exists t; +create table t(a char); +insert into t values(null),(null),(null),(null); +insert into t values('a'),('b'); +--enable_warnings +select /*+ stream_agg() */ distinct * from t; +--disable_warnings +set tidb_max_chunk_size = default; + +# TestIssue15958 +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; +select avg(y) from t; +set tidb_max_chunk_size = default; + +# TestIssue17216 +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; + +# TestIssue23277 +drop table if exists t; +create table t(a tinyint(1)); +insert into t values (-120), (127); +--sorted_result +select avg(a) from t group by a; +drop table t; +create table t(a smallint(1)); +insert into t values (-120), (127); +--sorted_result +select avg(a) from t group by a; +drop table t; +create table t(a mediumint(1)); +insert into t values (-120), (127); +--sorted_result +select avg(a) from t group by a; +drop table t; +create table t(a int(1)); +insert into t values (-120), (127); +--sorted_result +select avg(a) from t group by a; +drop table t; +create table t(a bigint(1)); +insert into t values (-120), (127); +--sorted_result +select avg(a) from t group by a; +drop table t; + +# TestAvgDecimal +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); +--sorted_result +SELECT AVG( col_bigint / col_smallint) AS field1 FROM td; +--sorted_result +SELECT AVG(col_bigint) OVER (PARTITION BY col_smallint) as field2 FROM td where col_smallint = -23828; +drop table td; + +# TestIssue23314 +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; + +# TestIssue35295 +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 ; +SELECT /*+HASH_AGG()*/ col1,sum(money) FROM t100 WHERE logtime>='2022-06-09 00:00:00' AND col1=100 ; +set @@tidb_partition_prune_mode = default; + +# TestIssue27751 +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; +select group_concat(nname order by 1 desc separator '#' ) from t; +set @@group_concat_max_len=default; + +# TestIssue44795 +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='brief' 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; +set tidb_opt_agg_push_down=default; + +# TestIssue26885 +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; +--sorted_result +select c1 + 0 from t1; +SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1; +alter table t1 add index idx(c1); ; +--sorted_result +select c1 + 0 from t1; +SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1; +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; +SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1; +SET sql_mode = default; + +# TestAggregation +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; +select bit_or(c) from t where NULL; +select bit_xor(c) from t where NULL; +select approx_count_distinct(c) from t where NULL; +select count(*) from t; +select count(*) from t group by d order by c; +select distinct 99 from t group by d having d > 0; +select count(*) from t having 1 = 0; +select c,d from t group by d order by d; +select - c, c as d from t group by c having null not between c and avg(distinct d) - d; +select - c as c from t group by c having t.c > 5; +select t1.c from t t1, t t2 group by c having c > 5; +select count(*) from (select d, c from t) k where d != 0 group by d order by c; +select c as a from t group by d having a < 0; +select c as a from t group by d having sum(a) = 2; +select count(distinct c) from t group by d order by c; +select approx_count_distinct(c) from t group by d order by c; +select sum(c) as a from t group by d order by a; +select sum(c) as a, sum(c+1), sum(c), sum(c+1) from t group by d order by a; +select count(distinct c,d) from t; +select approx_count_distinct(c,d) from t; +--error 1064 +select count(c,d) from t; +select d*2 as ee, sum(c) from t group by ee order by ee; +select sum(distinct c) as a from t group by d order by a; +select min(c) as a from t group by d order by a; +select max(c) as a from t group by d order by a; +select avg(c) as a from t group by d order by a; +select c, approx_count_distinct(d) as a from t group by c order by a, c; +select d, d + 1 from t group by d order by d; +select count(*) from t; +select count(distinct d) from t; +select approx_count_distinct(d) from t; +select count(*) as a from t group by d having sum(c) > 3 order by a; +select max(c) from t group by d having sum(c) > 3 order by avg(c) desc; +select sum(-1) from t a left outer join t b on not null is null; +select count(*), b.d from t a left join t b on a.c = b.d group by b.d order by b.d; +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; +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; +select count(*), b.d from t b right join t a on a.c = b.d group by b.d order by b.d; +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; +select count(*) from t a , t b; +select count(*) from t a , t b, t c; +select count(*) from t a , t b where a.c = b.d; +select count(a.d), sum(b.c) from t a , t b where a.c = b.d order by a.d; +select count(*) from t a , t b, t c where a.c = b.d and b.d = c.d; +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; +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; +select count(*) from t a , t b where a.c = b.d and a.c + b.d = 2; +select count(*) from t a join t b having sum(a.c) < 0; +select count(*) from t a join t b where a.c < 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; +select 1-d as d from t having d < 0 order by d desc; +select 1-d as d from t having d + 1 < 0 order by d + 1; +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; +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; +select d*d as d from t group by d having d = -1; +select d, 1-d as d, c as d from t order by d; +select d, 1-d as d, c as d from t order by d+1; +select d, 1-d as d, c as d from t group by d order by d; +select d as d1, t.d as d1, 1-d as d1, c as d1 from t having d1 < 10 order by d; +select d*d as d1, c as d1 from t group by d1 order by d1; +select d*d as d1, c as d1 from t group by 2; +select * from t group by 2 order by d; +select * , sum(d) from t group by 1 order by d; +select sum(d), t.* from t group by 2 order by d; +select d as d, c as d from t group by d + 1 order by t.d; +select c as d, c as d from t group by d order by d; +--error 1052 +select d as d, c as d from t group by d; +--error 1052 +select t.d, c as d from t group by d; +select *, c+1 as d from t group by 3 order by d; +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; +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; +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; +select sum(b) from (select * from t1) t; +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; +select sum(b) from (select * from t1) t group by a order by a; +select count(b) from (select * from t1) t group by a order by a; +select max(b) from (select * from t1) t group by a order by a; +select min(b) from (select * from t1) t group by a order by a; +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; +select sum(distinct b) from (select * from t1) t group by a order by a; +select count(distinct b) from (select * from t1) t group by a order by a; +select approx_count_distinct(b) from (select * from t1) t group by a order by a; +select max(distinct b) from (select * from t1) t group by a order by a; +select min(distinct b) from (select * from t1) t group by a order by a; +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; +select sum(distinct b) from (select * from t1) t group by a order by a; +select count(distinct b) from (select * from t1) t group by a order by a; +select max(distinct b) from (select * from t1) t group by a order by a; +select min(distinct b) from (select * from t1) t group by a order by a; +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; +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; +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; +set @@session.tidb_opt_insubq_to_join_and_agg = 1; +select sum(c1 in (select * from t2)) from t1; +select sum(c1) k from (select * from t1 union all select * from t2)t group by c1 * 2 order by k; +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; +select approx_count_distinct( c), approx_count_distinct( a,b) from t; +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; +select distinct approx_count_distinct( a) from t; +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; +select 10 from idx_agg group by b; +select 11 from idx_agg group by a; +set @@tidb_init_chunk_size=1; +--sorted_result +select group_concat(b) from idx_agg group by b; +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; +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; +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; +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; +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; +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; +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; +select max(a) from t group by a order by 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; +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; +insert into t value(1, null), (null, 1), (1, 2), (3, 4); +select group_concat(a, b), group_concat(distinct a,b) from t; +set @@session.tidb_opt_distinct_agg_push_down = 0; +select count(distinct a) from t; +set @@session.tidb_opt_distinct_agg_push_down = 1; +select count(distinct a) from t; +set @@session.tidb_opt_distinct_agg_push_down = 0; +select approx_count_distinct( a) from t; +drop table t; +create table t(a decimal(10, 4)); +select 10 from t group by a; +insert into t value(0), (-0.9871), (-0.9871); +select 10 from t group by a; +select sum(a) from (select a from t union all select a from t) tmp; +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; +insert into t values(2, 3, 4, 5, 6, 7.2, 8.3, 9); +--sorted_result +select a, var_pop(b) over w, var_pop(c) over w from t window w as (partition by a); +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; +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; +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; +insert into t values(2, 322, 0.8, 2.22, 6); +--sorted_result +select a, variance(b) over w from t window w as (partition by a); +-- error 1305 +select std_samp(a) from t; +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; +select sum(a) from t1 group by c order by c; +select sum(b) from t1 group by a order by a; +select sum(b) from t1 group by c order by c; +select sum(c) from t1 group by a order by a; +select sum(c) from t1 group by b order by b; +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; +select stddev_pop(a) from t1 group by grp order by grp; +select sum(a)+count(a)+avg(a)+stddev_pop(a) as sum from t1 group by grp order by grp; +select std(all a) from t1; +select std(a) from t1 group by grp order by grp; +select sum(a)+count(a)+avg(a)+std(a) as sum from t1 group by grp order by grp; +select stddev(all a) from t1; +select stddev(a) from t1 group by grp order by grp; +select sum(a)+count(a)+avg(a)+stddev(a) as sum from t1 group by grp order by grp; +drop table if exists t1; +CREATE TABLE t1 (a int, b int); +select stddev_pop(b) from t1; +select std(b) from t1; +select stddev(b) from t1; +insert into t1 values (1,null); +select stddev_pop(b) from t1 group by a order by a; +select std(b) from t1 group by a order by a; +select stddev(b) from t1 group by a order by a; +insert into t1 values (1,null); +insert into t1 values (2,null); +select stddev_pop(b) from t1 group by a order by a; +select std(b) from t1 group by a order by a; +select stddev(b) from t1 group by a order by a; +insert into t1 values (2,1); +select stddev_pop(b) from t1 group by a order by a; +select std(b) from t1 group by a order by a; +select stddev(b) from t1 group by a order by a; +insert into t1 values (3,1); +select stddev_pop(b) from t1 group by a order by a; +select std(b) from t1 group by a order by a; +select stddev(b) from t1 group by a order by a; +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; +drop table if exists t1; +CREATE TABLE t1 (id int); +insert into t1 values (1),(2); +select stddev_pop(id) from t1; +insert into t1 values (1); +select stddev_pop(distinct id) from t1; +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; + +# TestOnlyFullGroupBy +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; +select max(a), any_value(c) from t group by d; +--error 1055 +select * from t group by d; +--error 1055 +select b-c from t group by b+c; +--error 1055 +select (b-c)*(b+c), min(a) from t group by b+c, b-c; +--error 1055 +select b between c and d from t group by b,c; +--error 1055 +select case b when 1 then c when 2 then d else d end from t group by b,c; +--error 1055 +select c > (select b from t) from t group by b; +--error 1055 +select c is null from t group by b; +--error 1055 +select c is true from t group by b; +--error 1055 +select (c+b)*d from t group by c,d; +--error 1055 +select b in (c,d) from t group by b,c; +--error 1055 +select b like '%a' from t group by c; +--error 1055 +select c REGEXP '1.*' from t group by b; +--error 1055 +select -b from t group by c; +--error 1055 +select a, max(b) from t; +--error 1055 +select sum(a)+b from t; +--error 1055 +select count(b), c from t; +select count(b), any_value(c) from t; +select count(b), any_value(c) + 2 from t; +--error 1055 +select distinct a, b, count(a) from t; +select a from t group by a,b,c; +select b from t group by b; +--error 1055 +select b*rand() from t group by b; +select b as e from t group by b; +select b+c from t group by b+c; +select b+c, min(a) from t group by b+c, b-c; +select b+c, min(a) from t group by b, c; +select b+c from t group by b,c; +--error 1055 +select b+c from (select b, b+rand() as c from t) t group by b; +select b between c and d from t group by b,c,d; +select case b when 1 then c when 2 then d else d end from t group by b,c,d; +select c > (select b from t) from t group by c; +select exists (select * from t) from t group by d; +select c is null from t group by c; +select c is true from t group by c; +select (c+b)*d from t group by c,b,d; +select b in (c,d) from t group by b,c,d; +select b like '%a' from t group by b; +select c REGEXP '1.*' from t group by c; +select -b from t group by b; +select max(a+b) from t; +select avg(a)+1 from t; +select count(c), 5 from t; +select * from t group by a; +select * from t group by b,d; +--error 1055 +select * from t group by b,c; +select * from t where c = d group by b, c; +select t.*, x.* from t, x where t.a = x.a group by t.a; +select t.*, x.* from t, x where t.b = x.b and t.d = x.d group by t.b, t.d; +select t.*, x.* from t, x where t.b = x.a group by t.b, t.d; +select t.b, x.* from t, x where t.b = x.a group by t.b; +select t.*, x.* from t, x where t.c = x.a group by t.b, t.c; +select t.*, x.* from t inner join x on t.a = x.a group by t.a; +select t.*, x.* from t inner join x on (t.b = x.b and t.d = x.d) group by t.b, x.d; +select t.b, x.* from t inner join x on t.b = x.b group by t.b, x.d; +select t.b, x.* from t left join x on t.b = x.b group by t.b, x.d; +select t.b, x.* from t left join x on x.b = t.b group by t.b, x.d; +select x.b, t.* from t right join x on x.b = t.b group by x.b, t.d; +select x.b, t.* from t right join x on t.b = x.b group by x.b, t.d; +--error 1055 +select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d; +--error 1055 +select t.b, x.* from t right join x on t.b = x.b group by t.b, x.d; +select * from (select * from t) as e group by a; +select * from (select * from t) as e group by b,d; +--error 1055 +select * from (select * from t) as e group by b,c; +select c from t group by c,d order by d; +--error 1055 +select c from t group by c order by d; +--error 1055 +select c from t,x group by t.c; +set sql_mode = default; +set @@session.tidb_enable_new_only_full_group_by_check = default; + +# TestIssue24676 +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; +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; +--error 1055 +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; +set sql_mode = default; + +# TestIssue13652 +set sql_mode = 'ONLY_FULL_GROUP_BY'; +drop table if exists t; +create table t(a real); +select a from t group by (a); +select a from t group by ((a)); +select a from t group by +a; +select a from t group by ((+a)); +-- error 1055 +select a from t group by (-a); +set sql_mode = default;