[MDEV-19829] CURRENT_TIMESTAMP produces zeros via trigger Created: 2019-06-21  Updated: 2020-08-25  Resolved: 2019-08-05

Status: Closed
Project: MariaDB Server
Component/s: Triggers
Affects Version/s: 5.5, 10.0, 10.1
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Muhammad Irfan Assignee: Sergei Golubchik
Resolution: Won't Fix Votes: 0
Labels: None


 Description   

This already reported upstream https://bugs.mysql.com/bug.php?id=84077
I can repeat this issue on MariaDB Server 10.1.19 however this doesn't exists in 10.2 and 10.3

+-------------------------+------------------+
| Variable_name           | Value            |
+-------------------------+------------------+
| innodb_version          | 5.6.32-79.0      |
| protocol_version        | 10               |
| slave_type_conversions  |                  |
| version                 | 10.1.19-MariaDB  |
| version_comment         | MariaDB Server   |
| version_compile_machine | x86_64           |
| version_compile_os      | Linux            |
| version_malloc_library  | bundled jemalloc |
| version_ssl_library     | YaSSL 2.4.2      |
| wsrep_patch_version     | wsrep_25.16      |
+-------------------------+------------------+
10 rows in set (0.00 sec)
 
CREATE TABLE tbl(
a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
b INT NOT NULL DEFAULT '123');
 
CREATE TABLE tbl2(
c TIMESTAMP NOT NULL);
 
DELIMITER $
CREATE TRIGGER tbl_before_insert BEFORE INSERT ON tbl FOR EACH ROW
BEGIN
SET @a = NEW.a;
SET @b = NEW.b;
INSERT INTO tbl2(c) VALUES(NEW.a);
END;$
 
DELIMITER ;
INSERT INTO tbl VALUES ();
 
mysql [localhost:10119] {msandbox} (test) > INSERT INTO tbl1 VALUES ();
Query OK, 1 row affected (0.01 sec)
 
mysql [localhost:10119] {msandbox} (test) > SELECT * FROM tbl1;
+---------------------+-----+
| a                   | b   |
+---------------------+-----+
| 2019-06-21 15:11:05 | 123 |
+---------------------+-----+
1 row in set (0.00 sec)
 
mysql [localhost:10119] {msandbox} (test) > SELECT * FROM tbl2;
+---------------------+
| c                   |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
 
mysql [localhost:10119] {msandbox} (test) > SELECT @a, @b;
+---------------------+------+
| @a                  | @b   |
+---------------------+------+
| 0000-00-00 00:00:00 |  123 |
+---------------------+------+
1 row in set (0.00 sec)

column 'c' contains non-zero date time value.



 Comments   
Comment by Alice Sherepa [ 2019-06-24 ]

Reproducible on current 5.5-10.1

create table t1(a timestamp not null default current_timestamp);
create table t2(c timestamp not null);
 
create trigger tr1 before insert on t1 for each row insert into t2(c) values(new.a);
insert into t1 values ();
select * from t1;
select * from t2;
drop table t1, t2;

MariaDB [test]> select * from t1;
+---------------------+
| a                   |
+---------------------+
| 2019-06-24 10:19:25 |
+---------------------+
1 row in set (0.00 sec)
 
MariaDB [test]> select * from t2;
+---------------------+
| c                   |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.01 sec)

Comment by Alexander Barkov [ 2019-08-05 ]

The problem happens because in pre-10.2 versions, Item_trigger_field::save_in_field() is called before Field_timestamp::set_time(), so Item_trigger_field stores the `global` default TIMESTAMP value of '0000-00-00 00:00:00' into the table t2.

In 10.2+ this is fixed by these lines in fill_record():

  if (!update && table_arg->default_field &&
      table_arg->update_default_fields(0, ignore_errors))
    goto err;

These lines were added by this change set:

commit db7edfed17efe6bc3684b0fbacc0b0249e4f0fa2
Author: Michael Widenius <monty@mariadb.org>  2016-06-29 11:14:22
MDEV-7563 Support CHECK constraint as in (or close to) SQL Standard
MDEV-10134 Add full support for DEFAULT

monty, please check if it's feasible to backport a part of this commit into earlier versions.

Thanks.

Comment by Sergei Golubchik [ 2019-08-05 ]

It was part of a big feature in 10.2, I don't think it could or should be backported into an old GA branch.

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