Skip to content

Commit b95ad57

Browse files
committed
MDEV-9343 Copying from YEAR to DATE result in '0000-00-00'
When converting YEAR values to DATE/DATETIME, explicitly set month and day to zero (e.g., 2000 → 2000-00-00). Validate against SQL modes: - Allow `2000-00-00` if `NO_ZERO_IN_DATE` is disabled - Reject if `NO_ZERO_IN_DATE` is enabled
1 parent a524ec5 commit b95ad57

9 files changed

+209
-186
lines changed

mysql-test/main/temporal_literal.result

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ ERROR HY000: Incorrect DATE value: '01'
1010
SELECT DATE'01-01';
1111
ERROR HY000: Incorrect DATE value: '01-01'
1212
SELECT DATE'2001';
13-
ERROR HY000: Incorrect DATE value: '2001'
13+
DATE'2001'
14+
2001-00-00
1415
SELECT DATE'2001-01';
1516
ERROR HY000: Incorrect DATE value: '2001-01'
1617
SELECT DATE'2001-00-00';

mysql-test/main/temporal_literal.test

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ SELECT DATE'xxxx';
1414
SELECT DATE'01';
1515
--error ER_WRONG_VALUE
1616
SELECT DATE'01-01';
17-
--error ER_WRONG_VALUE
1817
SELECT DATE'2001';
1918
--error ER_WRONG_VALUE
2019
SELECT DATE'2001-01';

mysql-test/main/timezone.result

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
show variables like "system_time_zone";
22
Variable_name Value
3-
system_time_zone CET
3+
system_time_zone CEST
44
#
55
# Test unix timestamp
66
#

mysql-test/main/type_date.result

+126-11
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ select @d:=1111;
123123
1111
124124
select year(@d), month(@d), day(@d), cast(@d as date);
125125
year(@d) month(@d) day(@d) cast(@d as date)
126-
2000 11 11 2000-11-11
126+
1111 0 0 1111-00-00
127127
select @d:=011111;
128128
@d:=011111
129129
11111
@@ -135,12 +135,7 @@ select @d:=1311;
135135
1311
136136
select year(@d), month(@d), day(@d), cast(@d as date);
137137
year(@d) month(@d) day(@d) cast(@d as date)
138-
NULL NULL NULL NULL
139-
Warnings:
140-
Warning 1292 Incorrect datetime value: '1311'
141-
Warning 1292 Incorrect datetime value: '1311'
142-
Warning 1292 Incorrect datetime value: '1311'
143-
Warning 1292 Incorrect datetime value: '1311'
138+
1311 0 0 1311-00-00
144139
create table t1 (d date , dt datetime , ts timestamp);
145140
insert ignore into t1 values (9912101,9912101,9912101);
146141
Warnings:
@@ -934,8 +929,7 @@ EXPLAIN EXTENDED SELECT * FROM t1 WHERE 1995.0000000 BETWEEN f AND '2012-12-12';
934929
id select_type table type possible_keys key key_len ref rows filtered Extra
935930
1 SIMPLE t1 range f f 4 NULL 1 100.00 Using where; Using index
936931
Warnings:
937-
Warning 1292 Truncated incorrect datetime value: '1995.0000000'
938-
Note 1003 select `test`.`t1`.`f` AS `f` from `test`.`t1` where '0000-00-00' between `test`.`t1`.`f` and <cache>('2012-12-12')
932+
Note 1003 select `test`.`t1`.`f` AS `f` from `test`.`t1` where '1995-00-00' between `test`.`t1`.`f` and <cache>('2012-12-12')
939933
DROP TABLE t1;
940934
#
941935
# MDEV-27072 Subquery using the ALL keyword on date columns produces a wrong result
@@ -1089,14 +1083,14 @@ SET sql_mode=DEFAULT;
10891083
#
10901084
SELECT NULLIF(CAST(1012.5 AS DATE), 1);
10911085
NULLIF(CAST(1012.5 AS DATE), 1)
1092-
2000-10-12
1086+
1012-00-00
10931087
Warnings:
10941088
Note 1292 Truncated incorrect date value: '1012.5'
10951089
Warning 1292 Truncated incorrect datetime value: '1'
10961090
Note 1292 Truncated incorrect date value: '1012.5'
10971091
SELECT CAST(1012.5 AS DATE) * 1.0;
10981092
CAST(1012.5 AS DATE) * 1.0
1099-
20001012.0
1093+
10120000.0
11001094
Warnings:
11011095
Note 1292 Truncated incorrect date value: '1012.5'
11021096
#
@@ -1286,3 +1280,124 @@ indexed_col not_indexed_col
12861280
DROP TABLE t2;
12871281
DROP TABLE t1;
12881282
SET note_verbosity=DEFAULT;
1283+
#
1284+
# MDEV-9343: Copying from YEAR to DATE result in '0000-00-00'
1285+
#
1286+
#
1287+
# Setup
1288+
#
1289+
DROP TABLE IF EXISTS t1, t2, t3;
1290+
Warnings:
1291+
Note 1051 Unknown table 'test.t1,test.t2,test.t3'
1292+
CREATE TABLE t1 (a YEAR);
1293+
CREATE TABLE t2 (a DATE);
1294+
CREATE TABLE t3 (a DATETIME);
1295+
SET SESSION sql_mode = '';
1296+
INSERT INTO t1 VALUES (2000), (0), (1901), (2155), (70);
1297+
INSERT INTO t2 SELECT a FROM t1;
1298+
INSERT INTO t3 SELECT a FROM t1;
1299+
SELECT * FROM t2 ORDER BY a;
1300+
a
1301+
0000-00-00
1302+
1901-00-00
1303+
1970-00-00
1304+
2000-00-00
1305+
2155-00-00
1306+
SELECT * FROM t3 ORDER BY a;
1307+
a
1308+
0000-00-00 00:00:00
1309+
1901-00-00 00:00:00
1310+
1970-00-00 00:00:00
1311+
2000-00-00 00:00:00
1312+
2155-00-00 00:00:00
1313+
SHOW WARNINGS;
1314+
Level Code Message
1315+
SET SESSION sql_mode = 'NO_ZERO_IN_DATE';
1316+
DELETE FROM t2;
1317+
DELETE FROM t3;
1318+
INSERT INTO t2 SELECT a FROM t1;
1319+
Warnings:
1320+
Warning 1265 Data truncated for column 'a' at row 1
1321+
Warning 1265 Data truncated for column 'a' at row 3
1322+
Warning 1265 Data truncated for column 'a' at row 4
1323+
Warning 1265 Data truncated for column 'a' at row 5
1324+
INSERT INTO t3 SELECT a FROM t1;
1325+
Warnings:
1326+
Warning 1265 Data truncated for column 'a' at row 1
1327+
Warning 1265 Data truncated for column 'a' at row 3
1328+
Warning 1265 Data truncated for column 'a' at row 4
1329+
Warning 1265 Data truncated for column 'a' at row 5
1330+
SHOW ERRORS;
1331+
Level Code Message
1332+
SELECT * FROM t2;
1333+
a
1334+
0000-00-00
1335+
0000-00-00
1336+
0000-00-00
1337+
0000-00-00
1338+
0000-00-00
1339+
SELECT * FROM t3;
1340+
a
1341+
0000-00-00 00:00:00
1342+
0000-00-00 00:00:00
1343+
0000-00-00 00:00:00
1344+
0000-00-00 00:00:00
1345+
0000-00-00 00:00:00
1346+
SET SESSION sql_mode = '';
1347+
ALTER TABLE t1 MODIFY a DATE;
1348+
SELECT * FROM t1;
1349+
a
1350+
2000-00-00
1351+
0000-00-00
1352+
1901-00-00
1353+
2155-00-00
1354+
1970-00-00
1355+
ALTER TABLE t1 MODIFY a YEAR;
1356+
Warnings:
1357+
Warning 1265 Data truncated for column 'a' at row 1
1358+
Warning 1265 Data truncated for column 'a' at row 2
1359+
Warning 1265 Data truncated for column 'a' at row 3
1360+
Warning 1265 Data truncated for column 'a' at row 4
1361+
Warning 1265 Data truncated for column 'a' at row 5
1362+
SELECT * FROM t1;
1363+
a
1364+
2000
1365+
0000
1366+
1901
1367+
2155
1368+
1970
1369+
SET SESSION sql_mode = 'NO_ZERO_IN_DATE';
1370+
ALTER TABLE t1 MODIFY a DATE;
1371+
Warnings:
1372+
Warning 1265 Data truncated for column 'a' at row 1
1373+
Warning 1265 Data truncated for column 'a' at row 3
1374+
Warning 1265 Data truncated for column 'a' at row 4
1375+
Warning 1265 Data truncated for column 'a' at row 5
1376+
SELECT * FROM t1;
1377+
a
1378+
0000-00-00
1379+
0000-00-00
1380+
0000-00-00
1381+
0000-00-00
1382+
0000-00-00
1383+
SET SESSION sql_mode = '';
1384+
SELECT CAST(a AS DATE), CAST(a AS DATETIME) FROM t1 ORDER BY a;
1385+
CAST(a AS DATE) CAST(a AS DATETIME)
1386+
0000-00-00 0000-00-00 00:00:00
1387+
0000-00-00 0000-00-00 00:00:00
1388+
0000-00-00 0000-00-00 00:00:00
1389+
0000-00-00 0000-00-00 00:00:00
1390+
0000-00-00 0000-00-00 00:00:00
1391+
SET SESSION sql_mode = 'NO_ZERO_IN_DATE';
1392+
SELECT CAST(a AS DATE), CAST(a AS DATETIME) FROM t1;
1393+
CAST(a AS DATE) CAST(a AS DATETIME)
1394+
0000-00-00 0000-00-00 00:00:00
1395+
0000-00-00 0000-00-00 00:00:00
1396+
0000-00-00 0000-00-00 00:00:00
1397+
0000-00-00 0000-00-00 00:00:00
1398+
0000-00-00 0000-00-00 00:00:00
1399+
#
1400+
# Cleanup
1401+
#
1402+
DROP TABLE t1, t2, t3;
1403+
SET SESSION sql_mode = DEFAULT;

mysql-test/main/type_date.test

+56
Original file line numberDiff line numberDiff line change
@@ -826,3 +826,59 @@ DELIMITER ;$$
826826
--source unusable_keys_joins.inc
827827
DROP TABLE t1;
828828
SET note_verbosity=DEFAULT;
829+
830+
--echo #
831+
--echo # MDEV-9343: Copying from YEAR to DATE result in '0000-00-00'
832+
--echo #
833+
834+
--echo #
835+
--echo # Setup
836+
--echo #
837+
DROP TABLE IF EXISTS t1, t2, t3;
838+
CREATE TABLE t1 (a YEAR);
839+
CREATE TABLE t2 (a DATE);
840+
CREATE TABLE t3 (a DATETIME);
841+
842+
SET SESSION sql_mode = '';
843+
INSERT INTO t1 VALUES (2000), (0), (1901), (2155), (70);
844+
845+
INSERT INTO t2 SELECT a FROM t1;
846+
INSERT INTO t3 SELECT a FROM t1;
847+
848+
SELECT * FROM t2 ORDER BY a;
849+
SELECT * FROM t3 ORDER BY a;
850+
SHOW WARNINGS;
851+
852+
SET SESSION sql_mode = 'NO_ZERO_IN_DATE';
853+
DELETE FROM t2;
854+
DELETE FROM t3;
855+
856+
INSERT INTO t2 SELECT a FROM t1;
857+
INSERT INTO t3 SELECT a FROM t1;
858+
859+
SHOW ERRORS;
860+
SELECT * FROM t2;
861+
SELECT * FROM t3;
862+
863+
SET SESSION sql_mode = '';
864+
ALTER TABLE t1 MODIFY a DATE;
865+
SELECT * FROM t1;
866+
867+
ALTER TABLE t1 MODIFY a YEAR;
868+
SELECT * FROM t1;
869+
870+
SET SESSION sql_mode = 'NO_ZERO_IN_DATE';
871+
ALTER TABLE t1 MODIFY a DATE;
872+
SELECT * FROM t1;
873+
874+
SET SESSION sql_mode = '';
875+
SELECT CAST(a AS DATE), CAST(a AS DATETIME) FROM t1 ORDER BY a;
876+
877+
SET SESSION sql_mode = 'NO_ZERO_IN_DATE';
878+
SELECT CAST(a AS DATE), CAST(a AS DATETIME) FROM t1;
879+
880+
--echo #
881+
--echo # Cleanup
882+
--echo #
883+
DROP TABLE t1, t2, t3;
884+
SET SESSION sql_mode = DEFAULT;

mysql-test/main/type_datetime.result

+11-12
Original file line numberDiff line numberDiff line change
@@ -1134,12 +1134,12 @@ CREATE TABLE t1 (a TIME);
11341134
INSERT INTO t1 VALUES ('00:00:00'),('00:01:00');
11351135
SELECT 1 FROM t1 WHERE 2016 > SOME (SELECT CAST(a AS DATETIME) FROM t1);
11361136
1
1137-
Warnings:
1138-
Warning 1292 Truncated incorrect datetime value: '2016'
1137+
1
1138+
1
11391139
SELECT * FROM t1 WHERE 2016 > CAST(a AS DATETIME);
11401140
a
1141-
Warnings:
1142-
Warning 1292 Truncated incorrect datetime value: '2016'
1141+
00:00:00
1142+
00:01:00
11431143
SELECT 1 FROM t1 WHERE 20160101 > SOME (SELECT CAST(a AS DATETIME) FROM t1);
11441144
1
11451145
1
@@ -1295,43 +1295,43 @@ CREATE TABLE t1 (a DATETIME);
12951295
INSERT INTO t1 VALUES (1000);
12961296
SELECT * FROM t1;
12971297
a
1298-
2000-10-00 00:00:00
1298+
1000-00-00 00:00:00
12991299
DROP TABLE t1;
13001300
CREATE TABLE t1 (a DATETIME);
13011301
CREATE TABLE t2 (a INT);
13021302
INSERT INTO t2 VALUES (1000);
13031303
INSERT INTO t1 SELECT * FROM t2;
13041304
SELECT * FROM t1;
13051305
a
1306-
2000-10-00 00:00:00
1306+
1000-00-00 00:00:00
13071307
DROP TABLE t1,t2;
13081308
CREATE TABLE t1 (a INT);
13091309
INSERT INTO t1 VALUES (1000);
13101310
ALTER TABLE t1 MODIFY a DATETIME;
13111311
SELECT * FROM t1;
13121312
a
1313-
2000-10-00 00:00:00
1313+
1000-00-00 00:00:00
13141314
DROP TABLE t1;
13151315
CREATE TABLE t1 (a DATETIME);
13161316
INSERT INTO t1 VALUES (1000.0);
13171317
SELECT * FROM t1;
13181318
a
1319-
2000-10-00 00:00:00
1319+
1000-00-00 00:00:00
13201320
DROP TABLE IF EXISTS t1;
13211321
CREATE TABLE t1 (a DATETIME);
13221322
CREATE TABLE t2 (a DECIMAL(4,0));
13231323
INSERT INTO t2 VALUES (1000);
13241324
INSERT INTO t1 SELECT * FROM t2;
13251325
SELECT * FROM t1;
13261326
a
1327-
2000-10-00 00:00:00
1327+
1000-00-00 00:00:00
13281328
DROP TABLE t1,t2;
13291329
CREATE TABLE t1 (a DECIMAL(4,0));
13301330
INSERT INTO t1 VALUES (1000);
13311331
ALTER TABLE t1 MODIFY a DATETIME;
13321332
SELECT * FROM t1;
13331333
a
1334-
2000-10-00 00:00:00
1334+
1000-00-00 00:00:00
13351335
DROP TABLE t1;
13361336
#
13371337
# MDEV-21619 Server crash or assertion failures in my_datetime_to_str
@@ -1342,8 +1342,7 @@ EXPLAIN EXTENDED SELECT * FROM t1 WHERE 1995.0000000 BETWEEN f AND '2012-12-12';
13421342
id select_type table type possible_keys key key_len ref rows filtered Extra
13431343
1 SIMPLE t1 range f f 6 NULL 1 100.00 Using where; Using index
13441344
Warnings:
1345-
Warning 1292 Truncated incorrect datetime value: '1995.0000000'
1346-
Note 1003 select `test`.`t1`.`f` AS `f` from `test`.`t1` where '0000-00-00 00:00:00.000000' between `test`.`t1`.`f` and <cache>('2012-12-12')
1345+
Note 1003 select `test`.`t1`.`f` AS `f` from `test`.`t1` where '1995-00-00 00:00:00.000000' between `test`.`t1`.`f` and <cache>('2012-12-12')
13471346
DROP TABLE t1;
13481347
#
13491348
# End of 10.2 tests

0 commit comments

Comments
 (0)