From ece76dc09eb0851731ec04dafb8302174c6dde9b Mon Sep 17 00:00:00 2001 From: ClownXC Date: Sat, 4 Jan 2025 20:45:48 +0800 Subject: [PATCH] Add STARROCKS_TYPE_SYSTEM --- .../sql/dialect/StarRocksSqlDialect.java | 25 +++++++++++++++++++ .../rel/rel2sql/RelToSqlConverterTest.java | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.java b/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.java index 5e5886051dd6..3d47f327b645 100644 --- a/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.java +++ b/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.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.SqlAlienSystemTypeNameSpec; import org.apache.calcite.sql.SqlBasicTypeNameSpec; @@ -41,9 +43,32 @@ */ public class StarRocksSqlDialect extends MysqlSqlDialect { + public static final RelDataTypeSystem STARROCKS_TYPE_SYSTEM = + new RelDataTypeSystemImpl() { + @Override public int getMaxPrecision(SqlTypeName typeName) { + switch (typeName) { + case CHAR: + return 255; + case VARCHAR: + return 65533; + case VARBINARY: + return 1048576; + default: + return super.getMaxPrecision(typeName); + } + } + @Override public int getDefaultPrecision(SqlTypeName typeName) { + if (typeName == SqlTypeName.CHAR) { + return RelDataType.PRECISION_NOT_SPECIFIED; + } + return super.getDefaultPrecision(typeName); + } + }; + public static final SqlDialect.Context DEFAULT_CONTEXT = SqlDialect.EMPTY_CONTEXT .withDatabaseProduct(SqlDialect.DatabaseProduct.STARROCKS) .withIdentifierQuoteString("`") + .withDataTypeSystem(STARROCKS_TYPE_SYSTEM) .withNullCollation(NullCollation.LOW); public static final SqlDialect DEFAULT = new StarRocksSqlDialect(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 7b5941c64aee..52f22631a673 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 @@ -3758,7 +3758,7 @@ private SqlDialect nonOrdinalDialect() { @Test void testStarRocksCastToVarcharWithGreaterThanMaxPrecision() { final String query = "select cast(\"product_id\" as varchar(150000)), \"product_id\" " + "from \"product\" "; - final String expected = "SELECT CAST(`product_id` AS VARCHAR(65536)), `product_id`\n" + final String expected = "SELECT CAST(`product_id` AS VARCHAR(65533)), `product_id`\n" + "FROM `foodmart`.`product`"; sql(query).withStarRocks().ok(expected); }