use of org.apache.flink.table.planner.plan.trait.RelModifiedMonotonicity 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);
}
}
Aggregations