diff --git a/cmd/explaintest/r/explain_easy.result b/cmd/explaintest/r/explain_easy.result index 3c1f255066bec..d7f0d43c52418 100644 --- a/cmd/explaintest/r/explain_easy.result +++ b/cmd/explaintest/r/explain_easy.result @@ -387,7 +387,7 @@ Projection 5.00 root Column#17 └─Selection 2.40 cop[tikv] eq(3, test.t.a) └─IndexRangeScan 3.00 cop[tikv] table:t1, index:idx(b) range:[3,3], keep order:true drop table if exists t; -create table t(a int unsigned); +create table t(a int unsigned not null); explain format = 'brief' select t.a = '123455' from t; id estRows task access object operator info Projection 10000.00 root eq(test.t.a, 123455)->Column#3 diff --git a/cmd/explaintest/t/explain_easy.test b/cmd/explaintest/t/explain_easy.test index 99382306b8658..4d3b698bd7d72 100644 --- a/cmd/explaintest/t/explain_easy.test +++ b/cmd/explaintest/t/explain_easy.test @@ -71,7 +71,7 @@ explain format = 'brief' select t.c in (select count(*) from t s left join t t1 explain format = 'brief' select t.c in (select count(*) from t s right join t t1 on s.a = t1.a where 3 = t.a and t1.b = 3) from t; drop table if exists t; -create table t(a int unsigned); +create table t(a int unsigned not null); explain format = 'brief' select t.a = '123455' from t; explain format = 'brief' select t.a > '123455' from t; explain format = 'brief' select t.a != '123455' from t; diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index bd5e7867c91ea..774680efb173d 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -1354,7 +1354,10 @@ func (c *compareFunctionClass) refineArgs(ctx sessionctx.Context, args []Express if !isExceptional || (isExceptional && mysql.HasNotNullFlag(arg0Type.Flag)) { finalArg1 = arg1 } - if isExceptional && arg1.GetType().EvalType() == types.ETInt && mysql.HasNotNullFlag(arg0Type.Flag) { + // TODO if the plan doesn't care about whether the result of the function is null or false, we don't need + // to check the NotNullFlag, then more optimizations can be enabled. + isExceptional = isExceptional && mysql.HasNotNullFlag(arg0Type.Flag) + if isExceptional && arg1.GetType().EvalType() == types.ETInt { // Judge it is inf or -inf // For int: // inf: 01111111 & 1 == 1 @@ -1375,7 +1378,10 @@ func (c *compareFunctionClass) refineArgs(ctx sessionctx.Context, args []Express if !isExceptional || (isExceptional && mysql.HasNotNullFlag(arg1Type.Flag)) { finalArg0 = arg0 } - if isExceptional && arg0.GetType().EvalType() == types.ETInt && mysql.HasNotNullFlag(arg1Type.Flag) { + // TODO if the plan doesn't care about whether the result of the function is null or false, we don't need + // to check the NotNullFlag, then more optimizations can be enabled. + isExceptional = isExceptional && mysql.HasNotNullFlag(arg1Type.Flag) + if isExceptional && arg0.GetType().EvalType() == types.ETInt { if arg0.Value.GetInt64()&1 == 1 { isNegativeInfinite = true } else { diff --git a/expression/integration_test.go b/expression/integration_test.go index 3116e441fc024..b60fbf938a15d 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -9084,3 +9084,17 @@ func (s *testIntegrationSuite) TestIssue23889(c *C) { tk.MustQuery("SELECT ( test_decimal . `col_decimal` , test_decimal . `col_decimal` ) IN ( select * from test_t ) as field1 FROM test_decimal;").Check( testkit.Rows("", "0")) } + +func (s *testIntegrationSuite) TestRefineArgNullValues(c *C) { + defer s.cleanEnv(c) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table t(id int primary key, a int)") + tk.MustExec("create table s(a int)") + tk.MustExec("insert into s values(1),(2)") + + tk.MustQuery("select t.id = 1.234 from t right join s on t.a = s.a").Check(testkit.Rows( + "", + "", + )) +} diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 643a069ebdefc..113e2d181a6f5 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -1071,7 +1071,7 @@ func (s *testIntegrationSuite) TestPartitionPruningForInExpr(c *C) { tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int(11), b int) partition by range (a) (partition p0 values less than (4), partition p1 values less than(10), partition p2 values less than maxvalue);") + tk.MustExec("create table t(a int(11) not null, b int) partition by range (a) (partition p0 values less than (4), partition p1 values less than(10), partition p2 values less than maxvalue);") tk.MustExec("insert into t values (1, 1),(10, 10),(11, 11)") var input []string