Oracle SQL 适配Clickhouse SQL

116 阅读1分钟

前言

Oracle sql和clickhouse sql主要的区别在于数据类型、关键字和函数

1.数据类型

常见的数据类型主要可以分为字符型、数字型、时间型

1.Oracle

oracle相比于常见类型,还有大对象数据类型和RAW和ROWID数据类型

字符型:
	VARCHAR2、NVARCHAR2、CHAR
数字型:
	NUMBER、FLOAT、INTERGER
时间型:
	DATETIMESTAMPTIMESTAMP WITH TIMEZONEINTERVAL YEAR TO MONTHINTERVAL DAY TO SECOND
大对象型:
	CLOBNCLOB
Row和RowId

2.Clickhouse

oracle相比于常见类型,还有布尔类型、枚举类型、数组类型、元组类型和集合类型等等

字符型:
	String
数字型:
	UInt8, UInt16, UInt32, UInt64, UInt256, Int8, Int16, Int32, Int64, Int128, Int256
时间型:
	Date、DateTime、DateTime64
更多请参照clickhouse官方文档
https://clickhouse.tech/docs/en/sql-reference/data-types/

3.适配举例

原oracle sql

create table test.user(
	id INTEGER,
	username VARCHAR2(200),
	gender VARCHAR(10),
	birthday date)

适配clickhouse sql后

create table test.user(
	id UInt32,
	username String,
	gender String,
	birthday date
)ENGINE = Log

适配说明: 1.字段类型修改: INTEGER > UInt32 VARCHAR2 > String 2.clickhouse需要指定表引擎

2.关键字和函数

\oracleclickhouse
字符串union / union allunion all
条件判断case whenif / multIIf
字符串正则匹配regexp_likematch
字符串截取substr("username" ,0,6)索引从0开始substr("username" ,1,6)索引从1开始
字符定位返回下标索引instrposition
转成整型to_numberto_Int32
转成字符型to_chartoString
转成时间型to_datetoDate
正则匹配替换regexp_replacerepacleRegexpAll