Search in sources :

Example 6 with HepRelVertex

use of org.apache.calcite.plan.hep.HepRelVertex in project flink by apache.

the class FlinkRelDecorrelator method stripHep.

private static RelNode stripHep(RelNode rel) {
    if (rel instanceof HepRelVertex) {
        HepRelVertex hepRelVertex = (HepRelVertex) rel;
        rel = hepRelVertex.getCurrentRel();
    }
    return rel;
}
Also used : HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex)

Example 7 with HepRelVertex

use of org.apache.calcite.plan.hep.HepRelVertex in project hive by apache.

the class HiveSemiJoinRule method perform.

protected void perform(RelOptRuleCall call, ImmutableBitSet topRefs, RelNode topOperator, Join join, RelNode left, Aggregate aggregate) {
    LOG.debug("Matched HiveSemiJoinRule");
    final RelOptCluster cluster = join.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final ImmutableBitSet rightBits = ImmutableBitSet.range(left.getRowType().getFieldCount(), join.getRowType().getFieldCount());
    if (topRefs.intersects(rightBits)) {
        return;
    }
    final JoinInfo joinInfo = join.analyzeCondition();
    if (!joinInfo.rightSet().equals(ImmutableBitSet.range(aggregate.getGroupCount()))) {
        // By the way, neither a super-set nor a sub-set would work.
        return;
    }
    if (join.getJoinType() == JoinRelType.LEFT) {
        // since for LEFT join we are only interested in rows from LEFT we can get rid of right side
        call.transformTo(topOperator.copy(topOperator.getTraitSet(), ImmutableList.of(left)));
        return;
    }
    if (join.getJoinType() != JoinRelType.INNER) {
        return;
    }
    if (!joinInfo.isEqui()) {
        return;
    }
    LOG.debug("All conditions matched for HiveSemiJoinRule. Going to apply transformation.");
    final List<Integer> newRightKeyBuilder = Lists.newArrayList();
    final List<Integer> aggregateKeys = aggregate.getGroupSet().asList();
    for (int key : joinInfo.rightKeys) {
        newRightKeyBuilder.add(aggregateKeys.get(key));
    }
    final ImmutableIntList newRightKeys = ImmutableIntList.copyOf(newRightKeyBuilder);
    final RelNode newRight = aggregate.getInput();
    final RexNode newCondition = RelOptUtil.createEquiJoinCondition(left, joinInfo.leftKeys, newRight, newRightKeys, rexBuilder);
    RelNode semi = null;
    // is not expected further down the pipeline. see jira for more details
    if (aggregate.getInput() instanceof HepRelVertex && ((HepRelVertex) aggregate.getInput()).getCurrentRel() instanceof Join) {
        Join rightJoin = (Join) (((HepRelVertex) aggregate.getInput()).getCurrentRel());
        List<RexNode> projects = new ArrayList<>();
        for (int i = 0; i < rightJoin.getRowType().getFieldCount(); i++) {
            projects.add(rexBuilder.makeInputRef(rightJoin, i));
        }
        RelNode topProject = call.builder().push(rightJoin).project(projects, rightJoin.getRowType().getFieldNames(), true).build();
        semi = call.builder().push(left).push(topProject).semiJoin(newCondition).build();
    } else {
        semi = call.builder().push(left).push(aggregate.getInput()).semiJoin(newCondition).build();
    }
    call.transformTo(topOperator.copy(topOperator.getTraitSet(), ImmutableList.of(semi)));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ArrayList(java.util.ArrayList) Join(org.apache.calcite.rel.core.Join) JoinInfo(org.apache.calcite.rel.core.JoinInfo) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with HepRelVertex

use of org.apache.calcite.plan.hep.HepRelVertex in project hive by apache.

the class HiveAggregateJoinTransposeRule method isGroupingUnique.

/**
 * Determines weather the give grouping is unique.
 *
 * Consider a join which might produce non-unique rows; but later the results are aggregated again.
 * This method determines if there are sufficient columns in the grouping which have been present previously as unique column(s).
 */
private boolean isGroupingUnique(RelNode input, ImmutableBitSet groups) {
    if (groups.isEmpty()) {
        return false;
    }
    if (input instanceof HepRelVertex) {
        HepRelVertex vertex = (HepRelVertex) input;
        return isGroupingUnique(vertex.getCurrentRel(), groups);
    }
    RelMetadataQuery mq = input.getCluster().getMetadataQuery();
    Set<ImmutableBitSet> uKeys = mq.getUniqueKeys(input);
    if (uKeys == null) {
        return false;
    }
    for (ImmutableBitSet u : uKeys) {
        if (groups.contains(u)) {
            return true;
        }
    }
    if (input instanceof Join) {
        Join join = (Join) input;
        JoinInfo ji = JoinInfo.of(join.getLeft(), join.getRight(), join.getCondition());
        if (ji.isEqui()) {
            ImmutableBitSet newGroup = groups.intersect(InputFinder.bits(join.getCondition()));
            RelNode l = join.getLeft();
            RelNode r = join.getRight();
            int joinFieldCount = join.getRowType().getFieldCount();
            int lFieldCount = l.getRowType().getFieldCount();
            ImmutableBitSet groupL = newGroup.get(0, lFieldCount);
            ImmutableBitSet groupR = newGroup.get(lFieldCount, joinFieldCount).shift(-lFieldCount);
            if (isGroupingUnique(l, groupL)) {
                return true;
            }
            if (isGroupingUnique(r, groupR)) {
                return true;
            }
        }
    }
    if (input instanceof Project) {
        Project project = (Project) input;
        ImmutableBitSet.Builder newGroup = ImmutableBitSet.builder();
        for (int g : groups.asList()) {
            RexNode rex = project.getProjects().get(g);
            if (rex instanceof RexInputRef) {
                RexInputRef rexInputRef = (RexInputRef) rex;
                newGroup.set(rexInputRef.getIndex());
            }
        }
        return isGroupingUnique(project.getInput(), newGroup.build());
    }
    return false;
}
Also used : JoinInfo(org.apache.calcite.rel.core.JoinInfo) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) Project(org.apache.calcite.rel.core.Project) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelNode(org.apache.calcite.rel.RelNode) Join(org.apache.calcite.rel.core.Join) HiveJoin(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveJoin) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 9 with HepRelVertex

use of org.apache.calcite.plan.hep.HepRelVertex in project hive by apache.

the class HiveHepExtractRelNodeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final HepRelVertex rel = call.rel(0);
    call.transformTo(rel.getCurrentRel());
}
Also used : HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex)

Example 10 with HepRelVertex

use of org.apache.calcite.plan.hep.HepRelVertex in project hive by apache.

the class HiveFieldTrimmerRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    if (triggered) {
        // Bail out
        return;
    }
    RelNode node = call.rel(0);
    final HepRelVertex root = (HepRelVertex) call.getPlanner().getRoot();
    if (root.getCurrentRel() != node) {
        // Bail out
        return;
    }
    // The node is the root, release the kraken!
    node = HiveHepExtractRelNodeRule.execute(node);
    call.transformTo(trim(call, node));
    triggered = true;
}
Also used : HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) RelNode(org.apache.calcite.rel.RelNode)

Aggregations

HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)16 RelNode (org.apache.calcite.rel.RelNode)8 RexNode (org.apache.calcite.rex.RexNode)7 Join (org.apache.calcite.rel.core.Join)4 RexBuilder (org.apache.calcite.rex.RexBuilder)4 ArrayList (java.util.ArrayList)3 JoinInfo (org.apache.calcite.rel.core.JoinInfo)3 Project (org.apache.calcite.rel.core.Project)3 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)2 RelSubset (org.apache.calcite.plan.volcano.RelSubset)2 Aggregate (org.apache.calcite.rel.core.Aggregate)2 Sort (org.apache.calcite.rel.core.Sort)2 TableScan (org.apache.calcite.rel.core.TableScan)2 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)2 HashMap (java.util.HashMap)1 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)1 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)1 Calc (org.apache.calcite.rel.core.Calc)1 Collect (org.apache.calcite.rel.core.Collect)1