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: refine range builder when meet unsigned_int_col <cmp> -int_cnst #10471

Merged
merged 26 commits into from May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b4d76bb
fix unsigned_int_col >= -int_cnst
b41sh May 14, 2019
4fac640
update test case
b41sh May 15, 2019
5de506f
fix unsigned_int_col and unsigned_decimal_col
b41sh May 15, 2019
a7839f6
Merge branch 'master' into fix-unsigned_int_col
b41sh May 15, 2019
174ca27
Merge branch 'master' into fix-unsigned_int_col
b41sh May 16, 2019
2849d1a
Merge branch 'master' into fix-unsigned_int_col
b41sh May 16, 2019
e6e22a1
Merge branch 'master' into fix-unsigned_int_col
b41sh May 17, 2019
9180b77
Merge branch 'master' into fix-unsigned_int_col
b41sh May 18, 2019
ce60697
Merge branch 'master' into fix-unsigned_int_col
b41sh May 20, 2019
da12b29
Merge branch 'master' into fix-unsigned_int_col
b41sh May 22, 2019
e370c45
Merge branch 'master' into fix-unsigned_int_col
b41sh May 23, 2019
38711b2
add comment and testcase
b41sh May 23, 2019
c9a9ff2
Merge branch 'master' into fix-unsigned_int_col
b41sh May 23, 2019
e3a4e71
code optimize
b41sh May 24, 2019
a37b71b
add test case
b41sh May 24, 2019
8b78c17
Merge branch 'master' into fix-unsigned_int_col
b41sh May 24, 2019
8cb0cd8
Merge branch 'master' of https://github.com/pingcap/tidb into fix-uns…
b41sh May 28, 2019
84a484a
Merge branch 'fix-unsigned_int_col' of https://github.com/b41sh/tidb …
b41sh May 28, 2019
43421b5
extract as a function
b41sh May 28, 2019
c7fca21
use flag instead of err
b41sh May 28, 2019
e24ec63
comments optimize
b41sh May 28, 2019
63c3d58
Merge branch 'master' into fix-unsigned_int_col
b41sh May 30, 2019
dd6e53a
code optimize
b41sh May 30, 2019
b42908f
comments optimize
b41sh May 30, 2019
4a612d8
comments optimize
b41sh May 30, 2019
8c7dd42
Merge branch 'master' into fix-unsigned_int_col
XuHuaiyu May 31, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions util/ranger/points.go
Expand Up @@ -250,6 +250,11 @@ func (r *builder) buildFormBinOp(expr *expression.ScalarFunction) []point {
return nil
}

value, op, isValidRange := handleUnsignedIntCol(ft, value, op)
if !isValidRange {
return nil
}

switch op {
case ast.EQ:
startPoint := point{value: value, start: true}
Expand Down Expand Up @@ -339,6 +344,29 @@ func HandlePadCharToFullLength(sc *stmtctx.StatementContext, ft *types.FieldType
}
}

// handleUnsignedIntCol handles the case when unsigned column meets negative integer value.
// The three returned values are: fixed constant value, fixed operator, and a boolean
// which indicates whether the range is valid or not.
func handleUnsignedIntCol(ft *types.FieldType, val types.Datum, op string) (types.Datum, string, bool) {
isUnsigned := mysql.HasUnsignedFlag(ft.Flag)
isIntegerType := mysql.IsIntegerType(ft.Tp)
isNegativeInteger := (val.Kind() == types.KindInt64 && val.GetInt64() < 0)

if !isUnsigned || !isIntegerType || !isNegativeInteger {
return val, op, true
}

// If the operator is GT, GE or NE, the range should be [0, +inf].
// Otherwise the value is out of valid range.
if op == ast.GT || op == ast.GE || op == ast.NE {
op = ast.GE
val.SetUint64(0)
b41sh marked this conversation as resolved.
Show resolved Hide resolved
return val, op, true
}

return val, op, false
}

func (r *builder) buildFromIsTrue(expr *expression.ScalarFunction, isNot int) []point {
if isNot == 1 {
// NOT TRUE range is {[null null] [0, 0]}
Expand Down
40 changes: 40 additions & 0 deletions util/ranger/ranger_test.go
Expand Up @@ -660,6 +660,39 @@ func (s *testRangerSuite) TestIndexRangeForUnsignedInt(c *C) {
filterConds: "[]",
resultStr: `[(NULL,1) (2,9223372036854775810) (9223372036854775810,+inf]]`,
},
{
indexPos: 0,
exprStr: `a >= -2147483648`,
accessConds: "[ge(test.t.a, -2147483648)]",
filterConds: "[]",
resultStr: `[[0,+inf]]`,
},
{
indexPos: 0,
exprStr: `a > -2147483648`,
accessConds: "[gt(test.t.a, -2147483648)]",
filterConds: "[]",
resultStr: `[[0,+inf]]`,
},
{
indexPos: 0,
exprStr: `a != -2147483648`,
accessConds: "[ne(test.t.a, -2147483648)]",
filterConds: "[]",
resultStr: `[[0,+inf]]`,
},
{
exprStr: "a < -1 or a < 1",
accessConds: "[or(lt(test.t.a, -1), lt(test.t.a, 1))]",
filterConds: "[]",
resultStr: "[[-inf,1)]",
},
{
exprStr: "a < -1 and a < 1",
accessConds: "[lt(test.t.a, -1) lt(test.t.a, 1)]",
filterConds: "[]",
resultStr: "[]",
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -933,6 +966,13 @@ func (s *testRangerSuite) TestColumnRange(c *C) {
filterConds: "[]",
resultStr: "[(18446744073709500000,+inf]]",
},
{
colPos: 4,
exprStr: `e > -2147483648`,
accessConds: "[gt(test.t.e, -2147483648)]",
filterConds: "[]",
resultStr: "[[0,+inf]]",
},
}

for _, tt := range tests {
Expand Down