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

range: build right range for NULL value on index column (#19385) #19430

Merged
merged 1 commit into from
Aug 25, 2020
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
32 changes: 32 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5658,6 +5658,38 @@ func (s *testSuite1) TestIssue16854(c *C) {
tk.MustExec("drop table t")
}

func (s *testSuite) TestIssue16921(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a float);")
tk.MustExec("create index a on t(a);")
tk.MustExec("insert into t values (1.0), (NULL), (0), (2.0);")
tk.MustQuery("select `a` from `t` use index (a) where !`a`;").Check(testkit.Rows("0"))
tk.MustQuery("select `a` from `t` ignore index (a) where !`a`;").Check(testkit.Rows("0"))
tk.MustQuery("select `a` from `t` use index (a) where `a`;").Check(testkit.Rows("1", "2"))
tk.MustQuery("select `a` from `t` ignore index (a) where `a`;").Check(testkit.Rows("1", "2"))
tk.MustQuery("select a from t use index (a) where not a is true;").Check(testkit.Rows("<nil>", "0"))
tk.MustQuery("select a from t use index (a) where not not a is true;").Check(testkit.Rows("1", "2"))
tk.MustQuery("select a from t use index (a) where not not a;").Check(testkit.Rows("1", "2"))
tk.MustQuery("select a from t use index (a) where not not not a is true;").Check(testkit.Rows("<nil>", "0"))
tk.MustQuery("select a from t use index (a) where not not not a;").Check(testkit.Rows("0"))
}

func (s *testSuite) TestIssue19100(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)

tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (c decimal);")
tk.MustExec("create table t2 (c decimal, key(c));")
tk.MustExec("insert into t1 values (null);")
tk.MustExec("insert into t2 values (null);")
tk.MustQuery("select count(*) from t1 where not c;").Check(testkit.Rows("0"))
tk.MustQuery("select count(*) from t2 where not c;").Check(testkit.Rows("0"))
tk.MustQuery("select count(*) from t1 where c;").Check(testkit.Rows("0"))
tk.MustQuery("select count(*) from t2 where c;").Check(testkit.Rows("0"))
}

// this is from jira issue #5856
func (s *testSuite1) TestInsertValuesWithSubQuery(c *C) {
tk := testkit.NewTestKit(c, s.store)
Expand Down
25 changes: 22 additions & 3 deletions util/ranger/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tipb/go-tipb"
)

// Error instances.
Expand Down Expand Up @@ -312,8 +313,16 @@ func handleUnsignedIntCol(ft *types.FieldType, val types.Datum, op string) (type
return val, op, false
}

func (r *builder) buildFromIsTrue(expr *expression.ScalarFunction, isNot int) []point {
func (r *builder) buildFromIsTrue(expr *expression.ScalarFunction, isNot int, keepNull bool) []point {
if isNot == 1 {
if keepNull {
// Range is {[0, 0]}
startPoint := point{start: true}
startPoint.value.SetInt64(0)
endPoint := point{}
endPoint.value.SetInt64(0)
return []point{startPoint, endPoint}
}
// NOT TRUE range is {[null null] [0, 0]}
startPoint1 := point{start: true}
endPoint1 := point{}
Expand Down Expand Up @@ -490,7 +499,8 @@ func (r *builder) newBuildFromPatternLike(expr *expression.ScalarFunction) []poi
func (r *builder) buildFromNot(expr *expression.ScalarFunction) []point {
switch n := expr.FuncName.L; n {
case ast.IsTruth:
return r.buildFromIsTrue(expr, 1)
keepNull := r.isTrueKeepNull(expr)
return r.buildFromIsTrue(expr, 1, keepNull)
case ast.IsFalsity:
return r.buildFromIsFalse(expr, 1)
case ast.In:
Expand Down Expand Up @@ -546,7 +556,8 @@ func (r *builder) buildFromScalarFunc(expr *expression.ScalarFunction) []point {
case ast.LogicOr:
return r.union(r.build(expr.GetArgs()[0]), r.build(expr.GetArgs()[1]))
case ast.IsTruth:
return r.buildFromIsTrue(expr, 0)
keepNull := r.isTrueKeepNull(expr)
return r.buildFromIsTrue(expr, 0, keepNull)
case ast.IsFalsity:
return r.buildFromIsFalse(expr, 0)
case ast.In:
Expand Down Expand Up @@ -633,3 +644,11 @@ func (r *builder) merge(a, b []point, union bool) []point {
}
return mergedPoints[:curTail]
}

func (r *builder) isTrueKeepNull(expr *expression.ScalarFunction) bool {
switch expr.Function.PbCode() {
case tipb.ScalarFuncSig_DecimalIsTrueWithNull, tipb.ScalarFuncSig_RealIsTrueWithNull, tipb.ScalarFuncSig_IntIsTrueWithNull:
return true
}
return false
}