This is an automated email from the ASF dual-hosted git repository.
cancai pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new c72e267f9c [CALCITE-6900] Support Char type cast in ClickHouse Dialect
c72e267f9c is described below
commit c72e267f9c1b94abb3f4a7e31d14f7b4ed676ce1
Author: Yu Xu <[email protected]>
AuthorDate: Wed Mar 19 12:08:35 2025 +0800
[CALCITE-6900] Support Char type cast in ClickHouse Dialect
---
.../apache/calcite/sql/dialect/ClickHouseSqlDialect.java | 6 ++++++
.../apache/calcite/rel/rel2sql/RelToSqlConverterTest.java | 13 +++++++++++--
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java
b/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java
index 4cde76631f..fc85540872 100644
---
a/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java
+++
b/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java
@@ -38,6 +38,8 @@
import org.checkerframework.checker.nullness.qual.Nullable;
+import java.util.Locale;
+
import static
org.apache.calcite.util.RelToSqlConverterUtil.unparseBoolLiteralToCondition;
import static java.util.Objects.requireNonNull;
@@ -86,6 +88,10 @@ public ClickHouseSqlDialect(Context context) {
if (type instanceof BasicSqlType) {
SqlTypeName typeName = type.getSqlTypeName();
switch (typeName) {
+ case CHAR:
+ return createSqlDataTypeSpecByName(
+ String.format(Locale.ROOT, "FixedString(%s)",
+ type.getPrecision()), typeName, type.isNullable());
case VARCHAR:
return createSqlDataTypeSpecByName("String", typeName,
type.isNullable());
case TINYINT:
diff --git
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index a4a3c802fd..bd61e213e4 100644
---
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -3880,6 +3880,9 @@ private SqlDialect nonOrdinalDialect() {
.withSpark().ok(expectedSpark);
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6900">[CALCITE-6900]
+ * Support Char type cast in ClickHouse Dialect</a>. */
@Test void testCastToChar() {
String query = "select cast(\"product_id\" as char) from \"product\"";
final String expectedMysql = "SELECT CAST(`product_id` AS CHAR)\n"
@@ -3890,11 +3893,14 @@ private SqlDialect nonOrdinalDialect() {
+ "FROM `foodmart`.`product`";
final String expectedSpark = "SELECT CAST(`product_id` AS CHAR(1))\n"
+ "FROM `foodmart`.`product`";
+ final String expectedClickHouse = "SELECT CAST(`product_id` AS
`FixedString(1)`)\n"
+ + "FROM `foodmart`.`product`";
sql(query)
.withMysql().ok(expectedMysql)
.withMssql().ok(expectedMssql)
.withHive().ok(expectedHive)
- .withSpark().ok(expectedSpark);
+ .withSpark().ok(expectedSpark)
+ .withClickHouse().ok(expectedClickHouse);
}
@Test void testCastToCharWithPrecision() {
@@ -3907,11 +3913,14 @@ private SqlDialect nonOrdinalDialect() {
+ "FROM `foodmart`.`product`";
final String expectedSpark = "SELECT CAST(`product_id` AS CHAR(5))\n"
+ "FROM `foodmart`.`product`";
+ final String expectedClickHouse = "SELECT CAST(`product_id` AS
`FixedString(5)`)\n"
+ + "FROM `foodmart`.`product`";
sql(query)
.withMysql().ok(expectedMysql)
.withMssql().ok(expectedMssql)
.withHive().ok(expectedHive)
- .withSpark().ok(expectedSpark);
+ .withSpark().ok(expectedSpark)
+ .withClickHouse().ok(expectedClickHouse);
}
@Test void testSelectQueryWithLimitClauseWithoutOrder() {