From de017e9eea670d050ae5dc7519da11baf632efa0 Mon Sep 17 00:00:00 2001 From: Shenghui Wu <793703860@qq.com> Date: Tue, 19 Jul 2022 01:17:07 +0800 Subject: [PATCH] expression: forbid aggregate function with json type pushdown to tiflash wrongly (#36271) close pingcap/tidb#28753 --- expression/aggregation/aggregation.go | 5 ++- planner/core/integration_test.go | 50 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/expression/aggregation/aggregation.go b/expression/aggregation/aggregation.go index 19ab63afb28d3..5d22c587f3c34 100644 --- a/expression/aggregation/aggregation.go +++ b/expression/aggregation/aggregation.go @@ -222,8 +222,11 @@ func CheckAggPushFlash(aggFunc *AggFuncDesc) bool { } } switch aggFunc.Name { - case ast.AggFuncSum, ast.AggFuncCount, ast.AggFuncMin, ast.AggFuncMax, ast.AggFuncAvg, ast.AggFuncFirstRow, ast.AggFuncApproxCountDistinct, ast.AggFuncGroupConcat: + case ast.AggFuncCount, ast.AggFuncMin, ast.AggFuncMax, ast.AggFuncFirstRow, ast.AggFuncApproxCountDistinct: 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 3f15ccf4fffc8..0ba8d6d97145f 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -7117,3 +7117,53 @@ func TestGetFormatPushDownToTiFlash(t *testing.T) { " └─Projection 10000.00 mpp[tiflash] get_format(DATE, test.t.location)->Column#3", " └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo")) } + +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) +}