Skip to content

Commit

Permalink
expression: Add json_extract, cast(json as string), json_unquote push…
Browse files Browse the repository at this point in the history
… down support for tiflash (pingcap#39533)

ref pingcap/tiflash#6376, close pingcap#39458
  • Loading branch information
yibin87 authored Dec 2, 2022
1 parent 62b4e44 commit 7f2ae40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions expression/expr_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,18 @@ func TestExprPushDownToFlash(t *testing.T) {
require.NoError(t, err)
exprs = append(exprs, function)

// json_extract
function, err = NewFunction(mock.NewContext(), ast.JSONExtract, types.NewFieldType(mysql.TypeJSON), jsonColumn, stringColumn)
require.NoError(t, err)
exprs = append(exprs, function)

// json_unquote argument is cast(json as string)
subFunc, subErr := NewFunction(mock.NewContext(), ast.Cast, types.NewFieldType(mysql.TypeString), jsonColumn)
require.NoError(t, subErr)
function, err = NewFunction(mock.NewContext(), ast.JSONUnquote, types.NewFieldType(mysql.TypeString), subFunc)
require.NoError(t, err)
exprs = append(exprs, function)

// lpad
function, err = NewFunction(mock.NewContext(), ast.Lpad, types.NewFieldType(mysql.TypeString), stringColumn, int32Column, stringColumn)
require.NoError(t, err)
Expand Down Expand Up @@ -639,6 +651,11 @@ func TestExprPushDownToFlash(t *testing.T) {
require.NoError(t, err)
exprs = append(exprs, function)

// CastJsonAsString
function, err = NewFunction(mock.NewContext(), ast.Cast, types.NewFieldType(mysql.TypeString), jsonColumn)
require.NoError(t, err)
exprs = append(exprs, function)

// CastIntAsTime
function, err = NewFunction(mock.NewContext(), ast.Cast, types.NewFieldType(mysql.TypeDatetime), intColumn)
require.NoError(t, err)
Expand Down Expand Up @@ -958,6 +975,11 @@ func TestExprPushDownToFlash(t *testing.T) {

exprs = exprs[:0]

// json_unquote's argument is not cast(json as string)
function, err = NewFunction(mock.NewContext(), ast.JSONUnquote, types.NewFieldType(mysql.TypeString), stringColumn)
require.NoError(t, err)
exprs = append(exprs, function)

// Substring2Args: can not be pushed
function, err = NewFunction(mock.NewContext(), ast.Substr, types.NewFieldType(mysql.TypeString), binaryStringColumn, intColumn)
require.NoError(t, err)
Expand Down
10 changes: 8 additions & 2 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool {

ast.Sqrt, ast.Log, ast.Log2, ast.Log10, ast.Ln, ast.Exp, ast.Pow, ast.Sign,
ast.Radians, ast.Degrees, ast.Conv, ast.CRC32,
ast.JSONLength, ast.Repeat,
ast.JSONLength, ast.JSONExtract, ast.JSONUnquote, ast.Repeat,
ast.InetNtoa, ast.InetAton, ast.Inet6Ntoa, ast.Inet6Aton,
ast.Coalesce, ast.ASCII, ast.Length, ast.Trim, ast.Position, ast.Format, ast.Elt,
ast.LTrim, ast.RTrim, ast.Lpad, ast.Rpad,
Expand All @@ -1163,6 +1163,12 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool {
tipb.ScalarFuncSig_IfDuration,
tipb.ScalarFuncSig_CaseWhenDuration:
return false
case tipb.ScalarFuncSig_JsonUnquoteSig:
// TiFlash json_unquote now only supports json string generated by cast(json as string)
if childFunc, ok := function.GetArgs()[0].(*ScalarFunction); ok {
return childFunc.Function.PbCode() == tipb.ScalarFuncSig_CastJsonAsString
}
return false
}
return true
case ast.Regexp, ast.RegexpLike, ast.RegexpInStr, ast.RegexpSubstr:
Expand Down Expand Up @@ -1200,7 +1206,7 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool {
tipb.ScalarFuncSig_CastStringAsDecimal /*, tipb.ScalarFuncSig_CastDurationAsDecimal, tipb.ScalarFuncSig_CastJsonAsDecimal*/ :
return function.RetType.IsDecimalValid()
case tipb.ScalarFuncSig_CastDecimalAsString, tipb.ScalarFuncSig_CastIntAsString, tipb.ScalarFuncSig_CastRealAsString, tipb.ScalarFuncSig_CastTimeAsString,
tipb.ScalarFuncSig_CastStringAsString /*, tipb.ScalarFuncSig_CastDurationAsString, tipb.ScalarFuncSig_CastJsonAsString*/ :
tipb.ScalarFuncSig_CastStringAsString, tipb.ScalarFuncSig_CastJsonAsString /*, tipb.ScalarFuncSig_CastDurationAsString*/ :
return true
case tipb.ScalarFuncSig_CastDecimalAsTime, tipb.ScalarFuncSig_CastIntAsTime, tipb.ScalarFuncSig_CastRealAsTime, tipb.ScalarFuncSig_CastTimeAsTime,
tipb.ScalarFuncSig_CastStringAsTime /*, tipb.ScalarFuncSig_CastDurationAsTime, tipb.ScalarFuncSig_CastJsonAsTime*/ :
Expand Down

0 comments on commit 7f2ae40

Please sign in to comment.