Class Sort

All Implemented Interfaces:
Cloneable, RelOptNode, Hintable, RelNode
Direct Known Subclasses:
Bindables.BindableSort, CassandraSort, ElasticsearchSort, EnumerableLimitSort, EnumerableSort, GeodeSort, InnodbSort, JdbcRules.JdbcSort, LogicalSort, MongoSort

public abstract class Sort extends SingleRel implements Hintable
Relational expression that imposes a particular sort order on its input without otherwise changing its content.
  • Field Details

    • collation

      public final RelCollation collation
    • offset

      public final @Nullable RexNode offset
    • fetch

      public final @Nullable RexNode fetch
    • hints

      protected final com.google.common.collect.ImmutableList<RelHint> hints
  • Constructor Details

    • Sort

      protected Sort(RelOptCluster cluster, RelTraitSet traits, List<RelHint> hints, RelNode child, RelCollation collation, @Nullable RexNode offset, @Nullable RexNode fetch)
      Creates a Sort.
      Parameters:
      cluster - Cluster this relational expression belongs to
      traits - Traits
      hints - Hints for this node
      child - input relational expression
      collation - array of sort specifications
      offset - Expression for number of rows to discard before returning first row
      fetch - Expression for number of rows to fetch
    • Sort

      protected Sort(RelOptCluster cluster, RelTraitSet traits, RelNode child, RelCollation collation)
      Creates a Sort.
      Parameters:
      cluster - Cluster this relational expression belongs to
      traits - Traits
      child - input relational expression
      collation - array of sort specifications
    • Sort

      protected Sort(RelOptCluster cluster, RelTraitSet traits, RelNode child, RelCollation collation, @Nullable RexNode offset, @Nullable RexNode fetch)
      Creates a Sort.
      Parameters:
      cluster - Cluster this relational expression belongs to
      traits - Traits
      child - input relational expression
      collation - array of sort specifications
      offset - Expression for number of rows to discard before returning first row
      fetch - Expression for number of rows to fetch
    • Sort

      protected Sort(RelInput input)
      Creates a Sort by parsing serialized output.
  • Method Details

    • copy

      public final Sort copy(RelTraitSet traitSet, List<RelNode> inputs)
      Description copied from interface: RelNode
      Creates a copy of this relational expression, perhaps changing traits and inputs.

      Sub-classes with other important attributes are encouraged to create variants of this method with more parameters.

      Specified by:
      copy in interface RelNode
      Overrides:
      copy in class AbstractRelNode
      Parameters:
      traitSet - Trait set
      inputs - Inputs
      Returns:
      Copy of this relational expression, substituting traits and inputs
    • copy

      public final Sort copy(RelTraitSet traitSet, RelNode newInput, RelCollation newCollation)
    • copy

      public abstract Sort copy(RelTraitSet traitSet, RelNode newInput, RelCollation newCollation, @Nullable RexNode offset, @Nullable RexNode fetch)
    • computeSelfCost

      public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq)
      Returns the cost of this plan (not including children). The base implementation throws an error; derived classes should override.

      NOTE jvs 29-Mar-2006: Don't call this method directly. Instead, use RelMetadataQuery.getNonCumulativeCost(org.apache.calcite.rel.RelNode), which gives plugins a chance to override the rel's default ideas about cost.

      The CPU cost of a Sort has three main cases:

      • If fetch is zero, CPU cost is zero; otherwise,
      • if the sort keys are empty, we don't need to sort, only step over the rows, and therefore the CPU cost is min(fetch + offset, inputRowCount) * bytesPerRow; otherwise
      • we need to read and sort inputRowCount rows, with at most min(fetch + offset, inputRowCount) of them in the sort data structure at a time, giving a CPU cost of inputRowCount * log(min(fetch + offset, inputRowCount)) * bytesPerRow.

      The cost model factors in row width via bytesPerRow, because sorts need to move rows around, not just compare them; by making the cost higher if rows are wider, we discourage pushing a Project through a Sort. We assume that each field is 4 bytes, and we add 3 'virtual fields' to represent the per-row overhead. Thus a 1-field row is (3 + 1) * 4 = 16 bytes; a 5-field row is (3 + 5) * 4 = 32 bytes.

      The cost model does not consider a 5-field sort to be more expensive than, say, a 2-field sort, because both sorts will compare just one field most of the time.

      Specified by:
      computeSelfCost in interface RelNode
      Overrides:
      computeSelfCost in class AbstractRelNode
      Parameters:
      planner - Planner for cost calculation
      mq - Metadata query
      Returns:
      Cost of this plan (not including children)
    • accept

      public RelNode accept(RexShuttle shuttle)
      Description copied from interface: RelNode
      Accepts a visit from a shuttle. If the shuttle updates expression, then a copy of the relation should be created. This new relation might have a different row-type.
      Specified by:
      accept in interface RelNode
      Overrides:
      accept in class AbstractRelNode
      Parameters:
      shuttle - Shuttle
      Returns:
      A copy of this node incorporating changes made by the shuttle to this node's children
    • isEnforcer

      public boolean isEnforcer()
      Description copied from interface: RelNode
      Indicates whether it is an enforcer operator, e.g. PhysicalSort, PhysicalHashDistribute, etc. As an enforcer, the operator must be created only when required traitSet is not satisfied by its input.
      Specified by:
      isEnforcer in interface RelNode
      Overrides:
      isEnforcer in class AbstractRelNode
      Returns:
      Whether it is an enforcer operator
    • getCollation

      public RelCollation getCollation()
      Returns the array of RelFieldCollations asked for by the sort specification, from most significant to least significant.

      See also RelMetadataQuery.collations(RelNode), which lists all known collations. For example, ORDER BY time_id might also be sorted by the_year, the_month because of a known monotonicity constraint among the columns. getCollation would return [time_id] and collations would return [ [time_id], [the_year, the_month] ].

    • getSortExps

      public List<RexNode> getSortExps()
      Returns the sort expressions.
    • explainTerms

      public RelWriter explainTerms(RelWriter pw)
      Description copied from class: AbstractRelNode
      Describes the inputs and attributes of this relational expression. Each node should call super.explainTerms, then call the RelWriter.input(String, RelNode) and RelWriter.item(String, Object) methods for each input and attribute.
      Overrides:
      explainTerms in class SingleRel
      Parameters:
      pw - Plan writer
      Returns:
      Plan writer for fluent-explain pattern
    • getHints

      public com.google.common.collect.ImmutableList<RelHint> getHints()
      Description copied from interface: Hintable
      Returns the hints of this relational expressions as an immutable list.
      Specified by:
      getHints in interface Hintable