ic_db/store/query/aggregate/jobs/extrema.rs
1//! Retained extrema over the synchronous comparator and shared job lifecycle.
2
3use super::{Accumulator, AggregateJobs, JobHandle, JobStep, binding, start};
4use crate::{Entity, Error, GroupPage, GroupValue, Timestamp, Value, store::Query};
5use ic_storage::Memory;
6
7impl<E: Entity, M: Memory> Query<'_, E, M> {
8 /// Retain minima per exact key record, without scanning input. R selects one
9 /// exact ordered field, as in `group_min`; key and operand may overlap.
10 /// Retain all-absent groups with None. Keep at most 32 groups within the
11 /// complete 16 KiB job frame, including keys, winners and progress.
12 /// Group predicates, order and total limit bind the job. No automatic spill.
13 /// # Errors
14 /// Refuses unsupported shapes, page modifiers, expiry or occupied capacity.
15 /// # Panics
16 /// Supplier mutation failures trap the enclosing message.
17 pub fn start_group_min<K: Value, R: Value>(
18 self,
19 jobs: &mut AggregateJobs<M>,
20 now: Timestamp,
21 expires: Timestamp,
22 ) -> Result<JobHandle, Error> {
23 let admitted = self.group_extremum_plan::<K, R>(false)?;
24
25 self.start_group_value(jobs, now, expires, admitted)
26 }
27
28 /// Retain maxima per exact key record with `start_group_min`'s bounds.
29 /// Minimum and maximum have separate job identities.
30 /// # Errors
31 /// The same query, expiry and capacity errors as `start_group_min`.
32 /// # Panics
33 /// Supplier mutation failures trap the enclosing message.
34 pub fn start_group_max<K: Value, R: Value>(
35 self,
36 jobs: &mut AggregateJobs<M>,
37 now: Timestamp,
38 expires: Timestamp,
39 ) -> Result<JobHandle, Error> {
40 let admitted = self.group_extremum_plan::<K, R>(true)?;
41
42 self.start_group_value(jobs, now, expires, admitted)
43 }
44
45 /// Advance grouped minima with the retained revision, retry and expiry contract.
46 /// Finish all input before group filtering, ordering and limiting. Absent keys
47 /// form a group; all-absent operands yield None. Use `group_page` for completed
48 /// result pagination, retaining its continuation when mapping rows.
49 /// # Errors
50 /// Refuses mismatched queries/operations, corrupt winners, group/frame bounds
51 /// and oversized output without changing saved progress. Selection cannot
52 /// hide state exhaustion. No prefix winner is returned as an exact result.
53 /// # Panics
54 /// Supplier mutation failures trap the enclosing message.
55 pub fn resume_group_min<K: Value, R: Value>(
56 self,
57 jobs: &mut AggregateJobs<M>,
58 handle: &JobHandle,
59 sequence: u64,
60 now: Timestamp,
61 ) -> Result<JobStep<GroupPage<GroupValue<K, R>>>, Error> {
62 let admitted = self.group_extremum_plan::<K, R>(false)?;
63
64 self.resume_group_value(jobs, handle, sequence, now, admitted)
65 }
66
67 /// Advance grouped maxima with `resume_group_min`'s exactness and bounds.
68 /// Equal operands retain their first encountered value.
69 /// # Errors
70 /// The same query, checkpoint, resource and output errors as `resume_group_min`.
71 /// # Panics
72 /// Supplier mutation failures trap the enclosing message.
73 pub fn resume_group_max<K: Value, R: Value>(
74 self,
75 jobs: &mut AggregateJobs<M>,
76 handle: &JobHandle,
77 sequence: u64,
78 now: Timestamp,
79 ) -> Result<JobStep<GroupPage<GroupValue<K, R>>>, Error> {
80 let admitted = self.group_extremum_plan::<K, R>(true)?;
81
82 self.resume_group_value(jobs, handle, sequence, now, admitted)
83 }
84
85 /// Retain a minimum over one exact single-field result record, without scanning.
86 /// Uses `min`'s ordered types and absence semantics and `start_count`'s expiry,
87 /// revision and capacity bounds. Persist only the winning record, within the
88 /// complete 16 KiB job frame; no partial result or automatic spill.
89 /// # Errors
90 /// Refuses unsupported projections/query shapes, expiry or occupied capacity.
91 /// # Panics
92 /// Supplier mutation failures trap the enclosing message.
93 pub fn start_min<R: Value>(
94 self,
95 jobs: &mut AggregateJobs<M>,
96 now: Timestamp,
97 expires: Timestamp,
98 ) -> Result<JobHandle, Error> {
99 self.start_extremum::<R>(jobs, now, expires, false)
100 }
101
102 /// Retain a maximum with `start_min`'s types and bounds. Minimum and maximum
103 /// have distinct job identities, even when they select the same field.
104 /// # Errors
105 /// The same admission, expiry and capacity errors as `start_min`.
106 /// # Panics
107 /// Supplier mutation failures trap the enclosing message.
108 pub fn start_max<R: Value>(
109 self,
110 jobs: &mut AggregateJobs<M>,
111 now: Timestamp,
112 expires: Timestamp,
113 ) -> Result<JobHandle, Error> {
114 self.start_extremum::<R>(jobs, now, expires, true)
115 }
116
117 /// Advance a retained minimum with `resume_count`'s bounded work, retry,
118 /// revision and expiry contract. Ignore absent operands; empty/all-absent
119 /// input completes with None. Return the winning record only after exhaustion.
120 /// Equal values retain the first encountered value. A completed result stays
121 /// fixed after source writes, until expiry or cancellation.
122 /// # Errors
123 /// Refuses changed queries/operations, malformed winner bytes, work/state
124 /// bounds or oversized completed output. Refusal preserves the checkpoint.
125 /// A comparison key exceeding 4096 bytes refuses even for a losing candidate.
126 /// # Panics
127 /// Supplier mutation failures trap the enclosing message.
128 pub fn resume_min<R: Value>(
129 self,
130 jobs: &mut AggregateJobs<M>,
131 handle: &JobHandle,
132 sequence: u64,
133 now: Timestamp,
134 ) -> Result<JobStep<Option<R>>, Error> {
135 let (plan, reducer) = self.extremum_plan::<R>(false)?;
136
137 self.resume_value(jobs, handle, sequence, now, (plan, reducer))
138 }
139
140 /// Advance a retained maximum with `resume_min`'s exactness and bounds.
141 /// # Errors
142 /// The same query, resource and checkpoint errors as `resume_min`.
143 /// # Panics
144 /// Supplier mutation failures trap the enclosing message.
145 pub fn resume_max<R: Value>(
146 self,
147 jobs: &mut AggregateJobs<M>,
148 handle: &JobHandle,
149 sequence: u64,
150 now: Timestamp,
151 ) -> Result<JobStep<Option<R>>, Error> {
152 let (plan, reducer) = self.extremum_plan::<R>(true)?;
153
154 self.resume_value(jobs, handle, sequence, now, (plan, reducer))
155 }
156
157 fn start_extremum<R: Value>(
158 self,
159 jobs: &mut AggregateJobs<M>,
160 now: Timestamp,
161 expires: Timestamp,
162 maximum: bool,
163 ) -> Result<JobHandle, Error> {
164 let (plan, mut reducer) = self.extremum_plan::<R>(maximum)?;
165
166 start(
167 jobs,
168 binding(&plan, &reducer)?,
169 self.db.read()?.revision_by_id(E::SCHEMA.id)?,
170 now,
171 expires,
172 &Accumulator::Scalar(&mut reducer),
173 )
174 }
175}