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

ranger: handle decimal overflow properly when building index ranges (#23535) #23551

Merged
merged 4 commits into from
Mar 25, 2021
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
4 changes: 4 additions & 0 deletions util/ranger/ranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func convertPoint(sc *stmtctx.StatementContext, point *point, tp *types.FieldTyp
// see issue #20101: overflow when converting integer to year
} else if tp.Tp == mysql.TypeBit && terror.ErrorEqual(err, types.ErrDataTooLong) {
// see issue #19067: we should ignore the types.ErrDataTooLong when we convert value to TypeBit value
} else if tp.Tp == mysql.TypeNewDecimal && terror.ErrorEqual(err, types.ErrOverflow) {
// Ignore the types.ErrOverflow when we convert TypeNewDecimal values.
// A trimmed valid boundary point value would be returned then. Accordingly, the `excl` of the point
// would be adjusted. Impossible ranges would be skipped by the `validInterval` call later.
} else {
return point, errors.Trace(err)
}
Expand Down
34 changes: 34 additions & 0 deletions util/ranger/ranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,3 +1701,37 @@ func (s *testRangerSuite) TestPrefixIndexRangeScan(c *C) {
c.Assert(got, Equals, tt.resultStr, Commentf("different for expr %s", tt.exprStr))
}
}

func (s *testRangerSuite) TestIndexRangeForDecimal(c *C) {
defer testleak.AfterTest(c)()
dom, store, err := newDomainStoreWithBootstrap(c)
defer func() {
dom.Close()
store.Close()
}()
c.Assert(err, IsNil)
testKit := testkit.NewTestKit(c, store)
testKit.MustExec("use test;")
testKit.MustExec("drop table if exists t1, t2;")
testKit.MustExec("create table t1(a decimal unsigned, key(a));")
testKit.MustExec("insert into t1 values(0),(null);")
testKit.MustExec("create table t2(a int, b decimal unsigned, key idx(a,b));")
testKit.MustExec("insert into t2 values(1,0),(1,null);")

var input []string
var output []struct {
SQL string
Plan []string
Result []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = s.testData.ConvertRowsToStrings(testKit.MustQuery("explain format = 'brief' " + tt).Rows())
output[i].Result = s.testData.ConvertRowsToStrings(testKit.MustQuery(tt).Rows())
})
testKit.MustQuery("explain format = 'brief' " + tt).Check(testkit.Rows(output[i].Plan...))
testKit.MustQuery(tt).Check(testkit.Rows(output[i].Result...))
}
}
17 changes: 17 additions & 0 deletions util/ranger/testdata/ranger_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,22 @@
"select * from t where a > -1;",
"select * from t where a > 3;"
]
},
{
"name": "TestIndexRangeForDecimal",
"cases": [
"select * from t1 use index(a) where a in (-1,0);",
"select * from t1 use index(a) where a = -1;",
"select * from t1 use index(a) where a > -1;",
"select * from t1 use index(a) where a < -1;",
"select * from t1 use index(a) where a <= -1;",
"select * from t1 use index(a) where a >= -1;",
"select * from t2 use index(idx) where a = 1 and b in (-1,0);",
"select * from t2 use index(idx) where a = 1 and b = -1;",
"select * from t2 use index(idx) where a = 1 and b > -1;",
"select * from t2 use index(idx) where a = 1 and b < -1;",
"select * from t2 use index(idx) where a = 1 and b <= -1;",
"select * from t2 use index(idx) where a = 1 and b >= -1;"
]
}
]
107 changes: 107 additions & 0 deletions util/ranger/testdata/ranger_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -553,5 +553,112 @@
"Result": null
}
]
},
{
"Name": "TestIndexRangeForDecimal",
"Cases": [
{
"SQL": "select * from t1 use index(a) where a in (-1,0);",
"Plan": [
"IndexReader 10.00 root index:IndexRangeScan",
"└─IndexRangeScan 10.00 cop[tikv] table:t1, index:a(a) range:[0,0], keep order:false, stats:pseudo"
],
"Result": [
"0"
]
},
{
"SQL": "select * from t1 use index(a) where a = -1;",
"Plan": [
"TableDual 0.00 root rows:0"
],
"Result": null
},
{
"SQL": "select * from t1 use index(a) where a > -1;",
"Plan": [
"IndexReader 3333.33 root index:IndexRangeScan",
"└─IndexRangeScan 3333.33 cop[tikv] table:t1, index:a(a) range:[0,+inf], keep order:false, stats:pseudo"
],
"Result": [
"0"
]
},
{
"SQL": "select * from t1 use index(a) where a < -1;",
"Plan": [
"TableDual 0.00 root rows:0"
],
"Result": null
},
{
"SQL": "select * from t1 use index(a) where a <= -1;",
"Plan": [
"TableDual 0.00 root rows:0"
],
"Result": null
},
{
"SQL": "select * from t1 use index(a) where a >= -1;",
"Plan": [
"IndexReader 3333.33 root index:IndexRangeScan",
"└─IndexRangeScan 3333.33 cop[tikv] table:t1, index:a(a) range:[0,+inf], keep order:false, stats:pseudo"
],
"Result": [
"0"
]
},
{
"SQL": "select * from t2 use index(idx) where a = 1 and b in (-1,0);",
"Plan": [
"IndexReader 0.10 root index:IndexRangeScan",
"└─IndexRangeScan 0.10 cop[tikv] table:t2, index:idx(a, b) range:[1 0,1 0], keep order:false, stats:pseudo"
],
"Result": [
"1 0"
]
},
{
"SQL": "select * from t2 use index(idx) where a = 1 and b = -1;",
"Plan": [
"TableDual 0.00 root rows:0"
],
"Result": null
},
{
"SQL": "select * from t2 use index(idx) where a = 1 and b > -1;",
"Plan": [
"IndexReader 33.33 root index:IndexRangeScan",
"└─IndexRangeScan 33.33 cop[tikv] table:t2, index:idx(a, b) range:[1 0,1 +inf], keep order:false, stats:pseudo"
],
"Result": [
"1 0"
]
},
{
"SQL": "select * from t2 use index(idx) where a = 1 and b < -1;",
"Plan": [
"TableDual 0.00 root rows:0"
],
"Result": null
},
{
"SQL": "select * from t2 use index(idx) where a = 1 and b <= -1;",
"Plan": [
"TableDual 0.00 root rows:0"
],
"Result": null
},
{
"SQL": "select * from t2 use index(idx) where a = 1 and b >= -1;",
"Plan": [
"IndexReader 33.33 root index:IndexRangeScan",
"└─IndexRangeScan 33.33 cop[tikv] table:t2, index:idx(a, b) range:[1 0,1 +inf], keep order:false, stats:pseudo"
],
"Result": [
"1 0"
]
}
]
}
]