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 182b90955d [CALCITE-7069] Invalid unparse for INT UNSIGNED and BIGINT 
UNSIGNED in MysqlSqlDialect
182b90955d is described below

commit 182b90955d00f52227a520e58d4e3e042e4da5d3
Author: Zhe Hu <[email protected]>
AuthorDate: Fri Jun 27 08:55:07 2025 +0800

    [CALCITE-7069] Invalid unparse for INT UNSIGNED and BIGINT UNSIGNED in 
MysqlSqlDialect
---
 .../calcite/sql/dialect/DorisSqlDialect.java       | 18 ++++++++++++++++++
 .../calcite/sql/dialect/MysqlSqlDialect.java       | 12 ++++++++++++
 .../calcite/sql/dialect/StarRocksSqlDialect.java   |  5 +++++
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 22 +++++++++++++++++++++-
 4 files changed, 56 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/DorisSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/DorisSqlDialect.java
index 7d4e27901d..3f7e10d002 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/DorisSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/DorisSqlDialect.java
@@ -17,12 +17,16 @@
 package org.apache.calcite.sql.dialect;
 
 import org.apache.calcite.avatica.util.TimeUnitRange;
+import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.sql.SqlCall;
 import org.apache.calcite.sql.SqlDialect;
 import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlWriter;
 import org.apache.calcite.sql.fun.SqlFloorFunction;
 
+import org.checkerframework.checker.nullness.qual.Nullable;
+
 import static 
org.apache.calcite.util.RelToSqlConverterUtil.unparseSparkArrayAndMap;
 
 /**
@@ -60,4 +64,18 @@ public DorisSqlDialect(Context context) {
       break;
     }
   }
+
+  @Override public @Nullable SqlNode getCastSpec(RelDataType type) {
+    switch (type.getSqlTypeName()) {
+    case UTINYINT:
+    case USMALLINT:
+    case UINTEGER:
+    case UBIGINT:
+      throw new RuntimeException(
+          "Doris doesn't support UNSIGNED TINYINT/SMALLINT/INTEGER/BIGINT!");
+    default:
+      break;
+    }
+    return super.getCastSpec(type);
+  }
 }
diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java
index 35ea8c9311..f828e89fd7 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java
@@ -196,6 +196,18 @@ public MysqlSqlDialect(Context context) {
               type.getSqlTypeName(),
               SqlParserPos.ZERO),
           SqlParserPos.ZERO);
+    case UTINYINT:
+    case USMALLINT:
+    case UINTEGER:
+      throw new RuntimeException(
+          "MySQL doesn't support UNSIGNED TINYINT/SMALLINT/INTEGER!");
+    case UBIGINT:
+      return new SqlDataTypeSpec(
+          new SqlAlienSystemTypeNameSpec(
+              "UNSIGNED",
+              type.getSqlTypeName(),
+              SqlParserPos.ZERO),
+          SqlParserPos.ZERO);
     case TIMESTAMP:
       return new SqlDataTypeSpec(
           new SqlAlienSystemTypeNameSpec(
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 07d09b5a24..a877623bb2 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
@@ -189,6 +189,11 @@ public StarRocksSqlDialect(Context context) {
       return new SqlDataTypeSpec(
           new SqlBasicTypeNameSpec(SqlTypeName.BIGINT, SqlParserPos.ZERO),
           SqlParserPos.ZERO);
+    case UTINYINT:
+    case USMALLINT:
+    case UINTEGER:
+      throw new RuntimeException(
+          "StarRocks doesn't support UNSIGNED TINYINT/SMALLINT/INTEGER!");
     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 23fcd55036..c82696331e 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
@@ -3776,6 +3776,26 @@ private SqlDialect nonOrdinalDialect() {
         .withDoris().ok(expectedStarRocks);
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7069";>[CALCITE-7069]
+   * Invalid unparse for INT UNSIGNED and BIGINT UNSIGNED in 
MysqlSqlDialect</a>. */
+  @Test void testCastToUnsignedInMySQL() {
+    final String query1 = "select cast(\"product_id\" as bigint unsigned) from 
\"product\"";
+    // MySQL does not allow cast to BIGINT UNSIGNED; instead cast to UNSIGNED.
+    final String expectedMysql1 = "SELECT CAST(`product_id` AS UNSIGNED)\n"
+        + "FROM `foodmart`.`product`";
+    final String query2 = "select cast(\"product_id\" as integer unsigned) 
from \"product\"";
+    sql(query1)
+        .withMysql().ok(expectedMysql1)
+        .withStarRocks().ok(expectedMysql1)
+        .withDoris().throws_("Doris doesn't support UNSIGNED 
TINYINT/SMALLINT/INTEGER/BIGINT!");
+    sql(query2)
+        // MySQL does not allow cast to INTEGER UNSIGNED, and we shouldn't use 
the next level
+        .withMysql().throws_("MySQL doesn't support UNSIGNED 
TINYINT/SMALLINT/INTEGER!")
+        .withStarRocks().throws_("StarRocks doesn't support UNSIGNED 
TINYINT/SMALLINT/INTEGER!")
+        .withDoris().throws_("Doris doesn't support UNSIGNED 
TINYINT/SMALLINT/INTEGER/BIGINT!");
+  }
+
   /** Test case for
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-2719";>[CALCITE-2719]
    * MySQL does not support cast to BIGINT type</a>
@@ -4348,7 +4368,7 @@ private SqlDialect nonOrdinalDialect() {
    * Maximum precision of unsigned bigint type in MysqlSqlDialect should be 
20</a>. */
   @Test void testCastToUBigInt() {
     String query = "select cast(18446744073709551615 as bigint unsigned) from 
\"product\"";
-    final String expectedMysql = "SELECT CAST(18446744073709551615 AS BIGINT 
UNSIGNED)\n"
+    final String expectedMysql = "SELECT CAST(18446744073709551615 AS 
UNSIGNED)\n"
         + "FROM `foodmart`.`product`";
     final String errMsg = "org.apache.calcite.runtime.CalciteContextException: 
"
         + "From line 1, column 13 to line 1, column 32: "

Reply via email to