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

executor: fix the bug of step value overflow when split regions #23206

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion executor/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/binary"
"fmt"
"math"
"math/big"
"time"

"github.com/cznic/mathutil"
Expand Down Expand Up @@ -557,7 +558,12 @@ func (e *SplitTableRegionExec) calculateIntBoundValue() (lowerValue int64, step
if upperRecordID <= lowerRecordID {
return 0, 0, errors.Errorf("Split table `%s` region lower value %v should less than the upper value %v", e.tableInfo.Name, lowerRecordID, upperRecordID)
}
step = (upperRecordID - lowerRecordID) / int64(e.num)
// Prevent int64 overflow. See https://github.com/pingcap/tidb/issues/23159.
bigUpper := big.NewInt(upperRecordID)
bigLower := big.NewInt(lowerRecordID)
bigStep := big.NewInt(0).Sub(bigUpper, bigLower)
bigStep.Div(bigStep, big.NewInt(int64(e.num)))
step = bigStep.Int64()
lowerValue = lowerRecordID
}
if step < minRegionStepValue {
Expand Down
30 changes: 30 additions & 0 deletions executor/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,36 @@ func (s *testSplitIndex) TestSplitTable(c *C) {
}
}

func (s *testSplitIndex) TestCalculateIntBoundValue(c *C) {
tbInfo := &model.TableInfo{
Name: model.NewCIStr("t1"),
ID: rand.Int63(),
Columns: []*model.ColumnInfo{
{
Name: model.NewCIStr("c0"),
ID: 1,
Offset: 1,
DefaultValue: 0,
State: model.StatePublic,
FieldType: *types.NewFieldType(mysql.TypeLong),
},
},
}
ctx := mock.NewContext()
e := &SplitTableRegionExec{
baseExecutor: newBaseExecutor(ctx, nil, 0),
tableInfo: tbInfo,
handleCols: core.NewIntHandleCols(&expression.Column{RetType: types.NewFieldType(mysql.TypeLonglong)}),
lower: []types.Datum{types.NewDatum(-9223372036854775808)},
upper: []types.Datum{types.NewDatum(9223372036854775807)},
num: 4,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when REGIONS 1 is given?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add some edge test. And in the Integration Testing, the mysql client get the result:

mysql> create table if not exists `t1` (`id` bigint);
Query OK, 0 rows affected (0.00 sec)

mysql> split table `t1` between (-9223372036854775808) and (9223372036854775807) regions 1;
ERROR 1105 (HY000): Split table `t1` region step value should more than 1000, step -1 is invalid

}
lower, step, err := e.calculateIntBoundValue()
c.Assert(err, IsNil)
c.Assert(step, Equals, int64(4611686018427387903))
c.Assert(lower, Equals, int64(-9223372036854775808))
}

func (s *testSplitIndex) TestClusterIndexSplitTable(c *C) {
tbInfo := &model.TableInfo{
Name: model.NewCIStr("t"),
Expand Down