Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: fix unexpected panic when doing isNullRejected check (#22173) #22327

Merged
merged 4 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,17 @@ func foldConstant(expr Expression) (Expression, bool) {
return expr, isDeferredConst
}
if value.IsNull() {
if isDeferredConst {
return &Constant{Value: value, RetType: x.RetType, DeferredExpr: x}, true
}
// This Constant is created to compose the result expression of EvaluateExprWithNull when InNullRejectCheck
// is true. We just check whether the result expression is null or false and then let it die. Basically,
// the constant is used once briefly and will not be retained for a long time. Hence setting DeferredExpr
// of Constant to nil is ok.
return &Constant{Value: value, RetType: x.RetType}, false
}
if isTrue, err := value.ToBool(sc); err == nil && isTrue == 0 {
if isDeferredConst {
return &Constant{Value: value, RetType: x.RetType, DeferredExpr: x}, true
}
// This Constant is created to compose the result expression of EvaluateExprWithNull when InNullRejectCheck
// is true. We just check whether the result expression is null or false and then let it die. Basically,
// the constant is used once briefly and will not be retained for a long time. Hence setting DeferredExpr
// of Constant to nil is ok.
return &Constant{Value: value, RetType: x.RetType}, false
}
return expr, isDeferredConst
Expand Down
27 changes: 0 additions & 27 deletions expression/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,33 +234,6 @@ func (*testExpressionSuite) TestConstantFolding(c *C) {
}
}

func (*testExpressionSuite) TestDeferredExprNullConstantFold(c *C) {
nullConst := &Constant{
Value: types.NewDatum(nil),
RetType: types.NewFieldType(mysql.TypeTiny),
DeferredExpr: NewNull(),
}
tests := []struct {
condition Expression
deferred string
}{
{
condition: newFunction(ast.LT, newColumn(0), nullConst),
deferred: "lt(Column#0, <nil>)",
},
}
for _, tt := range tests {
comment := Commentf("different for expr %s", tt.condition)
sf, ok := tt.condition.(*ScalarFunction)
c.Assert(ok, IsTrue, comment)
sf.GetCtx().GetSessionVars().StmtCtx.InNullRejectCheck = true
newConds := FoldConstant(tt.condition)
newConst, ok := newConds.(*Constant)
c.Assert(ok, IsTrue, comment)
c.Assert(newConst.DeferredExpr.String(), Equals, tt.deferred, comment)
}
}

func (*testExpressionSuite) TestDeferredParamNotNull(c *C) {
ctx := mock.NewContext()
testTime := time.Now()
Expand Down
25 changes: 25 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7336,3 +7336,28 @@ func (s *testIntegrationSuite) TestDatetimeUserVariable(c *C) {
tk.MustExec("set @@tidb_enable_vectorized_expression = true")
c.Check(tk.MustQuery("select @p").Rows()[0][0] != "", IsTrue)
}

func (s *testIntegrationSuite) TestIssue22098(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("CREATE TABLE `ta` (" +
" `k` varchar(32) NOT NULL DEFAULT ' '," +
" `c0` varchar(32) NOT NULL DEFAULT ' '," +
" `c` varchar(18) NOT NULL DEFAULT ' '," +
" `e0` varchar(1) NOT NULL DEFAULT ' '," +
" PRIMARY KEY (`k`,`c0`,`c`)," +
" KEY `idx` (`c`,`e0`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
tk.MustExec("CREATE TABLE `tb` (" +
" `k` varchar(32) NOT NULL DEFAULT ' '," +
" `e` int(11) NOT NULL DEFAULT '0'," +
" `i` int(11) NOT NULL DEFAULT '0'," +
" `s` varchar(1) NOT NULL DEFAULT ' '," +
" `c` varchar(50) NOT NULL DEFAULT ' '," +
" PRIMARY KEY (`k`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
tk.MustExec("prepare stmt from \"select a.* from ta a left join tb b on a.k = b.k where (a.k <> '000000' and ((b.s = ? and i = ? ) or (b.s = ? and e = ?) or (b.s not in(?, ?))) and b.c like '%1%') or (a.c <> '000000' and a.k = '000000')\"")
tk.MustExec("set @a=3;set @b=20200414;set @c='a';set @d=20200414;set @e=3;set @f='a';")
tk.MustQuery("execute stmt using @a,@b,@c,@d,@e,@f").Check(testkit.Rows())
}