Skip to content

Commit

Permalink
cherry pick pingcap#35759 to release-5.2
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
LittleFall authored and ti-srebot committed Jun 28, 2022
1 parent 3279c08 commit 6debfd2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ func evaluateExprWithNull(ctx sessionctx.Context, schema *Schema, expr Expressio
for i, arg := range x.GetArgs() {
args[i] = evaluateExprWithNull(ctx, schema, arg)
}
return NewFunctionInternal(ctx, x.FuncName.L, x.RetType, args...)
return NewFunctionInternal(ctx, x.FuncName.L, x.RetType.Clone(), args...)
case *Column:
if !schema.Contains(x) {
return x
Expand Down
29 changes: 29 additions & 0 deletions expression/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,36 @@ func (s *testEvaluatorSuite) TestEvaluateExprWithNull(c *C) {
c.Assert(res.Equal(s.ctx, NewOne()), IsTrue)
}

<<<<<<< HEAD
func (s *testEvaluatorSuite) TestConstant(c *C) {
=======
func TestEvaluateExprWithNullNoChangeRetType(t *testing.T) {
ctx := createContext(t)
tblInfo := newTestTableBuilder("").add("col_str", mysql.TypeString, 0).build()
schema := tableInfoToSchemaForTest(tblInfo)

castStrAsJSON := BuildCastFunction(ctx, schema.Columns[0], types.NewFieldType(mysql.TypeJSON))
jsonConstant := &Constant{Value: types.NewDatum("123"), RetType: types.NewFieldType(mysql.TypeJSON)}

// initially has ParseToJSONFlag
flagInCast := castStrAsJSON.(*ScalarFunction).RetType.GetFlag()
require.True(t, mysql.HasParseToJSONFlag(flagInCast))

// cast's ParseToJSONFlag removed by `DisableParseJSONFlag4Expr`
eq, err := newFunctionForTest(ctx, ast.EQ, jsonConstant, castStrAsJSON)
require.NoError(t, err)
flagInCast = eq.(*ScalarFunction).GetArgs()[1].(*ScalarFunction).RetType.GetFlag()
require.False(t, mysql.HasParseToJSONFlag(flagInCast))

// after EvaluateExprWithNull, this flag should be still false
EvaluateExprWithNull(ctx, schema, eq)
flagInCast = eq.(*ScalarFunction).GetArgs()[1].(*ScalarFunction).RetType.GetFlag()
require.False(t, mysql.HasParseToJSONFlag(flagInCast))
}

func TestConstant(t *testing.T) {
ctx := createContext(t)
>>>>>>> 1f40fc72a... expression: use cloned RetType at `evaluateExprWithNull` when it may be changed. (#35759)
sc := &stmtctx.StatementContext{TimeZone: time.Local}
c.Assert(NewZero().IsCorrelated(), IsFalse)
c.Assert(NewZero().ConstItem(sc), IsTrue)
Expand Down
19 changes: 19 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4489,3 +4489,22 @@ func (s *testIntegrationSerialSuite) TestKeepPrunedConds(c *C) {
" └─Selection 0.01 cop[tikv] eq(test.t.b, 1), eq(test.t.c, 1)",
" └─IndexFullScan 10000.00 cop[tikv] table:t, partition:part_202103, index:idx(a, b, c) keep order:false, stats:pseudo"))
}

func TestIssue25813(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a json);")
tk.MustExec("insert into t values('{\"id\": \"ish\"}');")
tk.MustQuery("select t2.a from t t1 left join t t2 on t1.a=t2.a where t2.a->'$.id'='ish';").Check(testkit.Rows("{\"id\": \"ish\"}"))

tk.MustQuery("explain format = 'brief' select * from t t1 left join t t2 on t1.a=t2.a where t2.a->'$.id'='ish';").Check(testkit.Rows(
"Selection 8000.00 root eq(json_extract(test.t.a, \"$.id\"), cast(\"ish\", json BINARY))",
"└─HashJoin 10000.00 root left outer join, equal:[eq(test.t.a, test.t.a)]",
" ├─TableReader(Build) 8000.00 root data:Selection",
" │ └─Selection 8000.00 cop[tikv] not(isnull(cast(test.t.a, var_string(4294967295))))",
" │ └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo",
" └─TableReader(Probe) 10000.00 root data:TableFullScan",
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo"))
}

0 comments on commit 6debfd2

Please sign in to comment.