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

util/ranger: add more tests for fixing issue 44389 #44757

Closed
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
43 changes: 43 additions & 0 deletions util/ranger/ranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package ranger_test
import (
"context"
"fmt"
"strconv"
"strings"
"testing"

"github.com/pingcap/tidb/config"
Expand All @@ -31,6 +33,7 @@ import (
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/testdata"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/ranger"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -2620,3 +2623,43 @@ func TestIssue44389(t *testing.T) {
testKit.MustQuery(tt).Sort().Check(testkit.Rows(output[i].Result...))
}
}

func checkIndexRange(t *testing.T, tk *testkit.TestKit, expectedIndexName, expectedIndexRange string) {
tkProcess := tk.Session().ShowProcess()
ps := []*util.ProcessInfo{tkProcess}
tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps})
rows := tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)).Rows()
useIndex := false
for _, row := range rows {
if strings.Contains(row[4].(string), expectedIndexName) {
require.True(t, strings.Contains(row[6].(string), expectedIndexRange))
useIndex = true
break
}
}
require.True(t, useIndex)
}

func TestIssue44389ForPreparedPlanCache(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, c int, index idx_ab(a, b))")
tk.MustExec("set @@tidb_opt_fix_control = '44389:ON'")

// The plan using CNF item index range result, it
tk.MustExec("prepare stmt1 from 'select * from t where c = 10 and (a = 1 or (a = 3 and b = ?))'")
tk.MustExec("set @x = 2")
tk.MustExec("execute stmt1 using @x")
checkIndexRange(t, tk, "idx_ab", "[1,1], [3 2,3 2]")
tk.MustExec("execute stmt1 using @x")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))

tk.MustExec("prepare stmt2 from 'select * from t where c = 10 and (a = 1 or (a >= ? and a <= ? and b = 2))'")
tk.MustExec("set @x = 3, @y = 3")
tk.MustExec("execute stmt2 using @x, @y")
checkIndexRange(t, tk, "idx_ab", "[1,1], [3 2,3 2]")
tk.MustExec("execute stmt1 using @x")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}
1 change: 1 addition & 0 deletions util/ranger/testdata/ranger_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"name": "TestIssue44389",
"cases": [
"select * from t where c = 10 and (a = 'xx' or (a = 'kk' and b = 1))",
"select * from t where c = 10 and (a = 'xx' or (a = 'hh' and b > 2))",
"select * from t where c = 10 and ((a = 'xx' or a = 'yy') or ((a = 'kk' and b = 1) or (a = 'hh' and b = 2)))"
]
}
Expand Down
13 changes: 13 additions & 0 deletions util/ranger/testdata/ranger_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,19 @@
"xx 4 10"
]
},
{
"SQL": "select * from t where c = 10 and (a = 'xx' or (a = 'hh' and b > 2))",
"Plan": [
"IndexLookUp_11 0.04 root ",
"├─IndexRangeScan_8(Build) 43.33 cop[tikv] table:t, index:idx_ab(a, b) range:(\"hh\" 2,\"hh\" +inf], [\"xx\",\"xx\"], keep order:false, stats:pseudo",
"└─Selection_10(Probe) 0.04 cop[tikv] eq(test.t.c, 10)",
" └─TableRowIDScan_9 43.33 cop[tikv] table:t keep order:false, stats:pseudo"
],
"Result": [
"hh 3 10",
"xx 4 10"
]
},
{
"SQL": "select * from t where c = 10 and ((a = 'xx' or a = 'yy') or ((a = 'kk' and b = 1) or (a = 'hh' and b = 2)))",
"Plan": [
Expand Down