Search in sources :

Example 76 with RelMetadataQuery

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

the class RelMetadataTest method testAllPredicatesAggregate3.

@Test
public void testAllPredicatesAggregate3() {
    final String sql = "select * from (select a, max(b) as b from (\n" + "  select empno as a, sal as b from emp)subq\n" + "group by a) \n" + "where b = 5";
    final RelNode rel = convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    RelOptPredicateList inputSet = mq.getAllPredicates(rel);
    // Filter on aggregate, we cannot infer lineage
    assertNull(inputSet);
}
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 77 with RelMetadataQuery

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

the class RelMetadataTest method testPullUpPredicatesFromAggregation.

/**
 * Unit test for
 * {@link org.apache.calcite.rel.metadata.RelMdPredicates#getPredicates(Aggregate, RelMetadataQuery)}.
 */
@Test
public void testPullUpPredicatesFromAggregation() {
    final String sql = "select a, max(b) from (\n" + "  select 1 as a, 2 as b from emp)subq\n" + "group by a";
    final Aggregate rel = (Aggregate) convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    RelOptPredicateList inputSet = mq.getPulledUpPredicates(rel);
    ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
    assertThat(pulledUpPredicates, sortsAs("[=($0, 1)]"));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) Aggregate(org.apache.calcite.rel.core.Aggregate) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 78 with RelMetadataQuery

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

the class RelMetadataTest method checkRowCount.

private void checkRowCount(String sql, double expected, double expectedMin, double expectedMax) {
    RelNode rel = convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final Double result = mq.getRowCount(rel);
    assertThat(result, notNullValue());
    assertEquals(expected, result, 0d);
    final Double max = mq.getMaxRowCount(rel);
    assertThat(max, notNullValue());
    assertEquals(expectedMax, max, 0d);
    final Double min = mq.getMinRowCount(rel);
    assertThat(max, notNullValue());
    assertEquals(expectedMin, min, 0d);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode)

Example 79 with RelMetadataQuery

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

the class RelMetadataTest method testPullUpPredicatesOnConstant.

@Test
public void testPullUpPredicatesOnConstant() {
    final String sql = "select deptno, mgr, x, 'y' as y, z from (\n" + "  select deptno, mgr, cast(null as integer) as x, cast('1' as int) as z\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);
    assertThat(list.pulledUpPredicates, sortsAs("[<($0, 10), =($3, 'y'), =($4, 1), IS NULL($1), IS NULL($2)]"));
}
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 80 with RelMetadataQuery

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

the class RelMetadataTest method testPullUpPredicatesForExprsItr.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1960">[CALCITE-1960]
 * RelMdPredicates.getPredicates is slow if there are many equivalent
 * columns</a>. There are much less duplicates after
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2205">[CALCITE-2205]</a>.
 * Since this is a performance problem, the test result does not
 * change, but takes over 15 minutes before the fix and 6 seconds after.
 */
@Test(timeout = 20_000)
public void testPullUpPredicatesForExprsItr() {
    // If we're running Windows, we are probably in a VM and the test may
    // exceed timeout by a small margin.
    Assume.assumeThat("Too slow to run on Windows", File.separatorChar, Is.is('/'));
    final String sql = "select a.EMPNO, a.ENAME\n" + "from (select * from sales.emp ) a\n" + "join (select * from sales.emp  ) b\n" + "on a.empno = b.deptno\n" + "  and a.comm = b.comm\n" + "  and a.mgr=b.mgr\n" + "  and (a.empno < 10 or a.comm < 3 or a.deptno < 10\n" + "    or a.job ='abc' or a.ename='abc' or a.sal='30' or a.mgr >3\n" + "    or a.slacker is not null  or a.HIREDATE is not null\n" + "    or b.empno < 9 or b.comm < 3 or b.deptno < 10 or b.job ='abc'\n" + "    or b.ename='abc' or b.sal='30' or b.mgr >3 or b.slacker )\n" + "join emp c\n" + "on b.mgr =a.mgr and a.empno =b.deptno and a.comm=b.comm\n" + "  and a.deptno=b.deptno and a.job=b.job and a.ename=b.ename\n" + "  and a.mgr=b.deptno and a.slacker=b.slacker";
    // Lock to ensure that only one test is using this method at a time.
    try (final JdbcAdapterTest.LockWrapper ignore = JdbcAdapterTest.LockWrapper.lock(LOCK)) {
        final RelNode rel = convertSql(sql);
        final RelMetadataQuery mq = RelMetadataQuery.instance();
        RelOptPredicateList inputSet = mq.getPulledUpPredicates(rel.getInput(0));
        assertThat(inputSet.pulledUpPredicates.size(), is(18));
    }
}
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)

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