Skip to main content

sort_governor/
spec.rs

1//! The caller's description of a sort job — what is being sorted and
2//! roughly how big it is. The Sorter plans against this, never against the
3//! rows themselves.
4
5/// A request to sort a relation, sized but not yet executed.
6///
7/// `estimated_rows` / `estimated_bytes` are the planner's only view of the
8/// input magnitude; they need not be exact, but a gross under-estimate
9/// pushes a large sort onto the in-memory path and a gross over-estimate
10/// wastes spill buffers. Callers derive them from cardinality estimates and
11/// per-row width.
12#[derive(Debug, Clone)]
13pub struct SortSpec {
14    estimated_rows: u64,
15    estimated_bytes: u64,
16    dedup: bool,
17    label: &'static str,
18}
19
20impl SortSpec {
21    /// Describe a sort of `estimated_rows` rows totalling roughly
22    /// `estimated_bytes` of key + value bytes.
23    #[must_use]
24    pub fn new(estimated_rows: u64, estimated_bytes: u64) -> Self {
25        Self {
26            estimated_rows,
27            estimated_bytes,
28            dedup: false,
29            label: "sort",
30        }
31    }
32
33    /// Request that equal-key rows be collapsed to one during the merge.
34    #[must_use]
35    pub fn with_dedup(mut self, dedup: bool) -> Self {
36        self.dedup = dedup;
37        self
38    }
39
40    /// Attach a short static label used in tracing and statistics.
41    #[must_use]
42    pub fn labelled(mut self, label: &'static str) -> Self {
43        self.label = label;
44        self
45    }
46
47    /// Estimated number of rows to be sorted.
48    #[must_use]
49    pub fn estimated_rows(&self) -> u64 {
50        self.estimated_rows
51    }
52
53    /// Estimated total key + value bytes to be sorted.
54    #[must_use]
55    pub fn estimated_bytes(&self) -> u64 {
56        self.estimated_bytes
57    }
58
59    /// Whether equal-key rows are collapsed during the merge.
60    #[must_use]
61    pub fn dedup(&self) -> bool {
62        self.dedup
63    }
64
65    /// The short static label for tracing and stats.
66    #[must_use]
67    pub fn label(&self) -> &'static str {
68        self.label
69    }
70}