Search in sources :

Example 71 with RelMetadataQuery

use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.

the class EnumerableLimit method create.

/**
 * Creates an EnumerableLimit.
 */
public static EnumerableLimit create(final RelNode input, RexNode offset, RexNode fetch) {
    final RelOptCluster cluster = input.getCluster();
    final RelMetadataQuery mq = cluster.getMetadataQuery();
    final RelTraitSet traitSet = cluster.traitSetOf(EnumerableConvention.INSTANCE).replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {

        public List<RelCollation> get() {
            return RelMdCollation.limit(mq, input);
        }
    }).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {

        public RelDistribution get() {
            return RelMdDistribution.limit(mq, input);
        }
    });
    return new EnumerableLimit(cluster, traitSet, input, offset, fetch);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelCollation(org.apache.calcite.rel.RelCollation) Supplier(com.google.common.base.Supplier) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelDistribution(org.apache.calcite.rel.RelDistribution)

Example 72 with RelMetadataQuery

use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.

the class EnumerableMergeJoin method create.

public static EnumerableMergeJoin create(RelNode left, RelNode right, RexLiteral condition, ImmutableIntList leftKeys, ImmutableIntList rightKeys, JoinRelType joinType) throws InvalidRelException {
    final RelOptCluster cluster = right.getCluster();
    RelTraitSet traitSet = cluster.traitSet();
    if (traitSet.isEnabled(RelCollationTraitDef.INSTANCE)) {
        final RelMetadataQuery mq = cluster.getMetadataQuery();
        final List<RelCollation> collations = RelMdCollation.mergeJoin(mq, left, right, leftKeys, rightKeys);
        traitSet = traitSet.replace(collations);
    }
    return new EnumerableMergeJoin(cluster, traitSet, left, right, condition, leftKeys, rightKeys, ImmutableSet.<CorrelationId>of(), joinType);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelCollation(org.apache.calcite.rel.RelCollation) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 73 with RelMetadataQuery

use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.

the class RelOptUtil method containsNullableFields.

/**
 * Determines whether any of the fields in a given relational expression may
 * contain null values, taking into account constraints on the field types and
 * also deduced predicates.
 *
 * <p>The method is cautious: It may sometimes return {@code true} when the
 * actual answer is {@code false}. In particular, it does this when there
 * is no executor, or the executor is not a sub-class of
 * {@link RexExecutorImpl}.
 */
private static boolean containsNullableFields(RelNode r) {
    final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
    final RelDataType rowType = r.getRowType();
    final List<RexNode> list = new ArrayList<>();
    final RelMetadataQuery mq = r.getCluster().getMetadataQuery();
    for (RelDataTypeField field : rowType.getFieldList()) {
        if (field.getType().isNullable()) {
            list.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, rexBuilder.makeInputRef(field.getType(), field.getIndex())));
        }
    }
    if (list.isEmpty()) {
        // All columns are declared NOT NULL.
        return false;
    }
    final RelOptPredicateList predicates = mq.getPulledUpPredicates(r);
    if (predicates.pulledUpPredicates.isEmpty()) {
        // declared NULL are really NOT NULL.
        return true;
    }
    final RexExecutor executor = r.getCluster().getPlanner().getExecutor();
    if (!(executor instanceof RexExecutorImpl)) {
        // Cannot proceed without an executor.
        return true;
    }
    final RexImplicationChecker checker = new RexImplicationChecker(rexBuilder, (RexExecutorImpl) executor, rowType);
    final RexNode first = RexUtil.composeConjunction(rexBuilder, predicates.pulledUpPredicates, false);
    final RexNode second = RexUtil.composeConjunction(rexBuilder, list, false);
    // It does, so we have no nullable columns.
    return !checker.implies(first, second);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexExecutor(org.apache.calcite.rex.RexExecutor) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 74 with RelMetadataQuery

use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.

the class RelMetadataTest method testExpressionLineageInnerJoinRight.

@Test
public void testExpressionLineageInnerJoinRight() {
    // ename is column 0 in catalog.sales.bonus
    final RelNode rel = convertSql("select bonus.ename from emp join bonus using (ename)");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RexNode ref = RexInputRef.of(0, rel.getRowType().getFieldList());
    final Set<RexNode> r = mq.getExpressionLineage(rel, ref);
    assertThat(r.size(), is(1));
    final RexTableInputRef result = (RexTableInputRef) r.iterator().next();
    assertTrue(result.getQualifiedName().equals(ImmutableList.of("CATALOG", "SALES", "BONUS")));
    assertThat(result.getIndex(), is(0));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RexTableInputRef(org.apache.calcite.rex.RexTableInputRef) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 75 with RelMetadataQuery

use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.

the class RelMetadataTest method testGroupByEmptyUniqueKeys.

@Test
public void testGroupByEmptyUniqueKeys() {
    RelNode rel = convertSql("select count(*) from emp");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    Set<ImmutableBitSet> result = mq.getUniqueKeys(rel);
    assertThat(result, CoreMatchers.<Set<ImmutableBitSet>>equalTo(ImmutableSet.of(ImmutableBitSet.of())));
    assertUniqueConsistent(rel);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Test(org.junit.Test)

Aggregations

RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)122 RelNode (org.apache.calcite.rel.RelNode)88 Test (org.junit.Test)43 RexNode (org.apache.calcite.rex.RexNode)38 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)24 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)21 ArrayList (java.util.ArrayList)19 RexBuilder (org.apache.calcite.rex.RexBuilder)19 RelBuilder (org.apache.calcite.tools.RelBuilder)19 ImmutableList (com.google.common.collect.ImmutableList)12 Aggregate (org.apache.calcite.rel.core.Aggregate)12 RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)12 RelOptCluster (org.apache.calcite.plan.RelOptCluster)11 RelTraitSet (org.apache.calcite.plan.RelTraitSet)11 AggregateCall (org.apache.calcite.rel.core.AggregateCall)11 RelCollation (org.apache.calcite.rel.RelCollation)10 RelDistribution (org.apache.calcite.rel.RelDistribution)10 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)10 HashMap (java.util.HashMap)9 List (java.util.List)9