Skip to content

Commit

Permalink
expr: support push tikv supported functions down (#32553)
Browse files Browse the repository at this point in the history
ref #31846
  • Loading branch information
Yisaer committed Mar 1, 2022
1 parent 86582f3 commit fccc4be
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 18 deletions.
8 changes: 4 additions & 4 deletions cmd/explaintest/r/explain_generate_column_substitute.result
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,14 @@ StreamAgg 4.80 root group by:upper(test.t.b), funcs:max(upper(test.t.b))->Colum
└─IndexFullScan 6.00 cop[tikv] table:t, index:expression_index_2(upper(`b`)) keep order:true
desc format = 'brief' select count(upper(b)) from t use index() group by upper(b);
id estRows task access object operator info
HashAgg 6.00 root group by:Column#9, funcs:count(Column#8)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#8, upper(test.t.b)->Column#9
HashAgg 6.00 root group by:Column#11, funcs:count(Column#10)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#10, upper(test.t.b)->Column#11
└─TableReader 6.00 root data:TableFullScan
└─TableFullScan 6.00 cop[tikv] table:t keep order:false
desc format = 'brief' select max(upper(b)) from t use index() group by upper(b);
id estRows task access object operator info
HashAgg 6.00 root group by:Column#9, funcs:max(Column#8)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#8, upper(test.t.b)->Column#9
HashAgg 6.00 root group by:Column#11, funcs:max(Column#10)->Column#7
└─Projection 6.00 root upper(test.t.b)->Column#10, upper(test.t.b)->Column#11
└─TableReader 6.00 root data:TableFullScan
└─TableFullScan 6.00 cop[tikv] table:t keep order:false
drop table if exists t;
Expand Down
10 changes: 5 additions & 5 deletions cmd/explaintest/r/index_merge.result
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,11 @@ c1 c2 c3 c4 c5
explain select /*+ use_index_merge(t1) */ * from t1 where (c1 < 10 or c2 < 10) and greatest(c1, c2, c4) = 1 order by 1;
id estRows task access object operator info
Sort_5 4433.77 root test.t1.c1
└─Selection_8 4433.77 root eq(greatest(test.t1.c1, test.t1.c2, test.t1.c4), 1)
└─IndexMerge_12 5542.21 root
├─IndexRangeScan_9(Build) 3323.33 cop[tikv] table:t1, index:c1(c1) range:[-inf,10), keep order:false, stats:pseudo
├─IndexRangeScan_10(Build) 3323.33 cop[tikv] table:t1, index:c2(c2) range:[-inf,10), keep order:false, stats:pseudo
└─TableRowIDScan_11(Probe) 5542.21 cop[tikv] table:t1 keep order:false, stats:pseudo
└─IndexMerge_12 4433.77 root
├─IndexRangeScan_8(Build) 3323.33 cop[tikv] table:t1, index:c1(c1) range:[-inf,10), keep order:false, stats:pseudo
├─IndexRangeScan_9(Build) 3323.33 cop[tikv] table:t1, index:c2(c2) range:[-inf,10), keep order:false, stats:pseudo
└─Selection_11(Probe) 4433.77 cop[tikv] eq(greatest(test.t1.c1, test.t1.c2, test.t1.c4), 1)
└─TableRowIDScan_10 5542.21 cop[tikv] table:t1 keep order:false, stats:pseudo
select /*+ use_index_merge(t1) */ * from t1 where (c1 < 10 or c2 < 10) and greatest(c1, c2, c4) = 1 order by 1;
c1 c2 c3 c4 c5
1 1 1 1 1
Expand Down
6 changes: 6 additions & 0 deletions expression/distsql_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *ti
f = &builtinTruncateRealSig{base}
case tipb.ScalarFuncSig_TruncateDecimal:
f = &builtinTruncateDecimalSig{base}
case tipb.ScalarFuncSig_TruncateUint:
f = &builtinTruncateUintSig{base}
case tipb.ScalarFuncSig_LogicalAnd:
f = &builtinLogicAndSig{base}
case tipb.ScalarFuncSig_LogicalOr:
Expand Down Expand Up @@ -1006,6 +1008,8 @@ func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *ti
f = &builtinLocate3ArgsSig{base}
case tipb.ScalarFuncSig_Lower:
f = &builtinLowerSig{base}
case tipb.ScalarFuncSig_LowerUTF8:
f = &builtinLowerUTF8Sig{base}
case tipb.ScalarFuncSig_LpadUTF8:
f = &builtinLpadUTF8Sig{base, maxAllowedPacket}
case tipb.ScalarFuncSig_Lpad:
Expand Down Expand Up @@ -1064,6 +1068,8 @@ func getSignatureByPB(ctx sessionctx.Context, sigCode tipb.ScalarFuncSig, tp *ti
f = &builtinUnHexSig{base}
case tipb.ScalarFuncSig_Upper:
f = &builtinUpperSig{base}
case tipb.ScalarFuncSig_UpperUTF8:
f = &builtinUpperUTF8Sig{base}
case tipb.ScalarFuncSig_ToBinary:
f = &builtinInternalToBinarySig{base}
case tipb.ScalarFuncSig_FromBinary:
Expand Down
34 changes: 30 additions & 4 deletions expression/expr_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,36 @@ func TestExprPushDownToTiKV(t *testing.T) {
retType: types.NewFieldType(mysql.TypeDate),
args: []Expression{dateColumn, dateColumn},
},
{
functionName: ast.Lower,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.InsertFunc,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn, intColumn, intColumn, stringColumn},
},
{
functionName: ast.Greatest,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{intColumn, intColumn},
},
{
functionName: ast.Least,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{intColumn, intColumn},
},
{
functionName: ast.Upper,
retType: types.NewFieldType(mysql.TypeString),
args: []Expression{stringColumn},
},
{
functionName: ast.Mod,
retType: types.NewFieldType(mysql.TypeInt24),
args: []Expression{intColumn, intColumn},
},
}

for _, tc := range testcases {
Expand All @@ -1380,10 +1410,6 @@ func TestExprPushDownToTiKV(t *testing.T) {
exprs = append(exprs, function)
}

function, err = NewFunction(mock.NewContext(), ast.Mod, types.NewFieldType(mysql.TypeInt24), intColumn, intColumn)
require.NoError(t, err)
exprs = append(exprs, function)

pushed, remained = PushDownExprs(sc, exprs, client, kv.TiKV)
require.Len(t, pushed, len(exprs))
require.Len(t, remained, 0)
Expand Down
5 changes: 3 additions & 2 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ func scalarExprSupportedByTiKV(sf *ScalarFunction) bool {

// compare functions.
ast.LT, ast.LE, ast.EQ, ast.NE, ast.GE, ast.GT, ast.NullEQ, ast.In, ast.IsNull, ast.Like, ast.IsTruthWithoutNull, ast.IsTruthWithNull, ast.IsFalsity,
ast.Greatest, ast.Least, /* ast.Interval */

// arithmetical functions.
ast.PI, ast.Truncate,
Expand All @@ -968,7 +969,7 @@ func scalarExprSupportedByTiKV(sf *ScalarFunction) bool {

// string functions.
ast.Bin, ast.Unhex, ast.Locate, ast.Ord, ast.Lpad, ast.Rpad,
ast.Trim, ast.FromBase64, ast.ToBase64, /* ast.Upper, ast.Lower, ast.InsertFunc */
ast.Trim, ast.FromBase64, ast.ToBase64, ast.Upper, ast.Lower, ast.InsertFunc,
ast.MakeSet, ast.SubstringIndex, ast.Instr, ast.Quote, ast.Oct,
ast.FindInSet, ast.Repeat,
ast.Length, ast.BitLength, ast.Concat, ast.ConcatWS, ast.Replace, ast.ASCII, ast.Hex,
Expand All @@ -988,7 +989,7 @@ func scalarExprSupportedByTiKV(sf *ScalarFunction) bool {
ast.MonthName, ast.MakeDate, ast.TimeToSec, ast.MakeTime,
ast.DateFormat,
ast.Hour, ast.Minute, ast.Second, ast.MicroSecond, ast.Month,
/* ast.DayName */ ast.DayOfMonth, ast.DayOfWeek, ast.DayOfYear,
ast.DayName, ast.DayOfMonth, ast.DayOfWeek, ast.DayOfYear,
ast.Weekday, ast.WeekOfYear, ast.Year,
ast.FromDays, ast.ToDays,
ast.PeriodAdd, ast.PeriodDiff, /*ast.TimestampDiff, ast.DateAdd, ast.FromUnixTime,*/
Expand Down
26 changes: 23 additions & 3 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2725,9 +2725,9 @@ func TestScalarFunctionPushDown(t *testing.T) {
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where month(d);").
CheckAt([]int{0, 3, 6}, rows)

//rows[1][2] = "dayname(test.t.d)"
//tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where dayname(d);").
// CheckAt([]int{0, 3, 6}, rows)
rows[1][2] = "dayname(test.t.d)"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where dayname(d);").
CheckAt([]int{0, 3, 6}, rows)

rows[1][2] = "dayofmonth(test.t.d)"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where dayofmonth(d);").
Expand Down Expand Up @@ -2852,6 +2852,26 @@ func TestScalarFunctionPushDown(t *testing.T) {
rows[1][2] = "gt(test.t.d, sysdate())"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where d > sysdate()").
CheckAt([]int{0, 3, 6}, rows)

rows[1][2] = "lower(test.t.c)"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where lower(c)").
CheckAt([]int{0, 3, 6}, rows)

rows[1][2] = "upper(test.t.c)"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where upper(c)").
CheckAt([]int{0, 3, 6}, rows)

rows[1][2] = "insert_func(test.t.c, 1, 1, \"c\")"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where insert_func(c, 1, 1,'c')").
CheckAt([]int{0, 3, 6}, rows)

rows[1][2] = "greatest(test.t.id, 1)"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where greatest(id, 1)").
CheckAt([]int{0, 3, 6}, rows)

rows[1][2] = "least(test.t.id, 1)"
tk.MustQuery("explain analyze select /*+read_from_storage(tikv[t])*/ * from t where least(id, 1)").
CheckAt([]int{0, 3, 6}, rows)
}

func TestDistinctScalarFunctionPushDown(t *testing.T) {
Expand Down

0 comments on commit fccc4be

Please sign in to comment.