LightDB Oracle模式支持systimestamp(n)

背景

目前,lightdb 在兼容 oracle,目标是 oracle 用户不用修改 sql 代码无缝切换到 lightdb 数据库。oracle中支持systimestamp(n)函数,n的范围为0-9,因此在23.4版本中,新增对systimestamp(n)函数的全面支持。

注:不带参数的systimestamp在之前版本已经实现。

lightdb@oracle=# select systimestamp from dual;
         systimestamp          
-------------------------------
 2023-11-23 09:34:08.352753+08
(1 row)

使用案例及相关说明

函数说明

systimestamp返回数据库所在系统的系统日期,包括秒的小数部分(oracle支持9位小数,即精确到纳秒级,而lightdb目前支持6位小数,即精确到微秒级)和时区。返回类型为TIMESTAMP WITH TIME ZONE。

参数的含义及使用规范

在systimestamp(n)中,n用来指定系统日期中秒数据小数部分的保留位数的最大值,例如n=3,表示返回的系统日期中,秒数据四舍五入最多保留3位小数。请注意标红字体,这是因为PG会自动省略秒小数位末尾的0,如下图所示:

lightdb@oracle=# select systimestamp(3),systimestamp(6);
        systimestamp        |         systimestamp          
----------------------------+-------------------------------
 2023-11-23 10:04:56.395+08 | 2023-11-23 10:04:56.394574+08
(1 row)

lightdb@oracle=# select systimestamp(3),systimestamp(6);
       systimestamp        |         systimestamp          
---------------------------+-------------------------------
 2023-11-23 10:26:09.42+08 | 2023-11-23 10:26:09.420394+08
(1 row)

参数n必须为0-9的整数,不支持浮点类型,不支持表达式,不支持表列名。

lightdb@oracle=# select systimestamp(1.0);
ERROR:  syntax error at or near "1.0"
LINE 1: select systimestamp(1.0);
                            ^
lightdb@oracle=# create table t(a int,b float);
CREATE TABLE
lightdb@oracle=# insert into t values(1,1.0);
INSERT 0 1
lightdb@oracle=# select systimestamp(a) from t;
ERROR:  syntax error at or near "a"
LINE 1: select systimestamp(a) from t;
                            ^
lightdb@oracle=# select systimestamp(b) from t;
ERROR:  syntax error at or near "b"
LINE 1: select systimestamp(b) from t;
     
lightdb@oracle=# select systimestamp(1+2);
ERROR:  syntax error at or near "+"
LINE 1: select systimestamp(1+2);

lightdb@oracle=# select systimestamp(-1);
ERROR:  syntax error at or near "-"
LINE 1: select systimestamp(-1);
                            ^
lightdb@oracle=# select systimestamp(10);
ERROR:  datetime/interval precision is out of range
LINE 1: select systimestamp(10);
                 

当n>6时,函数返回的秒小数部分仍为最多6位小数,这是由于原生PG只支持6位小数所致。 

lightdb@oracle=# select systimestamp(6),systimestamp(7),systimestamp(8),systimestamp(9);
         systimestamp          |         systimestamp          |         systimestamp          |         systimestamp          
-------------------------------+-------------------------------+-------------------------------+-------------------------------
 2023-11-23 10:17:37.857939+08 | 2023-11-23 10:17:37.857939+08 | 2023-11-23 10:17:37.857939+08 | 2023-11-23 10:17:37.857939+08
(1 row)

lightdb@oracle=# select systimestamp(3),systimestamp(7),systimestamp(8),systimestamp(9);
       systimestamp        |         systimestamp          |         systimestamp          |         systimestamp          
---------------------------+-------------------------------+-------------------------------+-------------------------------
 2023-11-23 10:17:50.61+08 | 2023-11-23 10:17:50.609771+08 | 2023-11-23 10:17:50.609771+08 | 2023-11-23 10:17:50.609771+08
(1 row)

支持sql语句级获取最新时间戳

systimestamp和systimestamp(n)都是支持sql语句级别获取当前时间戳的,即在同一个事务中执行多次,获取的时间戳是不同的,每次获取的都是最新的时间戳。

lightdb@oracle=# begin;
BEGIN
lightdb@oracle=*# select systimestamp,systimestamp(3),systimestamp(9);
         systimestamp         |       systimestamp        |         systimestamp         
------------------------------+---------------------------+------------------------------
 2023-11-23 09:46:12.90956+08 | 2023-11-23 09:46:12.91+08 | 2023-11-23 09:46:12.90956+08
(1 row)

lightdb@oracle=*# select systimestamp,systimestamp(3),systimestamp(9);
         systimestamp          |       systimestamp        |         systimestamp          
-------------------------------+---------------------------+-------------------------------
 2023-11-23 09:46:13.860494+08 | 2023-11-23 09:46:13.86+08 | 2023-11-23 09:46:13.860494+08
(1 row)

lightdb@oracle=*# select systimestamp,systimestamp(3),systimestamp(9);
         systimestamp          |        systimestamp        |         systimestamp          
-------------------------------+----------------------------+-------------------------------
 2023-11-23 09:46:15.242484+08 | 2023-11-23 09:46:15.242+08 | 2023-11-23 09:46:15.242484+08
(1 row)

lightdb@oracle=*# end;
COMMIT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值