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

planner: fix outer join's bad change to inner join depend on wrong null-reject check logic #38698

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 24 additions & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,19 +880,42 @@ func evaluateExprWithNullInNullRejectCheck(ctx sessionctx.Context, schema *Schem
// If all the args are Null Constant and affected by the column schema, then we should keep it.
// Otherwise, we shouldn't let Null Constant which affected by the column schema participate in computing in `And` and `OR`
// due to the result of `AND` and `OR are uncertain if one of the arguments is NULL.
//
// case: null AND 1 we should keep the null value's continuity, the final result can be determinant as null.
// case: null OR 0 we should keep the null value's continuity, the final result can be determinant as null.
continueNullConstant := false
if !allArgsNullFromSet {
for i := range args {
if cons, ok := args[i].(*Constant); ok && cons.Value.IsNull() && nullFromSets[i] {
if x.FuncName.L == ast.LogicAnd {
args[i] = NewOne()
if consOther, ok := args[1-i].(*Constant); ok {
// if the other side is true, then the null can be continued as null value up.
b, err := consOther.Value.ToBool(ctx.GetSessionVars().StmtCtx)
if err == nil && b == 1 {
continueNullConstant = true
}
} else {
args[i] = NewOne()
}
}
if x.FuncName.L == ast.LogicOr {
// if the other side is false, then the null can be continued as null value up.
if consOther, ok := args[1-i].(*Constant); ok {
b, err := consOther.Value.ToBool(ctx.GetSessionVars().StmtCtx)
if err == nil && b == 0 {
continueNullConstant = true
}
}
args[i] = NewZero()
}
}
}
}
c := NewFunctionInternal(ctx, x.FuncName.L, x.RetType.Clone(), args...)
if continueNullConstant {
c = &Constant{Value: types.Datum{}, RetType: types.NewFieldType(mysql.TypeNull)}
}

cons, ok := c.(*Constant)
// If the return expr is Null Constant, and all the Null Constant arguments are affected by column schema,
// then we think the result Null Constant is also affected by the column schema
Expand Down
17 changes: 17 additions & 0 deletions planner/core/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,20 @@ func TestOuterJoinOnNull(t *testing.T) {
tk.MustQuery("SELECT * FROM t0 LEFT JOIN t1 ON NULL WHERE t1.c0 or true; ").Check(testkit.Rows("> 1 <nil>"))
tk.MustQuery("SELECT * FROM t0 LEFT JOIN t1 ON NULL WHERE not(t1.c0 and false); ").Check(testkit.Rows("> 1 <nil>"))
}

// https://github.com/pingcap/tidb/issues/38654
func TestIssue38430(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE t0(c0 INT);")
tk.MustExec("CREATE TABLE t1(c0 INT);")
tk.MustExec("INSERT INTO t1 VALUES (1);")

tk.MustQuery("SELECT * FROM t0 RIGHT JOIN t1 ON t0.c0;").Check(testkit.Rows("<nil> 1"))
tk.MustQuery("SELECT ((NOT ('i'))AND(t0.c0)) IS NULL FROM t0 RIGHT JOIN t1 ON t0.c0;").Check(testkit.Rows("1"))
// outer join can't be an inner join case.
tk.MustQuery("SELECT * FROM t0 RIGHT JOIN t1 ON t0.c0 WHERE ((NOT ('i'))AND(t0.c0)) IS NULL;").Check(testkit.Rows("<nil> 1"))
// outer join to inner join case.
tk.MustQuery("SELECT * FROM t0 RIGHT JOIN t1 ON t0.c0 WHERE ((NOT NOT ('i'))AND(t0.c0)) IS NULL;").Check(testkit.Rows())
}