Repository: calcite
Updated Branches:
  refs/heads/master 6c7a7edd1 -> efec74deb


[CALCITE-2719] In JDBC adapter for MySQL, fix cast to INTEGER and BIGINT (Piotr 
Bojko)

MySQL does not support casts to INTEGER and BIGINT; instead we must
cast to SIGNED.

Close apache/calcite#953


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/dfb29f76
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/dfb29f76
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/dfb29f76

Branch: refs/heads/master
Commit: dfb29f7627d02f6e4414be7bc42822a26684778c
Parents: 6c7a7ed
Author: Piotr Bojko <[email protected]>
Authored: Sun Dec 2 23:04:12 2018 +0100
Committer: Julian Hyde <[email protected]>
Committed: Mon Dec 3 17:08:11 2018 -0800

----------------------------------------------------------------------
 .../calcite/sql/dialect/MysqlSqlDialect.java    |  3 ++-
 .../rel/rel2sql/RelToSqlConverterTest.java      | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/dfb29f76/core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java
----------------------------------------------------------------------
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 807ba6c..6446386 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
@@ -105,7 +105,8 @@ public class MysqlSqlDialect extends SqlDialect {
       return new SqlDataTypeSpec(new SqlIdentifier("CHAR", SqlParserPos.ZERO),
           type.getPrecision(), -1, null, null, SqlParserPos.ZERO);
     case INTEGER:
-      return new SqlDataTypeSpec(new SqlIdentifier("_UNSIGNED", 
SqlParserPos.ZERO),
+    case BIGINT:
+      return new SqlDataTypeSpec(new SqlIdentifier("_SIGNED", 
SqlParserPos.ZERO),
           type.getPrecision(), -1, null, null, SqlParserPos.ZERO);
     }
     return super.getCastSpec(type);

http://git-wip-us.apache.org/repos/asf/calcite/blob/dfb29f76/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
----------------------------------------------------------------------
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 ca27782..fb6a539 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
@@ -646,6 +646,26 @@ public class RelToSqlConverterTest {
     sql(query).dialect(HiveSqlDialect.DEFAULT).ok(expected);
   }
 
+  @Test public void testMysqlCastToBigint() {
+    // MySQL does not allow cast to BIGINT; instead cast to SIGNED.
+    final String query = "select cast(\"product_id\" as bigint) from 
\"product\"";
+    final String expected = "SELECT CAST(`product_id` AS SIGNED)\n"
+        + "FROM `foodmart`.`product`";
+    sql(query).withMysql().ok(expected);
+  }
+
+
+  @Test public void testMysqlCastToInteger() {
+    // MySQL does not allow cast to INTEGER; instead cast to SIGNED.
+    final String query = "select \"employee_id\",\n"
+        + "  cast(\"salary_paid\" * 10000 as integer)\n"
+        + "from \"salary\"";
+    final String expected = "SELECT `employee_id`,"
+        + " CAST(`salary_paid` * 10000 AS SIGNED)\n"
+        + "FROM `foodmart`.`salary`";
+    sql(query).withMysql().ok(expected);
+  }
+
   @Test public void 
testHiveSelectQueryWithOrderByDescAndHighNullsWithVersionGreaterThanOrEq21() {
     final HiveSqlDialect hive2_1Dialect =
         new HiveSqlDialect(SqlDialect.EMPTY_CONTEXT

Reply via email to