Search in sources :

Example 1 with StreamPhysicalRel

use of org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalRel in project flink by apache.

the class RankProcessStrategy method analyzeRankProcessStrategies.

/**
 * Gets {@link RankProcessStrategy} based on input, partitionKey and orderKey.
 */
static List<RankProcessStrategy> analyzeRankProcessStrategies(StreamPhysicalRel rank, ImmutableBitSet partitionKey, RelCollation orderKey) {
    FlinkRelMetadataQuery mq = (FlinkRelMetadataQuery) rank.getCluster().getMetadataQuery();
    List<RelFieldCollation> fieldCollations = orderKey.getFieldCollations();
    boolean isUpdateStream = !ChangelogPlanUtils.inputInsertOnly(rank);
    RelNode input = rank.getInput(0);
    if (isUpdateStream) {
        Set<ImmutableBitSet> upsertKeys = mq.getUpsertKeysInKeyGroupRange(input, partitionKey.toArray());
        if (upsertKeys == null || upsertKeys.isEmpty() || // upsert key should contains partition key
        upsertKeys.stream().noneMatch(k -> k.contains(partitionKey))) {
            // and we fall back to using retract rank
            return Collections.singletonList(RETRACT_STRATEGY);
        } else {
            FlinkRelMetadataQuery fmq = FlinkRelMetadataQuery.reuseOrCreate(mq);
            RelModifiedMonotonicity monotonicity = fmq.getRelModifiedMonotonicity(input);
            boolean isMonotonic = false;
            if (monotonicity != null && !fieldCollations.isEmpty()) {
                isMonotonic = fieldCollations.stream().allMatch(collation -> {
                    SqlMonotonicity fieldMonotonicity = monotonicity.fieldMonotonicities()[collation.getFieldIndex()];
                    RelFieldCollation.Direction direction = collation.direction;
                    if ((fieldMonotonicity == SqlMonotonicity.DECREASING || fieldMonotonicity == SqlMonotonicity.STRICTLY_DECREASING) && direction == RelFieldCollation.Direction.ASCENDING) {
                        // is decreasing
                        return true;
                    } else if ((fieldMonotonicity == SqlMonotonicity.INCREASING || fieldMonotonicity == SqlMonotonicity.STRICTLY_INCREASING) && direction == RelFieldCollation.Direction.DESCENDING) {
                        // is increasing
                        return true;
                    } else {
                        // it is monotonic
                        return fieldMonotonicity == SqlMonotonicity.CONSTANT;
                    }
                });
            }
            if (isMonotonic) {
                // TODO: choose a set of primary key
                return Arrays.asList(new UpdateFastStrategy(upsertKeys.iterator().next().toArray()), RETRACT_STRATEGY);
            } else {
                return Collections.singletonList(RETRACT_STRATEGY);
            }
        }
    } else {
        return Collections.singletonList(APPEND_FAST_STRATEGY);
    }
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) SqlMonotonicity(org.apache.calcite.sql.validate.SqlMonotonicity) Arrays(java.util.Arrays) JsonCreator(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator) FlinkRelMetadataQuery(org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery) RelModifiedMonotonicity(org.apache.flink.table.planner.plan.trait.RelModifiedMonotonicity) JsonSubTypes(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes) Set(java.util.Set) RelNode(org.apache.calcite.rel.RelNode) FlinkChangelogModeInferenceProgram(org.apache.flink.table.planner.plan.optimize.program.FlinkChangelogModeInferenceProgram) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) StringUtils(org.apache.commons.lang3.StringUtils) StreamPhysicalRel(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalRel) JsonProperty(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty) JsonIgnore(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnore) List(java.util.List) RelCollation(org.apache.calcite.rel.RelCollation) JsonTypeInfo(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeInfo) JsonIgnoreProperties(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnoreProperties) JsonTypeName(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName) Collections(java.util.Collections) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) SqlMonotonicity(org.apache.calcite.sql.validate.SqlMonotonicity) FlinkRelMetadataQuery(org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery) RelModifiedMonotonicity(org.apache.flink.table.planner.plan.trait.RelModifiedMonotonicity)

Aggregations

Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 RelCollation (org.apache.calcite.rel.RelCollation)1 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)1 RelNode (org.apache.calcite.rel.RelNode)1 SqlMonotonicity (org.apache.calcite.sql.validate.SqlMonotonicity)1 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)1 StringUtils (org.apache.commons.lang3.StringUtils)1 JsonCreator (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator)1 JsonIgnore (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnore)1 JsonIgnoreProperties (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 JsonProperty (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty)1 JsonSubTypes (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonTypeName (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName)1 FlinkRelMetadataQuery (org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery)1 StreamPhysicalRel (org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalRel)1 FlinkChangelogModeInferenceProgram (org.apache.flink.table.planner.plan.optimize.program.FlinkChangelogModeInferenceProgram)1