https://github.com/starrocks/sqltransformer
https://github.com/starrocks/sqltransformer
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/starrocks/sqltransformer
- Owner: StarRocks
- Created: 2024-03-25T11:08:33.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-28T02:25:25.000Z (about 2 years ago)
- Last Synced: 2026-04-01T09:31:51.494Z (3 months ago)
- Language: Java
- Size: 287 KB
- Stars: 22
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ClickHouse SQL Rewriter
# Introduction
The ClickHouse SQL Rewriter provides the ability to convert ClickHouse SQL queries into StarRocks/Doris SQL queries. It offers several features to facilitate this conversion:
1. Function Rewrite:
* The most commonly used ClickHouse functions are automatically rewritten to their syntactically equivalent counterparts in StarRocks.
* Overload resolution: For overloaded functions in ClickHouse, the rewriter supports global field type reference for resolution. Users can also provide optional table-level field type information for better resolution. For example, the toDateTime function can parse either a Unix timestamp (e.g., `1711405674`) or a string with a datetime pattern (e.g., `2024-01-01 00:00:00`).
2. Scoped Calculated Column:
* ClickHouse supports ad hoc calculated columns for reuse within the same level. The ClickHouse SQL Rewriter also supports this feature.
3. Ambiguous Column Resolution:
* ClickHouse allows users to omit the subquery label of a column, even if it appears in both the left and right parts of a join. For example, `select c from (select c from a) t1 left join (select c from b) t2` is allowed in ClickHouse but would cause an error (`c is ambiguous`). The ClickHouse SQL Rewriter can rewrite `c` to `t1.c`, resolving the ambiguity.
4. Automatic Subquery Alias:
* Similar to ambiguous columns, ClickHouse allows subqueries without aliases. The ClickHouse SQL Rewriter automatically assigns labels to subqueries without aliases.
5. Complex Syntax Rewrite:
* Window clause: ClickHouse allows users to specify a window clause using the syntax window ` as (partition by `).
* Array Join Clause: ClickHouse supports specifying an array join clause.
* Double Quote Strip: In ClickHouse, double quotes are used to quote identifiers, while in StarRocks and most other SQL dialects, they are used for strings. The ClickHouse SQL Rewriter handles the necessary conversions.
6. More:
* The `BaseSqlBuilder` template allows easy conversion of ClickHouse SQL to any desired SQL dialect. Simply subclass `BaseSqlBuilder` and start using it.
# Quick Start
```java
String clickhouseSql = "SELECT toDateTime(date_time) FROM db.table";
StarRocksSqlBuilder sr = new StarRocksSqlBuilder();
String starRocksSql = sr.getStarRocksSql(clickhouseSql);
System.out.println(starRocksSql);
```