lightdb empty_clob/empty_blob 函数兼容性升级

发布时间 2023-12-25 17:41:37作者: winter-loo

背景

在 Oracle 中,长度为 0 的字符串被视为 NULL. 下文中长度为 0 的字符串被称为 EMPTY_STRING. 而 PostgreSQL 能够区别对待 EMPTY_STRING 和 NULL. 为了兼容 Oracle 的行为,在 LightDB 23.4 版本前,已经基本将 EMPTY_STRING 当成了 NULL. 如, 以下 sql,

select ''::text is null;

在 Postgres 中,返回的是 false, 在 LightDB oralce 模式中返回的是 true. 但是 CLOB 数据类型有个特殊的函数 empty_clob(),该函数返回值的长度为 0,但不是 NULL. 在 LightDB 23.4 版本前,empty_clob() 函数就是使用 ''::text 实现的,因此导致了 empty_clob() 函数与 Oracle 不兼容。

在 LightDB 23.4 版本中,我们重新实现了 empty_clob() 函数,确保了该函数行为和 Oracle 一致。

使用案例

以下 sql 需要在 LightDB 的 oracle 模式数据库下运行,

  1. 基本行为测试
--= false
select empty_clob() is null;

--= 0
select length(empty_clob());

select dbms_lob.getlength(empty_clob());
  1. 作为列的默认值
create table t(a int, b clob default empty_clob());
insert into t(a) values (1);
insert into t values (2, 'hello');
--= 2
select count(*) from t where b is not null;
--= 1
select count(*) from t where length(b) > 0;
  1. 使用在其他字符串函数中
--= 1
select 1 from dual where trim(empty_clob() from empty_clob()) is not null;
--= 1
select 1 from dual where trim('x' from 'x') is null;
--= 1
select 1 from dual where trim('x' from to_clob('x')) is not null;
--= not null
select nvl2(empty_clob(), 'not null', 'null') from dual;