You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2018/05/11 19:58:15 UTC

calcite git commit: [CALCITE-2305] JDBC adapter generates invalid casts on PostgreSQL, because PostgreSQL does not have TINYINT and DOUBLE types

Repository: calcite
Updated Branches:
  refs/heads/master e1326ace2 -> 4433b102b


[CALCITE-2305] JDBC adapter generates invalid casts on PostgreSQL, because PostgreSQL does not have TINYINT and DOUBLE types

Close apache/calcite#689


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

Branch: refs/heads/master
Commit: 4433b102bdeede2da9eadedb2257cc76be32ab8e
Parents: e1326ac
Author: Chris Baynes <bi...@gmail.com>
Authored: Wed May 9 10:55:41 2018 +0200
Committer: Julian Hyde <jh...@apache.org>
Committed: Fri May 11 10:26:33 2018 -0700

----------------------------------------------------------------------
 .../sql/dialect/PostgresqlSqlDialect.java       | 24 ++++++++++++++++++++
 .../apache/calcite/test/JdbcAdapterTest.java    | 17 ++++++++++++++
 2 files changed, 41 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/4433b102/core/src/main/java/org/apache/calcite/sql/dialect/PostgresqlSqlDialect.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/sql/dialect/PostgresqlSqlDialect.java b/core/src/main/java/org/apache/calcite/sql/dialect/PostgresqlSqlDialect.java
index 6a6014e..61028a9 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/PostgresqlSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/PostgresqlSqlDialect.java
@@ -17,11 +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.SqlDataTypeSpec;
 import org.apache.calcite.sql.SqlDialect;
+import org.apache.calcite.sql.SqlIdentifier;
 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.apache.calcite.sql.parser.SqlParserPos;
 
 /**
  * A <code>SqlDialect</code> implementation for the PostgreSQL database.
@@ -41,6 +46,25 @@ public class PostgresqlSqlDialect extends SqlDialect {
     return false;
   }
 
+  @Override public SqlNode getCastSpec(RelDataType type) {
+    String castSpec;
+    switch (type.getSqlTypeName()) {
+    case TINYINT:
+      // Postgres has no tinyint (1 byte), so instead cast to smallint (2 bytes)
+      castSpec = "_smallint";
+      break;
+    case DOUBLE:
+      // Postgres has a double type but it is named differently
+      castSpec = "_double precision";
+      break;
+    default:
+      return super.getCastSpec(type);
+    }
+
+    return new SqlDataTypeSpec(new SqlIdentifier(castSpec, SqlParserPos.ZERO),
+        -1, -1, null, null, SqlParserPos.ZERO);
+  }
+
   @Override protected boolean requiresAliasForFromItems() {
     return true;
   }

http://git-wip-us.apache.org/repos/asf/calcite/blob/4433b102/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java b/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
index a4db644..e65fe65 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
@@ -470,6 +470,23 @@ public class JdbcAdapterTest {
             + "FROM \"foodmart\".\"expense_fact\"");
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-2305">[CALCITE-2305]
+   * JDBC adapter generates invalid casts on PostgreSQL, because PostgreSQL does
+   * not have TINYINT and DOUBLE types</a>. */
+  @Test public void testCast() {
+    CalciteAssert
+        .model(JdbcTest.FOODMART_MODEL)
+        .enable(CalciteAssert.DB == CalciteAssert.DatabaseInstance.POSTGRESQL)
+        .query("select cast(\"store_id\" as TINYINT),"
+            + "cast(\"store_id\" as DOUBLE)"
+            + " from \"expense_fact\"")
+        .runs()
+        .planHasSql("SELECT CAST(\"store_id\" AS SMALLINT),"
+            + " CAST(\"store_id\" AS DOUBLE PRECISION)\n"
+            + "FROM \"foodmart\".\"expense_fact\"");
+  }
+
   @Test public void testOverRowsBetweenBoundFollowingAndFollowing() {
     CalciteAssert
         .model(JdbcTest.FOODMART_MODEL)