This is an automated email from the ASF dual-hosted git repository.
mbudiu 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 bc60174737 [CALCITE-6909] ClickHouse Dailect can not support accurate
precision
bc60174737 is described below
commit bc60174737c6c01e86615f793399860495cddbba
Author: Yu Xu <[email protected]>
AuthorDate: Mon Mar 24 12:45:17 2025 +0800
[CALCITE-6909] ClickHouse Dailect can not support accurate precision
---
.../calcite/sql/dialect/ClickHouseSqlDialect.java | 30 +++++++++++++++++++++-
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 22 +++++++++++++++-
2 files changed, 50 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 fc85540872..6b64725971 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
@@ -19,6 +19,8 @@
import org.apache.calcite.avatica.util.TimeUnitRange;
import org.apache.calcite.config.NullCollation;
import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
import org.apache.calcite.sql.SqlAbstractDateTimeLiteral;
import org.apache.calcite.sql.SqlBasicTypeNameSpec;
import org.apache.calcite.sql.SqlCall;
@@ -48,10 +50,36 @@
* A <code>SqlDialect</code> implementation for the ClickHouse database.
*/
public class ClickHouseSqlDialect extends SqlDialect {
+ public static final RelDataTypeSystem TYPE_SYSTEM =
+ new RelDataTypeSystemImpl() {
+ @Override public int getMaxPrecision(SqlTypeName typeName) {
+ switch (typeName) {
+ case DECIMAL:
+ return 76;
+ default:
+ return super.getMaxPrecision(typeName);
+ }
+ }
+
+ @Override public int getMaxScale(SqlTypeName typeName) {
+ switch (typeName) {
+ case DECIMAL:
+ return 76;
+ default:
+ return super.getMaxScale(typeName);
+ }
+ }
+
+ @Override public int getMaxNumericScale() {
+ return getMaxScale(SqlTypeName.DECIMAL);
+ }
+ };
+
public static final SqlDialect.Context DEFAULT_CONTEXT =
SqlDialect.EMPTY_CONTEXT
.withDatabaseProduct(SqlDialect.DatabaseProduct.CLICKHOUSE)
.withIdentifierQuoteString("`")
- .withNullCollation(NullCollation.LOW);
+ .withNullCollation(NullCollation.LOW)
+ .withDataTypeSystem(TYPE_SYSTEM);
public static final SqlDialect DEFAULT = new
ClickHouseSqlDialect(DEFAULT_CONTEXT);
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 f8fdf44e13..b815987236 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
@@ -1418,9 +1418,13 @@ private static String toSql(RelNode root, SqlDialect
dialect,
+ "from \"product\" ";
final String expectedRedshift = "SELECT CAST(\"product_id\" AS DECIMAL(38,
2))\n"
+ "FROM \"foodmart\".\"product\"";
+ final String expectedClickHouse = "SELECT CAST(`product_id` AS DECIMAL(60,
2))\n"
+ + "FROM `foodmart`.`product`";
sql(query)
.withRedshift()
- .ok(expectedRedshift);
+ .ok(expectedRedshift)
+ .withClickHouse()
+ .ok(expectedClickHouse);
}
/**
@@ -5866,6 +5870,16 @@ private void checkLiteral2(String expression, String
expected) {
.ok(expected);
}
+ /** Test for <a
href="https://issues.apache.org/jira/browse/CALCITE-6909">[CALCITE-6909]
+ * ClickHouse dialect should limit the Precision and Scale of the Decimal
type
+ * to be within 76</a>. */
+ @Test void testClickHouseDecimalPrecision() {
+ final String sql = "SELECT CAST(1.23 AS DECIMAL(76, 20))";
+ final String expected = "SELECT 1.23000000000000000000";
+ sql(sql).withClickHouseModifiedDecimalTypeSystem()
+ .ok(expected);
+ }
+
/** Test for <a
href="https://issues.apache.org/jira/browse/CALCITE-5651">[CALCITE-5651]
* Inferred scale for decimal should not exceed maximum allowed scale</a>. */
@Test void testNumericScale() {
@@ -9821,6 +9835,12 @@ Sql withPostgresqlModifiedDecimalTypeSystem() {
return dialect(postgresqlSqlDialect);
}
+ Sql withClickHouseModifiedDecimalTypeSystem() {
+ final ClickHouseSqlDialect clickHouseSqlDialect =
+ new ClickHouseSqlDialect(ClickHouseSqlDialect.DEFAULT_CONTEXT);
+ return dialect(clickHouseSqlDialect);
+ }
+
Sql withOracleModifiedTypeSystem() {
// Oracle dialect with max length for varchar set to 512
final OracleSqlDialect oracleSqlDialect =