From cbfbfc2cba24d1bf47f3552c1d19f0e7252a33db Mon Sep 17 00:00:00 2001 From: Zhuhe Fang Date: Mon, 20 Dec 2021 11:11:46 +0800 Subject: [PATCH 1/3] cherry pick #30773 to release-5.3 Signed-off-by: ti-srebot --- executor/aggregate_test.go | 25 +++++++++++++++ executor/builder.go | 2 +- executor/tiflash_test.go | 46 ++++++++++++++++++++++++++++ expression/aggregation/descriptor.go | 9 ++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index b3ed40178f65b..b44157c654271 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -1461,6 +1461,31 @@ func (s *testSuiteAgg) TestIssue23277(c *C) { tk.MustExec("drop table t;") } +<<<<<<< HEAD +======= +func TestAvgDecimal(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + 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;") +} + +>>>>>>> e3c56b75e... executor: buildWindow cannot call typeInfer twice (#30773) // https://github.com/pingcap/tidb/issues/23314 func (s *testSuiteAgg) TestIssue23314(c *C) { tk := testkit.NewTestKit(c, s.store) diff --git a/executor/builder.go b/executor/builder.go index db5c9dfb9302d..3cc5afbdfd893 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -4132,7 +4132,7 @@ func (b *executorBuilder) buildWindow(v *plannercore.PhysicalWindow) Executor { partialResults := make([]aggfuncs.PartialResult, 0, len(v.WindowFuncDescs)) resultColIdx := v.Schema().Len() - len(v.WindowFuncDescs) for _, desc := range v.WindowFuncDescs { - aggDesc, err := aggregation.NewAggFuncDesc(b.ctx, desc.Name, desc.Args, false) + aggDesc, err := aggregation.NewAggFuncDescForWindowFunc(b.ctx, desc, false) if err != nil { b.err = err return nil diff --git a/executor/tiflash_test.go b/executor/tiflash_test.go index 5bc9503cc29ff..f61cb3c8ad0ce 100644 --- a/executor/tiflash_test.go +++ b/executor/tiflash_test.go @@ -773,6 +773,52 @@ func (s *tiflashTestSuite) TestUnionWithEmptyDualTable(c *C) { tk.MustQuery("select count(*) from (select a , b from t union all select a , c from t1 where false) tt").Check(testkit.Rows("1")) } +<<<<<<< HEAD +======= +func (s *tiflashTestSuite) TestAvgOverflow(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + // avg int + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a decimal(1,0))") + tk.MustExec("alter table t set tiflash replica 1") + tb := testGetTableByName(c, tk.Se, "test", "t") + err := domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true) + c.Assert(err, IsNil) + tk.MustExec("insert into t values(9)") + for i := 0; i < 16; i++ { + tk.MustExec("insert into t select * from t") + } + tk.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\"") + tk.MustExec("set @@session.tidb_enforce_mpp=ON") + tk.MustQuery("select avg(a) from t group by a").Check(testkit.Rows("9.0000")) + tk.MustExec("drop table if exists t") + + // avg decimal + tk.MustExec("drop table if exists td;") + tk.MustExec("create table td (col_bigint bigint(20), col_smallint smallint(6));") + tk.MustExec("alter table td set tiflash replica 1") + tb = testGetTableByName(c, tk.Se, "test", "td") + err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true) + c.Assert(err, IsNil) + 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.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\"") + tk.MustExec("set @@session.tidb_enforce_mpp=ON") + 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 if exists td;") +} + +>>>>>>> e3c56b75e... executor: buildWindow cannot call typeInfer twice (#30773) func (s *tiflashTestSuite) TestMppApply(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/expression/aggregation/descriptor.go b/expression/aggregation/descriptor.go index 1d5381f6c973d..2d1361a5fcb61 100644 --- a/expression/aggregation/descriptor.go +++ b/expression/aggregation/descriptor.go @@ -42,6 +42,7 @@ type AggFuncDesc struct { } // NewAggFuncDesc creates an aggregation function signature descriptor. +// this func cannot be called twice as the TypeInfer has changed the type of args in the first time. func NewAggFuncDesc(ctx sessionctx.Context, name string, args []expression.Expression, hasDistinct bool) (*AggFuncDesc, error) { b, err := newBaseFuncDesc(ctx, name, args) if err != nil { @@ -50,6 +51,14 @@ func NewAggFuncDesc(ctx sessionctx.Context, name string, args []expression.Expre return &AggFuncDesc{baseFuncDesc: b, HasDistinct: hasDistinct}, nil } +// NewAggFuncDescForWindowFunc creates an aggregation function from window functions, where baseFuncDesc may be ready. +func NewAggFuncDescForWindowFunc(ctx sessionctx.Context, Desc *WindowFuncDesc, hasDistinct bool) (*AggFuncDesc, error) { + if Desc.RetTp == nil { // safety check + return NewAggFuncDesc(ctx, Desc.Name, Desc.Args, hasDistinct) + } + return &AggFuncDesc{baseFuncDesc: baseFuncDesc{Desc.Name, Desc.Args, Desc.RetTp}, HasDistinct: hasDistinct}, nil +} + // String implements the fmt.Stringer interface. func (a *AggFuncDesc) String() string { buffer := bytes.NewBufferString(a.Name) From 6e8e743c8c9b682067031ccacf521b814ee4b306 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Wed, 23 Feb 2022 16:21:45 +0800 Subject: [PATCH 2/3] fix conflict Signed-off-by: guo-shaoge --- executor/aggregate_test.go | 9 ++------- executor/tiflash_test.go | 3 --- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index b44157c654271..369a1572d8580 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -1461,12 +1461,8 @@ func (s *testSuiteAgg) TestIssue23277(c *C) { tk.MustExec("drop table t;") } -<<<<<<< HEAD -======= -func TestAvgDecimal(t *testing.T) { - store, clean := testkit.CreateMockStore(t) - defer clean() - tk := testkit.NewTestKit(t, store) +func (s *testSuiteAgg) TestAvgDecimal(c *C) { + tk := testkit.NewTestKit(c, s.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));") @@ -1485,7 +1481,6 @@ func TestAvgDecimal(t *testing.T) { tk.MustExec("drop table td;") } ->>>>>>> e3c56b75e... executor: buildWindow cannot call typeInfer twice (#30773) // https://github.com/pingcap/tidb/issues/23314 func (s *testSuiteAgg) TestIssue23314(c *C) { tk := testkit.NewTestKit(c, s.store) diff --git a/executor/tiflash_test.go b/executor/tiflash_test.go index f61cb3c8ad0ce..28f42eb2f1ae9 100644 --- a/executor/tiflash_test.go +++ b/executor/tiflash_test.go @@ -773,8 +773,6 @@ func (s *tiflashTestSuite) TestUnionWithEmptyDualTable(c *C) { tk.MustQuery("select count(*) from (select a , b from t union all select a , c from t1 where false) tt").Check(testkit.Rows("1")) } -<<<<<<< HEAD -======= func (s *tiflashTestSuite) TestAvgOverflow(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") @@ -818,7 +816,6 @@ func (s *tiflashTestSuite) TestAvgOverflow(c *C) { tk.MustExec("drop table if exists td;") } ->>>>>>> e3c56b75e... executor: buildWindow cannot call typeInfer twice (#30773) func (s *tiflashTestSuite) TestMppApply(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") From 61096bbd8cc3cd05e75384bb33040e7fa6416366 Mon Sep 17 00:00:00 2001 From: Zhuhe Fang Date: Wed, 22 Jun 2022 16:53:25 +0800 Subject: [PATCH 3/3] Update tiflash_test.go --- executor/tiflash_test.go | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/executor/tiflash_test.go b/executor/tiflash_test.go index 557344ee05fa9..a2bcd815bfeff 100644 --- a/executor/tiflash_test.go +++ b/executor/tiflash_test.go @@ -795,28 +795,13 @@ func (s *tiflashTestSuite) TestUnionWithEmptyDualTable(c *C) { func (s *tiflashTestSuite) TestAvgOverflow(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") - // avg int - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a decimal(1,0))") - tk.MustExec("alter table t set tiflash replica 1") - tb := testGetTableByName(c, tk.Se, "test", "t") - err := domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true) - c.Assert(err, IsNil) - tk.MustExec("insert into t values(9)") - for i := 0; i < 16; i++ { - tk.MustExec("insert into t select * from t") - } - tk.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\"") - tk.MustExec("set @@session.tidb_enforce_mpp=ON") - tk.MustQuery("select avg(a) from t group by a").Check(testkit.Rows("9.0000")) - tk.MustExec("drop table if exists t") // avg decimal tk.MustExec("drop table if exists td;") tk.MustExec("create table td (col_bigint bigint(20), col_smallint smallint(6));") tk.MustExec("alter table td set tiflash replica 1") - tb = testGetTableByName(c, tk.Se, "test", "td") - err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true) + tb := testGetTableByName(c, tk.Se, "test", "td") + err := domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true) c.Assert(err, IsNil) tk.MustExec("insert into td values (null, 22876);") tk.MustExec("insert into td values (9220557287087669248, 32767);")