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