Search in sources :

Example 46 with RelMetadataQuery

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

the class RelMetadataTest method assertUniqueConsistent.

/**
 * Asserts that {@link RelMetadataQuery#getUniqueKeys(RelNode)}
 * and {@link RelMetadataQuery#areColumnsUnique(RelNode, ImmutableBitSet)}
 * return consistent results.
 */
private void assertUniqueConsistent(RelNode rel) {
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final Set<ImmutableBitSet> uniqueKeys = mq.getUniqueKeys(rel);
    final ImmutableBitSet allCols = ImmutableBitSet.range(0, rel.getRowType().getFieldCount());
    for (ImmutableBitSet key : allCols.powerSet()) {
        Boolean result2 = mq.areColumnsUnique(rel, key);
        assertTrue(result2 == null || result2 == isUnique(uniqueKeys, key));
    }
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet)

Example 47 with RelMetadataQuery

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

the class RelMetadataTest method checkFilterSelectivity.

private void checkFilterSelectivity(String sql, double expected) {
    RelNode rel = convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    Double result = mq.getSelectivity(rel, null);
    assertTrue(result != null);
    assertEquals(expected, result, EPSILON);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode)

Example 48 with RelMetadataQuery

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

the class RelMetadataTest method testPullUpPredicatesOnNullableConstant.

@Test
public void testPullUpPredicatesOnNullableConstant() {
    final String sql = "select nullif(1, 1) as c\n" + "  from emp\n" + "  where mgr is null and deptno < 10";
    final RelNode rel = convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    RelOptPredicateList list = mq.getPulledUpPredicates(rel);
    // Uses "IS NOT DISTINCT FROM" rather than "=" because cannot guarantee not null.
    assertThat(list.pulledUpPredicates, sortsAs("[IS NOT DISTINCT FROM($0, CASE(=(1, 1), null, 1))]"));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) Test(org.junit.Test)

Example 49 with RelMetadataQuery

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

the class RelMetadataTest method testExpressionLineageTwoColumnsSwapped.

@Test
public void testExpressionLineageTwoColumnsSwapped() {
    // deptno is column 7 in catalog.sales.emp
    // mgr is column 3 in catalog.sales.emp
    final RelNode rel = convertSql("select deptno, mgr from emp");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RexNode ref1 = RexInputRef.of(0, rel.getRowType().getFieldList());
    final Set<RexNode> r1 = mq.getExpressionLineage(rel, ref1);
    assertThat(r1.size(), is(1));
    final RexTableInputRef result1 = (RexTableInputRef) r1.iterator().next();
    assertTrue(result1.getQualifiedName().equals(EMP_QNAME));
    assertThat(result1.getIndex(), is(7));
    final RexNode ref2 = RexInputRef.of(1, rel.getRowType().getFieldList());
    final Set<RexNode> r2 = mq.getExpressionLineage(rel, ref2);
    assertThat(r2.size(), is(1));
    final RexTableInputRef result2 = (RexTableInputRef) r2.iterator().next();
    assertTrue(result2.getQualifiedName().equals(EMP_QNAME));
    assertThat(result2.getIndex(), is(3));
    assertThat(result1.getIdentifier(), is(result2.getIdentifier()));
}
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 50 with RelMetadataQuery

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

the class RelMetadataTest method testUnion.

@Test
public void testUnion() {
    RelNode rel = convertSql("select deptno from emp\n" + "union\n" + "select deptno from dept");
    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);
}
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