calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [7/7] incubator-calcite git commit: [CALCITE-665] ClassCastException in MongoDB adapter
Date Tue, 09 Jun 2015 22:08:11 GMT
[CALCITE-665] ClassCastException in MongoDB adapter


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

Branch: refs/heads/master
Commit: dbdb091dc6fa5d2322d87df65bde142276930129
Parents: 6609cb1
Author: Julian Hyde <jhyde@apache.org>
Authored: Mon Jun 8 10:25:55 2015 -0700
Committer: Julian Hyde <jhyde@apache.org>
Committed: Mon Jun 8 10:25:55 2015 -0700

----------------------------------------------------------------------
 .../adapter/mongodb/MongoEnumerator.java        | 21 ++++++++++---
 .../org/apache/calcite/test/MongoAdapterIT.java | 32 +++++++++++++++++---
 2 files changed, 44 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/dbdb091d/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoEnumerator.java
----------------------------------------------------------------------
diff --git a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoEnumerator.java
b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoEnumerator.java
index 94efffb..0821f32 100644
--- a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoEnumerator.java
+++ b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoEnumerator.java
@@ -19,6 +19,7 @@ package org.apache.calcite.adapter.mongodb;
 import org.apache.calcite.avatica.util.DateTimeUtils;
 import org.apache.calcite.linq4j.Enumerator;
 import org.apache.calcite.linq4j.function.Function1;
+import org.apache.calcite.linq4j.tree.Primitive;
 
 import com.mongodb.DBCursor;
 import com.mongodb.DBObject;
@@ -122,13 +123,23 @@ class MongoEnumerator implements Enumerator<Object> {
   }
 
   private static Object convert(Object o, Class clazz) {
-    if (o == null || clazz.isInstance(o)) {
+    if (o == null) {
+      return null;
+    }
+    Primitive primitive = Primitive.of(clazz);
+    if (primitive != null) {
+      clazz = primitive.boxClass;
+    } else {
+      primitive = Primitive.ofBox(clazz);
+    }
+    if (clazz.isInstance(o)) {
       return o;
     }
-    if (clazz == int.class || clazz == Integer.class) {
-      if (o instanceof Date) {
-        return (int) (((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY);
-      }
+    if (o instanceof Date && primitive != null) {
+      o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
+    }
+    if (o instanceof Number && primitive != null) {
+      return primitive.number((Number) o);
     }
     return o;
   }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/dbdb091d/mongodb/src/test/java/org/apache/calcite/test/MongoAdapterIT.java
----------------------------------------------------------------------
diff --git a/mongodb/src/test/java/org/apache/calcite/test/MongoAdapterIT.java b/mongodb/src/test/java/org/apache/calcite/test/MongoAdapterIT.java
index d3b37e1..f994142 100644
--- a/mongodb/src/test/java/org/apache/calcite/test/MongoAdapterIT.java
+++ b/mongodb/src/test/java/org/apache/calcite/test/MongoAdapterIT.java
@@ -22,9 +22,11 @@ import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.Util;
 
 import com.google.common.base.Function;
+import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 
+import org.hamcrest.CoreMatchers;
 import org.junit.Ignore;
 import org.junit.Test;
 
@@ -496,8 +498,8 @@ public class MongoAdapterIT {
         .query(
             "select state, avg(pop) as a from zips group by state order by state")
         .limit(2)
-        .returns("STATE=AK; A=2793.3230769230768\n"
-            + "STATE=AL; A=7126.255731922399\n")
+        .returns("STATE=AK; A=2793\n"
+            + "STATE=AL; A=7126\n")
         .queryContains(
             mongoChecker(
                 "{$project: {POP: '$pop', STATE: '$state'}}",
@@ -513,8 +515,8 @@ public class MongoAdapterIT {
         .query(
             "select state, avg(pop) as a, sum(pop) as s, count(pop) as c from zips group
by state order by state")
         .limit(2)
-        .returns("STATE=AK; A=2793.3230769230768; S=544698; C=195\n"
-            + "STATE=AL; A=7126.255731922399; S=4040587; C=567\n")
+        .returns("STATE=AK; A=2793; S=544698; C=195\n"
+            + "STATE=AL; A=7126; S=4040587; C=567\n")
         .queryContains(
             mongoChecker(
                 "{$project: {POP: '$pop', STATE: '$state'}}",
@@ -774,6 +776,28 @@ public class MongoAdapterIT {
         .query("select cast(_MAP['date'] as DATE) from \"datatypes\"")
         .returnsUnordered("EXPR$0=2012-09-05");
   }
+
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-665">[CALCITE-665]
+   * ClassCastException in MongoDB adapter</a>. */
+  @Test public void testCountViaInt() {
+    CalciteAssert.that()
+        .enable(enabled())
+        .with(ZIPS)
+        .query("select count(*) from zips")
+        .returns(
+            new Function<ResultSet, Void>() {
+              public Void apply(ResultSet input) {
+                try {
+                  assertThat(input.next(), CoreMatchers.is(true));
+                  assertThat(input.getInt(1), CoreMatchers.is(29353));
+                  return null;
+                } catch (SQLException e) {
+                  throw Throwables.propagate(e);
+                }
+              }
+            });
+  }
 }
 
 // End MongoAdapterIT.java


Mime
View raw message