Search in sources :

Example 1 with ScalarFunction

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

the class SQLExecEnvironment method registerFunction.

/**
 * Register custom function from given static method with this {@link SQLExecEnvironment}
 *
 * @param name Name of the scalar SQL function that needs make available to SQL Statement
 * @param clazz {@link Class} which contains given static method
 * @param methodName Name of the method from given clazz
 *
 * @return Return this {@link SQLExecEnvironment}
 */
public SQLExecEnvironment registerFunction(String name, Class clazz, String methodName) {
    Preconditions.checkNotNull(name, "Function name cannot be null");
    ScalarFunction scalarFunction = ScalarFunctionImpl.create(clazz, methodName);
    return registerFunction(name, scalarFunction);
}
Also used : ScalarFunction(org.apache.calcite.schema.ScalarFunction)

Example 2 with ScalarFunction

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction 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 ScalarFunction

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

the class SqlOperators method createUdfOperator.

private static SqlUserDefinedFunction createUdfOperator(String name, Method method, final SqlSyntax syntax, String funGroup, String jarPath) {
    Function function = ZetaSqlScalarFunctionImpl.create(method, funGroup, jarPath);
    final RelDataTypeFactory typeFactory = createTypeFactory();
    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 FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, i -> function.getParameters().get(i).isOptional());
    final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
    return new SqlUserDefinedFunction(new SqlIdentifier(name, SqlParserPos.ZERO), infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function) {

        @Override
        public SqlSyntax getSyntax() {
            return syntax;
        }
    };
}
Also used : FamilyOperandTypeChecker(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.FamilyOperandTypeChecker) SqlUserDefinedFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction) Function(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function) SqlFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction) AggregateFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.AggregateFunction) ScalarFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction) SqlUserDefinedAggFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlUserDefinedFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction) ScalarFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction) SqlTypeFamily(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) ArrayList(java.util.ArrayList) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter)

Example 4 with ScalarFunction

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

the class BeamCalcRelType method canImplement.

@Override
protected boolean canImplement(RexCall call) {
    final SqlOperator operator = call.getOperator();
    RexImpTable.RexCallImplementor implementor = RexImpTable.INSTANCE.get(operator);
    if (implementor == null) {
        // Reject methods with no implementation
        return false;
    }
    if (operator instanceof SqlUserDefinedFunction) {
        SqlUserDefinedFunction udf = (SqlUserDefinedFunction) call.op;
        if (udf.function instanceof ZetaSqlScalarFunctionImpl) {
            ZetaSqlScalarFunctionImpl scalarFunction = (ZetaSqlScalarFunctionImpl) udf.function;
            if (!scalarFunction.functionGroup.equals(BeamZetaSqlCatalog.USER_DEFINED_JAVA_SCALAR_FUNCTIONS)) {
                // Reject ZetaSQL Builtin Scalar Functions
                return false;
            }
            for (RexNode operand : call.getOperands()) {
                if (operand instanceof RexLocalRef) {
                    if (!supportsType(operand.getType())) {
                        LOG.error("User-defined function {} received unsupported operand type {}.", call.op.getName(), ((RexLocalRef) operand).getType());
                        return false;
                    }
                } else {
                    LOG.error("User-defined function {} received unrecognized operand kind {}.", call.op.getName(), operand.getKind());
                    return false;
                }
            }
        } else {
            // Reject other UDFs
            return false;
        }
    } else {
        // Reject Calcite implementations
        return false;
    }
    return true;
}
Also used : SqlUserDefinedFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction) ZetaSqlScalarFunctionImpl(org.apache.beam.sdk.extensions.sql.zetasql.translation.ZetaSqlScalarFunctionImpl) SqlOperator(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator) RexLocalRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLocalRef) RexImpTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.RexImpTable) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 5 with ScalarFunction

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

the class BeamZetaSqlCatalog method addUdfsFromSchema.

private void addUdfsFromSchema() {
    for (String functionName : calciteSchema.getFunctionNames()) {
        Collection<org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function> functions = calciteSchema.getFunctions(functionName);
        if (functions.size() != 1) {
            throw new IllegalArgumentException(String.format("Expected exactly 1 definition for function '%s', but found %d." + " Beam ZetaSQL supports only a single function definition per function name (BEAM-12073).", functionName, functions.size()));
        }
        for (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function function : functions) {
            List<String> path = Arrays.asList(functionName.split("\\."));
            if (function instanceof ScalarFunctionImpl) {
                ScalarFunctionImpl scalarFunction = (ScalarFunctionImpl) function;
                // for unsupported types.
                for (FunctionParameter parameter : scalarFunction.getParameters()) {
                    validateJavaUdfCalciteType(parameter.getType(typeFactory), functionName);
                }
                validateJavaUdfCalciteType(scalarFunction.getReturnType(typeFactory), functionName);
                Method method = scalarFunction.method;
                javaScalarUdfs.put(path, UserFunctionDefinitions.JavaScalarFunction.create(method, ""));
                FunctionArgumentType resultType = new FunctionArgumentType(ZetaSqlCalciteTranslationUtils.toZetaSqlType(scalarFunction.getReturnType(typeFactory)));
                FunctionSignature functionSignature = new FunctionSignature(resultType, getArgumentTypes(scalarFunction), 0L);
                zetaSqlCatalog.addFunction(new Function(path, USER_DEFINED_JAVA_SCALAR_FUNCTIONS, ZetaSQLFunctions.FunctionEnums.Mode.SCALAR, ImmutableList.of(functionSignature)));
            } else if (function instanceof UdafImpl) {
                UdafImpl<?, ?, ?> udaf = (UdafImpl) function;
                javaUdafs.put(path, udaf.getCombineFn());
                FunctionArgumentType resultType = new FunctionArgumentType(ZetaSqlCalciteTranslationUtils.toZetaSqlType(udaf.getReturnType(typeFactory)));
                FunctionSignature functionSignature = new FunctionSignature(resultType, getArgumentTypes(udaf), 0L);
                zetaSqlCatalog.addFunction(new Function(path, USER_DEFINED_JAVA_AGGREGATE_FUNCTIONS, ZetaSQLFunctions.FunctionEnums.Mode.AGGREGATE, ImmutableList.of(functionSignature)));
            } else {
                throw new IllegalArgumentException(String.format("Function %s has unrecognized implementation type %s.", functionName, function.getClass().getName()));
            }
        }
    }
}
Also used : ScalarFunctionImpl(org.apache.beam.sdk.extensions.sql.impl.ScalarFunctionImpl) FunctionArgumentType(com.google.zetasql.FunctionArgumentType) Method(java.lang.reflect.Method) FunctionSignature(com.google.zetasql.FunctionSignature) UdafImpl(org.apache.beam.sdk.extensions.sql.impl.UdafImpl) TableValuedFunction(com.google.zetasql.TableValuedFunction) Function(com.google.zetasql.Function) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.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