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

The following examples show how to use org.apache.calcite.util.ImmutableBitSet#nextSetBit() . 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: RelMdPredicates.java    From Bats with Apache License 2.0 5 votes vote down vote up
ExprsItr(ImmutableBitSet fields) {
  nextMapping = null;
  columns = new int[fields.cardinality()];
  columnSets = new BitSet[fields.cardinality()];
  iterationIdx = new int[fields.cardinality()];
  for (int j = 0, i = fields.nextSetBit(0); i >= 0; i = fields
      .nextSetBit(i + 1), j++) {
    columns[j] = i;
    columnSets[j] = equivalence.get(i);
    iterationIdx[j] = 0;
  }
  firstCall = true;
}
 
Example 2
Source File: LoptOptimizeJoinRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Locates from a list of filters those that correspond to a particular join
 * tree. Then, for each of those filters, extracts the fields corresponding
 * to a particular factor, setting them in a bitmap.
 *
 * @param multiJoin join factors being optimized
 * @param filters list of join filters
 * @param joinFactors bitmap containing the factors in a particular join
 * tree
 * @param factorStart the initial offset of the factor whose join keys will
 * be extracted
 * @param nFields the number of fields in the factor whose join keys will be
 * extracted
 * @param joinKeys the bitmap that will be set with the join keys
 */
private void setFactorJoinKeys(
    LoptMultiJoin multiJoin,
    List<RexNode> filters,
    ImmutableBitSet joinFactors,
    int factorStart,
    int nFields,
    ImmutableBitSet.Builder joinKeys) {
  for (RexNode joinFilter : filters) {
    ImmutableBitSet filterFactors =
        multiJoin.getFactorsRefByJoinFilter(joinFilter);

    // if all factors in the join filter are in the bitmap containing
    // the factors in a join tree, then from that filter, add the
    // fields corresponding to the specified factor to the join key
    // bitmap; in doing so, adjust the join keys so they start at
    // offset 0
    if (joinFactors.contains(filterFactors)) {
      ImmutableBitSet joinFields =
          multiJoin.getFieldsRefByJoinFilter(joinFilter);
      for (int field = joinFields.nextSetBit(factorStart);
           (field >= 0)
               && (field < (factorStart + nFields));
           field = joinFields.nextSetBit(field + 1)) {
        joinKeys.set(field - factorStart);
      }
    }
  }
}
 
Example 3
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
static Side of(ImmutableBitSet bitSet, int middle) {
    final int firstBit = bitSet.nextSetBit(0);
    if (firstBit < 0) {
        return EMPTY;
    }
    if (firstBit >= middle) {
        return RIGHT;
    }
    if (bitSet.nextSetBit(middle) < 0) {
        return LEFT;
    }
    return BOTH;
}
 
Example 4
Source File: RelMdPredicates.java    From calcite with Apache License 2.0 5 votes vote down vote up
ExprsItr(ImmutableBitSet fields) {
  nextMapping = null;
  columns = new int[fields.cardinality()];
  columnSets = new BitSet[fields.cardinality()];
  iterationIdx = new int[fields.cardinality()];
  for (int j = 0, i = fields.nextSetBit(0); i >= 0; i = fields
      .nextSetBit(i + 1), j++) {
    columns[j] = i;
    columnSets[j] = equivalence.get(i);
    iterationIdx[j] = 0;
  }
  firstCall = true;
}
 
Example 5
Source File: LoptOptimizeJoinRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Locates from a list of filters those that correspond to a particular join
 * tree. Then, for each of those filters, extracts the fields corresponding
 * to a particular factor, setting them in a bitmap.
 *
 * @param multiJoin join factors being optimized
 * @param filters list of join filters
 * @param joinFactors bitmap containing the factors in a particular join
 * tree
 * @param factorStart the initial offset of the factor whose join keys will
 * be extracted
 * @param nFields the number of fields in the factor whose join keys will be
 * extracted
 * @param joinKeys the bitmap that will be set with the join keys
 */
private void setFactorJoinKeys(
    LoptMultiJoin multiJoin,
    List<RexNode> filters,
    ImmutableBitSet joinFactors,
    int factorStart,
    int nFields,
    ImmutableBitSet.Builder joinKeys) {
  for (RexNode joinFilter : filters) {
    ImmutableBitSet filterFactors =
        multiJoin.getFactorsRefByJoinFilter(joinFilter);

    // if all factors in the join filter are in the bitmap containing
    // the factors in a join tree, then from that filter, add the
    // fields corresponding to the specified factor to the join key
    // bitmap; in doing so, adjust the join keys so they start at
    // offset 0
    if (joinFactors.contains(filterFactors)) {
      ImmutableBitSet joinFields =
          multiJoin.getFieldsRefByJoinFilter(joinFilter);
      for (int field = joinFields.nextSetBit(factorStart);
           (field >= 0)
               && (field < (factorStart + nFields));
           field = joinFields.nextSetBit(field + 1)) {
        joinKeys.set(field - factorStart);
      }
    }
  }
}
 
Example 6
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
static Side of(ImmutableBitSet bitSet, int middle) {
  final int firstBit = bitSet.nextSetBit(0);
  if (firstBit < 0) {
    return EMPTY;
  }
  if (firstBit >= middle) {
    return RIGHT;
  }
  if (bitSet.nextSetBit(middle) < 0) {
    return LEFT;
  }
  return BOTH;
}
 
Example 7
Source File: LoptMultiJoin.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Sets weighting for each combination of factors, depending on which join
 * filters reference which factors. Greater weight is given to equality
 * conditions. Also, sets bitmaps indicating which factors are referenced by
 * each factor within join filters that are comparisons.
 */
public void setFactorWeights() {
  factorWeights = new int[nJoinFactors][nJoinFactors];
  factorsRefByFactor = new ImmutableBitSet[nJoinFactors];
  for (int i = 0; i < nJoinFactors; i++) {
    factorsRefByFactor[i] = ImmutableBitSet.of();
  }

  for (RexNode joinFilter : allJoinFilters) {
    ImmutableBitSet factorRefs = factorsRefByJoinFilter.get(joinFilter);

    // don't give weights to non-comparison expressions
    if (!(joinFilter instanceof RexCall)) {
      continue;
    }
    if (!joinFilter.isA(SqlKind.COMPARISON)) {
      continue;
    }

    // OR the factors referenced in this join filter into the
    // bitmaps corresponding to each of the factors; however,
    // exclude the bit corresponding to the factor itself
    for (int factor : factorRefs) {
      factorsRefByFactor[factor] =
          factorsRefByFactor[factor]
              .rebuild()
              .addAll(factorRefs)
              .clear(factor)
              .build();
    }

    if (factorRefs.cardinality() == 2) {
      int leftFactor = factorRefs.nextSetBit(0);
      int rightFactor = factorRefs.nextSetBit(leftFactor + 1);

      final RexCall call = (RexCall) joinFilter;
      ImmutableBitSet leftFields = fieldBitmap(call.getOperands().get(0));
      ImmutableBitSet leftBitmap = factorBitmap(leftFields);

      // filter contains only two factor references, one on each
      // side of the operator
      int weight;
      if (leftBitmap.cardinality() == 1) {
        // give higher weight to equi-joins
        switch (joinFilter.getKind()) {
        case EQUALS:
          weight = 3;
          break;
        default:
          weight = 2;
        }
      } else {
        // cross product of two tables
        weight = 1;
      }
      setFactorWeight(weight, leftFactor, rightFactor);
    } else {
      // multiple factor references -- set a weight for each
      // combination of factors referenced within the filter
      final List<Integer> list  = ImmutableIntList.copyOf(factorRefs);
      for (int outer : list) {
        for (int inner : list) {
          if (outer != inner) {
            setFactorWeight(1, outer, inner);
          }
        }
      }
    }
  }
}
 
Example 8
Source File: LoptMultiJoin.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Sets weighting for each combination of factors, depending on which join
 * filters reference which factors. Greater weight is given to equality
 * conditions. Also, sets bitmaps indicating which factors are referenced by
 * each factor within join filters that are comparisons.
 */
public void setFactorWeights() {
  factorWeights = new int[nJoinFactors][nJoinFactors];
  factorsRefByFactor = new ImmutableBitSet[nJoinFactors];
  for (int i = 0; i < nJoinFactors; i++) {
    factorsRefByFactor[i] = ImmutableBitSet.of();
  }

  for (RexNode joinFilter : allJoinFilters) {
    ImmutableBitSet factorRefs = factorsRefByJoinFilter.get(joinFilter);

    // don't give weights to non-comparison expressions
    if (!(joinFilter instanceof RexCall)) {
      continue;
    }
    if (!joinFilter.isA(SqlKind.COMPARISON)) {
      continue;
    }

    // OR the factors referenced in this join filter into the
    // bitmaps corresponding to each of the factors; however,
    // exclude the bit corresponding to the factor itself
    for (int factor : factorRefs) {
      factorsRefByFactor[factor] =
          factorsRefByFactor[factor]
              .rebuild()
              .addAll(factorRefs)
              .clear(factor)
              .build();
    }

    if (factorRefs.cardinality() == 2) {
      int leftFactor = factorRefs.nextSetBit(0);
      int rightFactor = factorRefs.nextSetBit(leftFactor + 1);

      final RexCall call = (RexCall) joinFilter;
      ImmutableBitSet leftFields = fieldBitmap(call.getOperands().get(0));
      ImmutableBitSet leftBitmap = factorBitmap(leftFields);

      // filter contains only two factor references, one on each
      // side of the operator
      int weight;
      if (leftBitmap.cardinality() == 1) {
        // give higher weight to equi-joins
        switch (joinFilter.getKind()) {
        case EQUALS:
          weight = 3;
          break;
        default:
          weight = 2;
        }
      } else {
        // cross product of two tables
        weight = 1;
      }
      setFactorWeight(weight, leftFactor, rightFactor);
    } else {
      // multiple factor references -- set a weight for each
      // combination of factors referenced within the filter
      final List<Integer> list  = ImmutableIntList.copyOf(factorRefs);
      for (int outer : list) {
        for (int inner : list) {
          if (outer != inner) {
            setFactorWeight(1, outer, inner);
          }
        }
      }
    }
  }
}