Skip to content

Commit

Permalink
expression: forbid aggregate function with json type pushdown to tifl…
Browse files Browse the repository at this point in the history
…ash wrongly (pingcap#36271)

close pingcap#28753
  • Loading branch information
wshwsh12 authored Jul 18, 2022
1 parent 8af8341 commit de017e9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion expression/aggregation/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
50 changes: 50 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit de017e9

Please sign in to comment.