- Record Components:
groupingAttributes- the partition keys; empty means one global groupsortSpecs- within-group ordering; at least one requiredoffset- rows to skip within each group before takingcountcount- number of rows to keep per group; non-negativewithTies- whether rows tied with the last one kept are kept too; needs a sort keyleadingDirectionImplied- whether the first sort key was written with neitherASCnorDESC, so that it is ascending by default rather than by request; validation warns about it, since "top" reads as the highestinput- the source relation; never nulllocation- the source location of this node; never null
- All Implemented Interfaces:
RelNode
input by groupingAttributes
and, within each group, keeps the first count rows in sortSpecs
order (after skipping offset rows). A key with no direction is ascending,
so TOP 1 x keeps the row with the lowest x.
This is the "N highest rows per group" operation — top 3 orders by
amount per customer — that SQL forces into a
ROW_NUMBER() OVER (PARTITION BY … ORDER BY …) ≤ k plus an outer filter,
or a lateral join. Unlike a global LimitNode it cannot push down and
buffers each group; unlike an AggregationNode it returns whole rows.
The output schema is the input schema (it is a windowed filter — a row subset). At least one sort specification is required.
The surface syntax always names a partition key (TOP … PER …), but the
grouping list may be empty: that is the global top-N —
one group containing every row — which the optimizer produces by fusing a
λ over a τ (LIM-003). It has no spelling of its own
because λ/τ already is one.
withTies keeps, past the count-th row of a group, every further row
that ties with it on all the sort keys, as SQL's WITH TIES does: rows the
order cannot tell apart are kept or dropped together. A tie is equality under the
sort's own comparison, so two NULLs tie and 5 ties with 5.0.
TOP 2 WITH TIES over scores 9, 8, 8, 7 keeps three rows.
Example: TOP 3 amount DESC PER customer_id (Orders).
-
Constructor Summary
ConstructorsConstructorDescriptionTopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, long count, RelNode input) Convenience constructor for tests; no offset,SourceLocation.UNKNOWN.TopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, Optional<Long> offset, long count, boolean withTies, boolean leadingDirectionImplied, RelNode input, SourceLocation location) Creates an instance of aTopKNoderecord class.TopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, Optional<Long> offset, long count, boolean withTies, RelNode input, SourceLocation location) A top-k whose sort directions are all taken as requested, as one the optimizer or a builder makes rather than one a person wrote.TopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, Optional<Long> offset, long count, RelNode input, SourceLocation location) A top-k that cuts ties wherever the count falls. -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(RelNodeVisitor<R> visitor) longcount()Returns the value of thecountrecord component.final booleanIndicates whether some other object is "equal to" this one.Returns the value of thegroupingAttributesrecord component.final inthashCode()Returns a hash code value for this object.input()Returns the value of theinputrecord component.booleanReturns the value of theleadingDirectionImpliedrecord component.location()Returns the value of thelocationrecord component.offset()Returns the value of theoffsetrecord component.Returns the value of thesortSpecsrecord component.final StringtoString()Returns a string representation of this record class.This node overinput, every other component kept.booleanwithTies()Returns the value of thewithTiesrecord component.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.darkcollective.relix.ast.RelNode
children, mapChildren, materializationMode, prettyPrint, prettyPrint
-
Constructor Details
-
TopKNode
public TopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, Optional<Long> offset, long count, boolean withTies, boolean leadingDirectionImplied, RelNode input, SourceLocation location) Creates an instance of aTopKNoderecord class.- Parameters:
groupingAttributes- the value for thegroupingAttributesrecord componentsortSpecs- the value for thesortSpecsrecord componentoffset- the value for theoffsetrecord componentcount- the value for thecountrecord componentwithTies- the value for thewithTiesrecord componentleadingDirectionImplied- the value for theleadingDirectionImpliedrecord componentinput- the value for theinputrecord componentlocation- the value for thelocationrecord component
-
TopKNode
public TopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, Optional<Long> offset, long count, boolean withTies, RelNode input, SourceLocation location) A top-k whose sort directions are all taken as requested, as one the optimizer or a builder makes rather than one a person wrote.- Parameters:
groupingAttributes- the partition keys; empty means one global groupsortSpecs- within-group ordering; at least one requiredoffset- rows to skip within each group before takingcountcount- number of rows to keep per group; non-negativewithTies- whether rows tied with the last one kept are kept too; needs a sort keyinput- the source relation; never nulllocation- the source location of this node; never null
-
TopKNode
public TopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, Optional<Long> offset, long count, RelNode input, SourceLocation location) A top-k that cuts ties wherever the count falls.- Parameters:
groupingAttributes- the partition keys; empty means one global groupsortSpecs- within-group ordering; at least one requiredoffset- rows to skip within each group before takingcountcount- number of rows to keep per group; non-negativeinput- the source relation; never nulllocation- the source location of this node; never null
-
TopKNode
public TopKNode(List<String> groupingAttributes, List<SortSpecification> sortSpecs, long count, RelNode input) Convenience constructor for tests; no offset,SourceLocation.UNKNOWN.
-
-
Method Details
-
withInput
This node overinput, every other component kept.- Parameters:
input- the new input; never null- Returns:
- the node
- Since:
- 1.0
-
accept
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
groupingAttributes
Returns the value of thegroupingAttributesrecord component.- Returns:
- the value of the
groupingAttributesrecord component
-
sortSpecs
Returns the value of thesortSpecsrecord component.- Returns:
- the value of the
sortSpecsrecord component
-
offset
Returns the value of theoffsetrecord component.- Returns:
- the value of the
offsetrecord component
-
count
public long count()Returns the value of thecountrecord component.- Returns:
- the value of the
countrecord component
-
withTies
public boolean withTies()Returns the value of thewithTiesrecord component.- Returns:
- the value of the
withTiesrecord component
-
leadingDirectionImplied
public boolean leadingDirectionImplied()Returns the value of theleadingDirectionImpliedrecord component.- Returns:
- the value of the
leadingDirectionImpliedrecord component
-
input
Returns the value of theinputrecord component.- Returns:
- the value of the
inputrecord component
-
location
Returns the value of thelocationrecord component.
-