Search in sources :

Example 6 with GeneratedAggsHandleFunction

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

the class StreamExecOverAggregate method createUnboundedOverProcessFunction.

/**
 * Create an ProcessFunction for unbounded OVER window to evaluate final aggregate value.
 *
 * @param ctx code generator context
 * @param aggCalls physical calls to aggregate functions and their output field names
 * @param constants the constants in aggregates parameters, such as sum(1)
 * @param aggInputRowType physical type of the input row which consists of input and constants.
 * @param inputRowType physical type of the input row which only consists of input.
 * @param rowTimeIdx the index of the rowtime field or None in case of processing time.
 * @param isRowsClause it is a tag that indicates whether the OVER clause is ROWS clause
 */
private KeyedProcessFunction<RowData, RowData, RowData> createUnboundedOverProcessFunction(CodeGeneratorContext ctx, List<AggregateCall> aggCalls, List<RexLiteral> constants, RowType aggInputRowType, RowType inputRowType, int rowTimeIdx, boolean isRowsClause, ExecNodeConfig config, RelBuilder relBuilder) {
    AggregateInfoList aggInfoList = AggregateUtil.transformToStreamAggregateInfoList(// inputSchema.relDataType
    aggInputRowType, JavaScalaConversionUtil.toScala(aggCalls), new boolean[aggCalls.size()], // needRetraction
    false, // isStateBackendDataViews
    true, // needDistinctInfo
    true);
    LogicalType[] fieldTypes = inputRowType.getChildren().toArray(new LogicalType[0]);
    AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(ctx, relBuilder, JavaScalaConversionUtil.toScala(Arrays.asList(fieldTypes)), // copyInputField
    false);
    GeneratedAggsHandleFunction genAggsHandler = generator.needAccumulate().withConstants(JavaScalaConversionUtil.toScala(constants)).generateAggsHandler("UnboundedOverAggregateHelper", aggInfoList);
    LogicalType[] flattenAccTypes = Arrays.stream(aggInfoList.getAccTypes()).map(LogicalTypeDataTypeConverter::fromDataTypeToLogicalType).toArray(LogicalType[]::new);
    if (rowTimeIdx >= 0) {
        if (isRowsClause) {
            // ROWS unbounded over process function
            return new RowTimeRowsUnboundedPrecedingFunction<>(config.getStateRetentionTime(), config.getMaxIdleStateRetentionTime(), genAggsHandler, flattenAccTypes, fieldTypes, rowTimeIdx);
        } else {
            // RANGE unbounded over process function
            return new RowTimeRangeUnboundedPrecedingFunction<>(config.getStateRetentionTime(), config.getMaxIdleStateRetentionTime(), genAggsHandler, flattenAccTypes, fieldTypes, rowTimeIdx);
        }
    } else {
        return new ProcTimeUnboundedPrecedingFunction<>(config.getStateRetentionTime(), config.getMaxIdleStateRetentionTime(), genAggsHandler, flattenAccTypes);
    }
}
Also used : RowTimeRangeUnboundedPrecedingFunction(org.apache.flink.table.runtime.operators.over.RowTimeRangeUnboundedPrecedingFunction) AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) LogicalType(org.apache.flink.table.types.logical.LogicalType) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) GeneratedAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedAggsHandleFunction) RowTimeRowsUnboundedPrecedingFunction(org.apache.flink.table.runtime.operators.over.RowTimeRowsUnboundedPrecedingFunction) ProcTimeUnboundedPrecedingFunction(org.apache.flink.table.runtime.operators.over.ProcTimeUnboundedPrecedingFunction)

Example 7 with GeneratedAggsHandleFunction

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

the class StreamExecOverAggregate method createBoundedOverProcessFunction.

/**
 * Create an ProcessFunction for ROWS clause bounded OVER window to evaluate final aggregate
 * value.
 *
 * @param ctx code generator context
 * @param aggCalls physical calls to aggregate functions and their output field names
 * @param constants the constants in aggregates parameters, such as sum(1)
 * @param aggInputType physical type of the input row which consists of input and constants.
 * @param inputType physical type of the input row which only consists of input.
 * @param rowTimeIdx the index of the rowtime field or None in case of processing time.
 * @param isRowsClause it is a tag that indicates whether the OVER clause is ROWS clause
 */
private KeyedProcessFunction<RowData, RowData, RowData> createBoundedOverProcessFunction(CodeGeneratorContext ctx, List<AggregateCall> aggCalls, List<RexLiteral> constants, RowType aggInputType, RowType inputType, int rowTimeIdx, boolean isRowsClause, long precedingOffset, ExecNodeConfig config, RelBuilder relBuilder) {
    boolean[] aggCallNeedRetractions = new boolean[aggCalls.size()];
    Arrays.fill(aggCallNeedRetractions, true);
    AggregateInfoList aggInfoList = AggregateUtil.transformToStreamAggregateInfoList(// inputSchema.relDataType
    aggInputType, JavaScalaConversionUtil.toScala(aggCalls), aggCallNeedRetractions, // needInputCount,
    true, // isStateBackendDataViews
    true, // needDistinctInfo
    true);
    LogicalType[] fieldTypes = inputType.getChildren().toArray(new LogicalType[0]);
    AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(ctx, relBuilder, JavaScalaConversionUtil.toScala(Arrays.asList(fieldTypes)), // copyInputField
    false);
    GeneratedAggsHandleFunction genAggsHandler = generator.needRetract().needAccumulate().withConstants(JavaScalaConversionUtil.toScala(constants)).generateAggsHandler("BoundedOverAggregateHelper", aggInfoList);
    LogicalType[] flattenAccTypes = Arrays.stream(aggInfoList.getAccTypes()).map(LogicalTypeDataTypeConverter::fromDataTypeToLogicalType).toArray(LogicalType[]::new);
    if (rowTimeIdx >= 0) {
        if (isRowsClause) {
            return new RowTimeRowsBoundedPrecedingFunction<>(config.getStateRetentionTime(), config.getMaxIdleStateRetentionTime(), genAggsHandler, flattenAccTypes, fieldTypes, precedingOffset, rowTimeIdx);
        } else {
            return new RowTimeRangeBoundedPrecedingFunction<>(genAggsHandler, flattenAccTypes, fieldTypes, precedingOffset, rowTimeIdx);
        }
    } else {
        if (isRowsClause) {
            return new ProcTimeRowsBoundedPrecedingFunction<>(config.getStateRetentionTime(), config.getMaxIdleStateRetentionTime(), genAggsHandler, flattenAccTypes, fieldTypes, precedingOffset);
        } else {
            return new ProcTimeRangeBoundedPrecedingFunction<>(genAggsHandler, flattenAccTypes, fieldTypes, precedingOffset);
        }
    }
}
Also used : ProcTimeRowsBoundedPrecedingFunction(org.apache.flink.table.runtime.operators.over.ProcTimeRowsBoundedPrecedingFunction) AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) RowTimeRangeBoundedPrecedingFunction(org.apache.flink.table.runtime.operators.over.RowTimeRangeBoundedPrecedingFunction) ProcTimeRangeBoundedPrecedingFunction(org.apache.flink.table.runtime.operators.over.ProcTimeRangeBoundedPrecedingFunction) RowTimeRowsBoundedPrecedingFunction(org.apache.flink.table.runtime.operators.over.RowTimeRowsBoundedPrecedingFunction) LogicalType(org.apache.flink.table.types.logical.LogicalType) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) GeneratedAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedAggsHandleFunction)

Example 8 with GeneratedAggsHandleFunction

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

the class StreamExecLocalGroupAggregate method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    final ExecEdge inputEdge = getInputEdges().get(0);
    final Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    final RowType inputRowType = (RowType) inputEdge.getOutputType();
    final AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), planner.getRelBuilder(), JavaScalaConversionUtil.toScala(inputRowType.getChildren()), // the local aggregate result will be buffered, so need copy
    true);
    generator.needAccumulate().needMerge(0, true, null);
    if (needRetraction) {
        generator.needRetract();
    }
    final AggregateInfoList aggInfoList = AggregateUtil.transformToStreamAggregateInfoList(inputRowType, JavaScalaConversionUtil.toScala(Arrays.asList(aggCalls)), aggCallNeedRetractions, needRetraction, // isStateBackendDataViews
    false, // needDistinctInfo
    true);
    final GeneratedAggsHandleFunction aggsHandler = generator.generateAggsHandler("GroupAggsHandler", aggInfoList);
    final MiniBatchLocalGroupAggFunction aggFunction = new MiniBatchLocalGroupAggFunction(aggsHandler);
    final RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(grouping, (InternalTypeInfo<RowData>) inputTransform.getOutputType());
    final MapBundleOperator<RowData, RowData, RowData, RowData> operator = new MapBundleOperator<>(aggFunction, AggregateUtil.createMiniBatchTrigger(config), selector);
    return ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationMeta(LOCAL_GROUP_AGGREGATE_TRANSFORMATION, config), operator, InternalTypeInfo.of(getOutputType()), inputTransform.getParallelism());
}
Also used : Transformation(org.apache.flink.api.dag.Transformation) AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) RowType(org.apache.flink.table.types.logical.RowType) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) GeneratedAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedAggsHandleFunction) RowData(org.apache.flink.table.data.RowData) MapBundleOperator(org.apache.flink.table.runtime.operators.bundle.MapBundleOperator) RowDataKeySelector(org.apache.flink.table.runtime.keyselector.RowDataKeySelector) MiniBatchLocalGroupAggFunction(org.apache.flink.table.runtime.operators.aggregate.MiniBatchLocalGroupAggFunction)

Aggregations

AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)8 GeneratedAggsHandleFunction (org.apache.flink.table.runtime.generated.GeneratedAggsHandleFunction)8 AggsHandlerCodeGenerator (org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator)6 Transformation (org.apache.flink.api.dag.Transformation)5 RowData (org.apache.flink.table.data.RowData)5 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)5 RowType (org.apache.flink.table.types.logical.RowType)5 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)4 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)4 LogicalType (org.apache.flink.table.types.logical.LogicalType)4 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)3 TableException (org.apache.flink.table.api.TableException)2 EqualiserCodeGenerator (org.apache.flink.table.planner.codegen.EqualiserCodeGenerator)2 GroupSpec (org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec.GroupSpec)2 GeneratedRecordComparator (org.apache.flink.table.runtime.generated.GeneratedRecordComparator)2 GeneratedRecordEqualiser (org.apache.flink.table.runtime.generated.GeneratedRecordEqualiser)2 OverWindowFrame (org.apache.flink.table.runtime.operators.over.frame.OverWindowFrame)2 UnboundedOverWindowFrame (org.apache.flink.table.runtime.operators.over.frame.UnboundedOverWindowFrame)2 ArrayList (java.util.ArrayList)1 AggregateCall (org.apache.calcite.rel.core.AggregateCall)1