From 2a6436ccdc3d4a26fe25965d698c2b62d5fb1db0 Mon Sep 17 00:00:00 2001 From: yibin Date: Thu, 1 Dec 2022 14:00:07 +0800 Subject: [PATCH 1/2] Add json_extract, cast(json as string), json_unquote push down support for tiflash Signed-off-by: yibin --- expression/expr_to_pb_test.go | 22 ++++++++++++++++++++++ expression/expression.go | 10 ++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/expression/expr_to_pb_test.go b/expression/expr_to_pb_test.go index 6b1e7c11d05da..869757072eeb8 100644 --- a/expression/expr_to_pb_test.go +++ b/expression/expr_to_pb_test.go @@ -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) @@ -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) @@ -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) diff --git a/expression/expression.go b/expression/expression.go index 765d121997040..9b635f4f5f459 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -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, @@ -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 child_func, ok := function.GetArgs()[0].(*ScalarFunction); ok { + return child_func.Function.PbCode() == tipb.ScalarFuncSig_CastJsonAsString + } + return false } return true case ast.Regexp, ast.RegexpLike: @@ -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*/ : From abad855993b3a872f98d5c59ca92027f50b15e39 Mon Sep 17 00:00:00 2001 From: yibin Date: Thu, 1 Dec 2022 14:09:47 +0800 Subject: [PATCH 2/2] Little fix Signed-off-by: yibin --- expression/expression.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/expression/expression.go b/expression/expression.go index 9b635f4f5f459..2155376730748 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -1165,8 +1165,8 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool { return false case tipb.ScalarFuncSig_JsonUnquoteSig: // TiFlash json_unquote now only supports json string generated by cast(json as string) - if child_func, ok := function.GetArgs()[0].(*ScalarFunction); ok { - return child_func.Function.PbCode() == tipb.ScalarFuncSig_CastJsonAsString + if childFunc, ok := function.GetArgs()[0].(*ScalarFunction); ok { + return childFunc.Function.PbCode() == tipb.ScalarFuncSig_CastJsonAsString } return false }