-
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
types: fix delete error when convert string to float or int #10861
Conversation
Codecov Report
@@ Coverage Diff @@
## master #10861 +/- ##
===========================================
Coverage 81.2876% 81.2876%
===========================================
Files 423 423
Lines 90133 90133
===========================================
Hits 73267 73267
Misses 11561 11561
Partials 5305 5305 |
47619a2
to
6e87e72
Compare
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.
LGTM
@@ -580,6 +580,10 @@ func ConvertJSONToDecimal(sc *stmtctx.StatementContext, j json.BinaryJSON) (*MyD | |||
|
|||
// getValidFloatPrefix gets prefix of string which can be successfully parsed as float. | |||
func getValidFloatPrefix(sc *stmtctx.StatementContext, s string) (valid string, err error) { | |||
if sc.InDeleteStmt && s == "" { |
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 function is used in StrToUint
, maybe not only delete statement?
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.
The condition statementContext.InDeleteStmt == true
will keep it only work in delete statement.
And in delete statement, empty string can always convert to 0 without error and warning.(according to behaviour of mysql 5.x and 8.x ).
But in other statement like select, tidb is consistent with mysql(all with warning), so I restrict it in delete statement context.
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.
I suggest do not add more parameter for func testStrToFloat
, considering 2 points:
- This new parameter
inDeleteStmt
only use in one test case - String is empty. We don't need to bring this parameter to other test case, which is this PR has done. - The test cases of
TestStrToInt
andTestStrToUInt
are missing. According to the Issue's description Inconsisitent with Mysql When a varchar column only have empty string #10806, we actually need these test case.
In my view, we could add a new test function for the case of this issue
func testDeleteEmptyStringError(c *C) {
sc := new(stmtctx.StatementContext)
sc.InDeleteStmt = true
str := ""
expect := 0
val, err := StrToInt(sc, str)
c.Assert(err, IsNil)
c.Assert(val, Equals, int64(expect))
val1, err := StrToUint(sc, str)
c.Assert(err, IsNil)
c.Assert(val1, Equals, uint64(expect))
val2, err := StrToFloat(sc, str)
c.Assert(err, IsNil)
c.Assert(val2, Equals, float64(expect))
}
and call the new test function in the end of func TestStrToNum
testStrToFloat(c, "-1e649", -math.MaxFloat64, true, ErrTruncatedWrongVal)
testStrToFloat(c, "-1e649", -math.MaxFloat64, false, nil)
+ // for issue #10806
+ testDeleteEmptyStringError(c)
}
@Deardrops You are right, I have corrected unit test in this pr. |
LGTM |
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.
LGTM
@DQinYuan Sorry for the late review. Could you update the PR with the latest master branch and resolve the conflicts? |
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.
LGTM
What problem does this PR solve?
To fix the issue #10806
When delete from a varchar column only with empty string and a condition compare with a integer or float, tidb will return a "Data Truncated Error" and mysql(both 5.x and 8.x) will only return "Query OK, 0 rows affected (0.01 sec) "
What is changed and how it works?
Before:
After:
modify method
getValidFloatPrefix
in types/convert.goWhen stmt context is "delete"(i.e. sc.InDeleteStmt is true) and string is empty,
getValidFloatPrefix
directly return "0" and nil error.Check List
Tests