Java Code Examples for org.apache.calcite.util.ImmutableBitSet#intersects()

The following examples show how to use org.apache.calcite.util.ImmutableBitSet#intersects() . 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: JoinPushThroughJoinRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Splits a condition into conjunctions that do or do not intersect with
 * a given bit set.
 */
static void split(
    RexNode condition,
    ImmutableBitSet bitSet,
    List<RexNode> intersecting,
    List<RexNode> nonIntersecting) {
  for (RexNode node : RelOptUtil.conjunctions(condition)) {
    ImmutableBitSet inputBitSet = RelOptUtil.InputFinder.bits(node);
    if (bitSet.intersects(inputBitSet)) {
      intersecting.add(node);
    } else {
      nonIntersecting.add(node);
    }
  }
}
 
Example 2
Source File: JoinPushThroughJoinRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Splits a condition into conjunctions that do or do not intersect with
 * a given bit set.
 */
static void split(
    RexNode condition,
    ImmutableBitSet bitSet,
    List<RexNode> intersecting,
    List<RexNode> nonIntersecting) {
  for (RexNode node : RelOptUtil.conjunctions(condition)) {
    ImmutableBitSet inputBitSet = RelOptUtil.InputFinder.bits(node);
    if (bitSet.intersects(inputBitSet)) {
      intersecting.add(node);
    } else {
      nonIntersecting.add(node);
    }
  }
}
 
Example 3
Source File: RelMdPredicates.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Converts a predicate on a particular set of columns into a predicate on
 * a subset of those columns, weakening if necessary.
 *
 * <p>If not possible to simplify, returns {@code true}, which is the weakest
 * possible predicate.
 *
 * <p>Examples:<ol>
 * <li>The predicate {@code $7 = $9} on columns [7]
 *     becomes {@code $7 is not null}
 * <li>The predicate {@code $7 = $9 + $11} on columns [7, 9]
 *     becomes {@code $7 is not null or $9 is not null}
 * <li>The predicate {@code $7 = $9 and $9 = 5} on columns [7] becomes
 *   {@code $7 = 5}
 * <li>The predicate
 *   {@code $7 = $9 and ($9 = $1 or $9 = $2) and $1 > 3 and $2 > 10}
 *   on columns [7] becomes {@code $7 > 3}
 * </ol>
 *
 * <p>We currently only handle examples 1 and 2.
 *
 * @param rexBuilder Rex builder
 * @param input Input relational expression
 * @param r Predicate expression
 * @param columnsMapped Columns which the final predicate can reference
 * @return Predicate expression narrowed to reference only certain columns
 */
private RexNode projectPredicate(final RexBuilder rexBuilder, RelNode input,
    RexNode r, ImmutableBitSet columnsMapped) {
  ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r);
  if (columnsMapped.contains(rCols)) {
    // All required columns are present. No need to weaken.
    return r;
  }
  if (columnsMapped.intersects(rCols)) {
    final List<RexNode> list = new ArrayList<>();
    for (int c : columnsMapped.intersect(rCols)) {
      if (input.getRowType().getFieldList().get(c).getType().isNullable()
          && Strong.isNull(r, ImmutableBitSet.of(c))) {
        list.add(
            rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
                rexBuilder.makeInputRef(input, c)));
      }
    }
    if (!list.isEmpty()) {
      return RexUtil.composeDisjunction(rexBuilder, list);
    }
  }
  // Cannot weaken to anything non-trivial
  return rexBuilder.makeLiteral(true);
}
 
Example 4
Source File: SemiJoinRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected void perform(RelOptRuleCall call, Project project,
    Join join, RelNode left, Aggregate aggregate) {
  final RelOptCluster cluster = join.getCluster();
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  if (project != null) {
    final ImmutableBitSet bits =
        RelOptUtil.InputFinder.bits(project.getProjects(), null);
    final ImmutableBitSet rightBits =
        ImmutableBitSet.range(left.getRowType().getFieldCount(),
            join.getRowType().getFieldCount());
    if (bits.intersects(rightBits)) {
      return;
    }
  }
  final JoinInfo joinInfo = join.analyzeCondition();
  if (!joinInfo.rightSet().equals(
      ImmutableBitSet.range(aggregate.getGroupCount()))) {
    // Rule requires that aggregate key to be the same as the join key.
    // By the way, neither a super-set nor a sub-set would work.
    return;
  }
  if (!joinInfo.isEqui()) {
    return;
  }
  final RelBuilder relBuilder = call.builder();
  relBuilder.push(left);
  switch (join.getJoinType()) {
  case INNER:
    final List<Integer> newRightKeyBuilder = new ArrayList<>();
    final List<Integer> aggregateKeys = aggregate.getGroupSet().asList();
    for (int key : joinInfo.rightKeys) {
      newRightKeyBuilder.add(aggregateKeys.get(key));
    }
    final ImmutableIntList newRightKeys = ImmutableIntList.copyOf(newRightKeyBuilder);
    relBuilder.push(aggregate.getInput());
    final RexNode newCondition =
        RelOptUtil.createEquiJoinCondition(relBuilder.peek(2, 0),
            joinInfo.leftKeys, relBuilder.peek(2, 1), newRightKeys,
            rexBuilder);
    relBuilder.semiJoin(newCondition);
    break;

  case LEFT:
    // The right-hand side produces no more than 1 row (because of the
    // Aggregate) and no fewer than 1 row (because of LEFT), and therefore
    // we can eliminate the semi-join.
    break;

  default:
    throw new AssertionError(join.getJoinType());
  }
  if (project != null) {
    relBuilder.project(project.getProjects(), project.getRowType().getFieldNames());
  }
  call.transformTo(relBuilder.build());
}
 
Example 5
Source File: RelMdPredicates.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Converts a predicate on a particular set of columns into a predicate on
 * a subset of those columns, weakening if necessary.
 *
 * <p>If not possible to simplify, returns {@code true}, which is the weakest
 * possible predicate.
 *
 * <p>Examples:<ol>
 * <li>The predicate {@code $7 = $9} on columns [7]
 *     becomes {@code $7 is not null}
 * <li>The predicate {@code $7 = $9 + $11} on columns [7, 9]
 *     becomes {@code $7 is not null or $9 is not null}
 * <li>The predicate {@code $7 = $9 and $9 = 5} on columns [7] becomes
 *   {@code $7 = 5}
 * <li>The predicate
 *   {@code $7 = $9 and ($9 = $1 or $9 = $2) and $1 > 3 and $2 > 10}
 *   on columns [7] becomes {@code $7 > 3}
 * </ol>
 *
 * <p>We currently only handle examples 1 and 2.
 *
 * @param rexBuilder Rex builder
 * @param input Input relational expression
 * @param r Predicate expression
 * @param columnsMapped Columns which the final predicate can reference
 * @return Predicate expression narrowed to reference only certain columns
 */
private RexNode projectPredicate(final RexBuilder rexBuilder, RelNode input,
    RexNode r, ImmutableBitSet columnsMapped) {
  ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r);
  if (columnsMapped.contains(rCols)) {
    // All required columns are present. No need to weaken.
    return r;
  }
  if (columnsMapped.intersects(rCols)) {
    final List<RexNode> list = new ArrayList<>();
    for (int c : columnsMapped.intersect(rCols)) {
      if (input.getRowType().getFieldList().get(c).getType().isNullable()
          && Strong.isNull(r, ImmutableBitSet.of(c))) {
        list.add(
            rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
                rexBuilder.makeInputRef(input, c)));
      }
    }
    if (!list.isEmpty()) {
      return RexUtil.composeDisjunction(rexBuilder, list);
    }
  }
  // Cannot weaken to anything non-trivial
  return rexBuilder.makeLiteral(true);
}
 
Example 6
Source File: SemiJoinRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
protected void perform(RelOptRuleCall call, Project project,
    Join join, RelNode left, Aggregate aggregate) {
  final RelOptCluster cluster = join.getCluster();
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  if (project != null) {
    final ImmutableBitSet bits =
        RelOptUtil.InputFinder.bits(project.getProjects(), null);
    final ImmutableBitSet rightBits =
        ImmutableBitSet.range(left.getRowType().getFieldCount(),
            join.getRowType().getFieldCount());
    if (bits.intersects(rightBits)) {
      return;
    }
  } else {
    if (join.getJoinType().projectsRight()
        && !IS_EMPTY_AGGREGATE.test(aggregate)) {
      return;
    }
  }
  final JoinInfo joinInfo = join.analyzeCondition();
  if (!joinInfo.rightSet().equals(
      ImmutableBitSet.range(aggregate.getGroupCount()))) {
    // Rule requires that aggregate key to be the same as the join key.
    // By the way, neither a super-set nor a sub-set would work.
    return;
  }
  if (!joinInfo.isEqui()) {
    return;
  }
  final RelBuilder relBuilder = call.builder();
  relBuilder.push(left);
  switch (join.getJoinType()) {
  case SEMI:
  case INNER:
    final List<Integer> newRightKeyBuilder = new ArrayList<>();
    final List<Integer> aggregateKeys = aggregate.getGroupSet().asList();
    for (int key : joinInfo.rightKeys) {
      newRightKeyBuilder.add(aggregateKeys.get(key));
    }
    final ImmutableIntList newRightKeys = ImmutableIntList.copyOf(newRightKeyBuilder);
    relBuilder.push(aggregate.getInput());
    final RexNode newCondition =
        RelOptUtil.createEquiJoinCondition(relBuilder.peek(2, 0),
            joinInfo.leftKeys, relBuilder.peek(2, 1), newRightKeys,
            rexBuilder);
    relBuilder.semiJoin(newCondition);
    break;

  case LEFT:
    // The right-hand side produces no more than 1 row (because of the
    // Aggregate) and no fewer than 1 row (because of LEFT), and therefore
    // we can eliminate the semi-join.
    break;

  default:
    throw new AssertionError(join.getJoinType());
  }
  if (project != null) {
    relBuilder.project(project.getProjects(), project.getRowType().getFieldNames());
  }
  final RelNode relNode = relBuilder.build();
  call.transformTo(relNode);
}