[MDEV-29366] Inserting now() into auto_increment field caused value to jump to max Created: 2022-08-23  Updated: 2022-08-24  Resolved: 2022-08-24

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Insert
Affects Version/s: 10.5.8
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Roger Assignee: Sergei Golubchik
Resolution: Not a Bug Votes: 0
Labels: auto_increment, now
Environment:

openSUSE Leap 15.3



 Description   

When inserting into a table with an AUTO_INCREMENT column and inserting the function name now() it fails and causes the auto_increment value for the table to jump to the maximum for the data type. No further records can be inserted until the rouge rows are deleted and the auto_increment is reset. (Not a common mistake to make in scripting, but can happen if inserted fields are in the incorrect order.)

CREATE TABLE `T_test` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
 
insert into T_test values (now());
1 row(s) affected, 1 warning(s): 1264 Out of range value for column 'ID' at row 1
2147483647

Does the same for LOCALTIMESTAMP() & LOCALTIME()

select AUTO_INCREMENT from information_schema.TABLES where TABLE_NAME='T_test';
2147483647

set @nowtime=now(); insert into T_test values (@nowtime);  

does not give the error.



 Comments   
Comment by Sergei Golubchik [ 2022-08-24 ]

this is all very much expected. now() in a numeric context becomes something like 20220824192234 (this is the same value as 2022-08-24 19:22:34 without separators) and it overflows INT.

If you store it in a variable it is interpreted as a string '2022-08-24 19:22:34' and in a numeric context it becomes 2022, that does not overflow INT.

Generated at Thu Feb 08 10:08:00 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.