Search in sources :

Example 16 with PythonFunctionInfo

use of org.apache.flink.table.functions.python.PythonFunctionInfo in project flink by apache.

the class BatchExecPythonGroupAggregate method getPythonAggregateFunctionOperator.

@SuppressWarnings("unchecked")
private OneInputStreamOperator<RowData, RowData> getPythonAggregateFunctionOperator(ExecNodeConfig config, Configuration pythonConfig, RowType inputRowType, RowType outputRowType, int[] udafInputOffsets, PythonFunctionInfo[] pythonFunctionInfos) {
    final Class<?> clazz = CommonPythonUtil.loadClass(ARROW_PYTHON_AGGREGATE_FUNCTION_OPERATOR_NAME);
    RowType udfInputType = (RowType) Projection.of(udafInputOffsets).project(inputRowType);
    RowType udfOutputType = (RowType) Projection.range(auxGrouping.length, outputRowType.getFieldCount()).project(outputRowType);
    try {
        Constructor<?> ctor = clazz.getConstructor(Configuration.class, PythonFunctionInfo[].class, RowType.class, RowType.class, RowType.class, GeneratedProjection.class, GeneratedProjection.class, GeneratedProjection.class);
        return (OneInputStreamOperator<RowData, RowData>) ctor.newInstance(pythonConfig, pythonFunctionInfos, inputRowType, udfInputType, udfOutputType, ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "UdafInputProjection", inputRowType, udfInputType, udafInputOffsets), ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "GroupKey", inputRowType, (RowType) Projection.of(grouping).project(inputRowType), grouping), ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "GroupSet", inputRowType, (RowType) Projection.of(auxGrouping).project(inputRowType), auxGrouping));
    } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new TableException("Python BatchArrowPythonGroupAggregateFunctionOperator constructed failed.", e);
    }
}
Also used : PythonFunctionInfo(org.apache.flink.table.functions.python.PythonFunctionInfo) TableException(org.apache.flink.table.api.TableException) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) RowType(org.apache.flink.table.types.logical.RowType) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 17 with PythonFunctionInfo

use of org.apache.flink.table.functions.python.PythonFunctionInfo in project flink by apache.

the class StreamExecPythonGroupWindowAggregate method getPandasPythonStreamGroupWindowAggregateFunctionOperator.

@SuppressWarnings({ "unchecked", "rawtypes" })
private OneInputStreamOperator<RowData, RowData> getPandasPythonStreamGroupWindowAggregateFunctionOperator(ExecNodeConfig config, Configuration pythonConfig, RowType inputRowType, RowType outputRowType, WindowAssigner<?> windowAssigner, Trigger<?> trigger, long allowance, int inputTimeFieldIndex, int[] udafInputOffsets, PythonFunctionInfo[] pythonFunctionInfos, ZoneId shiftTimeZone) {
    Class clazz = CommonPythonUtil.loadClass(ARROW_STREAM_PYTHON_GROUP_WINDOW_AGGREGATE_FUNCTION_OPERATOR_NAME);
    RowType userDefinedFunctionInputType = (RowType) Projection.of(udafInputOffsets).project(inputRowType);
    RowType userDefinedFunctionOutputType = (RowType) Projection.range(grouping.length, outputRowType.getFieldCount() - namedWindowProperties.length).project(outputRowType);
    try {
        Constructor<OneInputStreamOperator<RowData, RowData>> ctor = clazz.getConstructor(Configuration.class, PythonFunctionInfo[].class, RowType.class, RowType.class, RowType.class, int.class, WindowAssigner.class, Trigger.class, long.class, NamedWindowProperty[].class, ZoneId.class, GeneratedProjection.class);
        return ctor.newInstance(pythonConfig, pythonFunctionInfos, inputRowType, userDefinedFunctionInputType, userDefinedFunctionOutputType, inputTimeFieldIndex, windowAssigner, trigger, allowance, namedWindowProperties, shiftTimeZone, ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "UdafInputProjection", inputRowType, userDefinedFunctionInputType, udafInputOffsets));
    } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new TableException("Python StreamArrowPythonGroupWindowAggregateFunctionOperator constructed failed.", e);
    }
}
Also used : NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) PythonFunctionInfo(org.apache.flink.table.functions.python.PythonFunctionInfo) TableException(org.apache.flink.table.api.TableException) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) RowType(org.apache.flink.table.types.logical.RowType) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 18 with PythonFunctionInfo

use of org.apache.flink.table.functions.python.PythonFunctionInfo in project flink by apache.

the class CommonPythonUtil method createPythonFunctionInfo.

private static PythonFunctionInfo createPythonFunctionInfo(RexCall pythonRexCall, Map<RexNode, Integer> inputNodes, FunctionDefinition functionDefinition) throws InvocationTargetException, IllegalAccessException {
    ArrayList<Object> inputs = new ArrayList<>();
    for (RexNode operand : pythonRexCall.getOperands()) {
        if (operand instanceof RexCall) {
            RexCall childPythonRexCall = (RexCall) operand;
            PythonFunctionInfo argPythonInfo = createPythonFunctionInfo(childPythonRexCall, inputNodes);
            inputs.add(argPythonInfo);
        } else if (operand instanceof RexLiteral) {
            RexLiteral literal = (RexLiteral) operand;
            inputs.add(convertLiteralToPython(literal, literal.getType().getSqlTypeName()));
        } else {
            if (inputNodes.containsKey(operand)) {
                inputs.add(inputNodes.get(operand));
            } else {
                Integer inputOffset = inputNodes.size();
                inputs.add(inputOffset);
                inputNodes.put(operand, inputOffset);
            }
        }
    }
    return new PythonFunctionInfo((PythonFunction) functionDefinition, inputs.toArray());
}
Also used : RexCall(org.apache.calcite.rex.RexCall) PythonFunctionInfo(org.apache.flink.table.functions.python.PythonFunctionInfo) RexLiteral(org.apache.calcite.rex.RexLiteral) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

PythonFunctionInfo (org.apache.flink.table.functions.python.PythonFunctionInfo)18 RowType (org.apache.flink.table.types.logical.RowType)13 TableException (org.apache.flink.table.api.TableException)9 OneInputStreamOperator (org.apache.flink.streaming.api.operators.OneInputStreamOperator)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 RowData (org.apache.flink.table.data.RowData)6 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 Configuration (org.apache.flink.configuration.Configuration)4 GeneratedProjection (org.apache.flink.table.runtime.generated.GeneratedProjection)4 InternalTypeInfo (org.apache.flink.table.runtime.typeutils.InternalTypeInfo)4 Constructor (java.lang.reflect.Constructor)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AggregateCall (org.apache.calcite.rel.core.AggregateCall)3 RexCall (org.apache.calcite.rex.RexCall)3 RexNode (org.apache.calcite.rex.RexNode)3 Transformation (org.apache.flink.api.dag.Transformation)3 ManagedMemoryUseCase (org.apache.flink.core.memory.ManagedMemoryUseCase)3 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)3 Projection (org.apache.flink.table.connector.Projection)3