Search in sources :

Example 26 with RelMetadataQuery

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

the class RelMetadataTest method testCorrelateUniqueKeys.

@Test
public void testCorrelateUniqueKeys() {
    final String sql = "select *\n" + "from (select distinct deptno from emp) as e,\n" + "  lateral (\n" + "    select * from dept where dept.deptno = e.deptno)";
    final RelNode rel = convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    assertThat(rel, isA((Class) Project.class));
    final Project project = (Project) rel;
    final Set<ImmutableBitSet> result = mq.getUniqueKeys(project);
    assertThat(result, sortsAs("[{0}]"));
    if (false) {
        assertUniqueConsistent(project);
    }
    assertThat(project.getInput(), isA((Class) Correlate.class));
    final Correlate correlate = (Correlate) project.getInput();
    final Set<ImmutableBitSet> result2 = mq.getUniqueKeys(correlate);
    assertThat(result2, sortsAs("[{0}]"));
    if (false) {
        assertUniqueConsistent(correlate);
    }
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) Correlate(org.apache.calcite.rel.core.Correlate) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Test(org.junit.Test)

Example 27 with RelMetadataQuery

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

the class RelMetadataTest method checkNodeTypeCount.

private void checkNodeTypeCount(String sql, Map<Class<? extends RelNode>, Integer> expected) {
    final RelNode rel = convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final Multimap<Class<? extends RelNode>, RelNode> result = mq.getNodeTypes(rel);
    assertThat(result, notNullValue());
    final Map<Class<? extends RelNode>, Integer> resultCount = new HashMap<>();
    for (Entry<Class<? extends RelNode>, Collection<RelNode>> e : result.asMap().entrySet()) {
        resultCount.put(e.getKey(), e.getValue().size());
    }
    assertEquals(expected, resultCount);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) HashMap(java.util.HashMap) Collection(java.util.Collection)

Example 28 with RelMetadataQuery

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

the class RelMetadataTest method checkRelSelectivity.

private void checkRelSelectivity(RelNode rel, double expected) {
    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)

Example 29 with RelMetadataQuery

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

the class RelMetadataTest method testJoinUniqueKeys.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-509">[CALCITE-509]
 * "RelMdColumnUniqueness uses ImmutableBitSet.Builder twice, gets
 * NullPointerException"</a>.
 */
@Test
public void testJoinUniqueKeys() {
    RelNode rel = convertSql("select * from emp join bonus using (ename)");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    Set<ImmutableBitSet> result = mq.getUniqueKeys(rel);
    assertThat(result.isEmpty(), is(true));
    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)

Example 30 with RelMetadataQuery

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

the class RelMetadataTest method testDistinctRowCountTableEmptyKey.

@Test
public void testDistinctRowCountTableEmptyKey() {
    RelNode rel = convertSql("select * from emp where deptno = 10");
    // empty key
    ImmutableBitSet groupKey = ImmutableBitSet.of();
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    Double result = mq.getDistinctRowCount(rel, groupKey, null);
    assertThat(result, is(1D));
}
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)231 RelNode (org.apache.calcite.rel.RelNode)154 RexNode (org.apache.calcite.rex.RexNode)60 Test (org.junit.jupiter.api.Test)55 ArrayList (java.util.ArrayList)42 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)42 RelOptCluster (org.apache.calcite.plan.RelOptCluster)39 RexBuilder (org.apache.calcite.rex.RexBuilder)36 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)32 RelBuilder (org.apache.calcite.tools.RelBuilder)29 RelTraitSet (org.apache.calcite.plan.RelTraitSet)26 List (java.util.List)22 Aggregate (org.apache.calcite.rel.core.Aggregate)20 HashMap (java.util.HashMap)19 RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)19 AggregateCall (org.apache.calcite.rel.core.AggregateCall)18 RexInputRef (org.apache.calcite.rex.RexInputRef)18 ImmutableList (com.google.common.collect.ImmutableList)17 RelDataType (org.apache.calcite.rel.type.RelDataType)17 Join (org.apache.calcite.rel.core.Join)16