SQL方言兼容
从 2.1 版本开始,Doris 可以支持多种 SQL 方言,如 Presto、Trino、Hive、PostgreSQL、Spark、Clickhouse 等等。通过这个功能,用户可以直接使用对应的 SQL 方言查询 Doris 中的数据,方便用户将原先的业务平滑的迁移到 Doris 中。
- 该功能目前是实验性功能,您在使用过程中如遇到任何问题,欢迎通过邮件组、GitHub issue 等方式进行反馈。
- 该功能只支持查询语句,不支持 DDL、DML 语句。
部署服务
- 下载最新版本的 SQL 方言转换工具(1.0.1)
- 在任意 FE 节点,通过以下命令启动服务:
nohup ./doris-sql-convertor-1.0.1-bin-x86 run --host=0.0.0.0 --port=5001 &
备注
- 该服务是一个无状态的服务,可随时启停。
5001是服务端口,可以任意指定为一个可用端口。- 建议在每个 FE 节点都单独启动一个服务。
- 启动 Doris 集群(2.1 或更高版本)
- 通过以下命令,在 Doris 中设置 SQL 方言转换服务的 URL:
MySQL> set global sql_converter_service_url = "http://127.0.0.1:5001/api/v1/convert"
备注
127.0.0.1:5001是 SQL 方言转换服务的部署节点 ip 和端口。
使用 SQL 方言
目前支持的方言类型包括:
prestotrinohivesparkpostgresclickhouse
示例:
- Presto
1. mysql> set sql_dialect=presto;
2. Query OK, 0 rows affected (0.00 sec)
4. mysql> SELECT cast(start_time as varchar(20)) as col1,
5. -> array_distinct(arr_int) as col2,
6. -> FILTER(arr_str, x -> x LIKE '%World%') as col3,
7. -> to_date(value,'%Y-%m-%d')as col4,
8. -> YEAR(start_time) as col5,
9. -> date_add('month', 1, start_time) as col6,
10. -> date_format(start_time, '%Y%m%d')as col7,
11. -> REGEXP_EXTRACT_ALL(value, '-.') as col8,
12. -> JSON_EXTRACT('{"id": "33"}', '$.id')as col9,
13. -> element_at(arr_int, 1) as col10,
14. -> date_trunc('day',start_time) as col11
15. -> FROM test_sqlconvert
16. -> where date_trunc('day',start_time)= DATE'2024-05-20'
17. -> order by id;
18. +---------------------+-----------+-----------+-------------+------+---------------------+----------+-------------+------+-------+---------------------+
19. | col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 | col11 |
20. +---------------------+-----------+-----------+-------------+------+---------------------+----------+-------------+------+-------+---------------------+
21. | 2024-05-20 13:14:52 | [1, 2, 3] | ["World"] | 2024-01-14 | 2024 | 2024-06-20 13:14:52 | 20240520 | ['-0','-1'] | "33" | 1 | 2024-05-20 00:00:00 |
22. +---------------------+-----------+-----------+-------------+------+---------------------+----------+-------------+------+-------+---------------------+
23. 1 row in set (0.13 sec)
- Clickhouse
1. mysql> set sql_dialect=clickhouse;
2. Query OK, 0 rows affected (0.01 sec)
4. mysql> select toString(start_time) as col1,
5. -> arrayCompact(arr_int) as col2,
6. -> arrayFilter(x -> x like '%World%',arr_str)as col3,
7. -> toDate(value) as col4,
8. -> toYear(start_time)as col5,
9. -> addMonths(start_time, 1)as col6,
10. -> toYYYYMMDD(start_time, 'US/Eastern')as col7,
11. -> extractAll(value, '-.')as co8,
12. -> JSONExtractString('{"id": "33"}' , 'id')as col9,
13. -> arrayElement(arr_int, 1) as col10,
14. -> date_trunc('day',start_time) as col11
15. -> FROM test_sqlconvert
16. -> where date_trunc('day',start_time)= '2024-05-20 00:00:00'
17. -> order by id;
18. +---------------------+-----------+-----------+------------+------+---------------------+----------+-------------+------+-------+---------------------+
19. | col1 | col2 | col3 | col4 | col5 | col6 | col7 | co8 | col9 | col10 | col11 |
20. +---------------------+-----------+-----------+------------+------+---------------------+----------+-------------+------+-------+---------------------+
21. | 2024-05-20 13:14:52 | [1, 2, 3] | ["World"] | 2024-01-14 | 2024 | 2024-06-20 13:14:52 | 20240520 | ['-0','-1'] | "33" | 1 | 2024-05-20 00:00:00 |
22. +---------------------+-----------+-----------+------------+------+---------------------+----------+-------------+------+-------+---------------------+
23. 1 row in set (0.04 sec)
