You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by lidong <li...@apache.org> on 2016/05/19 08:10:58 UTC

Error for custom agg function in generated java code

Hello guys,


This is Dong from Apache Kylin community.
I’m implementing an aggregation function according to:http://calcite.apache.org/docs/reference.html#user-defined-functions
The agg function has 2 args, one for column, the other one for constant, just like percentile_cont(sales, 0.5). So I put 2 Object args in the add method.


Here’s the structure of my agg function class:
public class CustomerAggFunc {
 public static CustomerAggCounter init() {
   return null;
  }
 public static CustomerAggCounter add(CustomerAggCounter counter, Object v, Object r) {
   CustomerAggCounter c = (CustomerAggCounter) v;
   if (counter == null) {
     return new CustomerAggCounter((double)r);
    } else {
      counter.merge(c);
     return counter;
    }
  }
 public static CustomerAggCounter merge(CustomerAggCounter counter0, CustomerAggCounter counter1) {    counter0.merge(counter1);
    return counter0;
  }
 public static double result(CustomerAggCounter counter) {
    return counter == null ? 0: counter.getResult();
  }
}


Then I useorg.apache.calcite.schema.impl.AggregateFunctionImpl.create(CustomerAggFunc.class) method to create register my implementation.


But the generated java code still calls the add() method with one object arg, likeadd(CustomerAggCounter counter, Object v).
/* 69 */  return child.groupBy(new org.apache.calcite.linq4j.function.Function1() {
/* 70 */    public Long apply(Object[] a0) {
/* 71 */     return (Long) a0[0];
/* 72 */    }
/* 73 */    public Object apply(Object a0) {
/* 74 */     return apply(
/* 75 */      (Object[]) a0);
/* 76 */    }
/* 77 */   }
/* 78 */   , new org.apache.calcite.linq4j.function.Function0() {
/* 79 */    public Object apply() {
/* 80 */    org.apache.kylin.measure.CustomAggCounter PERCENTILEa0s0;
/* 81 */     PERCENTILEa0s0 =org.apache.kylin.measure.CustomAggFunc.init();
/* 82 */     return new Record1_0(
/* 83 */       PERCENTILEa0s0);
/* 84 */    }
/* 85 */   }
/* 86 */   , new org.apache.calcite.linq4j.function.Function2() {
/* 87 */    public Record1_0 apply(Record1_0 acc, Object[] in) {
/* 88 */     final Object inp3_ = in[3];
/* 89 */     if (inp3_ != null) {
/* 90 */      acc.f0 =org.apache.kylin.measure.CustomAggFunc.add(acc.f0, inp3_);
/* 91 */     }
/* 92 */     return acc;
/* 93 */    }
/* 94 */    public Record1_0 apply(Object acc, Object in) {
/* 95 */     return apply(
/* 96 */      (Record1_0) acc,
/* 97 */      (Object[]) in);
/* 98 */    }
/* 99 */   }
/* 100 */   , new org.apache.calcite.linq4j.function.Function2() {
/* 101 */    public Object[] apply(Long key, Record1_0 acc) {
/* 102 */     return new Object[] {
/* 103 */       key,
/* 104 */       org.apache.kylin.measure.CustomAggFunc.result(acc.f0)};
/* 105 */    }
/* 106 */    public Object[] apply(Object key, Object acc) {
/* 107 */     return apply(
/* 108 */      (Long) key,
/* 109 */      (Record1_0) acc);
/* 110 */    }
/* 111 */   }
/* 112 */   );
/* 113 */ }


Then during compiling stage, there will be exceptions:
Caused by: org.codehaus.commons.compiler.CompileException: Line 90, Column 78: No applicable constructor/method found for actual parameters "org.apache.kylin.measure.CustomAggCounter, java.lang.Object"; candidates are: "public staticorg.apache.kylin.measure.CustomAggCounterorg.apache.kylin.measure.CustomAggFuncadd(org.apache.kylin.measure.CustomAggCounter, java.lang.Object, java.lang.Object)”


Thanks,
Dong