[ 
https://issues.apache.org/jira/browse/CALCITE-580?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14313814#comment-14313814
 ] 

Vladimir Sitnikov commented on CALCITE-580:
-------------------------------------------

[~julianhyde], to observe the issue, you need to call {{resultSet.getInt(..)}}.

Here's sample (CalciteAssert.assertArrayEqual is from 
https://github.com/vlsi/incubator-calcite/commit/9103ab012e84eb76c0296710ac35667934f9191a):
 
{code:java}
  @Test public void testAvgInt() throws Exception {
    final CalciteAssert.AssertThat with =
        CalciteAssert.that().withSchema("s", CATCHALL).with(Lex.JAVA);
    with
        .query("select primitiveLong, avg(primitiveInt)"
            + " from s.everyTypes group by primitiveLong order by 
primitiveLong")
        .returns(new Function<ResultSet, Void>() {
          public Void apply(ResultSet input) {
            ArrayList<Integer> res = new ArrayList<Integer>();
            try {
              while(input.next()) {
                res.add(input.getInt(2));
              }
            } catch (SQLException e) {
              throw new AssertionError(
                  "Unable to process results: " + e.getMessage(), e);
            }
            CalciteAssert.assertArrayEqual(
                "result", res.toArray(), new Integer[]{0, 2147483647});
            return null;
          }
        });
  }
{code}
{noformat}
/*   1 */ public static class Record2_0 implements java.io.Serializable {
/*   2 */   public int f0;
/*   3 */   public long f1;
/*   4 */   public Record2_0(int f0, long f1) {
/*   5 */     this.f0 = f0;
/*   6 */     this.f1 = f1;
/*   7 */   }
/*   8 */ 
/*   9 */   public boolean equals(Object o) {
/*  10 */     if (this == o) {
/*  11 */       return true;
/*  12 */     }
/*  13 */     if (!(o instanceof Record2_0)) {
/*  14 */       return false;
/*  15 */     }
/*  16 */     return this.f0 == ((Record2_0) o).f0 && this.f1 == ((Record2_0) 
o).f1;
/*  17 */   }
/*  18 */ 
/*  19 */   public int hashCode() {
/*  20 */     int h = 0;
/*  21 */     h = org.apache.calcite.runtime.Utilities.hash(h, this.f0);
/*  22 */     h = org.apache.calcite.runtime.Utilities.hash(h, this.f1);
/*  23 */     return h;
/*  24 */   }
/*  25 */ 
/*  26 */   public int compareTo(Record2_0 that) {
/*  27 */     int c;
/*  28 */     c = org.apache.calcite.runtime.Utilities.compare(this.f0, 
that.f0);
/*  29 */     if (c != 0) {
/*  30 */       return c;
/*  31 */     }
/*  32 */     c = org.apache.calcite.runtime.Utilities.compare(this.f1, 
that.f1);
/*  33 */     if (c != 0) {
/*  34 */       return c;
/*  35 */     }
/*  36 */     return 0;
/*  37 */   }
/*  38 */ 
/*  39 */   public String toString() {
/*  40 */     return "{f0=" + this.f0 + ", f1=" + this.f1 + "}";
/*  41 */   }
/*  42 */ 
/*  43 */ }
/*  44 */ 
/*  45 */ org.apache.calcite.DataContext root;
/*  46 */ 
/*  47 */ public org.apache.calcite.linq4j.Enumerable bind(final 
org.apache.calcite.DataContext root0) {
/*  48 */   root = root0;
/*  49 */   final org.apache.calcite.linq4j.Enumerable _inputEnumerable = 
org.apache.calcite.linq4j.Linq4j.asEnumerable(((org.apache.calcite.test.ReflectiveSchemaTest.CatchallSchema)
 ((org.apache.calcite.adapter.java.ReflectiveSchema) 
root.getRootSchema().getSubSchema("s").unwrap(org.apache.calcite.adapter.java.ReflectiveSchema.class)).getTarget()).everyTypes);
/*  50 */   final org.apache.calcite.linq4j.AbstractEnumerable child = new 
org.apache.calcite.linq4j.AbstractEnumerable(){
/*  51 */     public org.apache.calcite.linq4j.Enumerator enumerator() {
/*  52 */       return new org.apache.calcite.linq4j.Enumerator(){
/*  53 */           public final org.apache.calcite.linq4j.Enumerator 
inputEnumerator = _inputEnumerable.enumerator();
/*  54 */           public void reset() {
/*  55 */             inputEnumerator.reset();
/*  56 */           }
/*  57 */ 
/*  58 */           public boolean moveNext() {
/*  59 */             return inputEnumerator.moveNext();
/*  60 */           }
/*  61 */ 
/*  62 */           public void close() {
/*  63 */             inputEnumerator.close();
/*  64 */           }
/*  65 */ 
/*  66 */           public Object current() {
/*  67 */             final 
org.apache.calcite.test.ReflectiveSchemaTest.EveryType current = 
(org.apache.calcite.test.ReflectiveSchemaTest.EveryType) 
inputEnumerator.current();
/*  68 */             return new Object[] {
/*  69 */                 current.primitiveLong,
/*  70 */                 current.primitiveInt};
/*  71 */           }
/*  72 */ 
/*  73 */         };
/*  74 */     }
/*  75 */ 
/*  76 */   };
/*  77 */   final org.apache.calcite.linq4j.Enumerable _inputEnumerable0 = 
child.groupBy(new org.apache.calcite.linq4j.function.Function1() {
/*  78 */     public long apply(Object[] a0) {
/*  79 */       return org.apache.calcite.runtime.SqlFunctions.toLong(a0[0]);
/*  80 */     }
/*  81 */     public Object apply(Object a0) {
/*  82 */       return apply(
/*  83 */         (Object[]) a0);
/*  84 */     }
/*  85 */   }
/*  86 */   , new org.apache.calcite.linq4j.function.Function0() {
/*  87 */     public Object apply() {
/*  88 */       int $SUM0a0s0;
/*  89 */       $SUM0a0s0 = 0;
/*  90 */       long COUNTa1s0;
/*  91 */       COUNTa1s0 = 0L;
/*  92 */       return new Record2_0(
/*  93 */           $SUM0a0s0,
/*  94 */           COUNTa1s0);
/*  95 */     }
/*  96 */   }
/*  97 */   , new org.apache.calcite.linq4j.function.Function2() {
/*  98 */     public Record2_0 apply(Record2_0 acc, Object[] in) {
/*  99 */       acc.f0 = acc.f0 + 
org.apache.calcite.runtime.SqlFunctions.toInt(in[1]);
/* 100 */       acc.f1++;
/* 101 */       return acc;
/* 102 */     }
/* 103 */     public Record2_0 apply(Object acc, Object in) {
/* 104 */       return apply(
/* 105 */         (Record2_0) acc,
/* 106 */         (Object[]) in);
/* 107 */     }
/* 108 */   }
/* 109 */   , new org.apache.calcite.linq4j.function.Function2() {
/* 110 */     public Object[] apply(long key, Record2_0 acc) {
/* 111 */       return new Object[] {
/* 112 */           key,
/* 113 */           acc.f0,
/* 114 */           acc.f1};
/* 115 */     }
/* 116 */     public Object[] apply(Long key, Record2_0 acc) {
/* 117 */       return apply(
/* 118 */         key.longValue(),
/* 119 */         acc);
/* 120 */     }
/* 121 */     public Object[] apply(Object key, Object acc) {
/* 122 */       return apply(
/* 123 */         (Long) key,
/* 124 */         (Record2_0) acc);
/* 125 */     }
/* 126 */   }
/* 127 */   );
/* 128 */   final org.apache.calcite.linq4j.AbstractEnumerable child0 = new 
org.apache.calcite.linq4j.AbstractEnumerable(){
/* 129 */     public org.apache.calcite.linq4j.Enumerator enumerator() {
/* 130 */       return new org.apache.calcite.linq4j.Enumerator(){
/* 131 */           public final org.apache.calcite.linq4j.Enumerator 
inputEnumerator = _inputEnumerable0.enumerator();
/* 132 */           public void reset() {
/* 133 */             inputEnumerator.reset();
/* 134 */           }
/* 135 */ 
/* 136 */           public boolean moveNext() {
/* 137 */             return inputEnumerator.moveNext();
/* 138 */           }
/* 139 */ 
/* 140 */           public void close() {
/* 141 */             inputEnumerator.close();
/* 142 */           }
/* 143 */ 
/* 144 */           public Object current() {
/* 145 */             final Object[] current = (Object[]) 
inputEnumerator.current();
/* 146 */             return new Object[] {
/* 147 */                 current[0],
/* 148 */                 (long) 
org.apache.calcite.runtime.SqlFunctions.toInt(current[1]) / 
org.apache.calcite.runtime.SqlFunctions.toLong(current[2])};
/* 149 */           }
/* 150 */ 
/* 151 */         };
/* 152 */     }
/* 153 */ 
/* 154 */   };
/* 155 */   return child0.orderBy(new 
org.apache.calcite.linq4j.function.Function1() {
/* 156 */       public long apply(Object[] v) {
/* 157 */         return org.apache.calcite.runtime.SqlFunctions.toLong(v[0]);
/* 158 */       }
/* 159 */       public Object apply(Object v) {
/* 160 */         return apply(
/* 161 */           (Object[]) v);
/* 162 */       }
/* 163 */     }
/* 164 */     , 
org.apache.calcite.linq4j.function.Functions.nullsComparator(false, false));
/* 165 */ }
/* 166 */ 
/* 167 */ 
/* 168 */ public Class getElementType() {
/* 169 */   return java.lang.Object[].class;
/* 170 */ }
/* 171 */ 
/* 172 */ {noformat}

{noformat}
java.lang.RuntimeException: exception while executing [select primitiveLong, 
avg(primitiveInt) from s.everyTypes group by primitiveLong order by 
primitiveLong]
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.returns(CalciteAssert.java:1148)
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.returns(CalciteAssert.java:1138)
        at 
org.apache.calcite.test.ReflectiveSchemaTest.testAvgInt(ReflectiveSchemaTest.java:338)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
        at 
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
        at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
        at 
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to 
java.lang.Integer
        at 
org.apache.calcite.avatica.util.AbstractCursor$IntAccessor.getInt(AbstractCursor.java:460)
        at 
org.apache.calcite.avatica.AvaticaResultSet.getInt(AvaticaResultSet.java:246)
        at 
org.apache.calcite.test.ReflectiveSchemaTest$1.apply(ReflectiveSchemaTest.java:343)
        at 
org.apache.calcite.test.ReflectiveSchemaTest$1.apply(ReflectiveSchemaTest.java:338)
        at 
org.apache.calcite.test.CalciteAssert.assertQuery(CalciteAssert.java:466)
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.returns(CalciteAssert.java:1144)
        ... 28 more{noformat}

> Average aggregation on an Integer column throw java.lang.ClassCastException
> ---------------------------------------------------------------------------
>
>                 Key: CALCITE-580
>                 URL: https://issues.apache.org/jira/browse/CALCITE-580
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Low Chin Wei
>            Assignee: Julian Hyde
>
> There are 2 Integer columns: a, c in table abc.
> While running the following sql, it throw the ClassCastException:
> select a, avg(c) as agg from abc group by a
> Exception in thread "main" java.lang.ClassCastException: java.lang.Long 
> incompatible with java.lang.Integer
>       at 
> org.apache.calcite.avatica.util.AbstractCursor$IntAccessor.getInt(AbstractCursor.java:460)
>       at 
> org.apache.calcite.avatica.AvaticaResultSet.getInt(AvaticaResultSet.java:311)
> The generated code that I think causing the issue, it try to cast the result 
> to long.
> public Object current() {
>   final Object[] current = (Object[]) inputEnumerator.current();
>   return new Object[] {
>   current[0],
>   (long) org.apache.calcite.runtime.SqlFunctions.toInt(current[1]) / 
> org.apache.calcite.runtime.SqlFunctions.toLong(current[2])};
> }



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to