Search in sources :

Example 21 with Function

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

the class BeamDDLTest method unparseScalarFunction.

@Test
public void unparseScalarFunction() {
    SqlIdentifier name = new SqlIdentifier("foo", SqlParserPos.ZERO);
    SqlNode jarPath = SqlLiteral.createCharString("path/to/udf.jar", SqlParserPos.ZERO);
    SqlCreateFunction createFunction = new SqlCreateFunction(SqlParserPos.ZERO, false, name, jarPath, false);
    SqlWriter sqlWriter = new SqlPrettyWriter(BeamBigQuerySqlDialect.DEFAULT);
    createFunction.unparse(sqlWriter, 0, 0);
    assertEquals("CREATE FUNCTION foo USING JAR 'path/to/udf.jar'", sqlWriter.toSqlString().getSql());
}
Also used : SqlWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter) SqlPrettyWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 22 with Function

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

Example 23 with Function

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

the class BeamZetaSqlCatalog method addFunction.

void addFunction(ResolvedNodes.ResolvedCreateFunctionStmt createFunctionStmt) {
    String functionGroup = getFunctionGroup(createFunctionStmt);
    switch(functionGroup) {
        case USER_DEFINED_SQL_FUNCTIONS:
            sqlScalarUdfs.put(createFunctionStmt.getNamePath(), createFunctionStmt);
            break;
        case USER_DEFINED_JAVA_SCALAR_FUNCTIONS:
            String functionName = String.join(".", createFunctionStmt.getNamePath());
            for (FunctionArgumentType argumentType : createFunctionStmt.getSignature().getFunctionArgumentList()) {
                Type type = argumentType.getType();
                if (type == null) {
                    throw new UnsupportedOperationException("UDF templated argument types are not supported.");
                }
                validateJavaUdfZetaSqlType(type, functionName);
            }
            if (createFunctionStmt.getReturnType() == null) {
                throw new IllegalArgumentException("UDF return type must not be null.");
            }
            validateJavaUdfZetaSqlType(createFunctionStmt.getReturnType(), functionName);
            String jarPath = getJarPath(createFunctionStmt);
            ScalarFn scalarFn = javaUdfLoader.loadScalarFunction(createFunctionStmt.getNamePath(), jarPath);
            Method method = ScalarFnReflector.getApplyMethod(scalarFn);
            javaScalarUdfs.put(createFunctionStmt.getNamePath(), UserFunctionDefinitions.JavaScalarFunction.create(method, jarPath));
            break;
        case USER_DEFINED_JAVA_AGGREGATE_FUNCTIONS:
            jarPath = getJarPath(createFunctionStmt);
            // Try loading the aggregate function just to make sure it exists. LazyAggregateCombineFn
            // will need to fetch it again at runtime.
            javaUdfLoader.loadAggregateFunction(createFunctionStmt.getNamePath(), jarPath);
            Combine.CombineFn<?, ?, ?> combineFn = new LazyAggregateCombineFn<>(createFunctionStmt.getNamePath(), jarPath);
            javaUdafs.put(createFunctionStmt.getNamePath(), combineFn);
            break;
        default:
            throw new IllegalArgumentException(String.format("Encountered unrecognized function group %s.", functionGroup));
    }
    zetaSqlCatalog.addFunction(new Function(createFunctionStmt.getNamePath(), functionGroup, createFunctionStmt.getIsAggregate() ? ZetaSQLFunctions.FunctionEnums.Mode.AGGREGATE : ZetaSQLFunctions.FunctionEnums.Mode.SCALAR, ImmutableList.of(createFunctionStmt.getSignature())));
}
Also used : TableValuedFunction(com.google.zetasql.TableValuedFunction) Function(com.google.zetasql.Function) FunctionArgumentType(com.google.zetasql.FunctionArgumentType) Type(com.google.zetasql.Type) ZetaSQLType(com.google.zetasql.ZetaSQLType) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) ScalarFn(org.apache.beam.sdk.extensions.sql.udf.ScalarFn) Combine(org.apache.beam.sdk.transforms.Combine) FunctionArgumentType(com.google.zetasql.FunctionArgumentType) LazyAggregateCombineFn(org.apache.beam.sdk.extensions.sql.impl.LazyAggregateCombineFn) Method(java.lang.reflect.Method)

Example 24 with Function

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

the class ScalarFunctionImpl method createAll.

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

Example 25 with Function

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

the class BeamTableFunctionScanRule method convert.

@Override
public RelNode convert(RelNode relNode) {
    TableFunctionScan tableFunctionScan = (TableFunctionScan) relNode;
    // only support one input for table function scan.
    List<RelNode> inputs = new ArrayList<>();
    checkArgument(relNode.getInputs().size() == 1, "Wrong number of inputs for %s, expected 1 input but received: %s", BeamTableFunctionScanRel.class.getSimpleName(), relNode.getInputs().size());
    inputs.add(convert(relNode.getInput(0), relNode.getInput(0).getTraitSet().replace(BeamLogicalConvention.INSTANCE)));
    return new BeamTableFunctionScanRel(tableFunctionScan.getCluster(), tableFunctionScan.getTraitSet().replace(BeamLogicalConvention.INSTANCE), inputs, tableFunctionScan.getCall(), null, tableFunctionScan.getCall().getType(), Collections.EMPTY_SET);
}
Also used : BeamTableFunctionScanRel(org.apache.beam.sdk.extensions.sql.impl.rel.BeamTableFunctionScanRel) LogicalTableFunctionScan(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalTableFunctionScan) TableFunctionScan(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.TableFunctionScan) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList)

Aggregations

Function (org.apache.calcite.schema.Function)13 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)12 Method (java.lang.reflect.Method)11 ArrayList (java.util.ArrayList)11 SqlOperator (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator)6 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)4 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)4 TableMacro (org.apache.calcite.schema.TableMacro)4 Test (org.junit.Test)4 ResolvedExpr (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedExpr)3 ResolvedLiteral (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedLiteral)3 JdbcConnection (org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)3 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)3 Table (org.apache.calcite.schema.Table)3 Function (com.google.zetasql.Function)2 FunctionArgumentType (com.google.zetasql.FunctionArgumentType)2 FunctionSignature (com.google.zetasql.FunctionSignature)2 TableValuedFunction (com.google.zetasql.TableValuedFunction)2 Type (com.google.zetasql.Type)2 ResolvedAggregateFunctionCall (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAggregateFunctionCall)2