org.apache.calcite.schema.impl.AggregateFunctionImpl Java Examples

The following examples show how to use org.apache.calcite.schema.impl.AggregateFunctionImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StreamlineSqlImpl.java    From streamline with Apache License 2.0 6 votes vote down vote up
private void handleCreateFunction(SqlCreateFunction sqlCreateFunction) throws ClassNotFoundException {
  if(sqlCreateFunction.jarName() != null) {
    throw new UnsupportedOperationException("UDF 'USING JAR' not implemented");
  }
  Method method;
  Function function;
  if ((method=findMethod(sqlCreateFunction.className(), "evaluate")) != null) {
    function = ScalarFunctionImpl.create(method);
  } else if (findMethod(sqlCreateFunction.className(), "add") != null) {
    function = AggregateFunctionImpl.create(Class.forName(sqlCreateFunction.className()));
  } else {
    throw new RuntimeException("Invalid scalar or aggregate function");
  }
  schema.add(sqlCreateFunction.functionName().toUpperCase(), function);
  hasUdf = true;
}
 
Example #2
Source File: RelNodeCompiler.java    From streamline with Apache License 2.0 6 votes vote down vote up
private String aggregateResult(AggregateCall call, PrintWriter pw) {
  SqlAggFunction aggFunction = call.getAggregation();
  String aggregationName = call.getAggregation().getName();
  Type ty = typeFactory.getJavaClass(call.getType());
  String result;
  if (aggFunction instanceof SqlUserDefinedAggFunction) {
    AggregateFunction aggregateFunction = ((SqlUserDefinedAggFunction) aggFunction).function;
    result = doAggregateResult((AggregateFunctionImpl) aggregateFunction, reserveAggVarName(call), ty, pw);
  } else {
    List<BuiltinAggregateFunctions.TypeClass> typeClasses = BuiltinAggregateFunctions.TABLE.get(aggregationName);
    if (typeClasses == null) {
      throw new UnsupportedOperationException(aggregationName + " Not implemented");
    }
    result = doAggregateResult(AggregateFunctionImpl.create(findMatchingClass(aggregationName, typeClasses, ty)),
                               reserveAggVarName(call), ty, pw);
  }
  return result;
}
 
Example #3
Source File: RelNodeCompiler.java    From streamline with Apache License 2.0 6 votes vote down vote up
private String doAggregateResult(AggregateFunctionImpl aggFn, String varName, Type ty, PrintWriter pw) {
  String resultName = varName + "_result";
  Class<?> accumulatorType = aggFn.accumulatorType;
  Class<?> resultType = aggFn.resultType;
  List<String> args = new ArrayList<>();
  if (!aggFn.isStatic) {
    String aggObjName = String.format("%s_obj", varName);
    String aggObjClassName = aggFn.initMethod.getDeclaringClass().getCanonicalName();
    pw.println("          @SuppressWarnings(\"unchecked\")");
    pw.println(String.format("          final %1$s %2$s = (%1$s) accumulators.get(\"%2$s\");", aggObjClassName,
            aggObjName));
    args.add(aggObjName);
  }
  args.add(String.format("(%s)accumulators.get(\"%s\")", accumulatorType.getCanonicalName(), varName));
  pw.println(String.format("          final %s %s = %s;", resultType.getCanonicalName(),
                           resultName, printMethodCall(aggFn.resultMethod, args)));

  return resultName;
}
 
Example #4
Source File: RelNodeCompiler.java    From streamline with Apache License 2.0 6 votes vote down vote up
private void aggregate(AggregateCall call) {
  SqlAggFunction aggFunction = call.getAggregation();
  String aggregationName = call.getAggregation().getName();
  Type ty = typeFactory.getJavaClass(call.getType());
  if (call.getArgList().size() != 1) {
    if (aggregationName.equals("COUNT")) {
      if (call.getArgList().size() != 0) {
        throw new UnsupportedOperationException("Count with nullable fields");
      }
    }
  }
  if (aggFunction instanceof SqlUserDefinedAggFunction) {
    AggregateFunction aggregateFunction = ((SqlUserDefinedAggFunction) aggFunction).function;
    doAggregate((AggregateFunctionImpl) aggregateFunction, reserveAggVarName(call), ty, call.getArgList());
  } else {
    List<BuiltinAggregateFunctions.TypeClass> typeClasses = BuiltinAggregateFunctions.TABLE.get(aggregationName);
    if (typeClasses == null) {
      throw new UnsupportedOperationException(aggregationName + " Not implemented");
    }
    doAggregate(AggregateFunctionImpl.create(findMatchingClass(aggregationName, typeClasses, ty)),
                reserveAggVarName(call), ty, call.getArgList());
  }
}
 
Example #5
Source File: AggregateNode.java    From calcite with Apache License 2.0 6 votes vote down vote up
UdaAccumulatorFactory(AggregateFunctionImpl aggFunction,
    AggregateCall call, boolean nullIfEmpty) {
  this.aggFunction = aggFunction;
  if (call.getArgList().size() != 1) {
    throw new UnsupportedOperationException("in current implementation, "
        + "aggregate must have precisely one argument");
  }
  argOrdinal = call.getArgList().get(0);
  if (aggFunction.isStatic) {
    instance = null;
  } else {
    try {
      final Constructor<?> constructor =
          aggFunction.declaringClass.getConstructor();
      instance = constructor.newInstance();
    } catch (InstantiationException | IllegalAccessException
        | NoSuchMethodException | InvocationTargetException e) {
      throw new RuntimeException(e);
    }
  }
  this.nullIfEmpty = nullIfEmpty;
}
 
Example #6
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
SqlAggFunction createCustomAggFunction(String funcName, RelDataType returnType, Class<?> customAggFuncClz) {
    RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
    SqlIdentifier sqlIdentifier = new SqlIdentifier(funcName, new SqlParserPos(1, 1));
    AggregateFunction aggFunction = AggregateFunctionImpl.create(customAggFuncClz);
    List<RelDataType> argTypes = new ArrayList<RelDataType>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
    for (FunctionParameter o : aggFunction.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    return new SqlUserDefinedAggFunction(sqlIdentifier, ReturnTypes.explicit(returnType),
            InferTypes.explicit(argTypes), OperandTypes.family(typeFamilies), aggFunction, false, false,
            typeFactory);
}
 
Example #7
Source File: RelNodeCompiler.java    From streamline with Apache License 2.0 5 votes vote down vote up
private void doAggregate(AggregateFunctionImpl aggFn, String varName, Type ty, List<Integer> argList) {
  List<String> args = new ArrayList<>();
  Class<?> accumulatorType = aggFn.accumulatorType;
  if (!aggFn.isStatic) {
    String aggObjName = String.format("%s_obj", varName);
    String aggObjClassName = aggFn.initMethod.getDeclaringClass().getCanonicalName();
    pw.println(String.format("          if (!accumulators.containsKey(\"%s\")) { ", aggObjName));
    pw.println(String.format("            accumulators.put(\"%s\", new %s());", aggObjName, aggObjClassName));
    pw.println("          }");
    pw.println("          @SuppressWarnings(\"unchecked\")");
    pw.println(String.format("          final %1$s %2$s = (%1$s) accumulators.get(\"%2$s\");", aggObjClassName,
            aggObjName));
    args.add(aggObjName);
  }
  args.add(String.format("%1$s == null ? %2$s : (%3$s) %1$s",
                         "accumulators.get(\"" + varName + "\")",
                         printMethodCall(aggFn.initMethod, args),
                         accumulatorType.getCanonicalName()));
  if (argList.isEmpty()) {
    args.add("EMPTY_VALUES");
  } else {
    for (int i = 0; i < aggFn.valueTypes.size(); i++) {
      args.add(String.format("(%s) %s", aggFn.valueTypes.get(i).getCanonicalName(), "_data.get(" + argList.get(i) + ")"));
    }
  }
  pw.print(String.format("          accumulators.put(\"%s\", %s);\n",
                         varName,
                         printMethodCall(aggFn.addMethod, args)));
}
 
Example #8
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
SqlAggFunction createCustomAggFunction(String funcName, RelDataType returnType, Class<?> customAggFuncClz) {
    RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
    SqlIdentifier sqlIdentifier = new SqlIdentifier(funcName, new SqlParserPos(1, 1));
    AggregateFunction aggFunction = AggregateFunctionImpl.create(customAggFuncClz);
    List<RelDataType> argTypes = new ArrayList<RelDataType>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
    for (FunctionParameter o : aggFunction.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    return new SqlUserDefinedAggFunction(sqlIdentifier, ReturnTypes.explicit(returnType),
            InferTypes.explicit(argTypes), OperandTypes.family(typeFamilies), aggFunction, false, false,
            typeFactory);
}
 
Example #9
Source File: RexImpTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public UserDefinedAggReflectiveImplementor(AggregateFunctionImpl afi) {
  this.afi = afi;
}