Java Code Examples for org.apache.calcite.rel.RelCollations#EMPTY

The following examples show how to use org.apache.calcite.rel.RelCollations#EMPTY . 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: CassandraFilter.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Infer the implicit correlation from the unrestricted clustering keys.
 *
 * @return The collation of the filtered results
 */
public RelCollation getImplicitCollation() {
  // No collation applies if we aren't restricted to a single partition
  if (!isSinglePartition()) {
    return RelCollations.EMPTY;
  }

  // Pull out the correct fields along with their original collations
  List<RelFieldCollation> fieldCollations = new ArrayList<>();
  for (int i = restrictedClusteringKeys; i < clusteringKeys.size(); i++) {
    int fieldIndex = fieldNames.indexOf(clusteringKeys.get(i));
    RelFieldCollation.Direction direction = implicitFieldCollations.get(i).getDirection();
    fieldCollations.add(new RelFieldCollation(fieldIndex, direction));
  }

  return RelCollations.of(fieldCollations);
}
 
Example 2
Source File: EnumerableTraitsUtils.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * This function can be reused when a Join's traits pass-down shall only
 * pass through collation to left input.
 *
 * @param required required trait set for the join
 * @param joinType the join type
 * @param leftInputFieldCount number of field count of left join input
 * @param joinTraitSet trait set of the join
 */
static Pair<RelTraitSet, List<RelTraitSet>> passThroughTraitsForJoin(
    RelTraitSet required, JoinRelType joinType,
    int leftInputFieldCount, RelTraitSet joinTraitSet) {
  RelCollation collation = required.getCollation();
  if (collation == null
      || collation == RelCollations.EMPTY
      || joinType == JoinRelType.FULL
      || joinType == JoinRelType.RIGHT) {
    return null;
  }

  for (RelFieldCollation fc : collation.getFieldCollations()) {
    // If field collation belongs to right input: cannot push down collation.
    if (fc.getFieldIndex() >= leftInputFieldCount) {
      return null;
    }
  }

  RelTraitSet passthroughTraitSet = joinTraitSet.replace(collation);
  return Pair.of(passthroughTraitSet,
      ImmutableList.of(
          passthroughTraitSet,
          passthroughTraitSet.replace(RelCollations.EMPTY)));
}
 
Example 3
Source File: EnumerableTraitsUtils.java    From calcite with Apache License 2.0 6 votes vote down vote up
static Pair<RelTraitSet, List<RelTraitSet>> passThroughTraitsForProject(
    RelTraitSet required,
    List<RexNode> exps,
    RelDataType inputRowType,
    RelDataTypeFactory typeFactory,
    RelTraitSet currentTraits) {
  final RelCollation collation = required.getCollation();
  if (collation == null || collation == RelCollations.EMPTY) {
    return null;
  }

  final Mappings.TargetMapping map =
      RelOptUtil.permutationIgnoreCast(
          exps, inputRowType);

  if (collation.getFieldCollations().stream().anyMatch(
      rc -> !isCollationOnTrivialExpr(exps, typeFactory,
          map, rc, true))) {
    return null;
  }

  final RelCollation newCollation = collation.apply(map);
  return Pair.of(currentTraits.replace(collation),
      ImmutableList.of(currentTraits.replace(newCollation)));
}
 
Example 4
Source File: PigConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
private List<RelNode> optimizePlans(List<RelNode> originalRels,
    List<RelOptRule> rules) {
  final RelOptPlanner planner = originalRels.get(0).getCluster().getPlanner();
  // Remember old rule set of the planner before resetting it with new rules
  final List<RelOptRule> oldRules = planner.getRules();
  resetPlannerRules(planner, rules);
  final Program program = Programs.of(RuleSets.ofList(planner.getRules()));
  final List<RelNode> optimizedPlans = new ArrayList<>();
  for (RelNode rel : originalRels) {
    final RelCollation collation = rel instanceof Sort
        ? ((Sort) rel).collation
        : RelCollations.EMPTY;
    // Apply the planner to obtain the physical plan
    final RelNode physicalPlan = program.run(planner, rel,
        rel.getTraitSet().replace(EnumerableConvention.INSTANCE)
            .replace(collation).simplify(),
        ImmutableList.of(), ImmutableList.of());

    // Then convert the physical plan back to logical plan
    final RelNode logicalPlan = new ToLogicalConverter(builder).visit(physicalPlan);
    optimizedPlans.add(logicalPlan);
  }
  resetPlannerRules(planner, oldRules);
  return optimizedPlans;
}
 
Example 5
Source File: AggregateCall.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an AggregateCall.
 *
 * @param aggFunction Aggregate function
 * @param distinct    Whether distinct
 * @param argList     List of ordinals of arguments
 * @param type        Result type
 * @param name        Name (may be null)
 */
@Deprecated // to be removed before 2.0
public AggregateCall(
    SqlAggFunction aggFunction,
    boolean distinct,
    List<Integer> argList,
    RelDataType type,
    String name) {
  this(aggFunction, distinct, false,
      argList, -1, RelCollations.EMPTY, type, name);
}
 
Example 6
Source File: AggregateCall.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an AggregateCall.
 *
 * @param aggFunction Aggregate function
 * @param distinct    Whether distinct
 * @param argList     List of ordinals of arguments
 * @param type        Result type
 * @param name        Name (may be null)
 */
@Deprecated // to be removed before 2.0
public AggregateCall(
    SqlAggFunction aggFunction,
    boolean distinct,
    List<Integer> argList,
    RelDataType type,
    String name) {
  this(aggFunction, distinct, false, false,
      argList, -1, RelCollations.EMPTY, type, name);
}
 
Example 7
Source File: AggregateCall.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an equivalent AggregateCall that has a new name.
 *
 * @param name New name (may be null)
 */
public AggregateCall rename(String name) {
  if (Objects.equals(this.name, name)) {
    return this;
  }
  return new AggregateCall(aggFunction, distinct, approximate, ignoreNulls,
      argList,
      filterArg, RelCollations.EMPTY, type,
      name);
}
 
Example 8
Source File: EnumerableFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
    RelTraitSet required) {
  RelCollation collation = required.getCollation();
  if (collation == null || collation == RelCollations.EMPTY) {
    return null;
  }
  RelTraitSet traits = traitSet.replace(collation);
  return Pair.of(traits, ImmutableList.of(traits));
}
 
Example 9
Source File: EnumerableFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
    final RelTraitSet childTraits, final int childId) {
  RelCollation collation = childTraits.getCollation();
  if (collation == null || collation == RelCollations.EMPTY) {
    return null;
  }
  RelTraitSet traits = traitSet.replace(collation);
  return Pair.of(traits, ImmutableList.of(traits));
}
 
Example 10
Source File: RelTraitSerializers.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public T read(final Kryo kryo, final Input input, final Class<T> type) {
  final boolean isKnown = kryo.readObject(input, Boolean.class);
  final T result;
  if (isKnown) {
    final Integer pos = kryo.readObject(input, Integer.class);
    result = (T) (pos == 0 ? RelCollations.EMPTY:RelCollations.PRESERVE);
  } else {
    result = super.read(kryo, input, type);
  }

  final T normalized = (T) result.getTraitDef().canonize(result);
  kryo.reference(normalized);
  return normalized;
}
 
Example 11
Source File: AggregateCall.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an equivalent AggregateCall that has a new name.
 *
 * @param name New name (may be null)
 */
public AggregateCall rename(String name) {
  if (Objects.equals(this.name, name)) {
    return this;
  }
  return new AggregateCall(aggFunction, distinct, approximate,
      argList,
      filterArg, RelCollations.EMPTY, type,
      name);
}
 
Example 12
Source File: LimitRelBase.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public LimitRelBase(RelOptCluster cluster, RelTraitSet traitSet, RelNode child, RexNode offset, RexNode fetch, boolean pushDown) {
  super(cluster, traitSet.plus(RelCollations.EMPTY), child, RelCollations.EMPTY, offset, fetch);
  this.traitSet = traitSet;
  this.pushDown = pushDown;
}
 
Example 13
Source File: LimitRelBase.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelCollation getCollation() {
  return RelCollations.EMPTY;
}
 
Example 14
Source File: DremioSqlDialect.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static boolean isCollationEmpty(Sort sort) {
  return sort.getCollation() == null || sort.getCollation() == RelCollations.EMPTY;
}
 
Example 15
Source File: SqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static RelRootPlus of(RelNode rel, RelDataType validatedRowType, SqlKind kind, boolean contextSensitive) {
  final ImmutableIntList refs = ImmutableIntList.identity(validatedRowType.getFieldCount());
  final List<String> names = validatedRowType.getFieldNames();
  return new RelRootPlus(rel, validatedRowType, kind, Pair.zip(refs, names), RelCollations.EMPTY, contextSensitive);
}
 
Example 16
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
private PreparedResult prepare_(Supplier<RelNode> fn,
    RelDataType resultType) {
  Class runtimeContextClass = Object.class;
  init(runtimeContextClass);

  final RelNode rel = fn.get();
  final RelDataType rowType = rel.getRowType();
  final List<Pair<Integer, String>> fields =
      Pair.zip(ImmutableIntList.identity(rowType.getFieldCount()),
          rowType.getFieldNames());
  final RelCollation collation =
      rel instanceof Sort
          ? ((Sort) rel).collation
          : RelCollations.EMPTY;
  RelRoot root = new RelRoot(rel, resultType, SqlKind.SELECT, fields,
      collation, new ArrayList<>());

  if (timingTracer != null) {
    timingTracer.traceTime("end sql2rel");
  }

  final RelDataType jdbcType =
      makeStruct(rexBuilder.getTypeFactory(), resultType);
  fieldOrigins = Collections.nCopies(jdbcType.getFieldCount(), null);
  parameterRowType = rexBuilder.getTypeFactory().builder().build();

  // Structured type flattening, view expansion, and plugging in
  // physical storage.
  root = root.withRel(flattenTypes(root.rel, true));

  // Trim unused fields.
  root = trimUnusedFields(root);

  final List<Materialization> materializations = ImmutableList.of();
  final List<CalciteSchema.LatticeEntry> lattices = ImmutableList.of();
  root = optimize(root, materializations, lattices);

  if (timingTracer != null) {
    timingTracer.traceTime("end optimization");
  }

  return implement(root);
}
 
Example 17
Source File: EnumerableTraitsUtils.java    From calcite with Apache License 2.0 4 votes vote down vote up
static Pair<RelTraitSet, List<RelTraitSet>> deriveTraitsForProject(
    RelTraitSet childTraits, int childId, List<RexNode> exps,
    RelDataType inputRowType, RelDataTypeFactory typeFactory, RelTraitSet currentTraits) {
  final RelCollation collation = childTraits.getCollation();
  if (collation == null || collation == RelCollations.EMPTY) {
    return null;
  }

  final int maxField = Math.max(exps.size(),
      inputRowType.getFieldCount());
  Mappings.TargetMapping mapping = Mappings
      .create(MappingType.FUNCTION, maxField, maxField);
  for (Ord<RexNode> node : Ord.zip(exps)) {
    if (node.e instanceof RexInputRef) {
      mapping.set(((RexInputRef) node.e).getIndex(), node.i);
    } else if (node.e.isA(SqlKind.CAST)) {
      final RexNode operand = ((RexCall) node.e).getOperands().get(0);
      if (operand instanceof RexInputRef) {
        mapping.set(((RexInputRef) operand).getIndex(), node.i);
      }
    }
  }

  List<RelFieldCollation> collationFieldsToDerive = new ArrayList<>();
  for (RelFieldCollation rc : collation.getFieldCollations()) {
    if (isCollationOnTrivialExpr(exps, typeFactory, mapping, rc, false)) {
      collationFieldsToDerive.add(rc);
    } else {
      break;
    }
  }

  if (collationFieldsToDerive.size() > 0) {
    final RelCollation newCollation = RelCollations
        .of(collationFieldsToDerive).apply(mapping);
    return Pair.of(currentTraits.replace(newCollation),
        ImmutableList.of(currentTraits.replace(collation)));
  } else {
    return null;
  }
}