From 8d56ef6c7e126f70e48c5c5db0812d8ba086e69c Mon Sep 17 00:00:00 2001 From: Shenghui Wu <793703860@qq.com> Date: Tue, 19 Jul 2022 01:17:07 +0800 Subject: [PATCH] cherry pick #36271 to release-4.0 Signed-off-by: ti-srebot --- expression/aggregation/aggregation.go | 7 ++++ planner/core/integration_test.go | 50 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/expression/aggregation/aggregation.go b/expression/aggregation/aggregation.go index 39ae1190dcb94..7042ba58c0807 100644 --- a/expression/aggregation/aggregation.go +++ b/expression/aggregation/aggregation.go @@ -211,8 +211,15 @@ func CheckAggPushDown(aggFunc *AggFuncDesc, storeType kv.StoreType) bool { // CheckAggPushFlash checks whether an agg function can be pushed to flash storage. func CheckAggPushFlash(aggFunc *AggFuncDesc) bool { switch aggFunc.Name { +<<<<<<< HEAD case ast.AggFuncSum, ast.AggFuncCount, ast.AggFuncMin, ast.AggFuncMax, ast.AggFuncAvg, ast.AggFuncFirstRow, ast.AggFuncApproxCountDistinct: +======= + case ast.AggFuncCount, ast.AggFuncMin, ast.AggFuncMax, ast.AggFuncFirstRow, ast.AggFuncApproxCountDistinct: +>>>>>>> de017e9ee... expression: forbid aggregate function with json type pushdown to tiflash wrongly (#36271) return true + case ast.AggFuncSum, ast.AggFuncAvg, ast.AggFuncGroupConcat: + // Now tiflash doesn't support CastJsonAsReal and CastJsonAsString. + return aggFunc.Args[0].GetType().GetType() != mysql.TypeJSON } return false } diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 15805ff617e92..9ac1ba637c5cc 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -2161,3 +2161,53 @@ func (s *testIntegrationSuite) TestIssues29711(c *C) { )) } + +func TestAggWithJsonPushDownToTiFlash(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 t") + tk.MustExec("create table t(a json);") + tk.MustExec("insert into t values(null);") + tk.MustExec("set @@tidb_allow_mpp=1; set @@tidb_enforce_mpp=1;") + tk.MustExec("set @@tidb_isolation_read_engines = 'tiflash'") + + // Create virtual tiflash replica info. + dom := domain.GetDomain(tk.Session()) + is := dom.InfoSchema() + db, exists := is.SchemaByName(model.NewCIStr("test")) + require.True(t, exists) + for _, tblInfo := range db.Tables { + if tblInfo.Name.L == "t" { + tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{ + Count: 1, + Available: true, + } + } + } + + rows := [][]interface{}{ + {"HashAgg_6", "root", "funcs:avg(Column#4)->Column#3"}, + {"└─Projection_19", "root", "cast(test.t.a, double BINARY)->Column#4"}, + {" └─TableReader_12", "root", "data:TableFullScan_11"}, + {" └─TableFullScan_11", "cop[tiflash]", "keep order:false, stats:pseudo"}, + } + tk.MustQuery("explain select avg(a) from t;").CheckAt([]int{0, 2, 4}, rows) + + rows = [][]interface{}{ + {"HashAgg_6", "root", "funcs:sum(Column#4)->Column#3"}, + {"└─Projection_19", "root", "cast(test.t.a, double BINARY)->Column#4"}, + {" └─TableReader_12", "root", "data:TableFullScan_11"}, + {" └─TableFullScan_11", "cop[tiflash]", "keep order:false, stats:pseudo"}, + } + tk.MustQuery("explain select sum(a) from t;").CheckAt([]int{0, 2, 4}, rows) + + rows = [][]interface{}{ + {"HashAgg_6", "root", "funcs:group_concat(Column#4 separator \",\")->Column#3"}, + {"└─Projection_13", "root", "cast(test.t.a, var_string(4294967295))->Column#4"}, + {" └─TableReader_10", "root", "data:TableFullScan_9"}, + {" └─TableFullScan_9", "cop[tiflash]", "keep order:false, stats:pseudo"}, + } + tk.MustQuery("explain select /*+ hash_agg() */ group_concat(a) from t;").CheckAt([]int{0, 2, 4}, rows) +}