Search in sources :

Example 1 with Correlate

use of org.apache.calcite.rel.core.Correlate 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 2 with Correlate

use of org.apache.calcite.rel.core.Correlate in project flink by apache.

the class RelDecorrelator method getInvoke.

public Frame getInvoke(RelNode r, RelNode parent) {
    final Frame frame = dispatcher.invoke(r);
    // Should be removed after CALCITE-4333 is fixed
    if (frame != null && parent instanceof Correlate && r instanceof Sort) {
        Sort sort = (Sort) r;
        // of the correlation key actually.
        if (sort.offset != null || sort.fetch != null) {
            currentRel = parent;
            return null;
        }
    }
    // END FLINK MODIFICATION
    if (frame != null) {
        map.put(r, frame);
    }
    currentRel = parent;
    return frame;
}
Also used : Correlate(org.apache.calcite.rel.core.Correlate) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) Sort(org.apache.calcite.rel.core.Sort)

Example 3 with Correlate

use of org.apache.calcite.rel.core.Correlate in project calcite by apache.

the class MutableRels method toMutable.

public static MutableRel toMutable(RelNode rel) {
    if (rel instanceof HepRelVertex) {
        return toMutable(((HepRelVertex) rel).getCurrentRel());
    }
    if (rel instanceof RelSubset) {
        return toMutable(Util.first(((RelSubset) rel).getBest(), ((RelSubset) rel).getOriginal()));
    }
    if (rel instanceof TableScan) {
        return MutableScan.of((TableScan) rel);
    }
    if (rel instanceof Values) {
        return MutableValues.of((Values) rel);
    }
    if (rel instanceof Project) {
        final Project project = (Project) rel;
        final MutableRel input = toMutable(project.getInput());
        return MutableProject.of(input, project.getProjects(), project.getRowType().getFieldNames());
    }
    if (rel instanceof Filter) {
        final Filter filter = (Filter) rel;
        final MutableRel input = toMutable(filter.getInput());
        return MutableFilter.of(input, filter.getCondition());
    }
    if (rel instanceof Aggregate) {
        final Aggregate aggregate = (Aggregate) rel;
        final MutableRel input = toMutable(aggregate.getInput());
        return MutableAggregate.of(input, aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList());
    }
    if (rel instanceof Sort) {
        final Sort sort = (Sort) rel;
        final MutableRel input = toMutable(sort.getInput());
        return MutableSort.of(input, sort.getCollation(), sort.offset, sort.fetch);
    }
    if (rel instanceof Calc) {
        final Calc calc = (Calc) rel;
        final MutableRel input = toMutable(calc.getInput());
        return MutableCalc.of(input, calc.getProgram());
    }
    if (rel instanceof Exchange) {
        final Exchange exchange = (Exchange) rel;
        final MutableRel input = toMutable(exchange.getInput());
        return MutableExchange.of(input, exchange.getDistribution());
    }
    if (rel instanceof Collect) {
        final Collect collect = (Collect) rel;
        final MutableRel input = toMutable(collect.getInput());
        return MutableCollect.of(collect.getRowType(), input, collect.getFieldName());
    }
    if (rel instanceof Uncollect) {
        final Uncollect uncollect = (Uncollect) rel;
        final MutableRel input = toMutable(uncollect.getInput());
        return MutableUncollect.of(uncollect.getRowType(), input, uncollect.withOrdinality);
    }
    if (rel instanceof Window) {
        final Window window = (Window) rel;
        final MutableRel input = toMutable(window.getInput());
        return MutableWindow.of(window.getRowType(), input, window.groups, window.getConstants());
    }
    if (rel instanceof TableModify) {
        final TableModify modify = (TableModify) rel;
        final MutableRel input = toMutable(modify.getInput());
        return MutableTableModify.of(modify.getRowType(), input, modify.getTable(), modify.getCatalogReader(), modify.getOperation(), modify.getUpdateColumnList(), modify.getSourceExpressionList(), modify.isFlattened());
    }
    if (rel instanceof Sample) {
        final Sample sample = (Sample) rel;
        final MutableRel input = toMutable(sample.getInput());
        return MutableSample.of(input, sample.getSamplingParameters());
    }
    if (rel instanceof TableFunctionScan) {
        final TableFunctionScan tableFunctionScan = (TableFunctionScan) rel;
        final List<MutableRel> inputs = toMutables(tableFunctionScan.getInputs());
        return MutableTableFunctionScan.of(tableFunctionScan.getCluster(), tableFunctionScan.getRowType(), inputs, tableFunctionScan.getCall(), tableFunctionScan.getElementType(), tableFunctionScan.getColumnMappings());
    }
    // is a sub-class of Join.
    if (rel instanceof SemiJoin) {
        final SemiJoin semiJoin = (SemiJoin) rel;
        final MutableRel left = toMutable(semiJoin.getLeft());
        final MutableRel right = toMutable(semiJoin.getRight());
        return MutableSemiJoin.of(semiJoin.getRowType(), left, right, semiJoin.getCondition(), semiJoin.getLeftKeys(), semiJoin.getRightKeys());
    }
    if (rel instanceof Join) {
        final Join join = (Join) rel;
        final MutableRel left = toMutable(join.getLeft());
        final MutableRel right = toMutable(join.getRight());
        return MutableJoin.of(join.getRowType(), left, right, join.getCondition(), join.getJoinType(), join.getVariablesSet());
    }
    if (rel instanceof Correlate) {
        final Correlate correlate = (Correlate) rel;
        final MutableRel left = toMutable(correlate.getLeft());
        final MutableRel right = toMutable(correlate.getRight());
        return MutableCorrelate.of(correlate.getRowType(), left, right, correlate.getCorrelationId(), correlate.getRequiredColumns(), correlate.getJoinType());
    }
    if (rel instanceof Union) {
        final Union union = (Union) rel;
        final List<MutableRel> inputs = toMutables(union.getInputs());
        return MutableUnion.of(union.getRowType(), inputs, union.all);
    }
    if (rel instanceof Minus) {
        final Minus minus = (Minus) rel;
        final List<MutableRel> inputs = toMutables(minus.getInputs());
        return MutableMinus.of(minus.getRowType(), inputs, minus.all);
    }
    if (rel instanceof Intersect) {
        final Intersect intersect = (Intersect) rel;
        final List<MutableRel> inputs = toMutables(intersect.getInputs());
        return MutableIntersect.of(intersect.getRowType(), inputs, intersect.all);
    }
    throw new RuntimeException("cannot translate " + rel + " to MutableRel");
}
Also used : Uncollect(org.apache.calcite.rel.core.Uncollect) Collect(org.apache.calcite.rel.core.Collect) Values(org.apache.calcite.rel.core.Values) Union(org.apache.calcite.rel.core.Union) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) Intersect(org.apache.calcite.rel.core.Intersect) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) SemiJoin(org.apache.calcite.rel.core.SemiJoin) LogicalTableModify(org.apache.calcite.rel.logical.LogicalTableModify) TableModify(org.apache.calcite.rel.core.TableModify) RelSubset(org.apache.calcite.plan.volcano.RelSubset) LogicalWindow(org.apache.calcite.rel.logical.LogicalWindow) Window(org.apache.calcite.rel.core.Window) TableScan(org.apache.calcite.rel.core.TableScan) Correlate(org.apache.calcite.rel.core.Correlate) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) Sample(org.apache.calcite.rel.core.Sample) Join(org.apache.calcite.rel.core.Join) SemiJoin(org.apache.calcite.rel.core.SemiJoin) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) Calc(org.apache.calcite.rel.core.Calc) Exchange(org.apache.calcite.rel.core.Exchange) LogicalExchange(org.apache.calcite.rel.logical.LogicalExchange) Project(org.apache.calcite.rel.core.Project) LogicalTableFunctionScan(org.apache.calcite.rel.logical.LogicalTableFunctionScan) TableFunctionScan(org.apache.calcite.rel.core.TableFunctionScan) Filter(org.apache.calcite.rel.core.Filter) Aggregate(org.apache.calcite.rel.core.Aggregate) Minus(org.apache.calcite.rel.core.Minus)

Example 4 with Correlate

use of org.apache.calcite.rel.core.Correlate in project calcite by apache.

the class FilterCorrelateRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final Filter filter = call.rel(0);
    final Correlate corr = call.rel(1);
    final List<RexNode> aboveFilters = RelOptUtil.conjunctions(filter.getCondition());
    final List<RexNode> leftFilters = new ArrayList<>();
    final List<RexNode> rightFilters = new ArrayList<>();
    // Try to push down above filters. These are typically where clause
    // filters. They can be pushed down if they are not on the NULL
    // generating side.
    RelOptUtil.classifyFilters(corr, aboveFilters, JoinRelType.INNER, false, !corr.getJoinType().toJoinType().generatesNullsOnLeft(), !corr.getJoinType().toJoinType().generatesNullsOnRight(), aboveFilters, leftFilters, rightFilters);
    if (leftFilters.isEmpty() && rightFilters.isEmpty()) {
        // no filters got pushed
        return;
    }
    // Create Filters on top of the children if any filters were
    // pushed to them.
    final RexBuilder rexBuilder = corr.getCluster().getRexBuilder();
    final RelBuilder relBuilder = call.builder();
    final RelNode leftRel = relBuilder.push(corr.getLeft()).filter(leftFilters).build();
    final RelNode rightRel = relBuilder.push(corr.getRight()).filter(rightFilters).build();
    // Create the new Correlate
    RelNode newCorrRel = corr.copy(corr.getTraitSet(), ImmutableList.of(leftRel, rightRel));
    call.getPlanner().onCopy(corr, newCorrRel);
    if (!leftFilters.isEmpty()) {
        call.getPlanner().onCopy(filter, leftRel);
    }
    if (!rightFilters.isEmpty()) {
        call.getPlanner().onCopy(filter, rightRel);
    }
    // Create a Filter on top of the join if needed
    relBuilder.push(newCorrRel);
    relBuilder.filter(RexUtil.fixUp(rexBuilder, aboveFilters, RelOptUtil.getFieldTypeList(relBuilder.peek().getRowType())));
    call.transformTo(relBuilder.build());
}
Also used : Correlate(org.apache.calcite.rel.core.Correlate) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 5 with Correlate

use of org.apache.calcite.rel.core.Correlate in project drill by apache.

the class ComplexUnnestVisitor method visit.

@Override
public RelNode visit(LogicalCorrelate correlate) {
    RelNode left = correlate.getLeft().accept(this);
    leftInputs.put(correlate.getCorrelationId(), left);
    RelNode right = correlate.getRight().accept(this);
    // after rewriting right input, no need to create Correlate with new CorrelationId
    if (correlate.getRight() == right || left == leftInputs.get(correlate.getCorrelationId())) {
        if (correlate.getLeft() == left) {
            return correlate;
        }
        // changed only inputs, but CorrelationId left the same
        return correlate.copy(correlate.getTraitSet(), Arrays.asList(left, right));
    }
    Correlate newCorrelate = correlate.copy(correlate.getTraitSet(), leftInputs.get(correlate.getCorrelationId()), right, updatedCorrelationIds.get(correlate.getCorrelationId()), ImmutableBitSet.of(left.getRowType().getFieldCount()), correlate.getJoinType());
    RelBuilder builder = DrillRelFactories.LOGICAL_BUILDER.create(correlate.getCluster(), null);
    builder.push(newCorrelate);
    List<RexNode> topProjectExpressions = left.getRowType().getFieldList().stream().map(field -> builder.getRexBuilder().makeInputRef(newCorrelate, field.getIndex())).collect(Collectors.toList());
    // Accommodate the new $COMPLEX_FIELD_NAME column.
    int rightStartIndex = left.getRowType().getFieldList().size() + 1;
    switch(correlate.getJoinType()) {
        case LEFT:
        case INNER:
            // adds field from the right input of correlate to the top project
            topProjectExpressions.addAll(right.getRowType().getFieldList().stream().map(field -> builder.getRexBuilder().makeInputRef(newCorrelate, field.getIndex() + rightStartIndex)).collect(Collectors.toList()));
        // fall through
        case ANTI:
        case SEMI:
            builder.project(topProjectExpressions, correlate.getRowType().getFieldNames());
    }
    return builder.build();
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Project(org.apache.calcite.rel.core.Project) Uncollect(org.apache.calcite.rel.core.Uncollect) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) Arrays(java.util.Arrays) SqlKind(org.apache.calcite.sql.SqlKind) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) Correlate(org.apache.calcite.rel.core.Correlate) RexBuilder(org.apache.calcite.rex.RexBuilder) HashMap(java.util.HashMap) RelNode(org.apache.calcite.rel.RelNode) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Collectors(java.util.stream.Collectors) ImmutableList(org.apache.drill.shaded.guava.com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.calcite.rex.RexNode) RelBuilder(org.apache.calcite.tools.RelBuilder) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Map(java.util.Map) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) DrillRelFactories(org.apache.drill.exec.planner.logical.DrillRelFactories) RexShuttle(org.apache.calcite.rex.RexShuttle) Correlate(org.apache.calcite.rel.core.Correlate) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

Correlate (org.apache.calcite.rel.core.Correlate)5 RelNode (org.apache.calcite.rel.RelNode)3 Project (org.apache.calcite.rel.core.Project)3 LogicalCorrelate (org.apache.calcite.rel.logical.LogicalCorrelate)3 ArrayList (java.util.ArrayList)2 Filter (org.apache.calcite.rel.core.Filter)2 Sort (org.apache.calcite.rel.core.Sort)2 Uncollect (org.apache.calcite.rel.core.Uncollect)2 RexBuilder (org.apache.calcite.rex.RexBuilder)2 RexNode (org.apache.calcite.rex.RexNode)2 RelBuilder (org.apache.calcite.tools.RelBuilder)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 RelOptUtil (org.apache.calcite.plan.RelOptUtil)1 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)1 RelSubset (org.apache.calcite.plan.volcano.RelSubset)1 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)1