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: check NotNullFlag of columns when simplifying binary operations #24049

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/explaintest/t/explain_easy.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,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
Expand All @@ -1380,7 +1383,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 {
Expand Down
14 changes: 14 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9169,3 +9169,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("<nil>", "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(
"<nil>",
"<nil>",
))
}
2 changes: 1 addition & 1 deletion planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,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
Expand Down