From 8e80e7c96cf0c75c26244bd8a18e727fb8e45ac1 Mon Sep 17 00:00:00 2001 From: baishen Date: Fri, 30 Dec 2022 14:06:18 +0800 Subject: [PATCH] expression: implement `unhex` function pushdown to tiflash (#39898) ref pingcap/tidb#5112, ref pingcap/tiflash#5112 --- expression/expr_to_pb_test.go | 5 +++++ expression/expression.go | 2 +- planner/core/integration_test.go | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/expression/expr_to_pb_test.go b/expression/expr_to_pb_test.go index 1025f3c7fdcb1..341c03b552804 100644 --- a/expression/expr_to_pb_test.go +++ b/expression/expr_to_pb_test.go @@ -1239,6 +1239,11 @@ func TestExprPushDownToFlash(t *testing.T) { require.Equal(t, tipb.ScalarFuncSig_CastTimeAsDuration, function.(*ScalarFunction).Function.PbCode()) exprs = append(exprs, function) + // Unhex + function, err = NewFunction(mock.NewContext(), ast.Unhex, types.NewFieldType(mysql.TypeString), stringColumn) + require.NoError(t, err) + exprs = append(exprs, function) + pushed, remained = PushDownExprs(sc, exprs, client, kv.TiFlash) require.Len(t, pushed, len(exprs)) require.Len(t, remained, 0) diff --git a/expression/expression.go b/expression/expression.go index c7ce0b22c1c0e..00697c2df68ea 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -1267,7 +1267,7 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool { } case ast.IsTruthWithNull, ast.IsTruthWithoutNull, ast.IsFalsity: return true - case ast.Hex, ast.Bin: + case ast.Hex, ast.Unhex, ast.Bin: return true case ast.GetFormat: return true diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 31cd5aa9c346b..307971e2d77e1 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -7520,6 +7520,39 @@ func TestCastTimeAsDurationToTiFlash(t *testing.T) { tk.MustQuery("explain select cast(a as time), cast(b as time) from t;").CheckAt([]int{0, 2, 4}, rows) } +func TestUnhexPushDownToTiFlash(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(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 varchar(20));") + tk.MustExec("insert into t values(6162, '7469666C617368');") + tk.MustExec("set @@tidb_allow_mpp=1; set @@tidb_enforce_mpp=1;") + tk.MustExec("set @@tidb_isolation_read_engines = 'tiflash'") + + tbl, err := dom.InfoSchema().TableByName(model.CIStr{O: "test", L: "test"}, model.CIStr{O: "t", L: "t"}) + require.NoError(t, err) + // Set the hacked TiFlash replica for explain tests. + tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{Count: 1, Available: true} + + rows := [][]interface{}{ + {"TableReader_9", "root", "data:ExchangeSender_8"}, + {"└─ExchangeSender_8", "mpp[tiflash]", "ExchangeType: PassThrough"}, + {" └─Projection_4", "mpp[tiflash]", "unhex(cast(test.t.a, var_string(20)))->Column#4"}, + {" └─TableFullScan_7", "mpp[tiflash]", "keep order:false, stats:pseudo"}, + } + tk.MustQuery("explain select unhex(a) from t;").CheckAt([]int{0, 2, 4}, rows) + + rows = [][]interface{}{ + {"TableReader_9", "root", "data:ExchangeSender_8"}, + {"└─ExchangeSender_8", "mpp[tiflash]", "ExchangeType: PassThrough"}, + {" └─Projection_4", "mpp[tiflash]", "unhex(test.t.b)->Column#4"}, + {" └─TableFullScan_7", "mpp[tiflash]", "keep order:false, stats:pseudo"}, + } + tk.MustQuery("explain select unhex(b) from t;").CheckAt([]int{0, 2, 4}, rows) +} + func TestPartitionTableFallBackStatic(t *testing.T) { store, _ := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store)