Search in sources :

Example 6 with AggsHandlerCodeGenerator

use of org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator in project flink by apache.

the class StreamExecGlobalWindowAggregate method createAggsHandler.

private GeneratedNamespaceAggsHandleFunction<Long> createAggsHandler(String name, SliceAssigner sliceAssigner, AggregateInfoList aggInfoList, int mergedAccOffset, boolean mergedAccIsOnHeap, DataType[] mergedAccExternalTypes, ExecNodeConfig config, RelBuilder relBuilder, ZoneId shifTimeZone) {
    final AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), relBuilder, JavaScalaConversionUtil.toScala(localAggInputRowType.getChildren()), // copyInputField
    true).needAccumulate().needMerge(mergedAccOffset, mergedAccIsOnHeap, mergedAccExternalTypes);
    final List<WindowProperty> windowProperties = Arrays.asList(Arrays.stream(namedWindowProperties).map(NamedWindowProperty::getProperty).toArray(WindowProperty[]::new));
    return generator.generateNamespaceAggsHandler(name, aggInfoList, JavaScalaConversionUtil.toScala(windowProperties), sliceAssigner, shifTimeZone);
}
Also used : NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) WindowProperty(org.apache.flink.table.runtime.groupwindow.WindowProperty) NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator)

Example 7 with AggsHandlerCodeGenerator

use of org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator 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 8 with AggsHandlerCodeGenerator

use of org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator 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 9 with AggsHandlerCodeGenerator

use of org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator 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)

Example 10 with AggsHandlerCodeGenerator

use of org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator in project flink by apache.

the class StreamExecWindowAggregate method createAggsHandler.

private GeneratedNamespaceAggsHandleFunction<Long> createAggsHandler(SliceAssigner sliceAssigner, AggregateInfoList aggInfoList, ExecNodeConfig config, RelBuilder relBuilder, List<LogicalType> fieldTypes, ZoneId shiftTimeZone) {
    final AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), relBuilder, JavaScalaConversionUtil.toScala(fieldTypes), // copyInputField
    false).needAccumulate();
    if (sliceAssigner instanceof SliceSharedAssigner) {
        generator.needMerge(0, false, null);
    }
    final List<WindowProperty> windowProperties = Arrays.asList(Arrays.stream(namedWindowProperties).map(NamedWindowProperty::getProperty).toArray(WindowProperty[]::new));
    return generator.generateNamespaceAggsHandler("WindowAggsHandler", aggInfoList, JavaScalaConversionUtil.toScala(windowProperties), sliceAssigner, shiftTimeZone);
}
Also used : NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) WindowProperty(org.apache.flink.table.runtime.groupwindow.WindowProperty) NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) SliceSharedAssigner(org.apache.flink.table.runtime.operators.window.slicing.SliceSharedAssigner)

Aggregations

AggsHandlerCodeGenerator (org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator)10 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)8 AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)7 GeneratedAggsHandleFunction (org.apache.flink.table.runtime.generated.GeneratedAggsHandleFunction)6 RowType (org.apache.flink.table.types.logical.RowType)5 Transformation (org.apache.flink.api.dag.Transformation)4 RowData (org.apache.flink.table.data.RowData)4 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)4 LogicalType (org.apache.flink.table.types.logical.LogicalType)4 NamedWindowProperty (org.apache.flink.table.runtime.groupwindow.NamedWindowProperty)3 WindowProperty (org.apache.flink.table.runtime.groupwindow.WindowProperty)3 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)3 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)2 TableException (org.apache.flink.table.api.TableException)2 GroupSpec (org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec.GroupSpec)2 GeneratedRecordComparator (org.apache.flink.table.runtime.generated.GeneratedRecordComparator)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