Search in sources :

Example 1 with OverSpec

use of org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec in project flink by apache.

the class StreamExecOverAggregate method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    if (overSpec.getGroups().size() > 1) {
        throw new TableException("All aggregates must be computed on the same window.");
    }
    final OverSpec.GroupSpec group = overSpec.getGroups().get(0);
    final int[] orderKeys = group.getSort().getFieldIndices();
    final boolean[] isAscendingOrders = group.getSort().getAscendingOrders();
    if (orderKeys.length != 1 || isAscendingOrders.length != 1) {
        throw new TableException("The window can only be ordered by a single time column.");
    }
    if (!isAscendingOrders[0]) {
        throw new TableException("The window can only be ordered in ASCENDING mode.");
    }
    final int[] partitionKeys = overSpec.getPartition().getFieldIndices();
    if (partitionKeys.length > 0 && config.getStateRetentionTime() < 0) {
        LOG.warn("No state retention interval configured for a query which accumulates state. " + "Please provide a query configuration with valid retention interval to prevent " + "excessive state size. You may specify a retention time of 0 to not clean up the state.");
    }
    final ExecEdge inputEdge = getInputEdges().get(0);
    final Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    final RowType inputRowType = (RowType) inputEdge.getOutputType();
    final int orderKey = orderKeys[0];
    final LogicalType orderKeyType = inputRowType.getFields().get(orderKey).getType();
    // check time field && identify window rowtime attribute
    final int rowTimeIdx;
    if (isRowtimeAttribute(orderKeyType)) {
        rowTimeIdx = orderKey;
    } else if (isProctimeAttribute(orderKeyType)) {
        rowTimeIdx = -1;
    } else {
        throw new TableException("OVER windows' ordering in stream mode must be defined on a time attribute.");
    }
    final List<RexLiteral> constants = overSpec.getConstants();
    final List<String> fieldNames = new ArrayList<>(inputRowType.getFieldNames());
    final List<LogicalType> fieldTypes = new ArrayList<>(inputRowType.getChildren());
    IntStream.range(0, constants.size()).forEach(i -> fieldNames.add("TMP" + i));
    for (int i = 0; i < constants.size(); ++i) {
        fieldNames.add("TMP" + i);
        fieldTypes.add(FlinkTypeFactory.toLogicalType(constants.get(i).getType()));
    }
    final RowType aggInputRowType = RowType.of(fieldTypes.toArray(new LogicalType[0]), fieldNames.toArray(new String[0]));
    final CodeGeneratorContext ctx = new CodeGeneratorContext(config.getTableConfig());
    final KeyedProcessFunction<RowData, RowData, RowData> overProcessFunction;
    if (group.getLowerBound().isPreceding() && group.getLowerBound().isUnbounded() && group.getUpperBound().isCurrentRow()) {
        // unbounded OVER window
        overProcessFunction = createUnboundedOverProcessFunction(ctx, group.getAggCalls(), constants, aggInputRowType, inputRowType, rowTimeIdx, group.isRows(), config, planner.getRelBuilder());
    } else if (group.getLowerBound().isPreceding() && !group.getLowerBound().isUnbounded() && group.getUpperBound().isCurrentRow()) {
        final Object boundValue = OverAggregateUtil.getBoundary(overSpec, group.getLowerBound());
        if (boundValue instanceof BigDecimal) {
            throw new TableException("the specific value is decimal which haven not supported yet.");
        }
        // bounded OVER window
        final long precedingOffset = -1 * (long) boundValue + (group.isRows() ? 1 : 0);
        overProcessFunction = createBoundedOverProcessFunction(ctx, group.getAggCalls(), constants, aggInputRowType, inputRowType, rowTimeIdx, group.isRows(), precedingOffset, config, planner.getRelBuilder());
    } else {
        throw new TableException("OVER RANGE FOLLOWING windows are not supported yet.");
    }
    final KeyedProcessOperator<RowData, RowData, RowData> operator = new KeyedProcessOperator<>(overProcessFunction);
    OneInputTransformation<RowData, RowData> transform = ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationMeta(OVER_AGGREGATE_TRANSFORMATION, config), operator, InternalTypeInfo.of(getOutputType()), inputTransform.getParallelism());
    // set KeyType and Selector for state
    final RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(partitionKeys, InternalTypeInfo.of(inputRowType));
    transform.setStateKeySelector(selector);
    transform.setStateKeyType(selector.getProducedType());
    return transform;
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) Transformation(org.apache.flink.api.dag.Transformation) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) ArrayList(java.util.ArrayList) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowData(org.apache.flink.table.data.RowData) RowDataKeySelector(org.apache.flink.table.runtime.keyselector.RowDataKeySelector) KeyedProcessOperator(org.apache.flink.streaming.api.operators.KeyedProcessOperator) TableException(org.apache.flink.table.api.TableException) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) OverSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec) BigDecimal(java.math.BigDecimal)

Example 2 with OverSpec

use of org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec in project flink by apache.

the class StreamExecPythonOverAggregate method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    if (overSpec.getGroups().size() > 1) {
        throw new TableException("All aggregates must be computed on the same window.");
    }
    final OverSpec.GroupSpec group = overSpec.getGroups().get(0);
    final int[] orderKeys = group.getSort().getFieldIndices();
    final boolean[] isAscendingOrders = group.getSort().getAscendingOrders();
    if (orderKeys.length != 1 || isAscendingOrders.length != 1) {
        throw new TableException("The window can only be ordered by a single time column.");
    }
    if (!isAscendingOrders[0]) {
        throw new TableException("The window can only be ordered in ASCENDING mode.");
    }
    final int[] partitionKeys = overSpec.getPartition().getFieldIndices();
    if (partitionKeys.length > 0 && config.getStateRetentionTime() < 0) {
        LOG.warn("No state retention interval configured for a query which accumulates state. " + "Please provide a query configuration with valid retention interval to prevent " + "excessive state size. You may specify a retention time of 0 to not clean up the state.");
    }
    final ExecEdge inputEdge = getInputEdges().get(0);
    final Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    final RowType inputRowType = (RowType) inputEdge.getOutputType();
    final int orderKey = orderKeys[0];
    final LogicalType orderKeyType = inputRowType.getFields().get(orderKey).getType();
    // check time field && identify window rowtime attribute
    final int rowTimeIdx;
    if (isRowtimeAttribute(orderKeyType)) {
        rowTimeIdx = orderKey;
    } else if (isProctimeAttribute(orderKeyType)) {
        rowTimeIdx = -1;
    } else {
        throw new TableException("OVER windows' ordering in stream mode must be defined on a time attribute.");
    }
    if (group.getLowerBound().isPreceding() && group.getLowerBound().isUnbounded()) {
        throw new TableException("Python UDAF is not supported to be used in UNBOUNDED PRECEDING OVER windows.");
    } else if (!group.getUpperBound().isCurrentRow()) {
        throw new TableException("Python UDAF is not supported to be used in UNBOUNDED FOLLOWING OVER windows.");
    }
    Object boundValue = OverAggregateUtil.getBoundary(overSpec, group.getLowerBound());
    if (boundValue instanceof BigDecimal) {
        throw new TableException("the specific value is decimal which haven not supported yet.");
    }
    long precedingOffset = -1 * (long) boundValue;
    Configuration pythonConfig = CommonPythonUtil.getMergedConfig(planner.getExecEnv(), config.getTableConfig());
    OneInputTransformation<RowData, RowData> transform = createPythonOneInputTransformation(inputTransform, inputRowType, InternalTypeInfo.of(getOutputType()).toRowType(), rowTimeIdx, group.getAggCalls().toArray(new AggregateCall[0]), precedingOffset, group.isRows(), config.getStateRetentionTime(), config.getMaxIdleStateRetentionTime(), pythonConfig, config);
    if (CommonPythonUtil.isPythonWorkerUsingManagedMemory(pythonConfig)) {
        transform.declareManagedMemoryUseCaseAtSlotScope(ManagedMemoryUseCase.PYTHON);
    }
    // set KeyType and Selector for state
    final RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(partitionKeys, InternalTypeInfo.of(inputRowType));
    transform.setStateKeySelector(selector);
    transform.setStateKeyType(selector.getProducedType());
    return transform;
}
Also used : TableException(org.apache.flink.table.api.TableException) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) Transformation(org.apache.flink.api.dag.Transformation) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) Configuration(org.apache.flink.configuration.Configuration) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) OverSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec) BigDecimal(java.math.BigDecimal) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RowData(org.apache.flink.table.data.RowData) RowDataKeySelector(org.apache.flink.table.runtime.keyselector.RowDataKeySelector)

Example 3 with OverSpec

use of org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec in project flink by apache.

the class BatchExecPythonOverAggregate 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 inputType = (RowType) inputEdge.getOutputType();
    List<OverSpec.GroupSpec> groups = overSpec.getGroups();
    boolean[] isRangeWindows = new boolean[groups.size()];
    for (int i = 0; i < groups.size(); i++) {
        OverSpec.GroupSpec group = groups.get(i);
        List<AggregateCall> groupAggCalls = group.getAggCalls();
        aggCalls.addAll(groupAggCalls);
        for (int j = 0; j < groupAggCalls.size(); j++) {
            aggWindowIndex.add(i);
        }
        OverWindowMode mode = inferGroupMode(group);
        if (mode == OverWindowMode.ROW) {
            isRangeWindows[i] = false;
            if (isUnboundedWindow(group)) {
                lowerBoundary.add(Long.MIN_VALUE);
                upperBoundary.add(Long.MAX_VALUE);
            } else if (isUnboundedPrecedingWindow(group)) {
                lowerBoundary.add(Long.MIN_VALUE);
                upperBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getUpperBound()));
            } else if (isUnboundedFollowingWindow(group)) {
                lowerBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getLowerBound()));
                upperBoundary.add(Long.MAX_VALUE);
            } else if (isSlidingWindow(group)) {
                lowerBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getLowerBound()));
                upperBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getUpperBound()));
            } else {
                throw new TableException("Unsupported row window group spec " + group);
            }
        } else {
            isRangeWindows[i] = true;
            if (isUnboundedWindow(group)) {
                lowerBoundary.add(Long.MIN_VALUE);
                upperBoundary.add(Long.MAX_VALUE);
            } else if (isUnboundedPrecedingWindow(group)) {
                lowerBoundary.add(Long.MIN_VALUE);
                upperBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getUpperBound()));
            } else if (isUnboundedFollowingWindow(group)) {
                lowerBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getLowerBound()));
                upperBoundary.add(Long.MAX_VALUE);
            } else if (isSlidingWindow(group)) {
                lowerBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getLowerBound()));
                upperBoundary.add(OverAggregateUtil.getLongBoundary(overSpec, group.getUpperBound()));
            } else {
                throw new TableException("Unsupported range window group spec " + group);
            }
        }
    }
    Configuration pythonConfig = CommonPythonUtil.getMergedConfig(planner.getExecEnv(), config.getTableConfig());
    OneInputTransformation<RowData, RowData> transform = createPythonOneInputTransformation(inputTransform, inputType, InternalTypeInfo.of(getOutputType()).toRowType(), isRangeWindows, pythonConfig, config);
    if (CommonPythonUtil.isPythonWorkerUsingManagedMemory(pythonConfig)) {
        transform.declareManagedMemoryUseCaseAtSlotScope(ManagedMemoryUseCase.PYTHON);
    }
    return transform;
}
Also used : OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) Transformation(org.apache.flink.api.dag.Transformation) TableException(org.apache.flink.table.api.TableException) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) Configuration(org.apache.flink.configuration.Configuration) RowType(org.apache.flink.table.types.logical.RowType) OverSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RowData(org.apache.flink.table.data.RowData)

Aggregations

Transformation (org.apache.flink.api.dag.Transformation)3 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)3 TableException (org.apache.flink.table.api.TableException)3 RowData (org.apache.flink.table.data.RowData)3 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)3 OverSpec (org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec)3 RowType (org.apache.flink.table.types.logical.RowType)3 BigDecimal (java.math.BigDecimal)2 AggregateCall (org.apache.calcite.rel.core.AggregateCall)2 Configuration (org.apache.flink.configuration.Configuration)2 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)2 LogicalType (org.apache.flink.table.types.logical.LogicalType)2 ArrayList (java.util.ArrayList)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 KeyedProcessOperator (org.apache.flink.streaming.api.operators.KeyedProcessOperator)1 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)1