From ce03e3af9212a53c05098251e5a2b2bb1b3079e0 Mon Sep 17 00:00:00 2001 From: b41sh Date: Tue, 13 Dec 2022 23:44:45 +0800 Subject: [PATCH 1/2] feat: push down unhex function to tiflash --- 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 024bac00ef960..e41bda5067728 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 743c6b87dc6d0..f67ac876a691a 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(test.t.a)->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) From 9ef01d303529482422ad2467e0bd53d9f50119da Mon Sep 17 00:00:00 2001 From: b41sh Date: Wed, 14 Dec 2022 00:57:45 +0800 Subject: [PATCH 2/2] fix test --- planner/core/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index f67ac876a691a..7779c40ee437a 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -7539,7 +7539,7 @@ func TestUnhexPushDownToTiFlash(t *testing.T) { rows := [][]interface{}{ {"TableReader_9", "root", "data:ExchangeSender_8"}, {"└─ExchangeSender_8", "mpp[tiflash]", "ExchangeType: PassThrough"}, - {" └─Projection_4", "mpp[tiflash]", "unhex(test.t.a)->Column#4"}, + {" └─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)