-
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
types: fix string to integer cast #11295
Conversation
Codecov Report
@@ Coverage Diff @@
## master #11295 +/- ##
==========================================
- Coverage 81.84% 81.36% -0.48%
==========================================
Files 424 424
Lines 93108 90833 -2275
==========================================
- Hits 76202 73906 -2296
- Misses 11602 11612 +10
- Partials 5304 5315 +11
Continue to review full report at Codecov.
|
/run-all-tests tidb-test=pr/863 |
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
PTAL @zz-jason |
@@ -219,7 +219,10 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP | |||
p.IsTableDual = true | |||
return p | |||
} | |||
return nil | |||
// some scenarios cast to int with error, but we may use this value in point get | |||
if !terror.ErrorEqual(types.ErrTruncatedWrongVal, err) { |
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.
should we return a warning?
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.
warning has been added in truncated error handler
@@ -625,7 +649,7 @@ func getValidFloatPrefix(sc *stmtctx.StatementContext, s string) (valid string, | |||
valid = "0" | |||
} | |||
if validLen == 0 || validLen != len(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.
if len(str) == 0, validLen
can still be zero, but it's not truncated?
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 checked some behavior in MySQL, and get the following result
mysql> show create table t; +-------+--------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------+
| t | CREATE TABLE `t` (
`id` int(11) DEFAULT NULL,
`val` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into t values ('', 'a');
ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1
mysql> insert into t values ('0', 'a'), ('1', 'a');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t where id = '';
+------+------+
| id | val |
+------+------+
| 0 | a |
+------+------+
1 row in set (0.00 sec)
mysql> update t set val = 'b' where id = '';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t;
+------+------+
| id | val |
+------+------+
| 0 | b |
| 1 | a |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from t where id = '1.0';
+------+------+
| id | val |
+------+------+
| 1 | a |
+------+------+
1 row in set (0.00 sec)
mysql> update t set val = 'c' where id = '1.0';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 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.
so in select and update context, ''
will be cast to 0
without no warnings. Added these two conditions in the first logic of getValidFloatPrefix
b2df5b8
to
b5f0657
Compare
/run-all-tests |
/run-all-tests tidb-test=pr/863 |
/run-all-tests tidb-test=pr/863 |
/run-unit-tests |
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
cherry pick to release-2.1 failed |
cherry pick to release-3.0 failed |
@amyangfei Please cherry-pick this to release-2.1 |
What problem does this PR solve?
part of #11223 fix issue #11179
What is changed and how it works?
add a
ConvertStrToIntStrict
to control the way we convert string to int.getValidFloatPrefix
, thenfloatStrToIntStr
0-9
, (+
or-
in first bit).only set
ConvertStrToIntStrict
to true inselect/explain
context, which is compatible with MySQLfollowing is the cast behavior in insert of MySQL 5.7
Check List
Tests
Related changes