org.apache.calcite.util.mapping.IntPair Java Examples

The following examples show how to use org.apache.calcite.util.mapping.IntPair. 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: LatticeSuggester.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public String toString() {
  final StringBuilder b = new StringBuilder()
      .append("StepRef(")
      .append(source)
      .append(", ")
      .append(target)
      .append(",");
  for (IntPair key : step.keys) {
    b.append(' ')
        .append(step.source().field(key.source).getName())
        .append(':')
        .append(step.target().field(key.target).getName());
  }
  return b.append("):")
      .append(ordinalInQuery)
      .toString();
}
 
Example #2
Source File: Permutation.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Iterator<IntPair> iterator() {
  return new Iterator<IntPair>() {
    private int i = 0;

    public boolean hasNext() {
      return i < targets.length;
    }

    public IntPair next() {
      final IntPair pair = new IntPair(i, targets[i]);
      ++i;
      return pair;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }
  };
}
 
Example #3
Source File: Step.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public String toString() {
  final StringBuilder b = new StringBuilder()
      .append("Step(")
      .append(source)
      .append(", ")
      .append(target)
      .append(",");
  for (IntPair key : keys) {
    b.append(' ')
        .append(source().field(key.source).getName())
        .append(':')
        .append(target().field(key.target).getName());
  }
  return b.append(")")
      .toString();
}
 
Example #4
Source File: Permutation.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Iterator<IntPair> iterator() {
  return new Iterator<IntPair>() {
    private int i = 0;

    public boolean hasNext() {
      return i < targets.length;
    }

    public IntPair next() {
      final IntPair pair = new IntPair(i, targets[i]);
      ++i;
      return pair;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }
  };
}
 
Example #5
Source File: DremioFieldTrimmer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected TrimResult result(RelNode r, final Mapping mapping) {
  final RexBuilder rexBuilder = builder.getRexBuilder();
  for (final CorrelationId correlation : r.getVariablesSet()) {
    r = r.accept(
            new CorrelationReferenceFinder() {
              protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v =
                        (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.id.equals(correlation)) {
                  final int old = fieldAccess.getField().getIndex();
                  final int new_ = mapping.getTarget(old);
                  final RelDataTypeFactory.Builder typeBuilder =
                          builder.getTypeFactory().builder();
                  for (IntPair pair : mapping) {
                    if (pair.source < v.getType().getFieldCount()) {
                      typeBuilder.add(v.getType().getFieldList().get(pair.source));
                    }
                  }
                  final RexNode newV =
                          rexBuilder.makeCorrel(typeBuilder.build(), v.id);
                  if (old != new_) {
                    return rexBuilder.makeFieldAccess(newV, new_);
                  }
                }
                return fieldAccess;
              }
            });
  }
  return new TrimResult(r, mapping);
}
 
Example #6
Source File: LoptOptimizeJoinRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the equality portion of a self-join condition is between
 * identical keys that are unique.
 *
 * @param mq Metadata query
 * @param leftRel left side of the join
 * @param rightRel right side of the join
 * @param joinFilters the join condition
 *
 * @return true if the equality join keys are the same and unique
 */
private static boolean areSelfJoinKeysUnique(RelMetadataQuery mq,
    RelNode leftRel, RelNode rightRel, RexNode joinFilters) {
  final JoinInfo joinInfo = JoinInfo.of(leftRel, rightRel, joinFilters);

  // Make sure each key on the left maps to the same simple column as the
  // corresponding key on the right
  for (IntPair pair : joinInfo.pairs()) {
    final RelColumnOrigin leftOrigin =
        mq.getColumnOrigin(leftRel, pair.source);
    if (leftOrigin == null) {
      return false;
    }
    final RelColumnOrigin rightOrigin =
        mq.getColumnOrigin(rightRel, pair.target);
    if (rightOrigin == null) {
      return false;
    }
    if (leftOrigin.getOriginColumnOrdinal()
        != rightOrigin.getOriginColumnOrdinal()) {
      return false;
    }
  }

  // Now that we've verified that the keys are the same, see if they
  // are unique.  When removing self-joins, if needed, we'll later add an
  // IS NOT NULL filter on the join keys that are nullable.  Therefore,
  // it's ok if there are nulls in the unique key.
  return RelMdUtil.areColumnsDefinitelyUniqueWhenNullsFiltered(mq, leftRel,
      joinInfo.leftSet());
}
 
Example #7
Source File: LatticeSpace.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a list of {@link IntPair} that is sorted and unique. */
static List<IntPair> sortUnique(List<IntPair> keys) {
  if (keys.size() > 1) {
    // list may not be sorted; sort it
    keys = IntPair.ORDERING.immutableSortedCopy(keys);
    if (!IntPair.ORDERING.isStrictlyOrdered(keys)) {
      // list may contain duplicates; sort and eliminate duplicates
      final Set<IntPair> set = new TreeSet<>(IntPair.ORDERING);
      set.addAll(keys);
      keys = ImmutableList.copyOf(set);
    }
  }
  return keys;
}
 
Example #8
Source File: LatticeSpace.java    From calcite with Apache License 2.0 5 votes vote down vote up
Step addEdge(LatticeTable source, LatticeTable target, List<IntPair> keys) {
  keys = sortUnique(keys);
  final Step step = g.addEdge(source, target, keys);
  if (step != null) {
    return step;
  }
  for (Step step2 : g.getEdges(source, target)) {
    if (step2.keys.equals(keys)) {
      return step2;
    }
  }
  throw new AssertionError("addEdge failed, yet no edge present");
}
 
Example #9
Source File: Step.java    From calcite with Apache License 2.0 5 votes vote down vote up
boolean isBackwards(SqlStatisticProvider statisticProvider) {
  final RelOptTable sourceTable = source().t;
  final List<Integer> sourceColumns = IntPair.left(keys);
  final RelOptTable targetTable = target().t;
  final List<Integer> targetColumns = IntPair.right(keys);
  final boolean noDerivedSourceColumns =
      sourceColumns.stream().allMatch(i ->
          i < sourceTable.getRowType().getFieldCount());
  final boolean noDerivedTargetColumns =
      targetColumns.stream().allMatch(i ->
          i < targetTable.getRowType().getFieldCount());
  final boolean forwardForeignKey = noDerivedSourceColumns
      && noDerivedTargetColumns
      && statisticProvider.isForeignKey(sourceTable, sourceColumns,
          targetTable, targetColumns)
      && statisticProvider.isKey(targetTable, targetColumns);
  final boolean backwardForeignKey = noDerivedSourceColumns
      && noDerivedTargetColumns
      && statisticProvider.isForeignKey(targetTable, targetColumns,
          sourceTable, sourceColumns)
      && statisticProvider.isKey(sourceTable, sourceColumns);
  if (backwardForeignKey != forwardForeignKey) {
    return backwardForeignKey;
  }
  // Tie-break if it's a foreign key in neither or both directions
  return compare(sourceTable, sourceColumns, targetTable, targetColumns) < 0;
}
 
Example #10
Source File: Step.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a Step. */
static Step create(LatticeTable source, LatticeTable target,
    List<IntPair> keys, LatticeSpace space) {
  final StringBuilder b = new StringBuilder();
  for (IntPair key : keys) {
    b.append(' ')
        .append(space.fieldName(source, key.source))
        .append(':')
        .append(space.fieldName(target, key.target));
  }
  return new Step(source, target, keys, b.toString());
}
 
Example #11
Source File: Step.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Step(LatticeTable source, LatticeTable target,
    List<IntPair> keys, String keyString) {
  super(source, target);
  this.keys = ImmutableList.copyOf(keys);
  this.keyString = Objects.requireNonNull(keyString);
  assert IntPair.ORDERING.isStrictlyOrdered(keys); // ordered and unique
}
 
Example #12
Source File: MutableNode.java    From calcite with Apache License 2.0 5 votes vote down vote up
public int compare(MutableNode o1, MutableNode o2) {
  int c = Ordering.<String>natural().lexicographical().compare(
      o1.table.t.getQualifiedName(), o2.table.t.getQualifiedName());
  if (c == 0) {
    // The nodes have the same table. Now compare them based on the
    // columns they use as foreign key.
    c = Ordering.<Integer>natural().lexicographical().compare(
        IntPair.left(o1.step.keys), IntPair.left(o2.step.keys));
  }
  return c;
}
 
Example #13
Source File: MutableNode.java    From Bats with Apache License 2.0 5 votes vote down vote up
public int compare(MutableNode o1, MutableNode o2) {
  int c = Ordering.<String>natural().lexicographical().compare(
      o1.table.t.getQualifiedName(), o2.table.t.getQualifiedName());
  if (c == 0) {
    // The nodes have the same table. Now compare them based on the
    // columns they use as foreign key.
    c = Ordering.<Integer>natural().lexicographical().compare(
        IntPair.left(o1.step.keys), IntPair.left(o2.step.keys));
  }
  return c;
}
 
Example #14
Source File: LoptOptimizeJoinRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the equality portion of a self-join condition is between
 * identical keys that are unique.
 *
 * @param mq Metadata query
 * @param leftRel left side of the join
 * @param rightRel right side of the join
 * @param joinFilters the join condition
 *
 * @return true if the equality join keys are the same and unique
 */
private static boolean areSelfJoinKeysUnique(RelMetadataQuery mq,
    RelNode leftRel, RelNode rightRel, RexNode joinFilters) {
  final JoinInfo joinInfo = JoinInfo.of(leftRel, rightRel, joinFilters);

  // Make sure each key on the left maps to the same simple column as the
  // corresponding key on the right
  for (IntPair pair : joinInfo.pairs()) {
    final RelColumnOrigin leftOrigin =
        mq.getColumnOrigin(leftRel, pair.source);
    if (leftOrigin == null) {
      return false;
    }
    final RelColumnOrigin rightOrigin =
        mq.getColumnOrigin(rightRel, pair.target);
    if (rightOrigin == null) {
      return false;
    }
    if (leftOrigin.getOriginColumnOrdinal()
        != rightOrigin.getOriginColumnOrdinal()) {
      return false;
    }
  }

  // Now that we've verified that the keys are the same, see if they
  // are unique.  When removing self-joins, if needed, we'll later add an
  // IS NOT NULL filter on the join keys that are nullable.  Therefore,
  // it's ok if there are nulls in the unique key.
  return RelMdUtil.areColumnsDefinitelyUniqueWhenNullsFiltered(mq, leftRel,
      joinInfo.leftSet());
}
 
Example #15
Source File: LatticeSpace.java    From Bats with Apache License 2.0 5 votes vote down vote up
Step addEdge(LatticeTable source, LatticeTable target, List<IntPair> keys) {
  keys = sortUnique(keys);
  final Step step = g.addEdge(source, target, keys);
  if (step != null) {
    return step;
  }
  for (Step step2 : g.getEdges(source, target)) {
    if (step2.keys.equals(keys)) {
      return step2;
    }
  }
  throw new AssertionError("addEdge failed, yet no edge present");
}
 
Example #16
Source File: LatticeSpace.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns a list of {@link IntPair} that is sorted and unique. */
static List<IntPair> sortUnique(List<IntPair> keys) {
  if (keys.size() > 1) {
    // list may not be sorted; sort it
    keys = IntPair.ORDERING.immutableSortedCopy(keys);
    if (!IntPair.ORDERING.isStrictlyOrdered(keys)) {
      // list may contain duplicates; sort and eliminate duplicates
      final Set<IntPair> set = new TreeSet<>(IntPair.ORDERING);
      set.addAll(keys);
      keys = ImmutableList.copyOf(set);
    }
  }
  return keys;
}
 
Example #17
Source File: LatticeSpace.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns a list of {@link IntPair}, transposing source and target fields,
 * and ensuring the result is sorted and unique. */
static List<IntPair> swap(List<IntPair> keys) {
  return sortUnique(Lists.transform(keys, IntPair.SWAP));
}
 
Example #18
Source File: Permutation.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void setAll(Mapping mapping) {
  for (IntPair pair : mapping) {
    set(pair.source, pair.target);
  }
}
 
Example #19
Source File: Step.java    From Bats with Apache License 2.0 4 votes vote down vote up
Step(LatticeTable source, LatticeTable target, List<IntPair> keys) {
  super(source, target);
  this.keys = ImmutableList.copyOf(keys);
  assert IntPair.ORDERING.isStrictlyOrdered(keys); // ordered and unique
}
 
Example #20
Source File: JoinInfo.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns a list of (left, right) key ordinals. */
public List<IntPair> pairs() {
  return IntPair.zip(leftKeys, rightKeys);
}
 
Example #21
Source File: Step.java    From Bats with Apache License 2.0 4 votes vote down vote up
public Step createEdge(LatticeTable source, LatticeTable target,
    Object... attributes) {
  @SuppressWarnings({ "unchecked", "rawtypes" }) final List<IntPair> keys =
      (List) attributes[0];
  return new Step(source, target, keys);
}
 
Example #22
Source File: RelReferentialConstraint.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** The (source, target) column ordinals. */
List<IntPair> getColumnPairs();
 
Example #23
Source File: RelReferentialConstraintImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static RelReferentialConstraintImpl of(List<String> sourceQualifiedName,
    List<String> targetQualifiedName, List<IntPair> columnPairs) {
  return new RelReferentialConstraintImpl(
      sourceQualifiedName, targetQualifiedName, columnPairs);
}
 
Example #24
Source File: RelReferentialConstraintImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public List<IntPair> getColumnPairs() {
  return columnPairs;
}
 
Example #25
Source File: RelReferentialConstraintImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
private RelReferentialConstraintImpl(List<String> sourceQualifiedName,
    List<String> targetQualifiedName, List<IntPair> columnPairs) {
  this.sourceQualifiedName = ImmutableList.copyOf(sourceQualifiedName);
  this.targetQualifiedName = ImmutableList.copyOf(targetQualifiedName);
  this.columnPairs = ImmutableList.copyOf(columnPairs);
}
 
Example #26
Source File: RelReferentialConstraint.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** The (source, target) column ordinals. */
List<IntPair> getColumnPairs();
 
Example #27
Source File: JoinInfo.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Returns a list of (left, right) key ordinals. */
public List<IntPair> pairs() {
  return IntPair.zip(leftKeys, rightKeys);
}
 
Example #28
Source File: Permutation.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void setAll(Mapping mapping) {
  for (IntPair pair : mapping) {
    set(pair.source, pair.target);
  }
}
 
Example #29
Source File: Step.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Step createEdge(LatticeTable source, LatticeTable target,
    Object... attributes) {
  @SuppressWarnings("unchecked") final List<IntPair> keys =
      (List) attributes[0];
  return Step.create(source, target, keys, space);
}
 
Example #30
Source File: LatticeSpace.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Returns a list of {@link IntPair}, transposing source and target fields,
 * and ensuring the result is sorted and unique. */
static List<IntPair> swap(List<IntPair> keys) {
  return sortUnique(Lists.transform(keys, IntPair.SWAP));
}