Skip to content

Commit

Permalink
MDEV-15751 CURRENT_TIMESTAMP should return a TIMESTAMP [WITH TIME ZONE?]
Browse files Browse the repository at this point in the history
Changing the return type of the following functions:
  - CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(), NOW()
  - SYSDATE()
  - FROM_UNIXTIME()
from DATETIME to TIMESTAMP.

Note, the old function NOW() returning DATETIME is still available
as LOCALTIMESTAMP or LOCALTIMESTAMP(), e.g.:

  SELECT
    LOCALTIMESTAMP,     -- DATETIME
    CURRENT_TIMESTAMP;  -- TIMESTAMP

The change in the functions return data type fixes some problems
that occurred near a DST change:

- Problem #1

INSERT INTO t1 (timestamp_field) VALUES (CURRENT_TIMESTAMP);
INSERT INTO t1 (timestamp_field) VALUES (COALESCE(CURRENT_TIMESTAMP));

could result into two different values inserted.

- Problem #2

INSERT INTO t1 (timestamp_field) VALUES (FROM_UNIXTIME(1288477526));
INSERT INTO t1 (timestamp_field) VALUES (FROM_UNIXTIME(1288477526+3600));

could result into two equal TIMESTAMP values near a DST change.

Additional changes:

- FROM_UNIXTIME(0) now returns SQL NULL instead of '1970-01-01 00:00:00'
  (assuming time_zone='+00:00')

- UNIX_TIMESTAMP('1970-01-01 00:00:00') now returns SQL NULL instead of 0
  (assuming time_zone='+00:00'

These additional changes are needed for consistency with TIMESTAMP fields,
which cannot store '1970-01-01 00:00:00 +00:00'.
  • Loading branch information
abarkov authored and vuvova committed Oct 18, 2024
1 parent a812dba commit 554eb81
Show file tree
Hide file tree
Showing 31 changed files with 770 additions and 185 deletions.
2 changes: 1 addition & 1 deletion mysql-test/main/create.result
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ drop table t2;
create table t2 select now() as a , curtime() as b, curdate() as c , 1+1 as d , 1.0 + 1 as e , 33333333333333333 + 3 as f;
describe t2;
Field Type Null Key Default Extra
a datetime NO NULL
a timestamp NO NULL
b time NO NULL
c date NO NULL
d int(3) NO NULL
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/main/func_group.result
Original file line number Diff line number Diff line change
Expand Up @@ -781,12 +781,12 @@ insert into t1 values (now());
create table t2 select f2 from (select max(now()) f2 from t1) a;
show columns from t2;
Field Type Null Key Default Extra
f2 datetime YES NULL
f2 timestamp YES NULL
drop table t2;
create table t2 select f2 from (select now() f2 from t1) a;
show columns from t2;
Field Type Null Key Default Extra
f2 datetime NO NULL
f2 timestamp NO NULL
drop table t2, t1;
CREATE TABLE t1(
id int PRIMARY KEY,
Expand Down
125 changes: 123 additions & 2 deletions mysql-test/main/func_time.result
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,9 @@ from_unixtime(2147483647)
2038-01-19 06:14:07
select from_unixtime(0);
from_unixtime(0)
1970-01-01 03:00:00
NULL
Warnings:
Warning 1292 Truncated incorrect unixtime value: '0.0'
select unix_timestamp(from_unixtime(2147483647));
unix_timestamp(from_unixtime(2147483647))
2147483647
Expand Down Expand Up @@ -6323,10 +6325,12 @@ TIME('- 01:00:00') TIME('- 1 01:00:00')
SET time_zone='+00:00';
SELECT NULLIF(FROM_UNIXTIME('foo'), '2012-12-12 21:10:14');
NULLIF(FROM_UNIXTIME('foo'), '2012-12-12 21:10:14')
1970-01-01 00:00:00
NULL
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'foo'
Warning 1292 Truncated incorrect unixtime value: '0.0'
Warning 1292 Truncated incorrect DECIMAL value: 'foo'
Warning 1292 Truncated incorrect unixtime value: '0.0'
SET time_zone=DEFAULT;
#
# MDEV-18402 Assertion `sec.sec() <= 59' failed in Item_func_maketime::get_date
Expand Down Expand Up @@ -6437,3 +6441,120 @@ SET timestamp=DEFAULT;
#
# End of 11.6 tests
#
#
# Start of 11.7 tests
#
#
# MDEV-15751 CURRENT_TIMESTAMP should return a TIMESTAMP [WITH TIME ZONE?]
#
CREATE TABLE t1 (localtimestamp TIMESTAMP);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'localtimestamp TIMESTAMP)' at line 1
CREATE TABLE t1 (current_timestamp TIMESTAMP);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'current_timestamp TIMESTAMP)' at line 1
CREATE TABLE t1 AS
SELECT
localtimestamp,
localtimestamp(),
localtimestamp(0),
localtimestamp(1),
localtimestamp(2),
localtimestamp(3),
localtimestamp(4),
localtimestamp(5),
localtimestamp(6);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`localtimestamp` datetime NOT NULL,
`localtimestamp()` datetime NOT NULL,
`localtimestamp(0)` datetime NOT NULL,
`localtimestamp(1)` datetime(1) NOT NULL,
`localtimestamp(2)` datetime(2) NOT NULL,
`localtimestamp(3)` datetime(3) NOT NULL,
`localtimestamp(4)` datetime(4) NOT NULL,
`localtimestamp(5)` datetime(5) NOT NULL,
`localtimestamp(6)` datetime(6) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1 AS
SELECT
current_timestamp(),
current_timestamp(0),
current_timestamp(1),
current_timestamp(2),
current_timestamp(3),
current_timestamp(4),
current_timestamp(5),
current_timestamp(6);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`current_timestamp()` timestamp NOT NULL,
`current_timestamp(0)` timestamp NOT NULL,
`current_timestamp(1)` timestamp(1) NOT NULL,
`current_timestamp(2)` timestamp(2) NOT NULL,
`current_timestamp(3)` timestamp(3) NOT NULL,
`current_timestamp(4)` timestamp(4) NOT NULL,
`current_timestamp(5)` timestamp(5) NOT NULL,
`current_timestamp(6)` timestamp(6) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1 AS
SELECT
sysdate(),
sysdate(0),
sysdate(1),
sysdate(2),
sysdate(3),
sysdate(4),
sysdate(5),
sysdate(6);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`sysdate()` timestamp NOT NULL,
`sysdate(0)` timestamp NOT NULL,
`sysdate(1)` timestamp(1) NOT NULL,
`sysdate(2)` timestamp(2) NOT NULL,
`sysdate(3)` timestamp(3) NOT NULL,
`sysdate(4)` timestamp(4) NOT NULL,
`sysdate(5)` timestamp(5) NOT NULL,
`sysdate(6)` timestamp(6) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1 AS
SELECT
from_unixtime(1000000e0),
from_unixtime(1000000),
from_unixtime(1000000.1),
from_unixtime(1000000.12),
from_unixtime(1000000.123),
from_unixtime(1000000.1234),
from_unixtime(1000000.12345),
from_unixtime(1000000.123456),
from_unixtime(1000000.1234567),
from_unixtime(1000000.12345678),
from_unixtime(1000000.123456789),
from_unixtime(1000000.1234567891),
from_unixtime(1000000.12345678912);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`from_unixtime(1000000e0)` timestamp(6) NULL DEFAULT NULL,
`from_unixtime(1000000)` timestamp NULL DEFAULT NULL,
`from_unixtime(1000000.1)` timestamp(1) NULL DEFAULT NULL,
`from_unixtime(1000000.12)` timestamp(2) NULL DEFAULT NULL,
`from_unixtime(1000000.123)` timestamp(3) NULL DEFAULT NULL,
`from_unixtime(1000000.1234)` timestamp(4) NULL DEFAULT NULL,
`from_unixtime(1000000.12345)` timestamp(5) NULL DEFAULT NULL,
`from_unixtime(1000000.123456)` timestamp(6) NULL DEFAULT NULL,
`from_unixtime(1000000.1234567)` timestamp(6) NULL DEFAULT NULL,
`from_unixtime(1000000.12345678)` timestamp(6) NULL DEFAULT NULL,
`from_unixtime(1000000.123456789)` timestamp(6) NULL DEFAULT NULL,
`from_unixtime(1000000.1234567891)` timestamp(6) NULL DEFAULT NULL,
`from_unixtime(1000000.12345678912)` timestamp(6) NULL DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
#
# End of 11.7 tests
#
76 changes: 76 additions & 0 deletions mysql-test/main/func_time.test
Original file line number Diff line number Diff line change
Expand Up @@ -3293,3 +3293,79 @@ SET timestamp=DEFAULT;
--echo #
--echo # End of 11.6 tests
--echo #

--echo #
--echo # Start of 11.7 tests
--echo #

--echo #
--echo # MDEV-15751 CURRENT_TIMESTAMP should return a TIMESTAMP [WITH TIME ZONE?]
--echo #

--error ER_PARSE_ERROR
CREATE TABLE t1 (localtimestamp TIMESTAMP);
--error ER_PARSE_ERROR
CREATE TABLE t1 (current_timestamp TIMESTAMP);

CREATE TABLE t1 AS
SELECT
localtimestamp,
localtimestamp(),
localtimestamp(0),
localtimestamp(1),
localtimestamp(2),
localtimestamp(3),
localtimestamp(4),
localtimestamp(5),
localtimestamp(6);
SHOW CREATE TABLE t1;
DROP TABLE t1;


CREATE TABLE t1 AS
SELECT
current_timestamp(),
current_timestamp(0),
current_timestamp(1),
current_timestamp(2),
current_timestamp(3),
current_timestamp(4),
current_timestamp(5),
current_timestamp(6);
SHOW CREATE TABLE t1;
DROP TABLE t1;

CREATE TABLE t1 AS
SELECT
sysdate(),
sysdate(0),
sysdate(1),
sysdate(2),
sysdate(3),
sysdate(4),
sysdate(5),
sysdate(6);
SHOW CREATE TABLE t1;
DROP TABLE t1;

CREATE TABLE t1 AS
SELECT
from_unixtime(1000000e0),
from_unixtime(1000000),
from_unixtime(1000000.1),
from_unixtime(1000000.12),
from_unixtime(1000000.123),
from_unixtime(1000000.1234),
from_unixtime(1000000.12345),
from_unixtime(1000000.123456),
from_unixtime(1000000.1234567),
from_unixtime(1000000.12345678),
from_unixtime(1000000.123456789),
from_unixtime(1000000.1234567891),
from_unixtime(1000000.12345678912);
SHOW CREATE TABLE t1;
DROP TABLE t1;

--echo #
--echo # End of 11.7 tests
--echo #
4 changes: 2 additions & 2 deletions mysql-test/main/func_time_hires.result
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ t1 CREATE TABLE `t1` (
`sec_to_time(12345)` time DEFAULT NULL,
`sec_to_time(12345.6789)` time(4) DEFAULT NULL,
`sec_to_time(1234567e-2)` time(6) DEFAULT NULL,
`now()` datetime NOT NULL,
`now()` timestamp NOT NULL,
`curtime(0)` time NOT NULL,
`utc_timestamp(1)` datetime(1) NOT NULL,
`utc_time(2)` time(2) NOT NULL,
`current_time(3)` time(3) NOT NULL,
`current_timestamp(4)` datetime(4) NOT NULL,
`current_timestamp(4)` timestamp(4) NOT NULL,
`localtime(5)` time(5) NOT NULL,
`localtimestamp(6)` datetime(6) NOT NULL,
`time_to_sec(123456)` bigint(17) DEFAULT NULL,
Expand Down
6 changes: 3 additions & 3 deletions mysql-test/main/ps.result
Original file line number Diff line number Diff line change
Expand Up @@ -4574,21 +4574,21 @@ EXECUTE stmt USING CURRENT_TIMESTAMP;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` datetime NOT NULL
`c1` timestamp NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIMESTAMP(3);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` datetime(3) NOT NULL
`c1` timestamp(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIMESTAMP(6);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` datetime(6) NOT NULL
`c1` timestamp(6) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIME;
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/main/timezone.result
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ DROP TABLE t1;
select unix_timestamp('1970-01-01 01:00:00'),
unix_timestamp('1970-01-01 01:00:01');
unix_timestamp('1970-01-01 01:00:00') unix_timestamp('1970-01-01 01:00:01')
0 1
NULL 1
select unix_timestamp('1969-12-31 23:59:59'), unix_timestamp('1970-01-01 00:00:00'), unix_timestamp('1970-01-01 00:59:59');
unix_timestamp('1969-12-31 23:59:59') unix_timestamp('1970-01-01 00:00:00') unix_timestamp('1970-01-01 00:59:59')
NULL NULL NULL
Expand Down
Loading

0 comments on commit 554eb81

Please sign in to comment.