跳转至

兼容模式

兼容模式说明

简介

  • SeaboxMPP数据库提供一系列以compat_开头的单项参数,它们可控制是否兼容 oracle 或 mysql 的具体功能。
  • 同时提供总兼容参数sql_compatibility,它可设置数据库的兼容模式为Postgres,Oracle 或 MySQL。

单项参数

compat_cast_error_return_zero 将字符串转换为整型时,输入不合法返回0
compat_convert_type_implicit 支持char,varchar,text类型隐式转换为numeric
compat_date_and_timestamp 兼容Oracle date类型和to_date、to_timestamp函数
compat_emptystr_as_null 支持将空字符串视作null值
compat_remove_sort_without_group 支持查询语句中,无groupby的聚集函数情况下的orderby子句消除
compat_substr_position_nonpositive 兼容 Oracle substr函数
compat_sys_guid_format 兼容 Oracle sys_guid函数

compat_cast_error_return_zero

说明

将字符串转换为整型时,对输入不合法的处理。默认值为false,报错退出。当值为true时,返回0,兼容MySQL。

select cast('1?' as int);
取值范围
Boolean
默认值
false
参数设置类型
Coordinator,session,reload

compat_convert_type_implicit

说明

是否支持char,varchar,text类型隐式转换为numeric。默认值为false,不支持。当值为true时,支持,兼容Oracle。

create table cast_test1(a int, b char(100), c varchar(100), d text);
insert into cast_test1 values (1,'1','1','1'),(2,'2','2','2');
select a from cast_test1 where b=a;
select a from cast_test1 where c=a;
select a from cast_test1 where d=a;
取值范围
Boolean
默认值
false
参数设置类型
Coordinator,session,reload

compat_date_and_timestamp

说明

控制 to_date、to_timestamp 函数的返回值类型。默认值为false时,to_date 函数返回date类型,to_timestamp 函数返回timestamptz类型。当值为true时,二者均返回timestamp类型,date类型为timestamp(0)类型,兼容Oracle。

select to_date('2015-01-01 15:32:14.12345','yyyy-mm-dd hh24::mi::ss.ff');
select to_timestamp('2015-01-01 15:32:14.123456 PST','yyyy-mm-dd hh24::mi::ss.ff');
取值范围
Boolean
默认值
false
参数设置类型
Coordinator,session,reload

compat_emptystr_as_null

说明

是否支持将空字符串视作null值。默认值为false,不支持。当值为true时,具体表现如下:空串常量被视作null值,包括插入表中的数据或者函数参数;返回类型为字符串的函数返回null值代替空串;||操作符全连接null值时返回null,其余情况忽略null值。

-- 常量
select '' is null;
select length('');
-- 函数
select ltrim('xy', 'xy');
select substr('abcd', 8, 4);
-- || 操作符
select 'a' || null || 'b';
-- 字段
create table test_tb(id int, name varchar);
insert into test_tb values(1, '');
insert into test_tb values(2, null);
select * from test_tb where name is null;
select * from test_tb where name is not null;
select * from test_tb where name = '';
取值范围
Boolean
默认值
false
参数设置类型
read only

注:该参数默认关闭,当创建数据库时显式指定 compatibility 'emptystr_as_null'(参见CREATE DATABASE说明)或建库时sql_compatibility = oracle(参见下方sql_compatibility说明)时,该参数在新创建的数据库中被打开。

compat_remove_sort_without_group

说明

是否支持查询语句中,无groupby的聚集函数情况下的orderby子句消除。默认值为false,不支持。当值为true时,支持,兼容Oracle。

set compat_remove_sort_without_group = 0;
select sum(a) from t1 order by a;  -- return error
set compat_remove_sort_without_group = 1;
select sum(a) from t1 order by a;  -- return ok
取值范围
Boolean
默认值
false
参数设置类型
Coordinator,session,reload

compat_substr_position_nonpositive

说明

substr函数的位置参数符号的正负影响输出结果。默认值为false,当位置参数为0时从0开始查找。为负时从1开始查找,长度等于长度参数加位置参数减去1。当值为true时,位置参数为0时等同于1。为负时起始位置从后往前查找,当其绝对值大于字符串长度时,返回空串,兼容Oracle。

select substr('ABCDEFG', -5, 2);
取值范围
Boolean
默认值
false
参数设置类型
Coordinator,session,reload

compat_sys_guid_format

说明

sys_guid函数返回格式。默认值为false,返回全小写字符,且以-隔开。当值为true时,返回32位全大写字符,兼容Oracle。

select sys_guid();
取值范围
Boolean
默认值
false
参数设置类型
Coordinator,session,reload

总参数

sql_compatibility

sql_compatibility

说明

设置数据库sql的兼容模式,可选值为postgres、oracle、mysql 和 none,默认值为none。

sql_compatibility被设置为oracle时,以下session级参数及其值被设置如下:

compat_convert_type_implicit true
compat_date_and_timestamp true
compat_remove_sort_without_group true
compat_substr_position_nonpositive true
compat_sys_guid_format true

sql_compatibility被设置为mysql时,以下参数及其值被设置如下:

compat_cast_error_return_zero true

sql_compatibility被设置为postgres时,以下参数及其值被设置如下:

compat_cast_error_return_zero false
compat_date_and_timestamp false
compat_convert_type_implicit false
compat_remove_sort_without_group false
compat_sys_guid_format false

总参数sql_compatibility仅仅作为一个开关来控制所有单项参数,所有逻辑判断都是靠单项参数来完成的。同时,在通过sql_compatibility设置各单项参数的值之后,仍可以通过设置单项参数来覆盖其值,反之亦然。见下例:

seaboxsql=# show compat_convert_type_implicit;
compat_convert_type_implicit
-----------------------------
off
(1 row)

seaboxsql=# set sql_compatibility = oracle;
SET
seaboxsql=# show compat_convert_type_implicit;
compat_convert_type_implicit
-----------------------------
on
(1 row)

seaboxsql=# set compat_convert_type_implicit = off;
SET
seaboxsql=# show compat_convert_type_implicit;
compat_convert_type_implicit
-----------------------------
off
(1 row)

seaboxsql=# set compat_convert_type_implicit = on;
SET
seaboxsql=# show compat_convert_type_implicit;
compat_convert_type_implicit
-----------------------------
on
(1 row)

seaboxsql=# set sql_compatibility = postgres;
SET
seaboxsql=# show compat_convert_type_implicit;
compat_convert_type_implicit
-----------------------------
off
(1 row)

compat_emptystr_as_null参数同以上compat_开头的guc参数一样,也受总兼容参数sql_compatibility控制。但compat_emptystr_as_null为库级参数,它的值在创建数据库时被确定,此后无法被修改。当建库时显式指定compatibility 'emptystr_as_null'(参见CREATE DATABASE说明)或建库时sql_compatibility = oracle时,该参数在新创建的数据库中被打开。

```sql
seaboxsql=# set sql_compatibility = postgres;
SET
seaboxsql=# create database pg;
CREATE DATABASE

seaboxsql=# set sql_compatibility = oracle;
SET
seaboxsql=# create database ora;
CREATE DATABASE

seaboxsql=# \c pg
You are now connected to database "pg" as user "sy".
pg=# show compat_emptystr_as_null;
compat_emptystr_as_null
-------------------------
off
(1 row)

pg=# \c ora
You are now connected to database "ora" as user "sy".
ora=# show compat_emptystr_as_null;
compat_emptystr_as_null
-------------------------
on
(1 row)
```
取值范围
字符串
默认值
none
参数设置类型
Coordinator,session,reload