Java Code Examples for org.apache.calcite.util.ImmutableIntList#copyOf()

The following examples show how to use org.apache.calcite.util.ImmutableIntList#copyOf() . 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: JoinInfo.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates a {@code JoinInfo} by analyzing a condition. */
public static JoinInfo of(RelNode left, RelNode right, RexNode condition) {
  final List<Integer> leftKeys = new ArrayList<>();
  final List<Integer> rightKeys = new ArrayList<>();
  final List<Boolean> filterNulls = new ArrayList<>();
  RexNode remaining =
      RelOptUtil.splitJoinCondition(left, right, condition, leftKeys,
          rightKeys, filterNulls);
  if (remaining.isAlwaysTrue()) {
    return new EquiJoinInfo(ImmutableIntList.copyOf(leftKeys),
        ImmutableIntList.copyOf(rightKeys));
  } else {
    return new NonEquiJoinInfo(ImmutableIntList.copyOf(leftKeys),
        ImmutableIntList.copyOf(rightKeys), remaining);
  }
}
 
Example 2
Source File: ModifiableViewTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a mapping from the view index to the index in the underlying table.
 */
private static ImmutableIntList getNewColumnMapping(Table underlying,
    ImmutableIntList oldColumnMapping, List<RelDataTypeField> extendedColumns,
    RelDataTypeFactory typeFactory) {
  final List<RelDataTypeField> baseColumns =
      underlying.getRowType(typeFactory).getFieldList();
  final Map<String, Integer> nameToIndex = mapNameToIndex(baseColumns);

  final ImmutableList.Builder<Integer> newMapping = ImmutableList.builder();
  newMapping.addAll(oldColumnMapping);
  int newMappedIndex = baseColumns.size();
  for (RelDataTypeField extendedColumn : extendedColumns) {
    if (nameToIndex.containsKey(extendedColumn.getName())) {
      // The extended column duplicates a column in the underlying table.
      // Map to the index in the underlying table.
      newMapping.add(nameToIndex.get(extendedColumn.getName()));
    } else {
      // The extended column is not in the underlying table.
      newMapping.add(newMappedIndex++);
    }
  }
  return ImmutableIntList.copyOf(newMapping.build());
}
 
Example 3
Source File: RelJson.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelDistribution toDistribution(Map<String, Object> map) {
  final RelDistribution.Type type =
      Util.enumVal(RelDistribution.Type.class,
          (String) map.get("type"));

  ImmutableIntList list = EMPTY;
  if (map.containsKey("keys")) {
    List<Object> keysJson = (List<Object>) map.get("keys");
    ArrayList<Integer> keys = new ArrayList<>(keysJson.size());
    for (Object o : keysJson) {
      keys.add((Integer) o);
    }
    list = ImmutableIntList.copyOf(keys);
  }
  return RelDistributions.of(type, list);
}
 
Example 4
Source File: Bindables.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a BindableTableScan. */
public static BindableTableScan create(RelOptCluster cluster,
    RelOptTable relOptTable, List<RexNode> filters,
    List<Integer> projects) {
  final Table table = relOptTable.unwrap(Table.class);
  final RelTraitSet traitSet =
      cluster.traitSetOf(BindableConvention.INSTANCE)
          .replaceIfs(RelCollationTraitDef.INSTANCE, () -> {
            if (table != null) {
              return table.getStatistic().getCollations();
            }
            return ImmutableList.of();
          });
  return new BindableTableScan(cluster, traitSet, relOptTable,
      ImmutableList.copyOf(filters), ImmutableIntList.copyOf(projects));
}
 
Example 5
Source File: ModifiableViewTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a mapping from the view index to the index in the underlying table.
 */
private static ImmutableIntList getNewColumnMapping(Table underlying,
    ImmutableIntList oldColumnMapping, List<RelDataTypeField> extendedColumns,
    RelDataTypeFactory typeFactory) {
  final List<RelDataTypeField> baseColumns =
      underlying.getRowType(typeFactory).getFieldList();
  final Map<String, Integer> nameToIndex = mapNameToIndex(baseColumns);

  final ImmutableList.Builder<Integer> newMapping = ImmutableList.builder();
  newMapping.addAll(oldColumnMapping);
  int newMappedIndex = baseColumns.size();
  for (RelDataTypeField extendedColumn : extendedColumns) {
    if (nameToIndex.containsKey(extendedColumn.getName())) {
      // The extended column duplicates a column in the underlying table.
      // Map to the index in the underlying table.
      newMapping.add(nameToIndex.get(extendedColumn.getName()));
    } else {
      // The extended column is not in the underlying table.
      newMapping.add(newMappedIndex++);
    }
  }
  return ImmutableIntList.copyOf(newMapping.build());
}
 
Example 6
Source File: RelDistributions.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a hash distribution. */
public static RelDistribution hash(Collection<? extends Number> numbers) {
  ImmutableIntList list = ImmutableIntList.copyOf(numbers);
  if (numbers.size() > 1
      && !Ordering.natural().isOrdered(list)) {
    list = ImmutableIntList.copyOf(Ordering.natural().sortedCopy(list));
  }
  RelDistributionImpl trait =
      new RelDistributionImpl(RelDistribution.Type.HASH_DISTRIBUTED, list);
  return RelDistributionTraitDef.INSTANCE.canonize(trait);
}
 
Example 7
Source File: RelDistributions.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a range distribution. */
public static RelDistribution range(Collection<? extends Number> numbers) {
  ImmutableIntList list = ImmutableIntList.copyOf(numbers);
  RelDistributionImpl trait =
      new RelDistributionImpl(RelDistribution.Type.RANGE_DISTRIBUTED, list);
  return RelDistributionTraitDef.INSTANCE.canonize(trait);
}
 
Example 8
Source File: Window.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static ImmutableIntList getProjectOrdinals(final List<RexNode> exprs) {
    return ImmutableIntList.copyOf(new AbstractList<Integer>() {
        @Override
        public Integer get(int index) {
            return ((RexSlot) exprs.get(index)).getIndex();
        }

        @Override
        public int size() {
            return exprs.size();
        }
    });
}
 
Example 9
Source File: StarTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
  final List<RelDataType> typeList = new ArrayList<>();
  final List<Integer> fieldCounts = new ArrayList<>();
  for (Table table : tables) {
    final RelDataType rowType = table.getRowType(typeFactory);
    typeList.addAll(RelOptUtil.getFieldTypeList(rowType));
    fieldCounts.add(rowType.getFieldCount());
  }
  // Compute fieldCounts the first time this method is called. Safe to assume
  // that the field counts will be the same whichever type factory is used.
  if (this.fieldCounts == null) {
    this.fieldCounts = ImmutableIntList.copyOf(fieldCounts);
  }
  return typeFactory.createStructType(typeList, lattice.uniqueColumnNames());
}
 
Example 10
Source File: StarTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
  final List<RelDataType> typeList = new ArrayList<>();
  final List<Integer> fieldCounts = new ArrayList<>();
  for (Table table : tables) {
    final RelDataType rowType = table.getRowType(typeFactory);
    typeList.addAll(RelOptUtil.getFieldTypeList(rowType));
    fieldCounts.add(rowType.getFieldCount());
  }
  // Compute fieldCounts the first time this method is called. Safe to assume
  // that the field counts will be the same whichever type factory is used.
  if (this.fieldCounts == null) {
    this.fieldCounts = ImmutableIntList.copyOf(fieldCounts);
  }
  return typeFactory.createStructType(typeList, lattice.uniqueColumnNames());
}
 
Example 11
Source File: RelDistributions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates ordered immutable copy of keys collection.  */
private static ImmutableIntList normalizeKeys(Collection<? extends Number> keys) {
  ImmutableIntList list = ImmutableIntList.copyOf(keys);
  if (list.size() > 1
      && !Ordering.natural().isOrdered(list)) {
    list = ImmutableIntList.copyOf(Ordering.natural().sortedCopy(list));
  }
  return list;
}
 
Example 12
Source File: Window.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static ImmutableIntList getProjectOrdinals(final List<RexNode> exprs) {
  return ImmutableIntList.copyOf(
      new AbstractList<Integer>() {
        public Integer get(int index) {
          return ((RexSlot) exprs.get(index)).getIndex();
        }

        public int size() {
          return exprs.size();
        }
      });
}
 
Example 13
Source File: JoinInfo.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a {@code JoinInfo} by analyzing a condition. */
public static JoinInfo of(RelNode left, RelNode right, RexNode condition) {
  final List<Integer> leftKeys = new ArrayList<>();
  final List<Integer> rightKeys = new ArrayList<>();
  final List<Boolean> filterNulls = new ArrayList<>();
  final List<RexNode> nonEquiList = new ArrayList<>();
  RelOptUtil.splitJoinCondition(left, right, condition, leftKeys, rightKeys,
      filterNulls, nonEquiList);
  return new JoinInfo(ImmutableIntList.copyOf(leftKeys),
      ImmutableIntList.copyOf(rightKeys), ImmutableList.copyOf(nonEquiList));
}
 
Example 14
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);
          }
        }
      }
    }
  }
}
 
Example 15
Source File: TableScanNode.java    From calcite with Apache License 2.0 4 votes vote down vote up
private static TableScanNode createProjectableFilterable(Compiler compiler,
    TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects,
    ProjectableFilterableTable pfTable) {
  final DataContext root = compiler.getDataContext();
  final ImmutableIntList originalProjects = projects;
  for (;;) {
    final List<RexNode> mutableFilters = Lists.newArrayList(filters);
    final int[] projectInts;
    if (projects == null
        || projects.equals(TableScan.identity(rel.getTable()))) {
      projectInts = null;
    } else {
      projectInts = projects.toIntArray();
    }
    final Enumerable<Object[]> enumerable1 =
        pfTable.scan(root, mutableFilters, projectInts);
    for (RexNode filter : mutableFilters) {
      if (!filters.contains(filter)) {
        throw RESOURCE.filterableTableInventedFilter(filter.toString())
            .ex();
      }
    }
    final ImmutableBitSet usedFields =
        RelOptUtil.InputFinder.bits(mutableFilters, null);
    if (projects != null) {
      int changeCount = 0;
      for (int usedField : usedFields) {
        if (!projects.contains(usedField)) {
          // A field that is not projected is used in a filter that the
          // table rejected. We won't be able to apply the filter later.
          // Try again without any projects.
          projects =
              ImmutableIntList.copyOf(
                  Iterables.concat(projects, ImmutableList.of(usedField)));
          ++changeCount;
        }
      }
      if (changeCount > 0) {
        continue;
      }
    }
    final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable1);
    final ImmutableIntList rejectedProjects;
    if (Objects.equals(projects, originalProjects)) {
      rejectedProjects = null;
    } else {
      // We projected extra columns because they were needed in filters. Now
      // project the leading columns.
      rejectedProjects = ImmutableIntList.identity(originalProjects.size());
    }
    return createEnumerable(compiler, rel, rowEnumerable, projects,
        mutableFilters, rejectedProjects);
  }
}
 
Example 16
Source File: RelDistributions.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Creates a range distribution. */
public static RelDistribution range(Collection<? extends Number> numbers) {
  ImmutableIntList list = ImmutableIntList.copyOf(numbers);
  return of(RelDistribution.Type.RANGE_DISTRIBUTED, list);
}
 
Example 17
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);
}
 
Example 18
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 19
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());
}