Search in sources :

Example 11 with Function

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

the class ZetaSqlScalarFunctionImpl method create.

/**
 * Creates {@link org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function} from
 * given method. When {@code eval} method does not suit, {@code null} is returned.
 *
 * @param method method that is used to implement the function
 * @param functionGroup ZetaSQL function group identifier. Different function groups may have
 *     divergent translation paths.
 * @return created {@link Function} or null
 */
public static Function create(Method method, String functionGroup, String jarPath) {
    validateMethod(method);
    CallImplementor implementor = createImplementor(method);
    return new ZetaSqlScalarFunctionImpl(method, implementor, functionGroup, jarPath);
}
Also used : CallImplementor(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.CallImplementor)

Example 12 with Function

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

the class BeamZetaSqlCatalogTest method rejectsScalarFunctionImplWithUnsupportedParameterType.

@Test
public void rejectsScalarFunctionImplWithUnsupportedParameterType() throws NoSuchMethodException {
    JdbcConnection jdbcConnection = createJdbcConnection();
    SchemaPlus calciteSchema = jdbcConnection.getCurrentSchemaPlus();
    Method method = TakesArrayTimeFn.class.getMethod("eval", List.class);
    calciteSchema.add("take_array", ScalarFunctionImpl.create(method));
    thrown.expect(UnsupportedOperationException.class);
    thrown.expectMessage("Calcite type TIME not allowed in function take_array");
    BeamZetaSqlCatalog.create(calciteSchema, jdbcConnection.getTypeFactory(), SqlAnalyzer.baseAnalyzerOptions());
}
Also used : SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) JdbcConnection(org.apache.beam.sdk.extensions.sql.impl.JdbcConnection) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 13 with Function

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

the class SqlCaseWithValueOperatorRewriter method apply.

@Override
public RexNode apply(RexBuilder rexBuilder, List<RexNode> operands) {
    Preconditions.checkArgument(operands.size() % 2 == 0 && !operands.isEmpty(), "$case_with_value should have an even number of arguments greater than 0 in function call" + " (The value operand, the else operand, and paired when/then operands).");
    SqlOperator op = SqlStdOperatorTable.CASE;
    List<RexNode> newOperands = new ArrayList<>();
    RexNode value = operands.get(0);
    for (int i = 1; i < operands.size() - 2; i += 2) {
        RexNode when = operands.get(i);
        RexNode then = operands.get(i + 1);
        newOperands.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, ImmutableList.of(value, when)));
        newOperands.add(then);
    }
    RexNode elseOperand = Iterables.getLast(operands);
    newOperands.add(elseOperand);
    return rexBuilder.makeCall(op, newOperands);
}
Also used : SqlOperator(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 14 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 create.

/**
 * Creates {@link org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.schema.Function} from
 * given method. When {@code eval} method does not suit, {@code null} is returned.
 *
 * @param method method that is used to implement the function
 * @param jarPath Path to jar that contains the method.
 * @return created {@link Function} or null
 */
public static Function create(Method method, String jarPath) {
    validateMethod(method);
    CallImplementor implementor = createImplementor(method);
    return new ScalarFunctionImpl(method, implementor, jarPath);
}
Also used : CallImplementor(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.CallImplementor)

Example 15 with Function

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

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