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: use ordered index with is null predicate #54512

Closed
wants to merge 3 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
2 changes: 1 addition & 1 deletion pkg/planner/core/casetest/index/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ go_test(
],
data = glob(["testdata/**"]),
flaky = True,
shard_count = 5,
shard_count = 6,
deps = [
"//pkg/testkit",
"//pkg/testkit/testdata",
Expand Down
12 changes: 12 additions & 0 deletions pkg/planner/core/casetest/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,15 @@ func TestRangeIntersection(t *testing.T) {
tk.MustQuery(sql).Sort().Check(testkit.Rows(output[i].Result...))
}
}

func TestOrderedIndexWithIsNull(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE t1 (a int key, b int, c int, index (b, c));")
tk.MustQuery("explain select a from t1 where b is null order by c").Check(testkit.Rows(
"Projection_6 10.00 root test.t1.a",
"└─IndexReader_12 10.00 root index:IndexRangeScan_11",
" └─IndexRangeScan_11 10.00 cop[tikv] table:t1, index:b(b, c) range:[NULL,NULL], keep order:true, stats:pseudo",
))
}
42 changes: 28 additions & 14 deletions pkg/util/ranger/detacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func getPotentialEqOrInColOffset(sctx *rangerctx.RangerContext, expr expression.
return i
}
}
case ast.IsNull:
if c, ok := f.GetArgs()[0].(*expression.Column); ok {
for i, col := range cols {
if col.EqualColumn(c) {
return i
}
}
}
}
return -1
}
Expand Down Expand Up @@ -635,27 +643,33 @@ func allEqOrIn(expr expression.Expression) bool {
}
}
return true
case ast.EQ, ast.NullEQ, ast.In:
case ast.EQ, ast.NullEQ, ast.In, ast.IsNull:
return true
}
return false
}

func extractValueInfo(expr expression.Expression) *valueInfo {
if f, ok := expr.(*expression.ScalarFunction); ok && (f.FuncName.L == ast.EQ || f.FuncName.L == ast.NullEQ) {
getValueInfo := func(c *expression.Constant) *valueInfo {
mutable := c.ParamMarker != nil || c.DeferredExpr != nil
var value *types.Datum
if !mutable {
value = &c.Value
if f, ok := expr.(*expression.ScalarFunction); ok {
if f.FuncName.L == ast.IsNull {
var value = types.NewDatum(nil)
return &valueInfo{&value, false}
}
if f.FuncName.L == ast.EQ || f.FuncName.L == ast.NullEQ {
getValueInfo := func(c *expression.Constant) *valueInfo {
mutable := c.ParamMarker != nil || c.DeferredExpr != nil
var value *types.Datum
if !mutable {
value = &c.Value
}
return &valueInfo{value, mutable}
}
if c, ok := f.GetArgs()[0].(*expression.Constant); ok {
return getValueInfo(c)
}
if c, ok := f.GetArgs()[1].(*expression.Constant); ok {
return getValueInfo(c)
}
return &valueInfo{value, mutable}
}
if c, ok := f.GetArgs()[0].(*expression.Constant); ok {
return getValueInfo(c)
}
if c, ok := f.GetArgs()[1].(*expression.Constant); ok {
return getValueInfo(c)
}
}
return nil
Expand Down