-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
expression, parser: fix issue #3691, cast compatibility #3894
Merged
Merged
Changes from 44 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
7762b8b
expression: handle int builtinUnaryOpSig overflow
winkyao f87e0af
expression: fix #3762, signed integer overflow handle in minus unary …
winkyao 465dd1b
expression: fix #3762, add builtin_op_test.go, some builtin test cases
winkyao 17f1e5f
*: tiny cleanup
winkyao 5589c9e
expression: fix #3691, builtin CAST func compatibilities, partially s…
winkyao c61fea1
Squashed commit of the following:
winkyao c8f218f
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao 1d15330
expression: set the correct unary minus function ScalarFunction.RetType
winkyao 4319ade
Merge branch 'winkyao/fix_issue_3762' into winkyao/cast_compability
winkyao a803f9d
expression: rewrite unary minus builtin function to handle overflow.
winkyao d6e6c25
plan: fix popRowArg bug: when get expression.Constatn and getRowLen i…
winkyao ae342fc
*:git stash
winkyao 869e1f6
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao b305a4f
expression: base #3868 pr, and rewrite builtin function unary minus t…
winkyao bedd270
expression: refactor builtin unary minus function
winkyao 0e5de09
expression: tiny refactor
winkyao 4045075
expression: tiny cleanup
winkyao 636170d
expression: cleanup
winkyao 33bf09c
expresion: cleanup
winkyao eb58c48
expression: tiny cleanup
winkyao b199772
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao 6005a18
Merge branch 'master' into winkyao/fix_issue_3762
winkyao e2aa622
expression: add some comment
winkyao d0395bf
Merge branch 'winkyao/fix_issue_3762' of https://github.com/pingcap/t…
winkyao ffc0df7
Merge branch 'master' into winkyao/fix_issue_3762
winkyao 27202d6
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao 03dc0e5
Merge branch 'winkyao/fix_issue_3762' of https://github.com/pingcap/t…
winkyao 7f01b42
expression: adjust commet
winkyao 14e9711
Merge branch 'winkyao/fix_issue_3762' into winkyao/cast_compability
winkyao 19b2fd9
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao 8f32c92
expression: add more test cases, and make the code more readable
winkyao 197ec13
types,expression,executor: fix #3691 cast compabilities
winkyao 151b0fe
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao c61275c
Merge branch 'master' into winkyao/cast_compability
winkyao e3e5f94
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao 684c314
expression: tiny cleanup
winkyao 4edca6b
Merge branch 'winkyao/cast_compability' of https://github.com/pingcap…
winkyao 028658d
executor: add test case
winkyao 07c31ac
*: cleanup
winkyao bbe1260
Merge branch 'master' into winkyao/cast_compability
winkyao b51e4f1
*: refactor, add parser test case
winkyao 8d98e4d
Merge branch 'winkyao/cast_compability' of https://github.com/pingcap…
winkyao 68f0a1d
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao 266971b
Merge branch 'master' into winkyao/cast_compability
winkyao 1699498
Merge branch 'master' of https://github.com/pingcap/tidb into winkyao…
winkyao 55d25a1
expression: cleanup
winkyao 8ac311c
Merge branch 'winkyao/cast_compability' of https://github.com/pingcap…
winkyao 4ff23cd
Merge branch 'master' into winkyao/cast_compability
XuHuaiyu 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 |
---|---|---|
|
@@ -22,13 +22,16 @@ | |
package expression | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/ast" | ||
"github.com/pingcap/tidb/context" | ||
"github.com/pingcap/tidb/model" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/terror" | ||
"github.com/pingcap/tidb/util/charset" | ||
"github.com/pingcap/tidb/util/types" | ||
) | ||
|
@@ -544,22 +547,24 @@ func (b *builtinCastDecimalAsIntSig) evalInt(row []types.Datum) (res int64, isNu | |
if isNull || err != nil { | ||
return res, isNull, errors.Trace(err) | ||
} | ||
|
||
// Round is needed for both unsigned and signed. | ||
var to types.MyDecimal | ||
val.Round(&to, 0, types.ModeHalfEven) | ||
|
||
if mysql.HasUnsignedFlag(b.tp.Flag) { | ||
var ( | ||
floatVal float64 | ||
uintRes uint64 | ||
) | ||
floatVal, err = val.ToFloat64() | ||
if err != nil { | ||
return res, false, errors.Trace(err) | ||
} | ||
uintRes, err = types.ConvertFloatToUint(sc, floatVal, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeDouble) | ||
var uintRes uint64 | ||
uintRes, err = to.ToUint() | ||
res = int64(uintRes) | ||
} else { | ||
var to types.MyDecimal | ||
val.Round(&to, 0, types.ModeHalfEven) | ||
res, err = to.ToInt() | ||
} | ||
|
||
if terror.ErrorEqual(err, types.ErrOverflow) { | ||
warnErr := types.ErrTruncatedWrongVal.GenByArgs("DECIMAL", val) | ||
err = sc.HandleOverflow(err, warnErr) | ||
} | ||
|
||
return res, false, errors.Trace(err) | ||
} | ||
|
||
|
@@ -638,6 +643,30 @@ type builtinCastStringAsIntSig struct { | |
baseIntBuiltinFunc | ||
} | ||
|
||
// handleOverflow handle the overflow caused by cast string as int, | ||
// see https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html. | ||
// When an out-of-range value is assigned to an integer column, MySQL stores the value representing the corresponding endpoint of the column data type range. If it is in select statement, it will return the | ||
// endpoint value with a warning. | ||
func (b *builtinCastStringAsIntSig) handleOverflow(origRes int64, origStr string, origErr error, isNegative bool) (res int64, err error) { | ||
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. may add a comment for this. |
||
res, err = origRes, origErr | ||
if err == nil { | ||
return | ||
} | ||
|
||
sc := b.getCtx().GetSessionVars().StmtCtx | ||
if sc.InSelectStmt && terror.ErrorEqual(origErr, types.ErrOverflow) { | ||
if isNegative { | ||
res = math.MinInt64 | ||
} else { | ||
uval := uint64(math.MaxUint64) | ||
res = int64(uval) | ||
} | ||
warnErr := types.ErrTruncatedWrongVal.GenByArgs("INTEGER", origStr) | ||
err = sc.HandleOverflow(origErr, warnErr) | ||
} | ||
return | ||
} | ||
|
||
func (b *builtinCastStringAsIntSig) evalInt(row []types.Datum) (res int64, isNull bool, err error) { | ||
sc := b.getCtx().GetSessionVars().StmtCtx | ||
if IsHybridType(b.args[0]) { | ||
|
@@ -647,13 +676,30 @@ func (b *builtinCastStringAsIntSig) evalInt(row []types.Datum) (res int64, isNul | |
if isNull || err != nil { | ||
return res, isNull, errors.Trace(err) | ||
} | ||
if mysql.HasUnsignedFlag(b.tp.Flag) { | ||
var ures uint64 | ||
|
||
val = strings.TrimSpace(val) | ||
isNegative := false | ||
if len(val) > 1 && val[0] == '-' { // negative number | ||
isNegative = true | ||
} | ||
|
||
var ures uint64 | ||
if isNegative { | ||
res, err = types.StrToInt(sc, val) | ||
if err == nil { | ||
// If overflow, don't append this warnings | ||
sc.AppendWarning(types.ErrCastNegIntAsUnsigned) | ||
} | ||
} else { | ||
ures, err = types.StrToUint(sc, val) | ||
res = int64(ures) | ||
} else { | ||
res, err = types.StrToInt(sc, val) | ||
|
||
if err == nil && !mysql.HasUnsignedFlag(b.tp.Flag) && ures > uint64(math.MaxInt64) { | ||
sc.AppendWarning(types.ErrCastAsSignedOverflow) | ||
} | ||
} | ||
|
||
res, err = b.handleOverflow(res, val, err, isNegative) | ||
return res, false, errors.Trace(err) | ||
} | ||
|
||
|
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
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
Oops, something went wrong.
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.
remove this empty line.