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 |
|
PTAL @zz-jason |
| } | ||
| 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.
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.
if len(str) == 0, validLen can still be zero, but it's not truncated?
There was a problem hiding this comment.
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.
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 |
|
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
ConvertStrToIntStrictto control the way we convert string to int.getValidFloatPrefix, thenfloatStrToIntStr0-9, (+or-in first bit).only set
ConvertStrToIntStrictto true inselect/explaincontext, which is compatible with MySQLfollowing is the cast behavior in insert of MySQL 5.7
Check List
Tests
Related changes