pub struct OrderBy { /* private fields */ }Expand description
Order by specification for queries
Implementations§
Source§impl OrderBy
impl OrderBy
Sourcepub fn compare<R>(
&self,
a: &R,
b: &R,
get_value: impl Fn(&R, &str) -> Value,
) -> Ordering
pub fn compare<R>( &self, a: &R, b: &R, get_value: impl Fn(&R, &str) -> Value, ) -> Ordering
Compare two rows according to all sort keys
The get_value closure takes a row and column name, returning the value.
Sourcepub fn sort_rows<R>(
&self,
rows: &mut [R],
get_value: impl Fn(&R, &str) -> Value,
)
pub fn sort_rows<R>( &self, rows: &mut [R], get_value: impl Fn(&R, &str) -> Value, )
Sort a slice of rows in place
Sourcepub fn dispatch_sort<R>(
&self,
rows: Vec<R>,
prefix_keys: usize,
limit: Option<usize>,
get_value: impl Fn(&R, &str) -> Value,
) -> Vec<R>
pub fn dispatch_sort<R>( &self, rows: Vec<R>, prefix_keys: usize, limit: Option<usize>, get_value: impl Fn(&R, &str) -> Value, ) -> Vec<R>
Phase 3.2 dispatch entry point. Combines OrderBy::sort_rows
and incremental_sort_top_k behind a single callable that
the planner / executor invokes after deciding the strategy
via planner::pathkeys::plan_sort.
prefix_keys is the number of leading sort keys the input
already satisfies (0 if unknown). limit is the LIMIT k for
top-k early termination, or None for an unbounded sort.
Behavior matrix:
prefix_keys == 0&&limit == None→ fullsort_rows.prefix_keys == 0&&limit == Some(k)→ full sort then truncate to k.prefix_keys > 0&&limit == Some(k)→ callincremental_sort_top_k(prefix_keys, k).prefix_keys > 0&&limit == None→ walk groups and sort within each, no early termination (still cheaper than full sort because each group is independent).
Sourcepub fn incremental_sort_top_k<R>(
&self,
rows: Vec<R>,
prefix_keys: usize,
k: usize,
get_value: impl Fn(&R, &str) -> Value,
) -> Vec<R>
pub fn incremental_sort_top_k<R>( &self, rows: Vec<R>, prefix_keys: usize, k: usize, get_value: impl Fn(&R, &str) -> Value, ) -> Vec<R>
Incremental top-K sort.
Fase 4 P3 win: when the upstream operator already returns
rows in prefix_keys order (e.g. an index scan whose key
is a prefix of the requested ORDER BY), this method walks
the input in chunks of equal-prefix rows, sorts each chunk
by the remaining keys, emits up to k rows total, and
terminates as soon as the budget is met.
Mirrors PG’s nodeIncrementalSort.c algorithm, simplified:
- Walk
rowsleft-to-right, grouping by equal-prefix. - For each group, full-sort by
self.keys[prefix_keys..](the suffix not already covered by upstream order). - Append at most
k - emittedrows from the sorted group to the output, then advance to the next group. - Stop iteration entirely once
emitted == k.
Caller contract: rows MUST already be sorted by
self.keys[..prefix_keys]. Violating this produces wrong
results — the planner is responsible for verifying input
pathkey order before choosing this operator.
When prefix_keys == 0 this degenerates to a regular
top-k sort using sort_rows + truncate. When
prefix_keys >= self.keys.len() the input is already
fully ordered and the method just truncates.
Sourcepub fn referenced_columns(&self) -> Vec<&str>
pub fn referenced_columns(&self) -> Vec<&str>
Get all referenced columns