Java Code Examples for org.apache.calcite.rel.RelCollation#satisfies()

The following examples show how to use org.apache.calcite.rel.RelCollation#satisfies() . 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: RelMdUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Returns whether a relational expression is already sorted and has fewer
 * rows than the sum of offset and limit.
 *
 * <p>If this is the case, it is safe to push down a
 * {@link org.apache.calcite.rel.core.Sort} with limit and optional offset. */
public static boolean checkInputForCollationAndLimit(RelMetadataQuery mq,
    RelNode input, RelCollation collation, RexNode offset, RexNode fetch) {
  // Check if the input is already sorted
  boolean alreadySorted = collation.getFieldCollations().isEmpty();
  for (RelCollation inputCollation : mq.collations(input)) {
    if (inputCollation.satisfies(collation)) {
      alreadySorted = true;
      break;
    }
  }
  // Check if we are not reducing the number of tuples
  boolean alreadySmaller = true;
  final Double rowCount = mq.getMaxRowCount(input);
  if (rowCount != null && fetch != null) {
    final int offsetVal = offset == null ? 0 : RexLiteral.intValue(offset);
    final int limit = RexLiteral.intValue(fetch);
    if ((double) offsetVal + (double) limit < rowCount) {
      alreadySmaller = false;
    }
  }
  return alreadySorted && alreadySmaller;
}
 
Example 2
Source File: StreamAggPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static void validateCollation(RelOptCluster cluster, RelNode child, ImmutableBitSet groupSet) {
  if (groupSet.isEmpty()) {
    // If no groups, no collation is required
    return;
  }

  final RelCollation requiredCollation = RelCollations.of(
      StreamSupport.stream(groupSet.spliterator(), false).map(RelFieldCollation::new).collect(Collectors.toList()));

  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final List<RelCollation> collations = mq.collations(child);

  for(RelCollation collation: collations) {
    if (collation.satisfies(requiredCollation)) {
      return;
    }
  }

  throw new AssertionError("child collations [" + collations + "] does not match expected collation [" + requiredCollation + "]");
}
 
Example 3
Source File: RelMdUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns whether a relational expression is already sorted and has fewer
 * rows than the sum of offset and limit.
 *
 * <p>If this is the case, it is safe to push down a
 * {@link org.apache.calcite.rel.core.Sort} with limit and optional offset. */
public static boolean checkInputForCollationAndLimit(RelMetadataQuery mq,
    RelNode input, RelCollation collation, RexNode offset, RexNode fetch) {
  // Check if the input is already sorted
  boolean alreadySorted = collation.getFieldCollations().isEmpty();
  for (RelCollation inputCollation : mq.collations(input)) {
    if (inputCollation.satisfies(collation)) {
      alreadySorted = true;
      break;
    }
  }
  // Check if we are not reducing the number of tuples
  boolean alreadySmaller = true;
  final Double rowCount = mq.getMaxRowCount(input);
  if (rowCount != null && fetch != null) {
    final int offsetVal = offset == null ? 0 : RexLiteral.intValue(offset);
    final int limit = RexLiteral.intValue(fetch);
    if ((double) offsetVal + (double) limit < rowCount) {
      alreadySmaller = false;
    }
  }
  return alreadySorted && alreadySmaller;
}
 
Example 4
Source File: IndexSelector.java    From Bats with Apache License 2.0 5 votes vote down vote up
private boolean buildAndCheckCollation(IndexProperties indexProps) {
  IndexDescriptor indexDesc = indexProps.getIndexDesc();
  FunctionalIndexInfo functionInfo = indexDesc.getFunctionalInfo();

  RelCollation inputCollation;
  // for the purpose of collation we can assume that a covering index scan would provide
  // the collation property that would be relevant for non-covering as well
  ScanPrel indexScanPrel =
      IndexPlanUtils.buildCoveringIndexScan(indexContext.getScan(), indexDesc.getIndexGroupScan(), indexContext, indexDesc);
  inputCollation = indexScanPrel.getTraitSet().getTrait(RelCollationTraitDef.INSTANCE);

  // we don't create collation for Filter because it will inherit the child's collation

  if (indexContext.hasLowerProject()) {
    inputCollation =
        IndexPlanUtils.buildCollationProject(indexContext.getLowerProject().getProjects(), null,
            indexContext.getScan(), functionInfo,indexContext);
  }

  if (indexContext.hasUpperProject()) {
    inputCollation =
        IndexPlanUtils.buildCollationProject(indexContext.getUpperProject().getProjects(), indexContext.getLowerProject(),
            indexContext.getScan(), functionInfo, indexContext);
  }

  if ((inputCollation != null) &&
       (inputCollation.satisfies(indexContext.getCollation()))) {
    return true;
  }

  return false;
}
 
Example 5
Source File: AbstractIndexPlanGenerator.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected static boolean toRemoveSort(RelCollation sortCollation, RelCollation inputCollation) {
  if ( (inputCollation != null) && inputCollation.satisfies(sortCollation)) {
    return true;
  }
  return false;
}