Teradata应用迁移到AnalyticDB for PostgreSQL指导
AnalyticDB for PostgreSQL(简称:ADB for PG)对Teradata语法有着很好的兼容,将Teradata应用迁移到ADB for PG,只需进行有限的修改。本文介绍将Teradata应用迁移到ADB for PG应该注意的事项。
1 建表语句
我们通过一个例子比较ADB for PG和Teradata的建表语句。对于如下的Teradata建表SQL语句,
CREATE MULTISET TABLE test_table,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT, DEFAULT MERGEBLOCKRATIO ( first_column DATE FORMAT 'YYYYMMDD' TITLE '第一列' NOT NULL, second_column INTEGER TITLE '第二列' NOT NULL , third_column CHAR(6) CHARACTER SET LATIN CASESPECIFIC TITLE '第三列' NOT NULL , fourth_column CHAR(20) CHARACTER SET LATIN CASESPECIFIC TITLE '第四列' NOT NULL, fifth_column CHAR(1) CHARACTER SET LATIN CASESPECIFIC TITLE '第五列' NOT NULL, sixth_column CHAR(24) CHARACTER SET LATIN CASESPECIFIC TITLE '第六列' NOT NULL, seventh_column VARCHAR(18) CHARACTER SET LATIN CASESPECIFIC TITLE '第七列' NOT NULL, eighth_column DECIMAL(18,0) TITLE '第八列' NOT NULL , nineth_column DECIMAL(18,6) TITLE '第九列' NOT NULL ) PRIMARY INDEX ( first_column ,fourth_column ) PARTITION BY RANGE_N(first_column BETWEEN DATE '1999-01-01' AND DATE '2050-12-31' EACH INTERVAL '1' DAY ); CREATE INDEX test_index (first_column, fourth_column) ON test_table;
可以修改成ADB for PG的建表语句:
CREATE TABLE test_table ( first_column DATE NOT NULL, second_column INTEGER NOT NULL , third_column CHAR(6) NOT NULL , fourth_column CHAR(20) NOT NULL, fifth_column CHAR(1) NOT NULL, sixth_column CHAR(24) NOT NULL, seventh_column VARCHAR(18) NOT NULL, eighth_column DECIMAL(18,0) NOT NULL , nineth_column DECIMAL(18,6) NOT NULL ) DISTRIBUTED BY ( first_column ,fourth_column ) PARTITION BY RANGE(first_column) (START (DATE '1999-01-01') INCLUSIVE END (DATE '2050-12-31') INCLUSIVE EVERY (INTERVAL '1 DAY' ) ); create index test_index on test_table(first_column, fourth_column);
通过以上例子,我们可以很清晰地分析ADB for PG和Teradata建表语句的异同:
1、ADB for PG和Teradata的数据类型是互相兼容的,数据类型不需要做修改;
2、ADB for PG和Teradata均支持分布列,但语法不同,Teradata是primary index,ADB for PG是distributed by;
3、ADB for PG和Teradata均支持PARTITION BY二级分区,语义相同但语法不同;
4、ADB for PG和Teradata均支持对表创建索引,但语法不同;
5、ADB for PG不支持TITLE关键字,但是支持单独对列添加注释COMMENT,语法为COMMENT ON COLUMN table_name.column_name IS 'XXX';
6、ADB for PG不能在定义char或者varchar时声明编码类型,而是在连接上数据库时,通过执行“SET client_encoding = latin1;”来申明编码类型。
2 导入导出数据格式
ADB for PG支持txt、csv格式的数据导入导出,和Teradata的区别就在于数据文件的分隔符:Teradata支持双分隔符,而ADB for PG只支持单分隔符。
3 SQL语句
ADB for PG和Teradata的SQL语法大部分都是兼容的,除了特定的Teradata语法、系统函数是需要进行修改的。
3.1 特定语法
3.3.1 cast
Teradata支持类似如下的cast语法:
cast(XXX as int format '999999') cast(XXX as date format 'YYYYMMDD')
而ADB for PG支持类似cast(XXX as int)、cast(XXX as date),不支持在cast中声明format。所以,对于cast(XXX as int format '999999'),需要编写函数来实现相同功能;而对于cast(XXX as date format 'YYYYMMDD'),ADB for PG支持date的显示格式为'YYYY-MM-DD',这个是不影响正常使用的。
3.3.2 qualify
Teradata的qualify关键字,用来根据用户的条件,进一步过滤前序排序计算函数得到的结果。如下是一个Teradata的qualify关键字使用例子:
SELECT itemid, sumprice, RANK() OVER (ORDER BY sumprice DESC) FROM (SELECT a1.item_id, SUM(a1.sale) FROM sales AS a1 GROUP BY a1.itemID) AS t1 (itemid, sumprice) QUALIFY RANK() OVER (ORDER BY sum_price DESC) <=100;
而ADB for PG是不支持qualify关键字的,所以需要将带qualify的sql语句,修改为子查询嵌套:
SELECT itemid, sumprice, rank from (SELECT itemid, sumprice, RANK() OVER (ORDER BY sumprice DESC) as rank FROM (SELECT a1.item_id, SUM(a1.sale) FROM sales AS a1 GROUP BY a1.itemID) AS t1 (itemid,sumprice) ) AS a where rank <=100;
3.3.3 macro
Teradata通过macro来执行一组SQL语句,一个典型的macro例子为:
CREATE MACRO Get_Emp_Salary(EmployeeNo INTEGER) AS ( SELECT EmployeeNo, NetPay FROM Salary WHERE EmployeeNo = :EmployeeNo; );
ADB for PG不支持macro,但是可以轻易地用ADB for PG的function来完成Teradata的macro功能:
CREATE OR REPLACE FUNCTION Get_Emp_Salary( EmployeeNo INTEGER, OUT EmployeeNo INTEGER, OUT NetPay FLOAT ) returns setof record AS $$ SELECT EmployeeNo,NetPay FROM Salary WHERE EmployeeNo = $1 $$ LANGUAGE SQL;
3.3.4 系统函数
ADB for PG和Teradata关键系统函数对照表:
作者:陆封