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;
}
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)));
}
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;
}
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());
}
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;
}
Aggregations