Skip to content

Commit

Permalink
expression: push down EXTRACT to TiFlash (pingcap#22832)
Browse files Browse the repository at this point in the history
  • Loading branch information
leiysky authored and LittleFall committed Mar 19, 2021
1 parent 49415a0 commit 4123284
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 13 additions & 2 deletions expression/expr_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ func (s *testEvaluatorSuite) TestExprPushDownToFlash(c *C) {
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ExtractDatetime: can be pushed
function, err = NewFunction(mock.NewContext(), ast.Extract, types.NewFieldType(mysql.TypeLonglong), stringColumn, datetimeColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// CastIntAsInt
function, err = NewFunction(mock.NewContext(), ast.Cast, types.NewFieldType(mysql.TypeLonglong), intColumn)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -726,9 +731,15 @@ func (s *testEvaluatorSuite) TestExprPushDownToFlash(c *C) {
function, err = NewFunction(mock.NewContext(), ast.JSONDepth, types.NewFieldType(mysql.TypeLonglong), jsonColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

// ExtractDatetimeFromString: can not be pushed
function, err = NewFunction(mock.NewContext(), ast.Extract, types.NewFieldType(mysql.TypeLonglong), stringColumn, stringColumn)
c.Assert(err, IsNil)
exprs = append(exprs, function)

pushed, remained := PushDownExprs(sc, exprs, client, kv.TiFlash)
c.Assert(len(pushed), Equals, len(exprs)-1)
c.Assert(len(remained), Equals, 1)
c.Assert(len(pushed), Equals, len(exprs)-2)
c.Assert(len(remained), Equals, 2)
}

func (s *testEvaluatorSuite) TestExprOnlyPushDownToFlash(c *C) {
Expand Down
15 changes: 12 additions & 3 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ func canFuncBePushed(sf *ScalarFunction, storeType kv.StoreType) bool {
ast.DateAdd,
ast.FromUnixTime,
ast.UnixTimestamp,
ast.Extract,

// encryption functions.
ast.MD5,
Expand All @@ -1059,8 +1060,9 @@ func canFuncBePushed(sf *ScalarFunction, storeType kv.StoreType) bool {
ast.Inet6Aton,
ast.IsIPv4,
ast.IsIPv4Compat,
ast.IsIPv4Mapped,leiyu
ast.IsIPv6:
ast.IsIPv4Mapped,
ast.IsIPv6,
ast.UUID:
ret = true

// A special case: Only push down Round by signature
Expand Down Expand Up @@ -1206,7 +1208,7 @@ func CanExprsPushDown(sc *stmtctx.StatementContext, exprs []Expression, client k
func scalarExprSupportedByTiKV(function *ScalarFunction) bool {
switch function.FuncName.L {
case ast.Substr, ast.Substring, ast.DateAdd, ast.TimestampDiff, ast.DateDiff, ast.UnixTimestamp,
ast.FromUnixTime:
ast.FromUnixTime, ast.Extract:
return false
default:
return true
Expand Down Expand Up @@ -1262,6 +1264,13 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool {
default:
return false
}
case ast.Extract:
switch function.Function.PbCode() {
case tipb.ScalarFuncSig_ExtractDatetime:
return true
default:
return false
}
default:
return false
}
Expand Down

0 comments on commit 4123284

Please sign in to comment.