Search in sources :

Example 6 with ScalarFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction in project calcite by apache.

the class ScalarFunctionImpl method createAll.

/**
 * Creates {@link org.apache.calcite.schema.ScalarFunction} for each method in
 * a given class.
 */
public static ImmutableMultimap<String, ScalarFunction> createAll(Class<?> clazz) {
    final ImmutableMultimap.Builder<String, ScalarFunction> builder = ImmutableMultimap.builder();
    for (Method method : clazz.getMethods()) {
        if (method.getDeclaringClass() == Object.class) {
            continue;
        }
        if (!Modifier.isStatic(method.getModifiers()) && !classHasPublicZeroArgsConstructor(clazz)) {
            continue;
        }
        final ScalarFunction function = create(method);
        builder.put(method.getName(), function);
    }
    return builder.build();
}
Also used : ScalarFunction(org.apache.calcite.schema.ScalarFunction) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Method(java.lang.reflect.Method)

Example 7 with ScalarFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction in project calcite by apache.

the class CalciteCatalogReader method toOp.

/**
 * Converts a function to a {@link org.apache.calcite.sql.SqlOperator}.
 *
 * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082]
 * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction
 * constructor.
 */
private static SqlOperator toOp(RelDataTypeFactory typeFactory, SqlIdentifier name, final Function function) {
    List<RelDataType> argTypes = new ArrayList<>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<>();
    for (FunctionParameter o : function.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    final Predicate<Integer> optional = new PredicateImpl<Integer>() {

        public boolean test(Integer input) {
            return function.getParameters().get(input).isOptional();
        }
    };
    final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, optional);
    final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
    if (function instanceof ScalarFunction) {
        return new SqlUserDefinedFunction(name, infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
    } else if (function instanceof AggregateFunction) {
        return new SqlUserDefinedAggFunction(name, infer((AggregateFunction) function), InferTypes.explicit(argTypes), typeChecker, (AggregateFunction) function, false, false, typeFactory);
    } else if (function instanceof TableMacro) {
        return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableMacro) function);
    } else if (function instanceof TableFunction) {
        return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableFunction) function);
    } else {
        throw new AssertionError("unknown function type " + function);
    }
}
Also used : SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) TableMacro(org.apache.calcite.schema.TableMacro) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) ArrayList(java.util.ArrayList) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) FamilyOperandTypeChecker(org.apache.calcite.sql.type.FamilyOperandTypeChecker) SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) ScalarFunction(org.apache.calcite.schema.ScalarFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) FunctionParameter(org.apache.calcite.schema.FunctionParameter)

Aggregations

ScalarFunction (org.apache.calcite.schema.ScalarFunction)4 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 FunctionParameter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter)2 SqlUserDefinedFunction (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction)2 AggregateFunction (org.apache.calcite.schema.AggregateFunction)2 TableFunction (org.apache.calcite.schema.TableFunction)2 TableMacro (org.apache.calcite.schema.TableMacro)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Function (com.google.zetasql.Function)1 FunctionArgumentType (com.google.zetasql.FunctionArgumentType)1 FunctionSignature (com.google.zetasql.FunctionSignature)1 TableValuedFunction (com.google.zetasql.TableValuedFunction)1 Map (java.util.Map)1 ScalarFunctionImpl (org.apache.beam.sdk.extensions.sql.impl.ScalarFunctionImpl)1 UdafImpl (org.apache.beam.sdk.extensions.sql.impl.UdafImpl)1 ZetaSqlScalarFunctionImpl (org.apache.beam.sdk.extensions.sql.zetasql.translation.ZetaSqlScalarFunctionImpl)1 RexImpTable (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.RexImpTable)1 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)1