calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [2/2] incubator-calcite git commit: [CALCITE-580] Average aggregation on an Integer column throws java.lang.ClassCastException
Date Tue, 10 Feb 2015 18:52:52 GMT
[CALCITE-580] Average aggregation on an Integer column throws java.lang.ClassCastException


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

Branch: refs/heads/master
Commit: cca42158f5b9b6c43059cbd10167f7cb72aeb6b8
Parents: 882477b
Author: Julian Hyde <jhyde@apache.org>
Authored: Tue Feb 10 01:58:15 2015 -0800
Committer: Julian Hyde <jhyde@apache.org>
Committed: Tue Feb 10 01:58:15 2015 -0800

----------------------------------------------------------------------
 .../adapter/enumerable/JavaRowFormat.java       |  26 +----
 .../calcite/test/ReflectiveSchemaTest.java      | 100 ++++++++++++++++---
 2 files changed, 90 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/cca42158/core/src/main/java/org/apache/calcite/adapter/enumerable/JavaRowFormat.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/JavaRowFormat.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/JavaRowFormat.java
index ac87aad..9cb54ff 100644
--- a/core/src/main/java/org/apache/calcite/adapter/enumerable/JavaRowFormat.java
+++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/JavaRowFormat.java
@@ -19,18 +19,15 @@ package org.apache.calcite.adapter.enumerable;
 import org.apache.calcite.adapter.java.JavaTypeFactory;
 import org.apache.calcite.interpreter.Row;
 import org.apache.calcite.linq4j.tree.Expression;
-import org.apache.calcite.linq4j.tree.ExpressionType;
 import org.apache.calcite.linq4j.tree.Expressions;
 import org.apache.calcite.linq4j.tree.MemberExpression;
 import org.apache.calcite.linq4j.tree.Types;
-import org.apache.calcite.linq4j.tree.UnaryExpression;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.runtime.FlatLists;
 import org.apache.calcite.runtime.Unit;
 import org.apache.calcite.util.BuiltInMethod;
 
 import java.lang.reflect.Type;
-import java.util.AbstractList;
 import java.util.List;
 
 /**
@@ -207,10 +204,8 @@ public enum JavaRowFormat {
       return Object.class;
     }
 
-    public Expression record(
-        Type javaRowClass, List<Expression> expressions) {
-      return Expressions.newArrayInit(
-          Object.class, stripCasts(expressions));
+    public Expression record(Type javaRowClass, List<Expression> expressions) {
+      return Expressions.newArrayInit(Object.class, expressions);
     }
 
     @Override public Expression comparer() {
@@ -257,23 +252,6 @@ public enum JavaRowFormat {
   public abstract Expression record(
       Type javaRowClass, List<Expression> expressions);
 
-  private static List<Expression> stripCasts(
-      final List<Expression> expressions) {
-    return new AbstractList<Expression>() {
-      public Expression get(int index) {
-        Expression expression = expressions.get(index);
-        while (expression.getNodeType() == ExpressionType.Convert) {
-          expression = ((UnaryExpression) expression).expression;
-        }
-        return expression;
-      }
-
-      public int size() {
-        return expressions.size();
-      }
-    };
-  }
-
   public Expression comparer() {
     return null;
   }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/cca42158/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java b/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java
index ddba621..7c5ba6f 100644
--- a/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java
+++ b/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java
@@ -18,6 +18,7 @@ package org.apache.calcite.test;
 
 import org.apache.calcite.adapter.java.ReflectiveSchema;
 import org.apache.calcite.avatica.util.DateTimeUtils;
+import org.apache.calcite.config.Lex;
 import org.apache.calcite.jdbc.CalciteConnection;
 import org.apache.calcite.linq4j.Enumerable;
 import org.apache.calcite.linq4j.Linq4j;
@@ -33,8 +34,10 @@ import org.apache.calcite.schema.SchemaPlus;
 import org.apache.calcite.schema.impl.AbstractSchema;
 import org.apache.calcite.schema.impl.TableMacroImpl;
 import org.apache.calcite.schema.impl.ViewTable;
+import org.apache.calcite.util.Util;
 
 import com.google.common.base.Function;
+import com.google.common.base.Throwables;
 
 import org.junit.Assert;
 import org.junit.Ignore;
@@ -328,18 +331,65 @@ public class ReflectiveSchemaTest {
   }
 
   private void checkAgg(CalciteAssert.AssertThat with, String fn) {
-    for (Field field
+    for (final Field field
         : fn.equals("avg") ? EveryType.numericFields() : EveryType.fields()) {
-      with.query("select " + fn + "(\"" + field.getName() + "\") as c\n"
-          + "from \"s\".\"everyTypes\"")
-          .returns(CalciteAssert.<ResultSet, Void>constantNull());
+      with.query(
+          "select " + fn + "(\"" + field.getName() + "\") as c\n"
+              + "from \"s\".\"everyTypes\"")
+          .returns(
+              new Function<ResultSet, Void>() {
+                public Void apply(ResultSet input) {
+                  int n = 0;
+                  try {
+                    while (input.next()) {
+                      final Object o = get(input);
+                      Util.discard(o);
+                      ++n;
+                    }
+                  } catch (SQLException e) {
+                    throw Throwables.propagate(e);
+                  }
+                  assertThat(n, equalTo(1));
+                  return null;
+                }
+
+                private Object get(ResultSet input) throws SQLException {
+                  final int type = input.getMetaData().getColumnType(1);
+                  switch (type) {
+                  case java.sql.Types.BOOLEAN:
+                    return input.getBoolean(1);
+                  case java.sql.Types.TINYINT:
+                    return input.getByte(1);
+                  case java.sql.Types.SMALLINT:
+                    return input.getShort(1);
+                  case java.sql.Types.INTEGER:
+                    return input.getInt(1);
+                  case java.sql.Types.BIGINT:
+                    return input.getLong(1);
+                  case java.sql.Types.REAL:
+                    return input.getFloat(1);
+                  case java.sql.Types.DOUBLE:
+                    return input.getDouble(1);
+                  case java.sql.Types.CHAR:
+                  case java.sql.Types.VARCHAR:
+                    return input.getString(1);
+                  case java.sql.Types.DATE:
+                    return input.getDate(1);
+                  case java.sql.Types.TIME:
+                    return input.getTime(1);
+                  case java.sql.Types.TIMESTAMP:
+                    return input.getTimestamp(1);
+                  default:
+                    throw new AssertionError(type);
+                  }
+                }
+              });
     }
   }
 
   @Test public void testClassNames() throws Exception {
     CalciteAssert.that()
-        .withSchema("s", CATCHALL)
-        .query("select * from \"s\".\"everyTypes\"")
+        .withSchema("s", CATCHALL).query("select * from \"s\".\"everyTypes\"")
         .returns(
             new Function<ResultSet, Void>() {
               public Void apply(ResultSet input) {
@@ -491,13 +541,37 @@ public class ReflectiveSchemaTest {
   }
 
   @Test public void testCastFromString() {
-    CalciteAssert.that()
-        .withSchema("s", CATCHALL)
+    CalciteAssert.that().withSchema("s", CATCHALL)
         .query("select cast(\"string\" as int) as c from \"s\".\"everyTypes\"")
         .returns("C=1\n"
             + "C=null\n");
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-580">CALCITE-580</a>,
+   * "Average aggregation on an Integer column throws ClassCastException". */
+  @Test public void testAvgInt() throws Exception {
+    CalciteAssert.that().withSchema("s", CATCHALL).with(Lex.JAVA)
+        .query("select primitiveLong, avg(primitiveInt)\n"
+                + "from s.everyTypes\n"
+                + "group by primitiveLong order by primitiveLong")
+        .returns(
+            new Function<ResultSet, Void>() {
+              public Void apply(ResultSet input) {
+                StringBuilder buf = new StringBuilder();
+                try {
+                  while (input.next()) {
+                    buf.append(input.getInt(2)).append("\n");
+                  }
+                } catch (SQLException e) {
+                  throw Throwables.propagate(e);
+                }
+                assertThat(buf.toString(), equalTo("0\n2147483647\n"));
+                return null;
+              }
+            });
+  }
+
   private static boolean isNumeric(Class type) {
     switch (Primitive.flavor(type)) {
     case BOX:
@@ -565,8 +639,9 @@ public class ReflectiveSchemaTest {
     CalciteAssert.that()
         .withSchema("s", new ReflectiveSchema(new JdbcTest.HrSchema()))
         .query("select * from table(\"s\".\"view\"('abc'))")
-        .returns("empid=2; deptno=10; name=Ab; salary=0.0; commission=null\n"
-            + "empid=4; deptno=10; name=Abd; salary=0.0; commission=null\n");
+        .returns(
+            "empid=2; deptno=10; name=Ab; salary=0.0; commission=null\n"
+                + "empid=4; deptno=10; name=Abd; salary=0.0; commission=null\n");
   }
 
   /** Finds a table-macro using reflection. */
@@ -575,8 +650,9 @@ public class ReflectiveSchemaTest {
     CalciteAssert.that()
         .withSchema("s", new ReflectiveSchema(new JdbcTest.HrSchema()))
         .query("select * from table(\"s\".\"foo\"(3))")
-        .returns("empid=2; deptno=10; name=Ab; salary=0.0; commission=null\n"
-            + "empid=4; deptno=10; name=Abd; salary=0.0; commission=null\n");
+        .returns(
+            "empid=2; deptno=10; name=Ab; salary=0.0; commission=null\n"
+                + "empid=4; deptno=10; name=Abd; salary=0.0; commission=null\n");
   }
 
   /** Table with single field as Integer[] */


Mime
View raw message