Search in sources :

Example 1 with ConstantRankRange

use of org.apache.flink.table.runtime.operators.rank.ConstantRankRange in project flink by apache.

the class RankRangeSerdeTest method testRankRange.

@Test
public void testRankRange() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    RankRange[] ranges = new RankRange[] { new ConstantRankRange(1, 2), new VariableRankRange(3), new ConstantRankRangeWithoutEnd(4) };
    for (RankRange range : ranges) {
        RankRange result = mapper.readValue(mapper.writeValueAsString(range), RankRange.class);
        assertEquals(range.toString(), result.toString());
    }
}
Also used : RankRange(org.apache.flink.table.runtime.operators.rank.RankRange) ConstantRankRange(org.apache.flink.table.runtime.operators.rank.ConstantRankRange) VariableRankRange(org.apache.flink.table.runtime.operators.rank.VariableRankRange) ConstantRankRangeWithoutEnd(org.apache.flink.table.runtime.operators.rank.ConstantRankRangeWithoutEnd) VariableRankRange(org.apache.flink.table.runtime.operators.rank.VariableRankRange) ConstantRankRange(org.apache.flink.table.runtime.operators.rank.ConstantRankRange) ObjectMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with ConstantRankRange

use of org.apache.flink.table.runtime.operators.rank.ConstantRankRange 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;
}
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) ZoneId(java.time.ZoneId) ConstantRankRange(org.apache.flink.table.runtime.operators.rank.ConstantRankRange) WindowAttachedWindowingStrategy(org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy) RowType(org.apache.flink.table.types.logical.RowType) RowData(org.apache.flink.table.data.RowData) PagedTypeSerializer(org.apache.flink.table.runtime.typeutils.PagedTypeSerializer) RowDataKeySelector(org.apache.flink.table.runtime.keyselector.RowDataKeySelector) GeneratedRecordComparator(org.apache.flink.table.runtime.generated.GeneratedRecordComparator) SortSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.SortSpec) RowDataSerializer(org.apache.flink.table.runtime.typeutils.RowDataSerializer)

Aggregations

ConstantRankRange (org.apache.flink.table.runtime.operators.rank.ConstantRankRange)2 ZoneId (java.time.ZoneId)1 Transformation (org.apache.flink.api.dag.Transformation)1 ObjectMapper (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper)1 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)1 TableException (org.apache.flink.table.api.TableException)1 RowData (org.apache.flink.table.data.RowData)1 WindowAttachedWindowingStrategy (org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy)1 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)1 SortSpec (org.apache.flink.table.planner.plan.nodes.exec.spec.SortSpec)1 GeneratedRecordComparator (org.apache.flink.table.runtime.generated.GeneratedRecordComparator)1 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)1 ConstantRankRangeWithoutEnd (org.apache.flink.table.runtime.operators.rank.ConstantRankRangeWithoutEnd)1 RankRange (org.apache.flink.table.runtime.operators.rank.RankRange)1 VariableRankRange (org.apache.flink.table.runtime.operators.rank.VariableRankRange)1 PagedTypeSerializer (org.apache.flink.table.runtime.typeutils.PagedTypeSerializer)1 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)1 RowType (org.apache.flink.table.types.logical.RowType)1 Test (org.junit.Test)1