Search in sources :

Example 1 with GeneratedProjection

use of org.apache.flink.table.runtime.generated.GeneratedProjection in project flink by apache.

the class PassThroughPythonStreamGroupWindowAggregateOperator method createProjection.

private Projection<RowData, BinaryRowData> createProjection(String name, int[] fields) {
    final RowType forwardedFieldType = new RowType(Arrays.stream(fields).mapToObj(i -> inputType.getFields().get(i)).collect(Collectors.toList()));
    final GeneratedProjection generatedProjection = ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(new TableConfig()), name, inputType, forwardedFieldType, fields);
    // noinspection unchecked
    return generatedProjection.newInstance(Thread.currentThread().getContextClassLoader());
}
Also used : GeneratedProjection(org.apache.flink.table.runtime.generated.GeneratedProjection) RowType(org.apache.flink.table.types.logical.RowType) TableConfig(org.apache.flink.table.api.TableConfig)

Example 2 with GeneratedProjection

use of org.apache.flink.table.runtime.generated.GeneratedProjection in project flink by apache.

the class StreamExecPythonOverAggregate method getPythonOverWindowAggregateFunctionOperator.

@SuppressWarnings("unchecked")
private OneInputStreamOperator<RowData, RowData> getPythonOverWindowAggregateFunctionOperator(ExecNodeConfig config, Configuration pythonConfig, RowType inputRowType, RowType outputRowType, int rowTiemIdx, long lowerBoundary, boolean isRowsClause, int[] udafInputOffsets, PythonFunctionInfo[] pythonFunctionInfos, long minIdleStateRetentionTime, long maxIdleStateRetentionTime) {
    RowType userDefinedFunctionInputType = (RowType) Projection.of(udafInputOffsets).project(inputRowType);
    RowType userDefinedFunctionOutputType = (RowType) Projection.range(inputRowType.getFieldCount(), outputRowType.getFieldCount()).project(outputRowType);
    GeneratedProjection generatedProjection = ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "UdafInputProjection", inputRowType, userDefinedFunctionInputType, udafInputOffsets);
    if (isRowsClause) {
        String className;
        if (rowTiemIdx != -1) {
            className = ARROW_PYTHON_OVER_WINDOW_ROWS_ROW_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME;
        } else {
            className = ARROW_PYTHON_OVER_WINDOW_ROWS_PROC_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME;
        }
        Class<?> clazz = CommonPythonUtil.loadClass(className);
        try {
            Constructor<?> ctor = clazz.getConstructor(Configuration.class, long.class, long.class, PythonFunctionInfo[].class, RowType.class, RowType.class, RowType.class, int.class, long.class, GeneratedProjection.class);
            return (OneInputStreamOperator<RowData, RowData>) ctor.newInstance(pythonConfig, minIdleStateRetentionTime, maxIdleStateRetentionTime, pythonFunctionInfos, inputRowType, userDefinedFunctionInputType, userDefinedFunctionOutputType, rowTiemIdx, lowerBoundary, generatedProjection);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new TableException("Python Arrow Over Rows Window Function Operator constructed failed.", e);
        }
    } else {
        String className;
        if (rowTiemIdx != -1) {
            className = ARROW_PYTHON_OVER_WINDOW_RANGE_ROW_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME;
        } else {
            className = ARROW_PYTHON_OVER_WINDOW_RANGE_PROC_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME;
        }
        Class<?> clazz = CommonPythonUtil.loadClass(className);
        try {
            Constructor<?> ctor = clazz.getConstructor(Configuration.class, PythonFunctionInfo[].class, RowType.class, RowType.class, RowType.class, int.class, long.class, GeneratedProjection.class);
            return (OneInputStreamOperator<RowData, RowData>) ctor.newInstance(pythonConfig, pythonFunctionInfos, inputRowType, userDefinedFunctionInputType, userDefinedFunctionOutputType, rowTiemIdx, lowerBoundary, generatedProjection);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new TableException("Python Arrow Over Range Window Function Operator constructed failed.", e);
        }
    }
}
Also used : PythonFunctionInfo(org.apache.flink.table.functions.python.PythonFunctionInfo) TableException(org.apache.flink.table.api.TableException) RowType(org.apache.flink.table.types.logical.RowType) InvocationTargetException(java.lang.reflect.InvocationTargetException) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) GeneratedProjection(org.apache.flink.table.runtime.generated.GeneratedProjection)

Example 3 with GeneratedProjection

use of org.apache.flink.table.runtime.generated.GeneratedProjection in project flink by apache.

the class BatchExecHashJoin method translateToPlanInternal.

@Override
@SuppressWarnings("unchecked")
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    ExecEdge leftInputEdge = getInputEdges().get(0);
    ExecEdge rightInputEdge = getInputEdges().get(1);
    Transformation<RowData> leftInputTransform = (Transformation<RowData>) leftInputEdge.translateToPlan(planner);
    Transformation<RowData> rightInputTransform = (Transformation<RowData>) rightInputEdge.translateToPlan(planner);
    // get input types
    RowType leftType = (RowType) leftInputEdge.getOutputType();
    RowType rightType = (RowType) rightInputEdge.getOutputType();
    JoinUtil.validateJoinSpec(joinSpec, leftType, rightType, false);
    int[] leftKeys = joinSpec.getLeftKeys();
    int[] rightKeys = joinSpec.getRightKeys();
    LogicalType[] keyFieldTypes = IntStream.of(leftKeys).mapToObj(leftType::getTypeAt).toArray(LogicalType[]::new);
    RowType keyType = RowType.of(keyFieldTypes);
    GeneratedJoinCondition condFunc = JoinUtil.generateConditionFunction(config.getTableConfig(), joinSpec.getNonEquiCondition().orElse(null), leftType, rightType);
    // projection for equals
    GeneratedProjection leftProj = ProjectionCodeGenerator.generateProjection(new CodeGeneratorContext(config.getTableConfig()), "HashJoinLeftProjection", leftType, keyType, leftKeys);
    GeneratedProjection rightProj = ProjectionCodeGenerator.generateProjection(new CodeGeneratorContext(config.getTableConfig()), "HashJoinRightProjection", rightType, keyType, rightKeys);
    Transformation<RowData> buildTransform;
    Transformation<RowData> probeTransform;
    GeneratedProjection buildProj;
    GeneratedProjection probeProj;
    int[] buildKeys;
    int[] probeKeys;
    RowType buildType;
    RowType probeType;
    int buildRowSize;
    long buildRowCount;
    long probeRowCount;
    boolean reverseJoin = !leftIsBuild;
    if (leftIsBuild) {
        buildTransform = leftInputTransform;
        buildProj = leftProj;
        buildType = leftType;
        buildRowSize = estimatedLeftAvgRowSize;
        buildRowCount = estimatedLeftRowCount;
        buildKeys = leftKeys;
        probeTransform = rightInputTransform;
        probeProj = rightProj;
        probeType = rightType;
        probeRowCount = estimatedLeftRowCount;
        probeKeys = rightKeys;
    } else {
        buildTransform = rightInputTransform;
        buildProj = rightProj;
        buildType = rightType;
        buildRowSize = estimatedRightAvgRowSize;
        buildRowCount = estimatedRightRowCount;
        buildKeys = rightKeys;
        probeTransform = leftInputTransform;
        probeProj = leftProj;
        probeType = leftType;
        probeRowCount = estimatedLeftRowCount;
        probeKeys = leftKeys;
    }
    // operator
    StreamOperatorFactory<RowData> operator;
    FlinkJoinType joinType = joinSpec.getJoinType();
    HashJoinType hashJoinType = HashJoinType.of(leftIsBuild, joinType.isLeftOuter(), joinType.isRightOuter(), joinType == FlinkJoinType.SEMI, joinType == FlinkJoinType.ANTI);
    if (LongHashJoinGenerator.support(hashJoinType, keyType, joinSpec.getFilterNulls())) {
        operator = LongHashJoinGenerator.gen(config.getTableConfig(), hashJoinType, keyType, buildType, probeType, buildKeys, probeKeys, buildRowSize, buildRowCount, reverseJoin, condFunc);
    } else {
        operator = SimpleOperatorFactory.of(HashJoinOperator.newHashJoinOperator(hashJoinType, condFunc, reverseJoin, joinSpec.getFilterNulls(), buildProj, probeProj, tryDistinctBuildRow, buildRowSize, buildRowCount, probeRowCount, keyType));
    }
    long managedMemory = config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_HASH_JOIN_MEMORY).getBytes();
    return ExecNodeUtil.createTwoInputTransformation(buildTransform, probeTransform, createTransformationName(config), createTransformationDescription(config), operator, InternalTypeInfo.of(getOutputType()), probeTransform.getParallelism(), managedMemory);
}
Also used : Transformation(org.apache.flink.api.dag.Transformation) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) HashJoinType(org.apache.flink.table.runtime.operators.join.HashJoinType) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) FlinkJoinType(org.apache.flink.table.runtime.operators.join.FlinkJoinType) RowData(org.apache.flink.table.data.RowData) GeneratedJoinCondition(org.apache.flink.table.runtime.generated.GeneratedJoinCondition) GeneratedProjection(org.apache.flink.table.runtime.generated.GeneratedProjection)

Example 4 with GeneratedProjection

use of org.apache.flink.table.runtime.generated.GeneratedProjection in project flink by apache.

the class KeySelectorUtil method getRowDataSelector.

/**
 * Create a RowDataKeySelector to extract keys from DataStream which type is {@link
 * InternalTypeInfo} of {@link RowData}.
 *
 * @param keyFields key fields
 * @param rowType type of DataStream to extract keys
 * @return the RowDataKeySelector to extract keys from DataStream which type is {@link
 *     InternalTypeInfo} of {@link RowData}.
 */
public static RowDataKeySelector getRowDataSelector(int[] keyFields, InternalTypeInfo<RowData> rowType) {
    if (keyFields.length > 0) {
        LogicalType[] inputFieldTypes = rowType.toRowFieldTypes();
        LogicalType[] keyFieldTypes = new LogicalType[keyFields.length];
        for (int i = 0; i < keyFields.length; ++i) {
            keyFieldTypes[i] = inputFieldTypes[keyFields[i]];
        }
        // do not provide field names for the result key type,
        // because we may have duplicate key fields and the field names may conflict
        RowType returnType = RowType.of(keyFieldTypes);
        RowType inputType = rowType.toRowType();
        GeneratedProjection generatedProjection = ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(new TableConfig()), "KeyProjection", inputType, returnType, keyFields);
        InternalTypeInfo<RowData> keyRowType = InternalTypeInfo.of(returnType);
        return new BinaryRowDataKeySelector(keyRowType, generatedProjection);
    } else {
        return EmptyRowDataKeySelector.INSTANCE;
    }
}
Also used : RowData(org.apache.flink.table.data.RowData) GeneratedProjection(org.apache.flink.table.runtime.generated.GeneratedProjection) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowType(org.apache.flink.table.types.logical.RowType) TableConfig(org.apache.flink.table.api.TableConfig) BinaryRowDataKeySelector(org.apache.flink.table.runtime.keyselector.BinaryRowDataKeySelector)

Aggregations

GeneratedProjection (org.apache.flink.table.runtime.generated.GeneratedProjection)4 RowType (org.apache.flink.table.types.logical.RowType)4 TableConfig (org.apache.flink.table.api.TableConfig)2 RowData (org.apache.flink.table.data.RowData)2 LogicalType (org.apache.flink.table.types.logical.LogicalType)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Transformation (org.apache.flink.api.dag.Transformation)1 OneInputStreamOperator (org.apache.flink.streaming.api.operators.OneInputStreamOperator)1 TableException (org.apache.flink.table.api.TableException)1 PythonFunctionInfo (org.apache.flink.table.functions.python.PythonFunctionInfo)1 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)1 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)1 GeneratedJoinCondition (org.apache.flink.table.runtime.generated.GeneratedJoinCondition)1 BinaryRowDataKeySelector (org.apache.flink.table.runtime.keyselector.BinaryRowDataKeySelector)1 FlinkJoinType (org.apache.flink.table.runtime.operators.join.FlinkJoinType)1 HashJoinType (org.apache.flink.table.runtime.operators.join.HashJoinType)1