java.lang.Object
com.darkcollective.relix.ast.Ordering

public final class Ordering extends Object
A sort ordering — the sequence of (column, direction) keys by which a relation's rows are ordered. Used to reason about delivered orderings (what a sub-tree already produces) versus required orderings (what an operator needs), so a redundant sort can be eliminated.

none() is the empty ordering — no guaranteed row order.

Ordering is a physical-flavoured property (it describes how rows are produced, not just the set of rows); this Phase-C1 form reasons about it at the logical level — τ establishes an order and order-preserving operators carry it — which is enough to remove a redundant τ. Null placement is deliberately omitted until the merge operators (Phase C2) need it.

Instances are immutable value objects.

  • Method Details

    • none

      public static Ordering none()
      The empty ordering — no guaranteed order.
      Returns:
      the empty ordering; never null
    • of

      public static Ordering of(List<SortSpecification> keys)
      An ordering by the given sort keys, in priority order.
      Parameters:
      keys - the ordered sort keys; must not be null (empty yields none())
      Returns:
      the ordering; never null
    • keys

      public List<SortSpecification> keys()
      The ordered sort keys of this ordering.
      Returns:
      an immutable list of sort keys; never null, possibly empty
    • satisfies

      public boolean satisfies(Ordering required)
      Whether this (delivered) ordering satisfies the required ordering — i.e. required is a prefix of this ordering. A relation sorted by (x, y) is also sorted by (x), so it satisfies a requirement for (x); but a relation sorted only by (x) does not satisfy a requirement for (x, y). Direction is compared too: ascending does not satisfy a descending requirement.
      Parameters:
      required - the ordering that must be satisfied; must not be null
      Returns:
      true if this ordering satisfies required
    • clusters

      public boolean clusters(Collection<String> columns)
      Whether this ordering clusters every duplicate of a row over columns adjacently — i.e. its keys cover all of columns (direction is irrelevant for clustering). When the whole row is covered, fully-equal rows are guaranteed contiguous, so a δ (DISTINCT) can deduplicate in a single linear pass instead of buffering a hash set.
      Parameters:
      columns - the columns that must all appear among this ordering's keys; must not be null
      Returns:
      true if every column in columns is an ordering key
    • groupsBy

      public boolean groupsBy(Collection<String> groupingKeys)
      Whether this ordering groups rows by groupingKeys contiguously — its leading keys (the first groupingKeys.size()) are exactly that set of columns (order and direction among them are irrelevant for grouping). When this holds, every group of equal groupingKeys is contiguous, so a γ (GROUP) can aggregate in a single linear pass holding only one group at a time.

      Empty groupingKeys (a scalar, whole-relation aggregate) never groups contiguously in this sense — it still has to read the whole input — so this returns false.

      Parameters:
      groupingKeys - the grouping-key columns; must not be null
      Returns:
      true if this ordering's leading keys are exactly groupingKeys
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object