Java Code Examples for org.apache.calcite.util.ImmutableIntList

The following examples show how to use org.apache.calcite.util.ImmutableIntList. These examples are extracted from open source projects. 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 Project: Bats   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private AggregateCall createAggregateCallWithBinding(
    RelDataTypeFactory typeFactory,
    SqlAggFunction aggFunction,
    RelDataType operandType,
    Aggregate oldAggRel,
    AggregateCall oldCall,
    int argOrdinal,
    int filter) {
  final Aggregate.AggCallBinding binding =
      new Aggregate.AggCallBinding(typeFactory, aggFunction,
          ImmutableList.of(operandType), oldAggRel.getGroupCount(),
          filter >= 0);
  return AggregateCall.create(aggFunction,
      oldCall.isDistinct(),
      oldCall.isApproximate(),
      ImmutableIntList.of(argOrdinal),
      filter,
      oldCall.collation,
      aggFunction.inferReturnType(binding),
      null);
}
 
Example 2
Source Project: calcite   Source File: EnumerableSortedAggregateRule.java    License: Apache License 2.0 6 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalAggregate agg = (LogicalAggregate) rel;
  if (!Aggregate.isSimple(agg)) {
    return null;
  }
  final RelTraitSet inputTraits = rel.getCluster()
      .traitSet().replace(EnumerableConvention.INSTANCE)
      .replace(
          RelCollations.of(
              ImmutableIntList.copyOf(
          agg.getGroupSet().asList())));
  final RelTraitSet selfTraits = inputTraits.replace(
      RelCollations.of(
      ImmutableIntList.identity(agg.getGroupSet().cardinality())));
  return new EnumerableSortedAggregate(
      rel.getCluster(),
      selfTraits,
      convert(agg.getInput(), inputTraits),
      agg.getGroupSet(),
      agg.getGroupSets(),
      agg.getAggCallList());
}
 
Example 3
Source Project: Bats   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode getRegrCountRexNode(Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    ImmutableIntList argOrdinals,
    ImmutableList<RelDataType> operandTypes,
    int filterArg) {
  final AggregateCall countArgAggCall =
      AggregateCall.create(SqlStdOperatorTable.REGR_COUNT,
          oldCall.isDistinct(),
          oldCall.isApproximate(),
          argOrdinals,
          filterArg,
          oldCall.collation,
          oldAggRel.getGroupCount(),
          oldAggRel,
          null,
          null);

  return oldAggRel.getCluster().getRexBuilder().addAggCall(countArgAggCall,
      oldAggRel.getGroupCount(),
      oldAggRel.indicator,
      newCalls,
      aggCallMapping,
      operandTypes);
}
 
Example 4
Source Project: calcite   Source File: ModifiableViewTable.java    License: 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 5
Source Project: Bats   Source File: ModifiableViewTable.java    License: 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 Project: flink   Source File: FlinkRelMdCollation.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to determine a {@link Join}'s collation assuming that it
 * uses a merge-join algorithm.
 *
 * <p>If the inputs are sorted on other keys <em>in addition to</em> the join
 * key, the result preserves those collations too.
 */
public static List<RelCollation> mergeJoin(
		RelMetadataQuery mq,
		RelNode left, RelNode right,
		ImmutableIntList leftKeys,
		ImmutableIntList rightKeys) {
	final com.google.common.collect.ImmutableList.Builder<RelCollation> builder =
			com.google.common.collect.ImmutableList.builder();

	final com.google.common.collect.ImmutableList<RelCollation> leftCollations = mq.collations(left);
	assert RelCollations.contains(leftCollations, leftKeys)
			: "cannot merge join: left input is not sorted on left keys";
	builder.addAll(leftCollations);

	final com.google.common.collect.ImmutableList<RelCollation> rightCollations = mq.collations(right);
	assert RelCollations.contains(rightCollations, rightKeys)
			: "cannot merge join: right input is not sorted on right keys";
	final int leftFieldCount = left.getRowType().getFieldCount();
	for (RelCollation collation : rightCollations) {
		builder.add(RelCollations.shift(collation, leftFieldCount));
	}
	return builder.build();
}
 
Example 7
Source Project: calcite   Source File: RelMdCollation.java    License: Apache License 2.0 6 votes vote down vote up
/** Helper method to determine a {@link Join}'s collation assuming that it
 * uses a merge-join algorithm.
 *
 * <p>If the inputs are sorted on other keys <em>in addition to</em> the join
 * key, the result preserves those collations too. */
public static List<RelCollation> mergeJoin(RelMetadataQuery mq,
    RelNode left, RelNode right,
    ImmutableIntList leftKeys, ImmutableIntList rightKeys, JoinRelType joinType) {
  assert EnumerableMergeJoin.isMergeJoinSupported(joinType)
      : "EnumerableMergeJoin unsupported for join type " + joinType;

  final ImmutableList<RelCollation> leftCollations = mq.collations(left);
  if (!joinType.projectsRight()) {
    return leftCollations;
  }

  final ImmutableList.Builder<RelCollation> builder = ImmutableList.builder();
  builder.addAll(leftCollations);

  final ImmutableList<RelCollation> rightCollations = mq.collations(right);
  final int leftFieldCount = left.getRowType().getFieldCount();
  for (RelCollation collation : rightCollations) {
    builder.add(RelCollations.shift(collation, leftFieldCount));
  }
  return builder.build();
}
 
Example 8
Source Project: calcite   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private AggregateCall createAggregateCallWithBinding(
    RelDataTypeFactory typeFactory,
    SqlAggFunction aggFunction,
    RelDataType operandType,
    Aggregate oldAggRel,
    AggregateCall oldCall,
    int argOrdinal,
    int filter) {
  final Aggregate.AggCallBinding binding =
      new Aggregate.AggCallBinding(typeFactory, aggFunction,
          ImmutableList.of(operandType), oldAggRel.getGroupCount(),
          filter >= 0);
  return AggregateCall.create(aggFunction,
      oldCall.isDistinct(),
      oldCall.isApproximate(),
      oldCall.ignoreNulls(),
      ImmutableIntList.of(argOrdinal),
      filter,
      oldCall.collation,
      aggFunction.inferReturnType(binding),
      null);
}
 
Example 9
Source Project: calcite   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode getRegrCountRexNode(Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    ImmutableIntList argOrdinals,
    ImmutableList<RelDataType> operandTypes,
    int filterArg) {
  final AggregateCall countArgAggCall =
      AggregateCall.create(SqlStdOperatorTable.REGR_COUNT,
          oldCall.isDistinct(),
          oldCall.isApproximate(),
          oldCall.ignoreNulls(),
          argOrdinals,
          filterArg,
          oldCall.collation,
          oldAggRel.getGroupCount(),
          oldAggRel,
          null,
          null);

  return oldAggRel.getCluster().getRexBuilder().addAggCall(countArgAggCall,
      oldAggRel.getGroupCount(),
      newCalls,
      aggCallMapping,
      operandTypes);
}
 
Example 10
Source Project: calcite   Source File: FilterTableScanRule.java    License: Apache License 2.0 6 votes vote down vote up
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
  final ImmutableIntList projects;
  final ImmutableList.Builder<RexNode> filters = ImmutableList.builder();
  if (scan instanceof Bindables.BindableTableScan) {
    final Bindables.BindableTableScan bindableScan =
        (Bindables.BindableTableScan) scan;
    filters.addAll(bindableScan.filters);
    projects = bindableScan.projects;
  } else {
    projects = scan.identity();
  }

  final Mapping mapping = Mappings.target(projects,
      scan.getTable().getRowType().getFieldCount());
  filters.add(
      RexUtil.apply(mapping.inverse(), filter.getCondition()));

  call.transformTo(
      Bindables.BindableTableScan.create(scan.getCluster(), scan.getTable(),
          filters.build(), projects));
}
 
Example 11
Source Project: calcite   Source File: TableScanNode.java    License: Apache License 2.0 6 votes vote down vote up
private static TableScanNode createFilterable(Compiler compiler,
    TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects,
    FilterableTable filterableTable) {
  final DataContext root = compiler.getDataContext();
  final List<RexNode> mutableFilters = Lists.newArrayList(filters);
  final Enumerable<Object[]> enumerable =
      filterableTable.scan(root, mutableFilters);
  for (RexNode filter : mutableFilters) {
    if (!filters.contains(filter)) {
      throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
    }
  }
  final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable);
  return createEnumerable(compiler, rel, rowEnumerable, null,
      mutableFilters, projects);
}
 
Example 12
Source Project: Bats   Source File: RelCollations.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns whether one of a list of collations indicates that the collection
 * is sorted on the given list of keys. */
public static boolean contains(List<RelCollation> collations,
    ImmutableIntList keys) {
  final List<Integer> distinctKeys = Util.distinctList(keys);
  for (RelCollation collation : collations) {
    if (contains(collation, distinctKeys)) {
      return true;
    }
  }
  return false;
}
 
Example 13
Source Project: Bats   Source File: MutableSemiJoin.java    License: Apache License 2.0 5 votes vote down vote up
private MutableSemiJoin(
    RelDataType rowType,
    MutableRel left,
    MutableRel right,
    RexNode condition,
    ImmutableIntList leftKeys,
    ImmutableIntList rightKeys) {
  super(MutableRelType.SEMIJOIN, left.cluster, rowType, left, right);
  this.condition = condition;
  this.leftKeys = leftKeys;
  this.rightKeys = rightKeys;
}
 
Example 14
Source Project: Bats   Source File: RelRoot.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a simple RelRoot. */
public static RelRoot of(RelNode rel, RelDataType rowType, SqlKind kind) {
  final ImmutableIntList refs =
      ImmutableIntList.identity(rowType.getFieldCount());
  final List<String> names = rowType.getFieldNames();
  return new RelRoot(rel, rowType, kind, Pair.zip(refs, names),
      RelCollations.EMPTY);
}
 
Example 15
Source Project: calcite   Source File: TableScanNode.java    License: Apache License 2.0 5 votes vote down vote up
private static TableScanNode createScannable(Compiler compiler, TableScan rel,
    ImmutableList<RexNode> filters, ImmutableIntList projects,
    ScannableTable scannableTable) {
  final Enumerable<Row> rowEnumerable =
      Enumerables.toRow(scannableTable.scan(compiler.getDataContext()));
  return createEnumerable(compiler, rel, rowEnumerable, null, filters,
      projects);
}
 
Example 16
Source Project: Bats   Source File: SemiJoinProjectTransposeRule.java    License: Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  SemiJoin semiJoin = call.rel(0);
  LogicalProject project = call.rel(1);

  // Convert the LHS semi-join keys to reference the child projection
  // expression; all projection expressions must be RexInputRefs,
  // otherwise, we wouldn't have created this semi-join.
  final List<Integer> newLeftKeys = new ArrayList<>();
  final List<Integer> leftKeys = semiJoin.getLeftKeys();
  final List<RexNode> projExprs = project.getProjects();
  for (int leftKey : leftKeys) {
    RexInputRef inputRef = (RexInputRef) projExprs.get(leftKey);
    newLeftKeys.add(inputRef.getIndex());
  }

  // convert the semijoin condition to reflect the LHS with the project
  // pulled up
  RexNode newCondition = adjustCondition(project, semiJoin);

  SemiJoin newSemiJoin =
      SemiJoin.create(project.getInput(), semiJoin.getRight(), newCondition,
          ImmutableIntList.copyOf(newLeftKeys), semiJoin.getRightKeys());

  // Create the new projection.  Note that the projection expressions
  // are the same as the original because they only reference the LHS
  // of the semijoin and the semijoin only projects out the LHS
  final RelBuilder relBuilder = call.builder();
  relBuilder.push(newSemiJoin);
  relBuilder.project(projExprs, project.getRowType().getFieldNames());

  call.transformTo(relBuilder.build());
}
 
Example 17
Source Project: Bats   Source File: EquiJoin.java    License: Apache License 2.0 5 votes vote down vote up
public EquiJoin(RelOptCluster cluster, RelTraitSet traits, RelNode left,
    RelNode right, RexNode condition, ImmutableIntList leftKeys,
    ImmutableIntList rightKeys, JoinRelType joinType,
    Set<String> variablesStopped) {
  super(cluster, traits, left, right, condition, leftKeys, rightKeys,
      CorrelationId.setOf(variablesStopped), joinType);
}
 
Example 18
Source Project: Bats   Source File: RelDistributions.java    License: 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 19
Source Project: Bats   Source File: RelDistributions.java    License: 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 20
Source Project: Bats   Source File: RelDistributions.java    License: Apache License 2.0 5 votes vote down vote up
public RelDistribution apply(Mappings.TargetMapping mapping) {
  if (keys.isEmpty()) {
    return this;
  }
  return getTraitDef().canonize(
      new RelDistributionImpl(type,
          ImmutableIntList.copyOf(
              Mappings.apply((Mapping) mapping, keys))));
}
 
Example 21
Source Project: Bats   Source File: Window.java    License: 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 22
Source Project: calcite   Source File: RelDistributions.java    License: Apache License 2.0 5 votes vote down vote up
public RelDistribution apply(Mappings.TargetMapping mapping) {
  if (keys.isEmpty()) {
    return this;
  }
  for (int key : keys) {
    if (mapping.getTargetOpt(key) == -1) {
      return ANY; // Some distribution keys are not mapped => any.
    }
  }
  List<Integer> mappedKeys0 = Mappings.apply2((Mapping) mapping, keys);
  ImmutableIntList mappedKeys = normalizeKeys(mappedKeys0);
  return of(type, mappedKeys);
}
 
Example 23
Source Project: Bats   Source File: SemiJoin.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a SemiJoin. */
public static SemiJoin create(RelNode left, RelNode right, RexNode condition,
    ImmutableIntList leftKeys, ImmutableIntList rightKeys) {
  final RelOptCluster cluster = left.getCluster();
  return new SemiJoin(cluster, cluster.traitSetOf(Convention.NONE), left,
      right, condition, leftKeys, rightKeys);
}
 
Example 24
Source Project: Bats   Source File: EquiJoin.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates an EquiJoin. */
public EquiJoin(RelOptCluster cluster, RelTraitSet traits, RelNode left,
    RelNode right, RexNode condition, ImmutableIntList leftKeys,
    ImmutableIntList rightKeys, Set<CorrelationId> variablesSet,
    JoinRelType joinType) {
  super(cluster, traits, left, right, condition, variablesSet, joinType);
  this.leftKeys = Objects.requireNonNull(leftKeys);
  this.rightKeys = Objects.requireNonNull(rightKeys);
}
 
Example 25
Source Project: Bats   Source File: EquiJoin.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public EquiJoin(RelOptCluster cluster, RelTraitSet traits, RelNode left,
    RelNode right, RexNode condition, ImmutableIntList leftKeys,
    ImmutableIntList rightKeys, JoinRelType joinType,
    Set<String> variablesStopped) {
  this(cluster, traits, left, right, condition, leftKeys, rightKeys,
      CorrelationId.setOf(variablesStopped), joinType);
}
 
Example 26
Source Project: calcite   Source File: ModifiableViewTable.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a ModifiableViewTable. */
public ModifiableViewTable(Type elementType, RelProtoDataType rowType,
    String viewSql, List<String> schemaPath, List<String> viewPath,
    Table table, Path tablePath, RexNode constraint,
    ImmutableIntList columnMapping) {
  super(elementType, rowType, viewSql, schemaPath, viewPath);
  this.table = table;
  this.tablePath = tablePath;
  this.constraint = constraint;
  this.columnMapping = columnMapping;
  this.initializerExpressionFactory = new ModifiableViewTableInitializerExpressionFactory();
}
 
Example 27
Source Project: calcite   Source File: EnumerableMergeJoin.java    License: Apache License 2.0 5 votes vote down vote up
private Mappings.TargetMapping buildMapping(boolean left2Right) {
  ImmutableIntList sourceKeys = left2Right ? joinInfo.leftKeys : joinInfo.rightKeys;
  ImmutableIntList targetKeys = left2Right ? joinInfo.rightKeys : joinInfo.leftKeys;
  Map<Integer, Integer> keyMap = new HashMap<>();
  for (int i = 0; i < joinInfo.leftKeys.size(); i++) {
    keyMap.put(sourceKeys.get(i), targetKeys.get(i));
  }

  Mappings.TargetMapping mapping = Mappings.target(keyMap,
      (left2Right ? left : right).getRowType().getFieldCount(),
      (left2Right ? right : left).getRowType().getFieldCount());
  return mapping;
}
 
Example 28
Source Project: Bats   Source File: RelBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private GroupKey groupKey_(ImmutableBitSet groupSet, boolean indicator, ImmutableList<ImmutableBitSet> groupSets) {
    if (groupSet.length() > peek().getRowType().getFieldCount()) {
        throw new IllegalArgumentException("out of bounds: " + groupSet);
    }
    Objects.requireNonNull(groupSets);
    final ImmutableList<RexNode> nodes = fields(ImmutableIntList.of(groupSet.toArray()));
    final List<ImmutableList<RexNode>> nodeLists = Util.transform(groupSets,
            bitSet -> fields(ImmutableIntList.of(bitSet.toArray())));
    return groupKey_(nodes, indicator, nodeLists);
}
 
Example 29
Source Project: Bats   Source File: ModifiableViewTable.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a ModifiableViewTable. */
public ModifiableViewTable(Type elementType, RelProtoDataType rowType,
    String viewSql, List<String> schemaPath, List<String> viewPath,
    Table table, Path tablePath, RexNode constraint,
    ImmutableIntList columnMapping) {
  super(elementType, rowType, viewSql, schemaPath, viewPath);
  this.table = table;
  this.tablePath = tablePath;
  this.constraint = constraint;
  this.columnMapping = columnMapping;
  this.initializerExpressionFactory = new ModifiableViewTableInitializerExpressionFactory();
}
 
Example 30
Source Project: Bats   Source File: StarTable.java    License: 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());
}