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

expression, parser: fix issue #3691, cast compatibility #3894

Merged
merged 48 commits into from
Aug 1, 2017

Conversation

winkyao
Copy link
Contributor

@winkyao winkyao commented Jul 26, 2017

fix #3691, also fix #645

this pr will fail the jenkins, so should be merged together.

winkyao and others added 30 commits July 17, 2017 11:13
Merge branch winkyao/fix_issue_3762 into winkyao/cast_compability
@XuHuaiyu XuHuaiyu changed the title expression, parser: fix issue #3691, cast compabilities expression, parser: fix issue #3691, cast compatibility Jul 28, 2017
@@ -1097,6 +1099,75 @@ func (s *testSuite) TestBuiltin(c *C) {
result = tk.MustQuery("select cast(-1 as unsigned)")
result.Check(testkit.Rows("18446744073709551615"))

// Fix issue #3691, cast compabilities.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ compabilities/ compatibility

@@ -23,12 +23,16 @@ package expression

import (
"strconv"
"strings"

Copy link
Contributor

Choose a reason for hiding this comment

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

remove this empty line.

@@ -23,12 +23,16 @@ package expression

import (
"strconv"
"strings"

"math"
Copy link
Contributor

Choose a reason for hiding this comment

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

move math to line 25

Copy link
Member

Choose a reason for hiding this comment

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

Why make check ignore this?

@@ -542,22 +546,24 @@ func (b *builtinCastDecimalAsIntSig) evalInt(row []types.Datum) (res int64, isNu
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}

// despite of unsigned or signed, Round is needed
Copy link
Contributor

Choose a reason for hiding this comment

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

Round is needed for both unsigned and signed.

@@ -106,6 +106,7 @@ func (s *testPlanSuite) TestInferType(c *C) {
{"substr(c_binary, c_int)", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag, 20, types.UnspecifiedLength},
{"uuid()", mysql.TypeVarString, charset.CharsetUTF8, 0, 36, types.UnspecifiedLength},
{"bit_length(c_char)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 10, 0},

Copy link
Contributor

Choose a reason for hiding this comment

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

remove this line?

@@ -422,6 +423,19 @@ func (sc *StatementContext) HandleTruncate(err error) error {
return err
}

// HandleOverflow treat ErrOverflow as warnings or returns the error bases on the StmtCtx.OverflowAsWarning state.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ treat/ treats

@@ -1131,6 +1131,11 @@ func ProduceDecWithSpecifiedTp(dec *MyDecimal, tp *FieldType, sc *variable.State
}
}
}

if terror.ErrorEqual(err, ErrOverflow) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we put this check into HandleOverflow ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@XuHuaiyu no, because the variable.session.go should not import "types" package, because "types" use "variable" package, it will cause circle import problem

@@ -37,35 +39,46 @@ var (
ErrWrongFieldSpec = terror.ClassTypes.New(codeWrongFieldSpec, "Wrong Field Spec")
// ErrBadNumber is return when parsing an invalid binary decimal number.
ErrBadNumber = terror.ClassTypes.New(codeBadNumber, "Bad Number")
// ErrCastSignedOverflow is returned when positive out-of-range integer, and convert to it's negative complement
ErrCastSignedOverflow = terror.ClassTypes.New(codeUnknown, msgCastSignedOverflow)
// ErrCastNegIntToUnsigned is returned when a negative integer cast to unsigned
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ ErrCastNegIntToUnsigned/ ErrCastNegIntAsUnsigned be better?

s/ cast/ be casted
s/ unsigned/ an unsigned int

add . at the end.

@@ -37,35 +39,46 @@ var (
ErrWrongFieldSpec = terror.ClassTypes.New(codeWrongFieldSpec, "Wrong Field Spec")
// ErrBadNumber is return when parsing an invalid binary decimal number.
ErrBadNumber = terror.ClassTypes.New(codeBadNumber, "Bad Number")
// ErrCastSignedOverflow is returned when positive out-of-range integer, and convert to it's negative complement
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. s/ ErrCastSignedOverflow/ ErrCastAsSignedOverflow be better?
  2. add . at the end

// see https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict
// said "For statements such as SELECT that do not change data, invalid values
// generate a warning in strict mode, not an error."
// and and https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
Copy link
Member

Choose a reason for hiding this comment

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

and and ?

@@ -23,12 +23,16 @@ package expression

import (
"strconv"
"strings"

"math"
Copy link
Member

Choose a reason for hiding this comment

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

Why make check ignore this?

uintRes, err = types.ConvertFloatToUint(sc, floatVal, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeDouble)
res = int64(uintRes)
var ures uint64
ures, err = to.ToUint()
Copy link
Member

Choose a reason for hiding this comment

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

The old name uintRes may be more readable.

@winkyao
Copy link
Contributor Author

winkyao commented Jul 29, 2017

@XuHuaiyu @zz-jason PTAL

msgOverflow = mysql.MySQLErrName[mysql.ErrDataOutOfRange]
msgOverflow = mysql.MySQLErrName[mysql.ErrDataOutOfRange]
msgTruncatedWrongVal = mysql.MySQLErrName[mysql.ErrTruncatedWrongValue]
msgCastSignedOverflow = "Cast to signed converted positive out-of-range integer to it's negative complement"
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ msgCastSignedOverflow/ msgCastAsSignedOverflow
s/ msgCastNegIntToUnsigned/ msgCastNegIntAsUnsigned

@@ -636,6 +641,27 @@ type builtinCastStringAsIntSig struct {
baseIntBuiltinFunc
}

func (b *builtinCastStringAsIntSig) handleOverflow(origRes int64, origStr string, origErr error, isNegative bool) (res int64, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

may add a comment for this.

sc.OverflowAsWarning = true

// cast('18446744073709551616' as unsigned);
tp1 := types.NewFieldType(mysql.TypeLonglong)
Copy link
Contributor

Choose a reason for hiding this comment

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

will this be more readable?

tp1 := &types.FieldType{
    Tp:
    Flag:
    Charset:
    ....
}

// create table t1(s1 time);
// insert into t1 values('11:11:11');
// select cast(s1 as decimal(7, 2)) from t1;
tpDecimal := types.NewFieldType(mysql.TypeNewDecimal)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@@ -5909,6 +5909,7 @@ IntegerType:
OptInteger:
{}
| "INTEGER"
| "INT"
Copy link
Contributor

Choose a reason for hiding this comment

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

add a test case in parser_test

@@ -422,6 +423,19 @@ func (sc *StatementContext) HandleTruncate(err error) error {
return err
}

// HandleOverflow treats ErrOverflow as warnings or returns the error bases on the StmtCtx.OverflowAsWarning state.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ bases/ based

@winkyao
Copy link
Contributor Author

winkyao commented Jul 31, 2017

@XuHuaiyu @jackysp PTAL

@jackysp
Copy link
Member

jackysp commented Aug 1, 2017

LGTM

@winkyao winkyao added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 1, 2017
XuHuaiyu
XuHuaiyu previously approved these changes Aug 1, 2017
Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

@XuHuaiyu XuHuaiyu added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Aug 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug. type/compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Builtin function "CAST" compatibilities Cast function should not fail.
3 participants