use of org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery 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);
}
}
use of org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery in project flink by apache.
the class CatalogStatisticsTest method testGetPartitionStatsFromCatalog.
@Test
public void testGetPartitionStatsFromCatalog() throws Exception {
TestPartitionableSourceFactory.createTemporaryTable(tEnv, "PartT", true);
createPartitionStats("A", 1);
createPartitionColumnStats("A", 1);
createPartitionStats("A", 2);
createPartitionColumnStats("A", 2);
RelNode t1 = ((PlannerBase) ((TableEnvironmentImpl) tEnv).getPlanner()).optimize(TableTestUtil.toRelNode(tEnv.sqlQuery("select id, name from PartT where part1 = 'A'")));
FlinkRelMetadataQuery mq = FlinkRelMetadataQuery.reuseOrCreate(t1.getCluster().getMetadataQuery());
assertEquals(200.0, mq.getRowCount(t1), 0.0);
assertEquals(Arrays.asList(8.0, 43.5), mq.getAverageColumnSizes(t1));
// long type
assertEquals(46.0, mq.getDistinctRowCount(t1, ImmutableBitSet.of(0), null), 0.0);
assertEquals(154.0, mq.getColumnNullCount(t1, 0), 0.0);
assertEquals(ValueInterval$.MODULE$.apply(BigDecimal.valueOf(-123L), BigDecimal.valueOf(763322L), true, true), mq.getColumnInterval(t1, 0));
// string type
assertEquals(40.0, mq.getDistinctRowCount(t1, ImmutableBitSet.of(1), null), 0.0);
assertEquals(0.0, mq.getColumnNullCount(t1, 1), 0.0);
assertNull(mq.getColumnInterval(t1, 1));
}
use of org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery in project flink by apache.
the class CatalogStatisticsTest method assertStatistics.
private void assertStatistics(TableEnvironment tEnv, String tableName) {
RelNode t1 = TableTestUtil.toRelNode(tEnv.sqlQuery("select * from " + tableName));
FlinkRelMetadataQuery mq = FlinkRelMetadataQuery.reuseOrCreate(t1.getCluster().getMetadataQuery());
assertEquals(100.0, mq.getRowCount(t1), 0.0);
assertColumnStatistics(t1, mq);
}
use of org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery in project flink by apache.
the class RelTimeIndicatorConverter method gatherIndicesToMaterialize.
private Set<Integer> gatherIndicesToMaterialize(Aggregate agg, RelNode newInput) {
List<RelDataType> inputFieldTypes = RelOptUtil.getFieldTypeList(newInput.getRowType());
Predicate<Integer> isTimeIndicator = idx -> isTimeIndicatorType(inputFieldTypes.get(idx));
// add arguments of agg calls
Set<Integer> aggCallArgs = agg.getAggCallList().stream().map(AggregateCall::getArgList).flatMap(List::stream).filter(isTimeIndicator).collect(Collectors.toSet());
FlinkRelMetadataQuery fmq = FlinkRelMetadataQuery.reuseOrCreate(agg.getCluster().getMetadataQuery());
RelWindowProperties windowProps = fmq.getRelWindowProperties(newInput);
// add grouping sets
Set<Integer> groupSets = agg.getGroupSets().stream().map(grouping -> {
if (windowProps != null && groupingContainsWindowStartEnd(grouping, windowProps)) {
// of window_time column
return grouping.except(windowProps.getWindowTimeColumns());
} else {
return grouping;
}
}).flatMap(set -> set.asList().stream()).filter(isTimeIndicator).collect(Collectors.toSet());
Set<Integer> timeIndicatorIndices = new HashSet<>(aggCallArgs);
timeIndicatorIndices.addAll(groupSets);
return timeIndicatorIndices;
}
use of org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery in project flink by apache.
the class CatalogConstraintTest method testWithoutPrimaryKey.
@Test
public void testWithoutPrimaryKey() throws Exception {
TableSchema tableSchema = TableSchema.builder().fields(new String[] { "a", "b", "c" }, new DataType[] { DataTypes.BIGINT(), DataTypes.STRING(), DataTypes.INT() }).build();
Map<String, String> properties = buildCatalogTableProperties(tableSchema);
catalog.createTable(new ObjectPath(databaseName, "T1"), new CatalogTableImpl(tableSchema, properties, ""), false);
RelNode t1 = TableTestUtil.toRelNode(tEnv.sqlQuery("select * from T1"));
FlinkRelMetadataQuery mq = FlinkRelMetadataQuery.reuseOrCreate(t1.getCluster().getMetadataQuery());
assertEquals(ImmutableSet.of(), mq.getUniqueKeys(t1));
}
Aggregations