Search in sources :

Example 1 with JavaUdfLoader

use of org.apache.beam.sdk.extensions.sql.impl.JavaUdfLoader in project beam by apache.

the class SqlCreateFunction method execute.

@Override
public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, functionName);
    SchemaPlus schema = pair.left.plus();
    String lastName = pair.right;
    if (!schema.getFunctions(lastName).isEmpty()) {
        throw SqlUtil.newContextException(functionName.getParserPosition(), RESOURCE.internal(String.format("Function %s is already defined.", lastName)));
    }
    JavaUdfLoader udfLoader = new JavaUdfLoader();
    // TODO(BEAM-12355) Support qualified function names.
    List<String> functionPath = ImmutableList.of(lastName);
    if (!(jarPath instanceof SqlCharStringLiteral)) {
        throw SqlUtil.newContextException(jarPath.getParserPosition(), RESOURCE.internal("Jar path is not instanceof SqlCharStringLiteral."));
    }
    String unquotedJarPath = ((SqlCharStringLiteral) jarPath).getNlsString().getValue();
    if (isAggregate) {
        // Try loading the aggregate function just to make sure it exists. LazyAggregateCombineFn will
        // need to fetch it again at runtime.
        udfLoader.loadAggregateFunction(functionPath, unquotedJarPath);
        LazyAggregateCombineFn<?, ?, ?> combineFn = new LazyAggregateCombineFn<>(functionPath, unquotedJarPath);
        schema.add(lastName, combineFn.getUdafImpl());
    } else {
        ScalarFn scalarFn = udfLoader.loadScalarFunction(functionPath, unquotedJarPath);
        Method method = ScalarFnReflector.getApplyMethod(scalarFn);
        Function function = ScalarFunctionImpl.create(method, unquotedJarPath);
        schema.add(lastName, function);
    }
}
Also used : Function(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function) ScalarFn(org.apache.beam.sdk.extensions.sql.udf.ScalarFn) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) LazyAggregateCombineFn(org.apache.beam.sdk.extensions.sql.impl.LazyAggregateCombineFn) JavaUdfLoader(org.apache.beam.sdk.extensions.sql.impl.JavaUdfLoader) SqlCharStringLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)1 JavaUdfLoader (org.apache.beam.sdk.extensions.sql.impl.JavaUdfLoader)1 LazyAggregateCombineFn (org.apache.beam.sdk.extensions.sql.impl.LazyAggregateCombineFn)1 ScalarFn (org.apache.beam.sdk.extensions.sql.udf.ScalarFn)1 CalciteSchema (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema)1 Function (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function)1 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)1 SqlCharStringLiteral (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral)1