Search in sources :

Example 1 with AggregateFunction

use of org.apache.calcite.schema.AggregateFunction in project storm by apache.

the class RelNodeCompiler method aggregate.

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());
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 2 with AggregateFunction

use of org.apache.calcite.schema.AggregateFunction in project calcite by apache.

the class ModelHandler method addFunctions.

/**
 * Creates and validates a {@link ScalarFunctionImpl}, and adds it to a
 * schema. If {@code methodName} is "*", may add more than one function.
 *
 * @param schema Schema to add to
 * @param functionName Name of function; null to derived from method name
 * @param path Path to look for functions
 * @param className Class to inspect for methods that may be user-defined
 *                  functions
 * @param methodName Method name;
 *                  null means use the class as a UDF;
 *                  "*" means add all methods
 * @param upCase Whether to convert method names to upper case, so that they
 *               can be called without using quotes
 */
public static void addFunctions(SchemaPlus schema, String functionName, List<String> path, String className, String methodName, boolean upCase) {
    final Class<?> clazz;
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("UDF class '" + className + "' not found");
    }
    final TableFunction tableFunction = TableFunctionImpl.create(clazz, Util.first(methodName, "eval"));
    if (tableFunction != null) {
        schema.add(functionName, tableFunction);
        return;
    }
    // Must look for TableMacro before ScalarFunction. Both have an "eval"
    // method.
    final TableMacro macro = TableMacroImpl.create(clazz);
    if (macro != null) {
        schema.add(functionName, macro);
        return;
    }
    if (methodName != null && methodName.equals("*")) {
        for (Map.Entry<String, ScalarFunction> entry : ScalarFunctionImpl.createAll(clazz).entries()) {
            String name = entry.getKey();
            if (upCase) {
                name = name.toUpperCase(Locale.ROOT);
            }
            schema.add(name, entry.getValue());
        }
        return;
    } else {
        final ScalarFunction function = ScalarFunctionImpl.create(clazz, Util.first(methodName, "eval"));
        if (function != null) {
            final String name;
            if (functionName != null) {
                name = functionName;
            } else if (upCase) {
                name = methodName.toUpperCase(Locale.ROOT);
            } else {
                name = methodName;
            }
            schema.add(name, function);
            return;
        }
    }
    if (methodName == null) {
        final AggregateFunction aggFunction = AggregateFunctionImpl.create(clazz);
        if (aggFunction != null) {
            schema.add(functionName, aggFunction);
            return;
        }
    }
    throw new RuntimeException("Not a valid function class: " + clazz + ". Scalar functions and table macros have an 'eval' method; " + "aggregate functions have 'init' and 'add' methods, and optionally " + "'initAdd', 'merge' and 'result' methods.");
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) ScalarFunction(org.apache.calcite.schema.ScalarFunction) AggregateFunction(org.apache.calcite.schema.AggregateFunction) TableFunction(org.apache.calcite.schema.TableFunction) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with AggregateFunction

use of org.apache.calcite.schema.AggregateFunction in project storm by apache.

the class RelNodeCompiler method aggregateResult.

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;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) AggregateFunction(org.apache.calcite.schema.AggregateFunction) AggregateFunctionImpl(org.apache.calcite.schema.impl.AggregateFunctionImpl) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 4 with AggregateFunction

use of org.apache.calcite.schema.AggregateFunction in project streamline by hortonworks.

the class RelNodeCompiler method aggregate.

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());
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 5 with AggregateFunction

use of org.apache.calcite.schema.AggregateFunction in project streamline by hortonworks.

the class RelNodeCompiler method aggregateResult.

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;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) AggregateFunction(org.apache.calcite.schema.AggregateFunction) AggregateFunctionImpl(org.apache.calcite.schema.impl.AggregateFunctionImpl) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Aggregations

AggregateFunction (org.apache.calcite.schema.AggregateFunction)6 RelDataType (org.apache.calcite.rel.type.RelDataType)5 SqlUserDefinedAggFunction (org.apache.calcite.sql.validate.SqlUserDefinedAggFunction)5 Type (java.lang.reflect.Type)4 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)4 ScalarFunction (org.apache.calcite.schema.ScalarFunction)2 TableFunction (org.apache.calcite.schema.TableFunction)2 TableMacro (org.apache.calcite.schema.TableMacro)2 AggregateFunctionImpl (org.apache.calcite.schema.impl.AggregateFunctionImpl)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JavaTypeFactoryImpl (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl)1 RelDataTypeFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory)1 AggregateFunction (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.AggregateFunction)1 FunctionParameter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter)1 PredicateImpl (org.apache.calcite.runtime.PredicateImpl)1 FunctionParameter (org.apache.calcite.schema.FunctionParameter)1 FamilyOperandTypeChecker (org.apache.calcite.sql.type.FamilyOperandTypeChecker)1 SqlTypeFamily (org.apache.calcite.sql.type.SqlTypeFamily)1