在信创适配中,有从MySQL迁移过来的程序,其中程序涉及日期加减数字的场景和使用datediff函数的场景。因此在LightDB-X 23.4版本中,MySQL兼容模式下对日期加减数字做了支持,与MySQL行为一致。
MySQL中日期加减数值型时返回的结果也是数值型。如下
mysql> select curdate() + 1;
+---------------+
| curdate() + 1 |
+---------------+
| 20231219 |
+---------------+
1 row in set (0.00 sec)
mysql> select curdate() + 1.2;
+-----------------+
| curdate() + 1.2 |
+-----------------+
| 20231219.2 |
+-----------------+
1 row in set (0.00 sec)
在LightDB MySQL兼容模式下,当date类型与整数相加减时返回bigint类型,当date类型与浮点型相加减时返回numeric。如下:
create database test_mysql with lightdb_syntax_compatible_type mysql;
\c test_mysql
lightdb@test_mysql=# select pg_typeof(curdate() + 1), curdate() + 1;
pg_typeof | ?column?
-----------+----------
bigint | 20231219
lightdb@test_mysql=# select pg_typeof(curdate() + 1.2), curdate() + 1.2;
pg_typeof | ?column?
-----------+------------
numeric | 20231219.2
(1 row)
当datediff的参数存在日期加减时,根据上一小节可以知道,入参可以是数值型,因此datediff需要对该场景进行支持,如下:
lightdb@test_mysql=# select datediff(curdate(), curdate() - 1);
datediff
----------
1
(1 row)
lightdb@test_mysql=# select datediff(curdate()+1, curdate());
datediff
----------
1
(1 row)
lightdb@test_mysql=# select datediff(curdate()+1, curdate()-1);
datediff
----------
2
(1 row)
lightdb@test_mysql=# select datediff(curdate()+1, curdate()-1.9);
datediff
----------
3
(1 row)
lightdb@test_mysql=# select datediff(curdate()+1.9, curdate()-1.9);
datediff
----------
3
(1 row)