use of org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy in project flink by apache.
the class StreamExecWindowAggregateBase method createSliceAssigner.
// ------------------------------------------------------------------------------------------
// Utilities
// ------------------------------------------------------------------------------------------
protected SliceAssigner createSliceAssigner(WindowingStrategy windowingStrategy, ZoneId shiftTimeZone) {
WindowSpec windowSpec = windowingStrategy.getWindow();
if (windowingStrategy instanceof WindowAttachedWindowingStrategy) {
int windowEndIndex = ((WindowAttachedWindowingStrategy) windowingStrategy).getWindowEnd();
// we don't need time attribute to assign windows, use a magic value in this case
SliceAssigner innerAssigner = createSliceAssigner(windowSpec, Integer.MAX_VALUE, shiftTimeZone);
return SliceAssigners.windowed(windowEndIndex, innerAssigner);
} else if (windowingStrategy instanceof SliceAttachedWindowingStrategy) {
int sliceEndIndex = ((SliceAttachedWindowingStrategy) windowingStrategy).getSliceEnd();
// we don't need time attribute to assign windows, use a magic value in this case
SliceAssigner innerAssigner = createSliceAssigner(windowSpec, Integer.MAX_VALUE, shiftTimeZone);
return SliceAssigners.sliced(sliceEndIndex, innerAssigner);
} else if (windowingStrategy instanceof TimeAttributeWindowingStrategy) {
final int timeAttributeIndex;
if (windowingStrategy.isRowtime()) {
timeAttributeIndex = ((TimeAttributeWindowingStrategy) windowingStrategy).getTimeAttributeIndex();
} else {
timeAttributeIndex = -1;
}
return createSliceAssigner(windowSpec, timeAttributeIndex, shiftTimeZone);
} else {
throw new UnsupportedOperationException(windowingStrategy + " is not supported yet.");
}
}
use of org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy in project flink by apache.
the class TwoStageOptimizedWindowAggregateRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final StreamPhysicalWindowAggregate windowAgg = call.rel(0);
final RelNode realInput = call.rel(2);
final WindowingStrategy windowing = windowAgg.windowing();
RelTraitSet localTraitSet = realInput.getTraitSet().plus(ModifyKindSetTrait.INSERT_ONLY()).plus(UpdateKindTrait.NONE());
StreamPhysicalLocalWindowAggregate localAgg = new StreamPhysicalLocalWindowAggregate(windowAgg.getCluster(), localTraitSet, realInput, windowAgg.grouping(), windowAgg.aggCalls(), windowing);
// grouping keys is forwarded by local agg, use indices instead of groupings
int[] globalGrouping = IntStream.range(0, windowAgg.grouping().length).toArray();
FlinkRelDistribution globalDistribution = createDistribution(globalGrouping);
// create exchange if needed
RelNode newInput = FlinkExpandConversionRule.satisfyDistribution(FlinkConventions.STREAM_PHYSICAL(), localAgg, globalDistribution);
RelTraitSet globalAggProvidedTraitSet = windowAgg.getTraitSet();
// we put sliceEnd/windowEnd at the end of local output fields
int endIndex = localAgg.getRowType().getFieldCount() - 1;
final WindowingStrategy globalWindowing;
if (windowing instanceof TimeAttributeWindowingStrategy) {
globalWindowing = new SliceAttachedWindowingStrategy(windowing.getWindow(), windowing.getTimeAttributeType(), endIndex);
} else {
globalWindowing = new WindowAttachedWindowingStrategy(windowing.getWindow(), windowing.getTimeAttributeType(), endIndex);
}
StreamPhysicalGlobalWindowAggregate globalAgg = new StreamPhysicalGlobalWindowAggregate(windowAgg.getCluster(), globalAggProvidedTraitSet, newInput, realInput.getRowType(), globalGrouping, windowAgg.aggCalls(), globalWindowing, windowAgg.namedWindowProperties());
call.transformTo(globalAgg);
}
use of org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy in project flink by apache.
the class StreamExecWindowJoin method translateToPlanInternal.
@Override
@SuppressWarnings("unchecked")
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
int leftWindowEndIndex = ((WindowAttachedWindowingStrategy) leftWindowing).getWindowEnd();
int rightWindowEndIndex = ((WindowAttachedWindowingStrategy) rightWindowing).getWindowEnd();
final ExecEdge leftInputEdge = getInputEdges().get(0);
final ExecEdge rightInputEdge = getInputEdges().get(1);
final Transformation<RowData> leftTransform = (Transformation<RowData>) leftInputEdge.translateToPlan(planner);
final Transformation<RowData> rightTransform = (Transformation<RowData>) rightInputEdge.translateToPlan(planner);
final RowType leftType = (RowType) leftInputEdge.getOutputType();
final RowType rightType = (RowType) rightInputEdge.getOutputType();
JoinUtil.validateJoinSpec(joinSpec, leftType, rightType, true);
final int[] leftJoinKey = joinSpec.getLeftKeys();
final int[] rightJoinKey = joinSpec.getRightKeys();
final InternalTypeInfo<RowData> leftTypeInfo = InternalTypeInfo.of(leftType);
final InternalTypeInfo<RowData> rightTypeInfo = InternalTypeInfo.of(rightType);
GeneratedJoinCondition generatedCondition = JoinUtil.generateConditionFunction(config.getTableConfig(), joinSpec, leftType, rightType);
ZoneId shiftTimeZone = TimeWindowUtil.getShiftTimeZone(leftWindowing.getTimeAttributeType(), config.getLocalTimeZone());
WindowJoinOperator operator = WindowJoinOperatorBuilder.builder().leftSerializer(leftTypeInfo.toRowSerializer()).rightSerializer(rightTypeInfo.toRowSerializer()).generatedJoinCondition(generatedCondition).leftWindowEndIndex(leftWindowEndIndex).rightWindowEndIndex(rightWindowEndIndex).filterNullKeys(joinSpec.getFilterNulls()).joinType(joinSpec.getJoinType()).withShiftTimezone(shiftTimeZone).build();
final RowType returnType = (RowType) getOutputType();
final TwoInputTransformation<RowData, RowData, RowData> transform = ExecNodeUtil.createTwoInputTransformation(leftTransform, rightTransform, createTransformationMeta(WINDOW_JOIN_TRANSFORMATION, config), operator, InternalTypeInfo.of(returnType), leftTransform.getParallelism());
// set KeyType and Selector for state
RowDataKeySelector leftSelect = KeySelectorUtil.getRowDataSelector(leftJoinKey, leftTypeInfo);
RowDataKeySelector rightSelect = KeySelectorUtil.getRowDataSelector(rightJoinKey, rightTypeInfo);
transform.setStateKeySelectors(leftSelect, rightSelect);
transform.setStateKeyType(leftSelect.getProducedType());
return transform;
}
use of org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy in project flink by apache.
the class StreamExecWindowDeduplicate method translateToPlanInternal.
@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
// validate window strategy
if (!windowing.isRowtime()) {
throw new TableException("Processing time Window Deduplication is not supported yet.");
}
int windowEndIndex;
if (windowing instanceof WindowAttachedWindowingStrategy) {
windowEndIndex = ((WindowAttachedWindowingStrategy) windowing).getWindowEnd();
} else {
throw new UnsupportedOperationException(windowing.getClass().getName() + " is not supported yet.");
}
ExecEdge inputEdge = getInputEdges().get(0);
Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
ZoneId shiftTimeZone = TimeWindowUtil.getShiftTimeZone(windowing.getTimeAttributeType(), config.getLocalTimeZone());
RowType inputType = (RowType) inputEdge.getOutputType();
RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(partitionKeys, InternalTypeInfo.of(inputType));
OneInputStreamOperator<RowData, RowData> operator = RowTimeWindowDeduplicateOperatorBuilder.builder().inputSerializer(new RowDataSerializer(inputType)).shiftTimeZone(shiftTimeZone).keySerializer((PagedTypeSerializer<RowData>) selector.getProducedType().toSerializer()).keepLastRow(keepLastRow).rowtimeIndex(orderKey).windowEndIndex(windowEndIndex).build();
OneInputTransformation<RowData, RowData> transform = ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationMeta(WINDOW_DEDUPLICATE_TRANSFORMATION, config), SimpleOperatorFactory.of(operator), InternalTypeInfo.of(getOutputType()), inputTransform.getParallelism(), WINDOW_RANK_MEMORY_RATIO);
// set KeyType and Selector for state
transform.setStateKeySelector(selector);
transform.setStateKeyType(selector.getProducedType());
return transform;
}
use of org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy in project flink by apache.
the class StreamExecWindowRank method translateToPlanInternal.
@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
// validate rank type
switch(rankType) {
case ROW_NUMBER:
break;
case RANK:
throw new TableException("RANK() function is not supported on Window TopN currently, only ROW_NUMBER() is supported.");
case DENSE_RANK:
throw new TableException("DENSE_RANK() function is not supported on Window TopN currently, only ROW_NUMBER() is supported.");
default:
throw new TableException(String.format("%s() function is not supported on Window TopN currently, only ROW_NUMBER() is supported.", rankType));
}
// validate window strategy
if (!windowing.isRowtime()) {
throw new TableException("Processing time Window TopN is not supported yet.");
}
int windowEndIndex;
if (windowing instanceof WindowAttachedWindowingStrategy) {
windowEndIndex = ((WindowAttachedWindowingStrategy) windowing).getWindowEnd();
} else {
throw new UnsupportedOperationException(windowing.getClass().getName() + " is not supported yet.");
}
ExecEdge inputEdge = getInputEdges().get(0);
RowType inputType = (RowType) inputEdge.getOutputType();
// validate rank range
ConstantRankRange constantRankRange;
if (rankRange instanceof ConstantRankRange) {
constantRankRange = (ConstantRankRange) rankRange;
} else {
throw new TableException(String.format("Rank strategy %s is not supported on window rank currently.", rankRange.toString(inputType.getFieldNames())));
}
Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
InternalTypeInfo<RowData> inputRowTypeInfo = InternalTypeInfo.of(inputType);
int[] sortFields = sortSpec.getFieldIndices();
RowDataKeySelector sortKeySelector = KeySelectorUtil.getRowDataSelector(sortFields, inputRowTypeInfo);
SortSpec.SortSpecBuilder builder = SortSpec.builder();
IntStream.range(0, sortFields.length).forEach(idx -> builder.addField(idx, sortSpec.getFieldSpec(idx).getIsAscendingOrder(), sortSpec.getFieldSpec(idx).getNullIsLast()));
SortSpec sortSpecInSortKey = builder.build();
ZoneId shiftTimeZone = TimeWindowUtil.getShiftTimeZone(windowing.getTimeAttributeType(), config.getLocalTimeZone());
GeneratedRecordComparator sortKeyComparator = ComparatorCodeGenerator.gen(config.getTableConfig(), "StreamExecSortComparator", RowType.of(sortSpec.getFieldTypes(inputType)), sortSpecInSortKey);
RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(partitionSpec.getFieldIndices(), inputRowTypeInfo);
OneInputStreamOperator<RowData, RowData> operator = WindowRankOperatorBuilder.builder().inputSerializer(new RowDataSerializer(inputType)).shiftTimeZone(shiftTimeZone).keySerializer((PagedTypeSerializer<RowData>) selector.getProducedType().toSerializer()).sortKeySelector(sortKeySelector).sortKeyComparator(sortKeyComparator).outputRankNumber(outputRankNumber).rankStart(constantRankRange.getRankStart()).rankEnd(constantRankRange.getRankEnd()).windowEndIndex(windowEndIndex).build();
OneInputTransformation<RowData, RowData> transform = ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationMeta(WINDOW_RANK_TRANSFORMATION, config), SimpleOperatorFactory.of(operator), InternalTypeInfo.of(getOutputType()), inputTransform.getParallelism(), WINDOW_RANK_MEMORY_RATIO);
// set KeyType and Selector for state
transform.setStateKeySelector(selector);
transform.setStateKeyType(selector.getProducedType());
return transform;
}
Aggregations