-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
ddl:fix create partitioned table with bigint column fail #7520
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,7 +178,7 @@ func checkPartitionFuncType(ctx sessionctx.Context, s *ast.CreateTableStmt, cols | |
|
||
// checkCreatePartitionValue checks whether `less than value` is strictly increasing for each partition. | ||
// Side effect: it may simplify the partition range definition from a constant expression to an integer. | ||
func checkCreatePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo, pi *model.PartitionInfo) error { | ||
func checkCreatePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo, pi *model.PartitionInfo, cols []*table.Column) error { | ||
defs := pi.Definitions | ||
if len(defs) <= 1 { | ||
return nil | ||
|
@@ -187,13 +187,14 @@ func checkCreatePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo, | |
if strings.EqualFold(defs[len(defs)-1].LessThan[0], partitionMaxValue) { | ||
defs = defs[:len(defs)-1] | ||
} | ||
var prevRangeValue int64 | ||
isUnsignedBigint := FindRangePartitionColTp(cols, pi) | ||
var prevRangeValue interface{} | ||
for i := 0; i < len(defs); i++ { | ||
if strings.EqualFold(defs[i].LessThan[0], partitionMaxValue) { | ||
return errors.Trace(ErrPartitionMaxvalue) | ||
} | ||
|
||
currentRangeValue, fromExpr, err := getRangeValue(ctx, tblInfo, defs[i].LessThan[0]) | ||
currentRangeValue, fromExpr, err := getRangeValue(ctx, tblInfo, defs[i].LessThan[0], isUnsignedBigint) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
@@ -207,8 +208,14 @@ func checkCreatePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo, | |
continue | ||
} | ||
|
||
if currentRangeValue <= prevRangeValue { | ||
return errors.Trace(ErrRangeNotIncreasing) | ||
if isUnsignedBigint { | ||
if currentRangeValue.(uint64) <= prevRangeValue.(uint64) { | ||
return errors.Trace(ErrRangeNotIncreasing) | ||
} | ||
} else { | ||
if currentRangeValue.(int64) <= prevRangeValue.(int64) { | ||
return errors.Trace(ErrRangeNotIncreasing) | ||
} | ||
} | ||
prevRangeValue = currentRangeValue | ||
} | ||
|
@@ -217,23 +224,34 @@ func checkCreatePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo, | |
|
||
// getRangeValue gets an integer from the range value string. | ||
// The returned boolean value indicates whether the input string is a constant expression. | ||
func getRangeValue(ctx sessionctx.Context, tblInfo *model.TableInfo, str string) (int64, bool, error) { | ||
|
||
if value, err := strconv.ParseInt(str, 10, 64); err == nil { | ||
return value, false, nil | ||
} | ||
func getRangeValue(ctx sessionctx.Context, tblInfo *model.TableInfo, str string, ifUnsignedBigint bool) (interface{}, bool, error) { | ||
// Unsigned bigint was converted to uint64 handle. | ||
if ifUnsignedBigint { | ||
if value, err := strconv.ParseUint(str, 10, 64); err == nil { | ||
return value, false, nil | ||
} | ||
|
||
// The range value maybe not an integer, it could be a constant expression. | ||
// For example, the following two cases are the same: | ||
// PARTITION p0 VALUES LESS THAN (TO_SECONDS('2004-01-01')) | ||
// PARTITION p0 VALUES LESS THAN (63340531200) | ||
if e, err1 := expression.ParseSimpleExprWithTableInfo(ctx, str, tblInfo); err1 == nil { | ||
res, isNull, err2 := e.EvalInt(ctx, chunk.Row{}) | ||
if err2 == nil && isNull == false { | ||
return res, true, nil | ||
if e, err1 := expression.ParseSimpleExprWithTableInfo(ctx, str, tblInfo); err1 == nil { | ||
res, isNull, err2 := e.EvalInt(ctx, chunk.Row{}) | ||
if err2 == nil && isNull == false { | ||
return uint64(res), true, nil | ||
} | ||
} | ||
} else { | ||
if value, err := strconv.ParseInt(str, 10, 64); err == nil { | ||
return value, false, nil | ||
} | ||
// The range value maybe not an integer, it could be a constant expression. | ||
// For example, the following two cases are the same: | ||
// PARTITION p0 VALUES LESS THAN (TO_SECONDS('2004-01-01')) | ||
// PARTITION p0 VALUES LESS THAN (63340531200) | ||
if e, err1 := expression.ParseSimpleExprWithTableInfo(ctx, str, tblInfo); err1 == nil { | ||
res, isNull, err2 := e.EvalInt(ctx, chunk.Row{}) | ||
if err2 == nil && isNull == false { | ||
return res, true, nil | ||
} | ||
} | ||
} | ||
|
||
return 0, false, ErrNotAllowedTypeInPartition.GenByArgs(str) | ||
} | ||
|
||
|
@@ -388,3 +406,15 @@ func checkConstraintIncludePartKey(partkeys []string, constraints map[string]str | |
} | ||
return true | ||
} | ||
|
||
// FindRangePartitionColTp finds the type of the partitioning key column type. | ||
// The returned boolean value indicates whether the partitioning key column type is unsigned bigint type. | ||
func FindRangePartitionColTp(cols []*table.Column, pi *model.PartitionInfo) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/FindRangePartitionColTp/isRangePartitionColUnsignedBigint There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Find column type returns a boolean is weird. |
||
for _, col := range cols { | ||
isUnsigned := col.Tp == mysql.TypeLonglong && mysql.HasUnsignedFlag(col.Flag) | ||
if isUnsigned && strings.Contains(strings.ToLower(pi.Expr), col.Name.L) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ifUnsignedBigint/unsignedBigint