calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [2/9] incubator-calcite git commit: [CALCITE-596] JDBC adapter incorrectly reads null values as 0 (Ng Jiunn Jye)
Date Mon, 23 Mar 2015 17:26:16 GMT
[CALCITE-596] JDBC adapter incorrectly reads null values as 0 (Ng Jiunn Jye)


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

Branch: refs/heads/master
Commit: 2b07a9e9680e662a6d9785deb2a4ed140026e95f
Parents: 3b10a6a
Author: Julian Hyde <jhyde@apache.org>
Authored: Mon Mar 23 01:39:12 2015 -0700
Committer: Julian Hyde <jhyde@apache.org>
Committed: Mon Mar 23 01:39:12 2015 -0700

----------------------------------------------------------------------
 .../adapter/jdbc/JdbcToEnumerableConverter.java | 11 ++++
 .../java/org/apache/calcite/test/JdbcTest.java  | 60 ++++++++++++++++++++
 2 files changed, 71 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/2b07a9e9/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcToEnumerableConverter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcToEnumerableConverter.java
b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcToEnumerableConverter.java
index 3fbf778..125d61b 100644
--- a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcToEnumerableConverter.java
+++ b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcToEnumerableConverter.java
@@ -232,6 +232,17 @@ public class JdbcToEnumerableConverter
         Expressions.statement(
             Expressions.assign(
                 target, source)));
+
+    // [CALCITE-596] If primitive type columns contain null value, returns null
+    // object
+    if (primitive != null) {
+      builder.add(
+          Expressions.ifThen(
+              Expressions.call(resultSet_, "wasNull"),
+              Expressions.statement(
+                  Expressions.assign(target,
+                      Expressions.constant(null)))));
+    }
   }
 
   private Method getMethod(SqlTypeName sqlTypeName, boolean nullable,

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/2b07a9e9/core/src/test/java/org/apache/calcite/test/JdbcTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/JdbcTest.java b/core/src/test/java/org/apache/calcite/test/JdbcTest.java
index 0942334..0024bf1 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcTest.java
@@ -6137,6 +6137,66 @@ public class JdbcTest {
         .returnsUnordered("DID=1", "DID=2");
   }
 
+  /**
+   * Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-596">[CALCITE-596]
+   * JDBC adapter incorrectly reads null values as 0</a>.
+   */
+  @Test public void testPrimitiveColumnsWithNullValues() throws Exception {
+    String hsqldbMemUrl = "jdbc:hsqldb:mem:.";
+    Connection baseConnection = DriverManager.getConnection(hsqldbMemUrl);
+    Statement baseStmt = baseConnection.createStatement();
+    baseStmt.execute("CREATE TABLE T1 (\n"
+        + "ID INTEGER,\n"
+        + "VALS DOUBLE)");
+    baseStmt.execute("INSERT INTO T1 VALUES (1, 1.0)");
+    baseStmt.execute("INSERT INTO T1 VALUES (2, null)");
+    baseStmt.execute("INSERT INTO T1 VALUES (null, 2.0)");
+
+    baseStmt.close();
+    baseConnection.commit();
+
+    Properties info = new Properties();
+    info.put("model",
+      "inline:"
+      + "{\n"
+      + "  version: '1.0',\n"
+      + "  defaultSchema: 'BASEJDBC',\n"
+      + "  schemas: [\n"
+      + "     {\n"
+      + "       type: 'jdbc',\n"
+      + "       name: 'BASEJDBC',\n"
+      + "       jdbcDriver: '" + jdbcDriver.class.getName() + "',\n"
+      + "       jdbcUrl: '" + hsqldbMemUrl + "',\n"
+      + "       jdbcCatalog: null,\n"
+      + "       jdbcSchema: null\n"
+      + "     }\n"
+      + "  ]\n"
+      + "}");
+
+    Connection calciteConnection = DriverManager.getConnection(
+      "jdbc:calcite:", info);
+
+    ResultSet rs = calciteConnection.prepareStatement("select * from t1")
+      .executeQuery();
+
+    assertThat(rs.next(), is(true));
+    assertThat((Integer) rs.getObject("ID"), equalTo(1));
+    assertThat((Double) rs.getObject("VALS"), equalTo(1.0));
+
+    assertThat(rs.next(), is(true));
+    assertThat((Integer) rs.getObject("ID"), equalTo(2));
+    assertThat((Double) rs.getObject("VALS"), nullValue());
+
+    assertThat(rs.next(), is(true));
+    assertThat(rs.getObject("ID"), nullValue());
+    assertThat((Double) rs.getObject("VALS"), equalTo(2.0));
+
+    rs.close();
+    calciteConnection.close();
+
+  }
+
   // Disable checkstyle, so it doesn't complain about fields like "customer_id".
   //CHECKSTYLE: OFF
 


Mime
View raw message