Java Code Examples for org.apache.calcite.rel.metadata.RelMetadataQuery#collations()

The following examples show how to use org.apache.calcite.rel.metadata.RelMetadataQuery#collations() . 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: 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 2
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to determine a
 * {@link org.apache.calcite.rel.core.Match}'s collation.
 */
public static List<RelCollation> match(RelMetadataQuery mq, RelNode input,
		RelDataType rowType,
		RexNode pattern,
		boolean strictStart,
		boolean strictEnd,
		Map<String, RexNode> patternDefinitions,
		Map<String, RexNode> measures,
		RexNode after,
		Map<String, ? extends SortedSet<String>> subsets,
		boolean allRows,
		ImmutableBitSet partitionKeys,
		RelCollation orderKeys,
		RexNode interval) {
	return mq.collations(input);
}
 
Example 3
Source File: RelFieldTrimmer.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Trims the fields of an input relational expression.
 *
 * @param rel        Relational expression
 * @param input      Input relational expression, whose fields to trim
 * @param fieldsUsed Bitmap of fields needed by the consumer
 * @return New relational expression and its field mapping
 */
protected TrimResult trimChild(RelNode rel, RelNode input, final ImmutableBitSet fieldsUsed,
        Set<RelDataTypeField> extraFields) {
    final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();

    // Fields that define the collation cannot be discarded.
    final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    final ImmutableList<RelCollation> collations = mq.collations(input);
    for (RelCollation collation : collations) {
        for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
            fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
        }
    }

    // Correlating variables are a means for other relational expressions to use
    // fields.
    for (final CorrelationId correlation : rel.getVariablesSet()) {
        rel.accept(new CorrelationReferenceFinder() {
            @Override
            protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v = (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.getCorrelationId().equals(correlation)) {
                    fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
                }
                return fieldAccess;
            }
        });
    }

    return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields);
}
 
Example 4
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 5 votes vote down vote up
public com.google.common.collect.ImmutableList<RelCollation> collations(RelSubset subset, RelMetadataQuery mq) {
	if (!Bug.CALCITE_1048_FIXED) {
		//if the best node is null, so we can get the collation based original node, due to
		//the original node is logically equivalent as the rel.
		RelNode rel = Util.first(subset.getBest(), subset.getOriginal());
		return mq.collations(rel);
	} else {
		throw new RuntimeException("CALCITE_1048 is fixed, so check this method again!");
	}
}
 
Example 5
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 5 votes vote down vote up
public static List<RelCollation> enumerableCorrelate(
		RelMetadataQuery mq,
		RelNode left,
		RelNode right,
		JoinRelType joinType) {
	// The current implementation always preserve the sort order of the left input
	return mq.collations(left);
}
 
Example 6
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 5 votes vote down vote up
public static List<RelCollation> enumerableSemiJoin(
		RelMetadataQuery mq,
		RelNode left,
		RelNode right) {
	// The current implementation always preserve the sort order of the left input
	return mq.collations(left);
}
 
Example 7
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 5 votes vote down vote up
public static List<RelCollation> enumerableBatchNestedLoopJoin(
		RelMetadataQuery mq,
		RelNode left,
		RelNode right,
		JoinRelType joinType) {
	// The current implementation always preserve the sort order of the left input
	return mq.collations(left);
}
 
Example 8
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 5 votes vote down vote up
private static List<RelCollation> enumerableJoin0(
		RelMetadataQuery mq,
		RelNode left,
		RelNode right,
		JoinRelType joinType) {
	// The current implementation can preserve the sort order of the left input if one of the
	// following conditions hold:
	// (i) join type is INNER or LEFT;
	// (ii) RelCollation always orders nulls last.
	final com.google.common.collect.ImmutableList<RelCollation> leftCollations = mq.collations(left);
	switch (joinType) {
		case SEMI:
		case ANTI:
		case INNER:
		case LEFT:
			return leftCollations;
		case RIGHT:
		case FULL:
			for (RelCollation collation : leftCollations) {
				for (RelFieldCollation field : collation.getFieldCollations()) {
					if (!(RelFieldCollation.NullDirection.LAST == field.nullDirection)) {
						return com.google.common.collect.ImmutableList.of();
					}
				}
			}
			return leftCollations;
	}
	return com.google.common.collect.ImmutableList.of();
}
 
Example 9
Source File: RelFieldTrimmer.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Trims the fields of an input relational expression.
 *
 * @param rel        Relational expression
 * @param input      Input relational expression, whose fields to trim
 * @param fieldsUsed Bitmap of fields needed by the consumer
 * @return New relational expression and its field mapping
 */
protected TrimResult trimChild(
    RelNode rel,
    RelNode input,
    final ImmutableBitSet fieldsUsed,
    Set<RelDataTypeField> extraFields) {
  final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();

  // Fields that define the collation cannot be discarded.
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  final ImmutableList<RelCollation> collations = mq.collations(input);
  for (RelCollation collation : collations) {
    for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
      fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
    }
  }

  // Correlating variables are a means for other relational expressions to use
  // fields.
  for (final CorrelationId correlation : rel.getVariablesSet()) {
    rel.accept(
        new CorrelationReferenceFinder() {
          protected RexNode handle(RexFieldAccess fieldAccess) {
            final RexCorrelVariable v =
                (RexCorrelVariable) fieldAccess.getReferenceExpr();
            if (v.id.equals(correlation)) {
              fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
            }
            return fieldAccess;
          }
        });
  }

  return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields);
}
 
Example 10
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 4 votes vote down vote up
public com.google.common.collect.ImmutableList<RelCollation> collations(Filter rel, RelMetadataQuery mq) {
	return mq.collations(rel.getInput());
}
 
Example 11
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 4 votes vote down vote up
public com.google.common.collect.ImmutableList<RelCollation> collations(TableModify rel, RelMetadataQuery mq) {
	return mq.collations(rel.getInput());
}
 
Example 12
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 4 votes vote down vote up
public com.google.common.collect.ImmutableList<RelCollation> collations(HepRelVertex rel, RelMetadataQuery mq) {
	return mq.collations(rel.getCurrentRel());
}
 
Example 13
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to determine a
 * {@link org.apache.calcite.rel.core.Window}'s collation.
 *
 * <p>A Window projects the fields of its input first, followed by the output
 * from each of its windows. Assuming (quite reasonably) that the
 * implementation does not re-order its input rows, then any collations of its
 * input are preserved.
 */
public static List<RelCollation> window(
		RelMetadataQuery mq,
		RelNode input,
		com.google.common.collect.ImmutableList<Window.Group> groups) {
	return mq.collations(input);
}
 
Example 14
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Helper method to determine a
 * {@link org.apache.calcite.rel.core.Filter}'s collation.
 */
public static List<RelCollation> filter(RelMetadataQuery mq, RelNode input) {
	return mq.collations(input);
}
 
Example 15
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Helper method to determine a
 * {@link org.apache.calcite.rel.core.Snapshot}'s collation.
 */
public static List<RelCollation> snapshot(RelMetadataQuery mq, RelNode input) {
	return mq.collations(input);
}
 
Example 16
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Helper method to determine a
 * limit's collation.
 */
public static List<RelCollation> limit(RelMetadataQuery mq, RelNode input) {
	return mq.collations(input);
}