use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testGetPredicatesForFilter.
@Test
public void testGetPredicatesForFilter() throws Exception {
final FrameworkConfig config = RelBuilderTest.config().build();
final RelBuilder builder = RelBuilder.create(config);
RelNode filter = builder.scan("EMP").filter(builder.call(NONDETERMINISTIC_OP)).build();
RelMetadataQuery mq = RelMetadataQuery.instance();
assertTrue(mq.getPulledUpPredicates(filter).pulledUpPredicates.isEmpty());
RelNode filter1 = builder.scan("EMP").filter(builder.call(SqlStdOperatorTable.EQUALS, builder.field(1, 0, 0), builder.field(1, 0, 1))).build();
assertEquals("=($0, $1)", mq.getPulledUpPredicates(filter1).pulledUpPredicates.get(0).toString());
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testDistributionSingleton.
@Test
public void testDistributionSingleton() {
final RelNode rel = convertSql("select * from emp");
final RelDistribution dist = RelDistributions.SINGLETON;
final LogicalExchange exchange = LogicalExchange.create(rel, dist);
final RelMetadataQuery mq = RelMetadataQuery.instance();
RelDistribution d = mq.getDistribution(exchange);
assertThat(d, is(dist));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testGroupByEmptyHavingUniqueKeys.
@Test
public void testGroupByEmptyHavingUniqueKeys() {
RelNode rel = convertSql("select count(*) from emp where 1 = 1");
final RelMetadataQuery mq = RelMetadataQuery.instance();
final Set<ImmutableBitSet> result = mq.getUniqueKeys(rel);
assertThat(result, CoreMatchers.<Set<ImmutableBitSet>>equalTo(ImmutableSet.of(ImmutableBitSet.of())));
assertUniqueConsistent(rel);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testGroupBy.
@Test
public void testGroupBy() {
RelNode rel = convertSql("select deptno, count(*), sum(sal) from emp\n" + "group by deptno");
final RelMetadataQuery mq = RelMetadataQuery.instance();
final Set<ImmutableBitSet> result = mq.getUniqueKeys(rel);
assertThat(result, CoreMatchers.<Set<ImmutableBitSet>>equalTo(ImmutableSet.of(ImmutableBitSet.of(0))));
assertUniqueConsistent(rel);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class AggregateFilterTransposeRule method onMatch.
public void onMatch(RelOptRuleCall call) {
final Aggregate aggregate = call.rel(0);
final Filter filter = call.rel(1);
// Do the columns used by the filter appear in the output of the aggregate?
final ImmutableBitSet filterColumns = RelOptUtil.InputFinder.bits(filter.getCondition());
final ImmutableBitSet newGroupSet = aggregate.getGroupSet().union(filterColumns);
final RelNode input = filter.getInput();
final RelMetadataQuery mq = call.getMetadataQuery();
final Boolean unique = mq.areColumnsUnique(input, newGroupSet);
if (unique != null && unique) {
// the rule fires forever: A-F => A-F-A => A-A-F-A => A-A-A-F-A => ...
return;
}
boolean allColumnsInAggregate = aggregate.getGroupSet().contains(filterColumns);
final Aggregate newAggregate = aggregate.copy(aggregate.getTraitSet(), input, false, newGroupSet, null, aggregate.getAggCallList());
final Mappings.TargetMapping mapping = Mappings.target(new Function<Integer, Integer>() {
public Integer apply(Integer a0) {
return newGroupSet.indexOf(a0);
}
}, input.getRowType().getFieldCount(), newGroupSet.cardinality());
final RexNode newCondition = RexUtil.apply(mapping, filter.getCondition());
final Filter newFilter = filter.copy(filter.getTraitSet(), newAggregate, newCondition);
if (allColumnsInAggregate && aggregate.getGroupType() == Group.SIMPLE) {
// Everything needed by the filter is returned by the aggregate.
assert newGroupSet.equals(aggregate.getGroupSet());
call.transformTo(newFilter);
} else {
// If aggregate uses grouping sets, we always need to split it.
// Otherwise, it means that grouping sets are not used, but the
// filter needs at least one extra column, and now aggregate it away.
final ImmutableBitSet.Builder topGroupSet = ImmutableBitSet.builder();
for (int c : aggregate.getGroupSet()) {
topGroupSet.set(newGroupSet.indexOf(c));
}
ImmutableList<ImmutableBitSet> newGroupingSets = null;
if (aggregate.getGroupType() != Group.SIMPLE) {
ImmutableList.Builder<ImmutableBitSet> newGroupingSetsBuilder = ImmutableList.builder();
for (ImmutableBitSet groupingSet : aggregate.getGroupSets()) {
final ImmutableBitSet.Builder newGroupingSet = ImmutableBitSet.builder();
for (int c : groupingSet) {
newGroupingSet.set(newGroupSet.indexOf(c));
}
newGroupingSetsBuilder.add(newGroupingSet.build());
}
newGroupingSets = newGroupingSetsBuilder.build();
}
final List<AggregateCall> topAggCallList = Lists.newArrayList();
int i = newGroupSet.cardinality();
for (AggregateCall aggregateCall : aggregate.getAggCallList()) {
final SqlAggFunction rollup = SubstitutionVisitor.getRollup(aggregateCall.getAggregation());
if (rollup == null) {
// This aggregate cannot be rolled up.
return;
}
if (aggregateCall.isDistinct()) {
// Cannot roll up distinct.
return;
}
topAggCallList.add(AggregateCall.create(rollup, aggregateCall.isDistinct(), aggregateCall.isApproximate(), ImmutableList.of(i++), -1, aggregateCall.type, aggregateCall.name));
}
final Aggregate topAggregate = aggregate.copy(aggregate.getTraitSet(), newFilter, aggregate.indicator, topGroupSet.build(), newGroupingSets, topAggCallList);
call.transformTo(topAggregate);
}
}
Aggregations