Search in sources :

Example 51 with RelMetadataQuery

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

the class RelMetadataTest method checkPercentageOriginalRows.

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

Example 52 with RelMetadataQuery

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

the class RelMetadataTest method testExpressionLineageStar.

@Test
public void testExpressionLineageStar() {
    // All columns in output
    final RelNode tableRel = convertSql("select * from emp");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RexNode ref = RexInputRef.of(4, tableRel.getRowType().getFieldList());
    final Set<RexNode> r = mq.getExpressionLineage(tableRel, ref);
    final String inputRef = RexInputRef.of(4, tableRel.getRowType().getFieldList()).toString();
    assertThat(r.size(), is(1));
    final String resultString = r.iterator().next().toString();
    assertThat(resultString, startsWith(EMP_QNAME.toString()));
    assertThat(resultString, endsWith(inputRef));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 53 with RelMetadataQuery

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

the class PlannerTest method checkMetadataPredicates.

/**
 * Helper method for testing {@link RelMetadataQuery#getPulledUpPredicates}
 * metadata.
 */
private void checkMetadataPredicates(String sql, String expectedPredicates) throws Exception {
    Planner planner = getPlanner(null);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelNode rel = planner.rel(validate).project();
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RelOptPredicateList predicates = mq.getPulledUpPredicates(rel);
    final String buf = predicates.pulledUpPredicates.toString();
    assertThat(buf, equalTo(expectedPredicates));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SqlNode(org.apache.calcite.sql.SqlNode)

Example 54 with RelMetadataQuery

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

the class TraitPropagationTest method testOne.

@Test
public void testOne() throws Exception {
    RelNode planned = run(new PropAction(), RULES);
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println(RelOptUtil.dumpPlan("LOGICAL PLAN", planned, SqlExplainFormat.TEXT, SqlExplainLevel.ALL_ATTRIBUTES));
    }
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    assertEquals("Sortedness was not propagated", 3, mq.getCumulativeCost(planned).getRows(), 0);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) AbstractRelNode(org.apache.calcite.rel.AbstractRelNode) RelNode(org.apache.calcite.rel.RelNode) Test(org.junit.Test)

Example 55 with RelMetadataQuery

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

the class NumberingRelWriter method explain_.

// ~ Methods ----------------------------------------------------------------
protected void explain_(RelNode rel, List<Pair<String, Object>> values) {
    List<RelNode> inputs = rel.getInputs();
    if (rel instanceof HashJoinPrel && ((HashJoinPrel) rel).isSwapped()) {
        HashJoinPrel joinPrel = (HashJoinPrel) rel;
        inputs = FlatLists.of(joinPrel.getRight(), joinPrel.getLeft());
    }
    RelMetadataQuery mq = RelMetadataQuery.instance();
    if (!mq.isVisibleInExplain(rel, detailLevel)) {
        // render children in place of this, at same level
        explainInputs(inputs);
        return;
    }
    StringBuilder s = new StringBuilder();
    OpId id = ids.get(rel);
    if (id != null) {
        s.append(String.format("%02d-%02d", id.fragmentId, id.opId));
    } else {
        s.append("     ");
    }
    s.append("  ");
    if (id != null && id.opId == 0) {
        for (int i = 0; i < spacer.get(); i++) {
            s.append('-');
        }
    } else {
        spacer.spaces(s);
    }
    s.append("  ");
    s.append(rel.getRelTypeName().replace("Prel", ""));
    if (detailLevel != SqlExplainLevel.NO_ATTRIBUTES) {
        int j = 0;
        for (Pair<String, Object> value : values) {
            if (value.right instanceof RelNode) {
                continue;
            }
            if (j++ == 0) {
                s.append("(");
            } else {
                s.append(", ");
            }
            s.append(value.left).append("=[").append(value.right).append("]");
        }
        if (j > 0) {
            s.append(")");
        }
    }
    if (detailLevel == SqlExplainLevel.ALL_ATTRIBUTES) {
        s.append(" : rowType = ").append(rel.getRowType()).append(": rowcount = ").append(mq.getRowCount(rel)).append(", cumulative cost = ").append(mq.getCumulativeCost(rel)).append(", id = ").append(rel.getId());
    }
    pw.println(s);
    spacer.add(2);
    explainInputs(inputs);
    spacer.subtract(2);
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) HashJoinPrel(org.apache.drill.exec.planner.physical.HashJoinPrel) OpId(org.apache.drill.exec.planner.physical.explain.PrelSequencer.OpId)

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