0
点赞
收藏
分享

微信扫一扫

postgresql与lightdb中的null行为及兼容性

少_游 2022-09-24 阅读 97

null首先跟char、varchar、bpchar(blank padded character)、text有关系。在oracle中,则与char、varchar2、clob有关。

其次,涉及的范围比较广,''、null、=、!=、is null、is not null、替换、计算长度、类型强转如cast(null as date),以及它们之间的组合。

zjh@postgres=# select null = '';
?column?
----------
f
(1 row)

zjh@postgres=# select null = null;
?column?
----------
t
(1 row)

zjh@postgres=# select null is null;
?column?
----------
t
(1 row)

zjh@postgres=# select '' is null;
?column?
----------
f
(1 row)

zjh@postgres=# select '' = '';
?column?
----------
t
(1 row)

zjh@postgres=# select '' = ' ';
?column?
----------
f
(1 row)

zjh@postgres=# select '' = ' '::bpchar;
?column?
----------
t
(1 row)

zjh@postgres=# select '' = ' '::char;
?column?
----------
t
(1 row)

zjh@postgres=# select '' = ' '::varchar;
?column?
----------
f
(1

zjh@postgres=# select '' = ' '::text;
?column?
----------
f
(1 row)

zjh@postgres=# select '' = trim(' ')::text;
?column?
----------
t
(1 row)

zjh@postgres=# select '' = trim(' '::text);
?column?
----------
t
(1

zjh@postgres=# select 1 from t where '' = null;
?column?
----------
(0 rows)

zjh@postgres=# select sum(id) from t where '' = null;
sum
-----

(1 row)

zjh@postgres=# select max(id) from t where '' = null;
max
-----

(1

zjh@postgres=# select 'abc' = 'abc '::text;
?column?
----------
f
(1 row)

zjh@postgres=# select 'abc' = 'abc '::bpchar;
?column?
----------
t
(1 row)

zjh@postgres=# select 'abc' = ' abc'::bpchar;
?column?
----------
f
(1 row)

zjh@postgres=# select 'abc' = ' abc'::varchar;
?column?
----------
f
(1 row)

zjh@postgres=# select 'abc' = ' abc'::text;
?column?
----------
f
(1

zjh@postgres=# select trim('abc ') = 'abc '::bpchar;
?column?
----------
t
(1

zjh@postgres=# select replace('12345','4','');
replace
---------
1235
(1 row)

zjh@postgres=# select replace('12345','4',null);
replace
---------

(1 row)

zjh@postgres=#
zjh@postgres=# select replace('12345',null,null);
replace
---------

(1 row)

zjh@postgres=# select replace('12345','','');
replace
---------
12345
(1

zjh@postgres=# select length(null);
length
--------

(1 row)

zjh@postgres=# select length('');
length
--------
0
(1

zjh@postgres=# select 1 from t where cast('' AS DATE) is null;
ERROR: invalid input syntax for type date: ""
LINE 1: select 1 from t where cast('' AS DATE) is null;
^
zjh@postgres=# select 1 from t where cast(null AS DATE) is null;
?column?
----------
1
(1 row)

zjh@postgres=# select 1 from t where cast('' AS bpchar(10)) is null;
?column?
----------
(0 rows)

zjh@postgres=# select 1,cast('' AS bpchar(10)) from t;
?column? | bpchar
----------+------------
1 |
(1 row)

zjh@postgres=# select 1,length(cast('' AS bpchar(10))) from t;
?column? | length
----------+--------
1 | 0
(1 row)

zjh@postgres=# select 1,length(cast(null AS bpchar(10))) from t;
?column? | length
----------+--------
1 |
(1

-- 虽然pg中null和''都被诊断为unkown,但是内部处理貌似仍然不同
zjh@postgres=# select pg_typeof('');
pg_typeof
-----------
unknown
(1 row)

zjh@postgres=# select pg_typeof(null);
pg_typeof
-----------
unknown
(1

 

======================下面是不等于=====================

 

​​https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL​​

​​https://linuxhint.com/bpchar-data-type-postgres/​​

​​https://www.postgresql.org/docs/current/datatype-character.html​​

​​https://www.geeksforgeeks.org/postgresql-difference-between-char-varchar-and-text/​​

oracle中null的行为

SQL> select 1 from dual where '' = '';

no rows selected

SQL> select 1 from dual where null = null;

no rows selected

SQL> select 1 from dual where null is null;

1
----------
1

SQL> select 1 from dual where '' is null;

1
----------
1

 

SQL> select sum(id) from t_null where '' = null;

SUM(ID)
----------


SQL> select * from t_null where '' = null;

no rows selected

SQL> select max(id) from t_null where '' = null;

MAX(ID)
----------


SQL> select count(id) from t_null where '' = null;

COUNT(ID)
----------
0

SQL> select replace('12345','4','') from dual;

REPL
----
1235

SQL> select replace('12345','4',null) from dual;

REPL
----
1235

SQL> select replace('12345',null,null) from dual;

REPLA
-----
12345

SQL> select replace('12345','','') from dual;

REPLA
-----
12345

 

SQL> select length('') from dual;

LENGTH('')
----------


SQL> select length(null) from dual;

LENGTH(NULL)
------------


SQL>

 

SQL> select 1 from t where cast('' AS DATE) is null;

1
----------
1

SQL> select 1 from t where cast(null AS DATE) is null;

1
----------
1

SQL> select 1 from t where cast('' AS char(10)) is null;

1
----------
1
SQL> select 1,cast('' AS char(10)) from t;

1 CAST(''ASC
---------- ----------
1

SQL> select 1,length(cast('' AS char(10))) from t;

1 LENGTH(CAST(''ASCHAR(10)))
---------- --------------------------
1

SQL> select 1,length(cast(null AS char(10))) from t;

1 LENGTH(CAST(NULLASCHAR(10)))
---------- ----------------------------
1

 

==============================下面不等于=================== 

​​https://community.oracle.com/tech/developers/discussion/1053012/difference-between-null-and​​

​​https://cloud.tencent.com/developer/article/1052571​​


总结

  在oracle里面,除了存储层面(包括PL/SQL)的char类型外,可以认为''几乎就是当做null来处理的,除了!=/=/in/not in操作外(因为本质上in、not in也是调用=/!=操作符实现的)。

  但是在原生pg中,''和null的实现是不一样的,虽然都是unknown,但是内部仍然不是相同的。

​​LightDB Enterprise Postgres--金融级关系型数据库,更快、更稳、更懂金融!​​



举报

相关推荐

0 条评论