This is an automated email from the ASF dual-hosted git repository. xiong 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 1079bcea97 [CALCITE-6426] Invalid unparse for INT and BIGINT in StarRocksDialect 1079bcea97 is described below commit 1079bcea97ce48973255e05ce600998edb1e7b4c Author: fan.luo <fan....@ly.com> AuthorDate: Mon Jun 3 20:19:39 2024 +0800 [CALCITE-6426] Invalid unparse for INT and BIGINT in StarRocksDialect --- .../calcite/sql/dialect/StarRocksSqlDialect.java | 12 +++++++ .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 38 +++++++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) 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 8e9950da7a..ded2bbf96e 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 @@ -21,6 +21,7 @@ import org.apache.calcite.config.NullCollation; import org.apache.calcite.rel.type.RelDataType; import org.apache.calcite.sql.SqlAbstractDateTimeLiteral; import org.apache.calcite.sql.SqlAlienSystemTypeNameSpec; +import org.apache.calcite.sql.SqlBasicTypeNameSpec; import org.apache.calcite.sql.SqlCall; import org.apache.calcite.sql.SqlDataTypeSpec; import org.apache.calcite.sql.SqlDialect; @@ -109,6 +110,17 @@ public class StarRocksSqlDialect extends MysqlSqlDialect { @Override public @Nullable SqlNode getCastSpec(RelDataType type) { switch (type.getSqlTypeName()) { + case INTEGER: + return new SqlDataTypeSpec( + new SqlAlienSystemTypeNameSpec( + "INT", + type.getSqlTypeName(), + SqlParserPos.ZERO), + SqlParserPos.ZERO); + case BIGINT: + return new SqlDataTypeSpec( + new SqlBasicTypeNameSpec(SqlTypeName.BIGINT, SqlParserPos.ZERO), + SqlParserPos.ZERO); case TIMESTAMP: return new SqlDataTypeSpec( new SqlAlienSystemTypeNameSpec( 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 69532ef02d..5334e2546e 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 @@ -3072,24 +3072,44 @@ class RelToSqlConverterTest { sql(query).dialect(HiveSqlDialect.DEFAULT).ok(expected); } - @Test void testMysqlCastToBigint() { - // MySQL does not allow cast to BIGINT; instead cast to SIGNED. + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-2719">[CALCITE-2719] + * MySQL does not support cast to BIGINT type</a> + * and + * <a href="https://issues.apache.org/jira/browse/CALCITE-6426">[CALCITE-6426] + * Invalid unparse for INT and BIGINT in StarRocksDialect</a>. */ + @Test void testCastToBigint() { final String query = "select cast(\"product_id\" as bigint) from \"product\""; - final String expected = "SELECT CAST(`product_id` AS SIGNED)\n" + // MySQL does not allow cast to BIGINT; instead cast to SIGNED. + final String expectedMysql = "SELECT CAST(`product_id` AS SIGNED)\n" + "FROM `foodmart`.`product`"; - sql(query).withMysql().ok(expected); + final String expectedStarRocks = "SELECT CAST(`product_id` AS BIGINT)\n" + + "FROM `foodmart`.`product`"; + sql(query) + .withMysql().ok(expectedMysql) + .withStarRocks().ok(expectedStarRocks); } - - @Test void testMysqlCastToInteger() { - // MySQL does not allow cast to INTEGER; instead cast to SIGNED. + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-2719">[CALCITE-2719] + * MySQL does not support cast to BIGINT type</a> + * and + * <a href="https://issues.apache.org/jira/browse/CALCITE-6426">[CALCITE-6426] + * Invalid unparse for INT and BIGINT in StarRocksDialect</a>. */ + @Test void testCastToInteger() { final String query = "select \"employee_id\",\n" + " cast(\"salary_paid\" * 10000 as integer)\n" + "from \"salary\""; - final String expected = "SELECT `employee_id`," + // MySQL does not allow cast to INTEGER; instead cast to SIGNED. + final String expectedMysql = "SELECT `employee_id`," + " CAST(`salary_paid` * 10000 AS SIGNED)\n" + "FROM `foodmart`.`salary`"; - sql(query).withMysql().ok(expected); + final String expectedStarRocks = "SELECT `employee_id`," + + " CAST(`salary_paid` * 10000 AS INT)\n" + + "FROM `foodmart`.`salary`"; + sql(query) + .withMysql().ok(expectedMysql) + .withStarRocks().ok(expectedStarRocks); } @Test void testHiveSelectQueryWithOrderByDescAndHighNullsWithVersionGreaterThanOrEq21() {