Java Code Examples for org.apache.calcite.tools.RelBuilder#pushAll()

The following examples show how to use org.apache.calcite.tools.RelBuilder#pushAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JoinRule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final Join join = call.rel(0);
  final RelNode left = join.getLeft();
  final RelNode right = join.getRight();
  final RelNode convertedLeft = convertIfNecessary(left);
  final RelNode convertedRight = convertIfNecessary(right);
  final RelBuilder builder = factory.create(join.getCluster(), null);

  builder.pushAll(ImmutableList.of(convertedLeft, convertedRight));
  builder.join(join.getJoinType(), join.getCondition());

  final RelNode newJoin = builder.build();
  if(newJoin != null) {
    call.transformTo(newJoin);
  }
}
 
Example 2
Source File: UnionToDistinctRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Union union = call.rel(0);
  if (union.all) {
    return; // nothing to do
  }
  final RelBuilder relBuilder = call.builder();
  relBuilder.pushAll(union.getInputs());
  relBuilder.union(true, union.getInputs().size());
  relBuilder.distinct();
  call.transformTo(relBuilder.build());
}
 
Example 3
Source File: JoinFilterCanonicalizationRule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create a join operator with a canonicalized version of {@code joinCondition}
 *
 * @param builder
 * @param joinType
 * @param joinCondition
 * @param left
 * @param right
 * @return the new join operator, or {@code null} if {@code joinCondition} hasn't changed.
 */
private RelNode canonicalizeJoinCondition(
    RelBuilder builder,
    JoinRelType joinType,
    RexNode joinCondition,
    RelNode left,
    RelNode right) {
  final List<Integer> leftKeys = Lists.newArrayList();
  final List<Integer> rightKeys = Lists.newArrayList();
  final List<Boolean> filterNulls = Lists.newArrayList();

  final RexNode remaining = RelOptUtil.splitJoinCondition(left, right, joinCondition, leftKeys, rightKeys, filterNulls);

  // Create a normalized join condition
  final RexNode newPartialJoinCondition = buildJoinCondition(builder.getRexBuilder(), left.getRowType(), right.getRowType(), leftKeys, rightKeys, filterNulls);
  // Add the remaining filter condition
  final RexNode newJoinCondition = RelOptUtil.andJoinFilters(builder.getRexBuilder(), newPartialJoinCondition, remaining);

  // terminate if the same condition as previously
  if (RexUtil.eq(joinCondition, newJoinCondition)) {
    return null;
  }

  builder.pushAll(ImmutableList.of(left, right));
  builder.join(joinType, newJoinCondition);

  return builder.build();
}
 
Example 4
Source File: UnionToDistinctRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Union union = call.rel(0);
  final RelBuilder relBuilder = call.builder();
  relBuilder.pushAll(union.getInputs());
  relBuilder.union(true, union.getInputs().size());
  relBuilder.distinct();
  call.transformTo(relBuilder.build());
}
 
Example 5
Source File: UnionMergeRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final SetOp topOp = call.rel(0);
  @SuppressWarnings("unchecked") final Class<? extends SetOp> setOpClass =
      (Class) operands.get(0).getMatchedClass();

  // For Union and Intersect, we want to combine the set-op that's in the
  // second input first.
  //
  // For example, we reduce
  //    Union(Union(a, b), Union(c, d))
  // to
  //    Union(Union(a, b), c, d)
  // in preference to
  //    Union(a, b, Union(c, d))
  //
  // But for Minus, we can only reduce the left input. It is not valid to
  // reduce
  //    Minus(a, Minus(b, c))
  // to
  //    Minus(a, b, c)
  //
  // Hence, that's why the rule pattern matches on generic RelNodes rather
  // than explicit sub-classes of SetOp.  By doing so, and firing this rule
  // in a bottom-up order, it allows us to only specify a single
  // pattern for this rule.
  final SetOp bottomOp;
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    bottomOp = call.rel(2);
  } else if (setOpClass.isInstance(call.rel(1))) {
    bottomOp = call.rel(1);
  } else {
    return;
  }

  // Can only combine (1) if all operators are ALL,
  // or (2) top operator is DISTINCT (i.e. not ALL).
  // In case (2), all operators become DISTINCT.
  if (topOp.all && !bottomOp.all) {
    return;
  }

  // Combine the inputs from the bottom set-op with the other inputs from
  // the top set-op.
  final RelBuilder relBuilder = call.builder();
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    relBuilder.push(topOp.getInput(0));
    relBuilder.pushAll(bottomOp.getInputs());
    // topOp.getInputs().size() may be more than 2
    for (int index = 2; index < topOp.getInputs().size(); index++) {
      relBuilder.push(topOp.getInput(index));
    }
  } else {
    relBuilder.pushAll(bottomOp.getInputs());
    relBuilder.pushAll(Util.skip(topOp.getInputs()));
  }
  int n = bottomOp.getInputs().size()
      + topOp.getInputs().size()
      - 1;
  if (topOp instanceof Union) {
    relBuilder.union(topOp.all, n);
  } else if (topOp instanceof Intersect) {
    relBuilder.intersect(topOp.all, n);
  } else if (topOp instanceof Minus) {
    relBuilder.minus(topOp.all, n);
  }
  call.transformTo(relBuilder.build());
}
 
Example 6
Source File: JoinNormalizationRule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Attempt to create a new join with a canonicalized join expression, and a possible filter
 * on top
 * @param builder
 * @param join
 * @return a new join tree (or same as original argument if no change)
 */
private RelNode getNewJoinCondition(RelBuilder builder, Join join) {
  final List<Integer> leftKeys = Lists.newArrayList();
  final List<Integer> rightKeys = Lists.newArrayList();
  final List<Boolean> filterNulls = Lists.newArrayList();

  final RexNode remaining = RelOptUtil.splitJoinCondition(join.getLeft(), join.getRight(), join.getCondition(), leftKeys, rightKeys, filterNulls);
  final boolean hasEquiJoins = leftKeys.size() == rightKeys.size() && leftKeys.size() > 0 ;
  final JoinRelType joinType = join.getJoinType();

  // If join has no equi-join condition, do not transform
  if (!hasEquiJoins) {
    return join;
  }

  // Create a new partial condition for the equi-join
  final RexNode partialCondition = JoinFilterCanonicalizationRule.buildJoinCondition(
      builder.getRexBuilder(),
      join.getLeft().getRowType(),
      join.getRight().getRowType(),
      leftKeys,
      rightKeys,
      filterNulls);

  // We do not know how to add filter for non-INNER joins (see DRILL-1337)
  if (joinType != JoinRelType.INNER) {
    final RexNode newJoinCondition = RexUtil.composeConjunction(builder.getRexBuilder(), ImmutableList.of(partialCondition, remaining), false);
    if (RexUtil.eq(join.getCondition(), newJoinCondition)) {
      // Condition is the same, do not create a new rel
      return join;
    }
    builder.pushAll(ImmutableList.of(join.getLeft(), join.getRight()));
    builder.join(joinType, newJoinCondition);

    return builder.build();
  }

  // Check if join condition has changed if pure equi-join
  if (remaining.isAlwaysTrue() && RexUtil.eq(join.getCondition(), partialCondition)) {
    return join;
  }

  // Return the new join with a filter on top
  builder.pushAll(ImmutableList.of(join.getLeft(), join.getRight()));
  builder.join(joinType, partialCondition);
  builder.filter(remaining);
  return builder.build();
}
 
Example 7
Source File: UnionMergeRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final SetOp topOp = call.rel(0);
  @SuppressWarnings("unchecked") final Class<? extends SetOp> setOpClass =
      (Class) operands.get(0).getMatchedClass();

  // For Union and Intersect, we want to combine the set-op that's in the
  // second input first.
  //
  // For example, we reduce
  //    Union(Union(a, b), Union(c, d))
  // to
  //    Union(Union(a, b), c, d)
  // in preference to
  //    Union(a, b, Union(c, d))
  //
  // But for Minus, we can only reduce the left input. It is not valid to
  // reduce
  //    Minus(a, Minus(b, c))
  // to
  //    Minus(a, b, c)
  //
  // Hence, that's why the rule pattern matches on generic RelNodes rather
  // than explicit sub-classes of SetOp.  By doing so, and firing this rule
  // in a bottom-up order, it allows us to only specify a single
  // pattern for this rule.
  final SetOp bottomOp;
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    bottomOp = call.rel(2);
  } else if (setOpClass.isInstance(call.rel(1))) {
    bottomOp = call.rel(1);
  } else {
    return;
  }

  // Can only combine (1) if all operators are ALL,
  // or (2) top operator is DISTINCT (i.e. not ALL).
  // In case (2), all operators become DISTINCT.
  if (topOp.all && !bottomOp.all) {
    return;
  }

  // Combine the inputs from the bottom set-op with the other inputs from
  // the top set-op.
  final RelBuilder relBuilder = call.builder();
  if (setOpClass.isInstance(call.rel(2))
      && !Minus.class.isAssignableFrom(setOpClass)) {
    relBuilder.push(topOp.getInput(0));
    relBuilder.pushAll(bottomOp.getInputs());
    // topOp.getInputs().size() may be more than 2
    for (int index = 2; index < topOp.getInputs().size(); index++) {
      relBuilder.push(topOp.getInput(index));
    }
  } else {
    relBuilder.pushAll(bottomOp.getInputs());
    relBuilder.pushAll(Util.skip(topOp.getInputs()));
  }
  int n = bottomOp.getInputs().size()
      + topOp.getInputs().size()
      - 1;
  if (topOp instanceof Union) {
    relBuilder.union(topOp.all, n);
  } else if (topOp instanceof Intersect) {
    relBuilder.intersect(topOp.all, n);
  } else if (topOp instanceof Minus) {
    relBuilder.minus(topOp.all, n);
  }
  call.transformTo(relBuilder.build());
}