[MDEV-8466] CAST works differently for DECIMAL/INT vs DOUBLE for empty strings Created: 2015-07-15  Updated: 2015-09-25  Resolved: 2015-09-17

Status: Closed
Project: MariaDB Server
Component/s: OTHER
Affects Version/s: 5.5, 10.0, 10.1
Fix Version/s: 10.1.8

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-8109 unexpected CAST result Closed
relates to MDEV-8300 CAST('' AS DECIMAL) is too strict on ... Stalled
relates to MDEV-8468 CAST and INSERT work differently for ... Closed
relates to MDEV-8806 Numeric CAST produce different warnin... Closed

 Description   

DECIMAL/INT vs DOUBLE work differently on CAST for empty strings:

SET sql_mode='STRICT_ALL_TABLES';
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (CAST('' AS INT));

and

SET sql_mode='STRICT_ALL_TABLES';
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a DECIMAL);
INSERT INTO t1 VALUES (CAST('' AS DECIMAL));

correctly return

ERROR 1292 (22007): Truncated incorrect DECIMAL value: ''

DOUBLE works differently:

SET sql_mode='STRICT_ALL_TABLES';
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a DOUBLE);
INSERT INTO t1 VALUES (CAST('' AS DOUBLE));

It accepts the value silently:

Query OK, 1 row affected (0.03 sec)

This happens because the CAST alone does not produce any warnings for DOUBLE:

SELECT CAST('' AS DOUBLE);

and produces a warning for DECIMAL and INT:

MariaDB [test]> SELECT CAST('' AS INT);
+-----------------+
| CAST('' AS INT) |
+-----------------+
|               0 |
+-----------------+
1 row in set, 1 warning (0.00 sec)
 
MariaDB [test]> SELECT CAST('' AS INT);
+-----------------+
| CAST('' AS INT) |
+-----------------+
|               0 |
+-----------------+
1 row in set, 1 warning (0.00 sec)
 
MariaDB [test]> SHOW WARNINGS;
+---------+------+---------------------------------------+
| Level   | Code | Message                               |
+---------+------+---------------------------------------+
| Warning | 1292 | Truncated incorrect INTEGER value: '' |
+---------+------+---------------------------------------+
1 row in set (0.00 sec)

During a discussion on maria-developers, Sergei and Bar agreed that a warning should be generated for all numeric data types.


Generated at Thu Feb 08 07:27:22 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.