-
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
*: set and use Flags to properly handle truncate error #2212
Conversation
StatementContext is also updated according to Flags.
@@ -374,7 +373,7 @@ func testStrToUint(c *C, str string, expect uint64, truncateAsErr bool, expectEr | |||
|
|||
func testStrToFloat(c *C, str string, expect float64, truncateAsErr bool, expectErr error) { |
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.
Why not pass IgnoreTruncate directly?
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.
Then too many test case need to change.
if sc.TruncateAsWarning { | ||
sc.AppendWarning(ErrTruncated) | ||
} else { | ||
err = ErrTruncated |
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.
Why not trace error?
tk.MustExec("insert sc values ('1.8'+1)") | ||
tk.MustQuery("select * from sc").Check(testkit.Rows("3")) | ||
|
||
// handle coprocessor flags, '1x' is an invalid data. |
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.
handle -> Handle , same as next line
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.
invalid data -> invalid int
strictModeSQL = "set sql_mode = 'STRICT_TRANS_TABLES'" | ||
nonStrictModeSQL = "set sql_mode = ''" | ||
) | ||
|
||
func (s *testSuite) TestStatementContext(c *C) { | ||
tk := testkit.NewTestKit(c, s.store) |
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.
Need we add defer here?
// FlagsToStatementContext creates a StatementContext from a `tipb.SelectRequest.Flags`. | ||
func FlagsToStatementContext(flags uint64) *variable.StatementContext { | ||
sc := new(variable.StatementContext) | ||
sc.IgnoreTruncate = flags&FlagIgnoreTruncate > 0 |
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.
(flags & flagignoreTruncate) != 0 is more cleare
@@ -986,8 +986,12 @@ func (d *Datum) convertToMysqlDecimal(sc *variable.StatementContext, target *Fie | |||
} else if frac != target.Decimal { | |||
dec.Round(dec, target.Decimal) | |||
if frac > target.Decimal { | |||
if sc.TruncateAsError { | |||
err = errors.Trace(ErrTruncated) | |||
if !sc.IgnoreTruncate { |
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.
Extract a function to reduce the nested layers and it can be used in convert.go
const ( | ||
FlagIgnoreTruncate uint64 = 1 | ||
FlagTruncateAsWarning uint64 = 1 << 1 | ||
) |
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.
const (
FlagA = 1 << iota
FlagB
)
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.
This flag is used by both TiDB and TiKV, we better make it explicit and consistent.
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.
Please add comments.
7e7bab8
to
4e1210d
Compare
@@ -348,9 +347,9 @@ func (s *testTypeConvertSuite) TestConvertToString(c *C) { | |||
} | |||
} | |||
|
|||
func testStrToInt(c *C, str string, expect int64, truncateAsErr bool, expectErr error) { | |||
func testStrToInt(c *C, str string, expect int64, truncateAsError bool, expectErr error) { |
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.
Why this function's arg is AsError but the followings is AsErr ?
4e1210d
to
5a3f3cc
Compare
@hanfei1991 @disksing @ngaut PTAL |
LGTM |
1 similar comment
LGTM |
StatementContext is also updated according to Flags.