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

*: set and use Flags to properly handle truncate error #2212

Merged
merged 5 commits into from
Dec 12, 2016

Conversation

coocood
Copy link
Member

@coocood coocood commented Dec 9, 2016

StatementContext is also updated according to Flags.

StatementContext is also updated according to Flags.
@coocood
Copy link
Member Author

coocood commented Dec 9, 2016

@zimulala @tiancaiamao @BusyJay PTAL

@@ -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) {
Copy link
Member

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?

Copy link
Member Author

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
Copy link
Member

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.
Copy link
Member

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

Copy link
Member

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)
Copy link
Member

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
Copy link
Member

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 {
Copy link
Contributor

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
)
Copy link
Contributor

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
)

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Please add comments.

@coocood coocood force-pushed the coocood/local-region-flag branch from 7e7bab8 to 4e1210d Compare December 9, 2016 10:42
@@ -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) {
Copy link
Member

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 ?

@coocood coocood force-pushed the coocood/local-region-flag branch from 4e1210d to 5a3f3cc Compare December 9, 2016 11:42
@coocood
Copy link
Member Author

coocood commented Dec 9, 2016

@hanfei1991 @disksing @ngaut PTAL

@hanfei1991
Copy link
Member

LGTM

1 similar comment
@zimulala
Copy link
Contributor

LGTM

@coocood coocood merged commit 1b8051d into master Dec 12, 2016
@coocood coocood deleted the coocood/local-region-flag branch December 12, 2016 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants