1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! Module: query::fluent::load::terminals::projection
//! Responsibility: fluent projected-value and ranked-row terminal public methods.
//! Does not own: aggregate semantics, materialization terminals, or executor routing.
//! Boundary: delegates slot/admission handoffs to `support` and value shaping to `output`.
use crate::{
db::{
PersistedRow,
query::{
builder::{
DistinctValuesBySlotTerminal, FirstValueBySlotTerminal, LastValueBySlotTerminal,
ValueProjectionExpr, ValuesBySlotTerminal, ValuesBySlotWithIdsTerminal,
},
explain::ExplainExecutionNodeDescriptor,
fluent::load::FluentLoadQuery,
intent::QueryError,
plan::AggregateKind,
},
response::EntityResponse,
},
traits::EntityValue,
types::Id,
value::OutputValue,
};
use super::output::{output, output_values, output_values_with_ids};
impl<E> FluentLoadQuery<'_, E>
where
E: PersistedRow,
{
/// Execute and return projected field values for the effective result window.
pub fn values_by(&self, field: impl AsRef<str>) -> Result<Vec<OutputValue>, QueryError>
where
E: EntityValue,
{
self.execute_slot_terminal(field, ValuesBySlotTerminal::new)
.map(output_values)
}
/// Execute and return projected values for one shared bounded projection
/// over the effective response window.
pub fn project_values<P>(&self, projection: &P) -> Result<Vec<OutputValue>, QueryError>
where
E: EntityValue,
P: ValueProjectionExpr,
{
self.with_admitted_non_paged_slot(projection.field(), |session, query, target_slot| {
session.execute_fluent_project_values_by_slot(
query,
target_slot,
projection.projection_plan().into_expr(),
)
})
.map(output_values)
}
/// Explain `project_values(projection)` routing without executing it.
pub fn explain_project_values<P>(
&self,
projection: &P,
) -> Result<ExplainExecutionNodeDescriptor, QueryError>
where
E: EntityValue,
P: ValueProjectionExpr,
{
self.explain_slot_terminal(projection.field(), ValuesBySlotTerminal::new)
}
/// Explain `values_by(field)` routing without executing the terminal.
pub fn explain_values_by(
&self,
field: impl AsRef<str>,
) -> Result<ExplainExecutionNodeDescriptor, QueryError>
where
E: EntityValue,
{
self.explain_slot_terminal(field, ValuesBySlotTerminal::new)
}
/// Execute and return the first `k` rows from the effective response window.
pub fn take(&self, take_count: u32) -> Result<EntityResponse<E>, QueryError>
where
E: EntityValue,
{
self.with_admitted_non_paged(|session, query| {
session.execute_fluent_take(query, take_count)
})
}
/// Execute and return the top `k` rows by `field` under deterministic
/// ordering `(field desc, primary_key asc)` over the effective response
/// window.
///
/// This terminal applies its own ordering and does not preserve query
/// `order_term(...)` row order in the returned rows. For `k = 1`, this
/// matches `max_by(field)` selection semantics.
pub fn top_k_by(
&self,
field: impl AsRef<str>,
take_count: u32,
) -> Result<EntityResponse<E>, QueryError>
where
E: EntityValue,
{
self.with_admitted_non_paged_slot(field, |session, query, target_slot| {
session.execute_fluent_top_k_rows_by_slot(query, target_slot, take_count)
})
}
/// Execute and return the bottom `k` rows by `field` under deterministic
/// ordering `(field asc, primary_key asc)` over the effective response
/// window.
///
/// This terminal applies its own ordering and does not preserve query
/// `order_term(...)` row order in the returned rows. For `k = 1`, this
/// matches `min_by(field)` selection semantics.
pub fn bottom_k_by(
&self,
field: impl AsRef<str>,
take_count: u32,
) -> Result<EntityResponse<E>, QueryError>
where
E: EntityValue,
{
self.with_admitted_non_paged_slot(field, |session, query, target_slot| {
session.execute_fluent_bottom_k_rows_by_slot(query, target_slot, take_count)
})
}
/// Execute and return projected values for the top `k` rows by `field`
/// under deterministic ordering `(field desc, primary_key asc)` over the
/// effective response window.
///
/// Ranking is applied before projection and does not preserve query
/// `order_term(...)` row order in the returned values. For `k = 1`, this
/// matches `max_by(field)` projected to one value.
pub fn top_k_by_values(
&self,
field: impl AsRef<str>,
take_count: u32,
) -> Result<Vec<OutputValue>, QueryError>
where
E: EntityValue,
{
self.with_admitted_non_paged_slot(field, |session, query, target_slot| {
session.execute_fluent_top_k_values_by_slot(query, target_slot, take_count)
})
.map(output_values)
}
/// Execute and return projected values for the bottom `k` rows by `field`
/// under deterministic ordering `(field asc, primary_key asc)` over the
/// effective response window.
///
/// Ranking is applied before projection and does not preserve query
/// `order_term(...)` row order in the returned values. For `k = 1`, this
/// matches `min_by(field)` projected to one value.
pub fn bottom_k_by_values(
&self,
field: impl AsRef<str>,
take_count: u32,
) -> Result<Vec<OutputValue>, QueryError>
where
E: EntityValue,
{
self.with_admitted_non_paged_slot(field, |session, query, target_slot| {
session.execute_fluent_bottom_k_values_by_slot(query, target_slot, take_count)
})
.map(output_values)
}
/// Execute and return projected id/value pairs for the top `k` rows by
/// `field` under deterministic ordering `(field desc, primary_key asc)`
/// over the effective response window.
///
/// Ranking is applied before projection and does not preserve query
/// `order_term(...)` row order in the returned values. For `k = 1`, this
/// matches `max_by(field)` projected to one `(id, value)` pair.
pub fn top_k_by_with_ids(
&self,
field: impl AsRef<str>,
take_count: u32,
) -> Result<Vec<(Id<E>, OutputValue)>, QueryError>
where
E: EntityValue,
{
self.with_admitted_non_paged_slot(field, |session, query, target_slot| {
session.execute_fluent_top_k_values_with_ids_by_slot(query, target_slot, take_count)
})
.map(output_values_with_ids)
}
/// Execute and return projected id/value pairs for the bottom `k` rows by
/// `field` under deterministic ordering `(field asc, primary_key asc)`
/// over the effective response window.
///
/// Ranking is applied before projection and does not preserve query
/// `order_term(...)` row order in the returned values. For `k = 1`, this
/// matches `min_by(field)` projected to one `(id, value)` pair.
pub fn bottom_k_by_with_ids(
&self,
field: impl AsRef<str>,
take_count: u32,
) -> Result<Vec<(Id<E>, OutputValue)>, QueryError>
where
E: EntityValue,
{
self.with_admitted_non_paged_slot(field, |session, query, target_slot| {
session.execute_fluent_bottom_k_values_with_ids_by_slot(query, target_slot, take_count)
})
.map(output_values_with_ids)
}
/// Execute and return distinct projected field values for the effective
/// result window, preserving first-observed value order.
pub fn distinct_values_by(&self, field: impl AsRef<str>) -> Result<Vec<OutputValue>, QueryError>
where
E: EntityValue,
{
self.execute_slot_terminal(field, DistinctValuesBySlotTerminal::new)
.map(output_values)
}
/// Explain `distinct_values_by(field)` routing without executing the terminal.
pub fn explain_distinct_values_by(
&self,
field: impl AsRef<str>,
) -> Result<ExplainExecutionNodeDescriptor, QueryError>
where
E: EntityValue,
{
self.explain_slot_terminal(field, DistinctValuesBySlotTerminal::new)
}
/// Execute and return projected field values paired with row ids for the
/// effective result window.
pub fn values_by_with_ids(
&self,
field: impl AsRef<str>,
) -> Result<Vec<(Id<E>, OutputValue)>, QueryError>
where
E: EntityValue,
{
self.execute_slot_terminal(field, ValuesBySlotWithIdsTerminal::new)
.map(output_values_with_ids)
}
/// Execute and return projected id/value pairs for one shared bounded
/// projection over the effective response window.
pub fn project_values_with_ids<P>(
&self,
projection: &P,
) -> Result<Vec<(Id<E>, OutputValue)>, QueryError>
where
E: EntityValue,
P: ValueProjectionExpr,
{
self.with_admitted_non_paged_slot(projection.field(), |session, query, target_slot| {
session.execute_fluent_project_values_with_ids_by_slot(
query,
target_slot,
projection.projection_plan().into_expr(),
)
})
.map(output_values_with_ids)
}
/// Explain `values_by_with_ids(field)` routing without executing the terminal.
pub fn explain_values_by_with_ids(
&self,
field: impl AsRef<str>,
) -> Result<ExplainExecutionNodeDescriptor, QueryError>
where
E: EntityValue,
{
self.explain_slot_terminal(field, ValuesBySlotWithIdsTerminal::new)
}
/// Execute and return the first projected field value in effective response
/// order, if any.
pub fn first_value_by(&self, field: impl AsRef<str>) -> Result<Option<OutputValue>, QueryError>
where
E: EntityValue,
{
self.execute_slot_terminal(field, FirstValueBySlotTerminal::new)
.map(|value| value.map(output))
}
/// Execute and return the first projected value for one shared bounded
/// projection in effective response order, if any.
pub fn project_first_value<P>(&self, projection: &P) -> Result<Option<OutputValue>, QueryError>
where
E: EntityValue,
P: ValueProjectionExpr,
{
self.with_admitted_non_paged_slot(projection.field(), |session, query, target_slot| {
session.execute_fluent_project_terminal_value_by_slot(
query,
target_slot,
AggregateKind::First,
projection.projection_plan().into_expr(),
)
})
.map(|value| value.map(output))
}
/// Explain `first_value_by(field)` routing without executing the terminal.
pub fn explain_first_value_by(
&self,
field: impl AsRef<str>,
) -> Result<ExplainExecutionNodeDescriptor, QueryError>
where
E: EntityValue,
{
self.explain_slot_terminal(field, FirstValueBySlotTerminal::new)
}
/// Execute and return the last projected field value in effective response
/// order, if any.
pub fn last_value_by(&self, field: impl AsRef<str>) -> Result<Option<OutputValue>, QueryError>
where
E: EntityValue,
{
self.execute_slot_terminal(field, LastValueBySlotTerminal::new)
.map(|value| value.map(output))
}
/// Execute and return the last projected value for one shared bounded
/// projection in effective response order, if any.
pub fn project_last_value<P>(&self, projection: &P) -> Result<Option<OutputValue>, QueryError>
where
E: EntityValue,
P: ValueProjectionExpr,
{
self.with_admitted_non_paged_slot(projection.field(), |session, query, target_slot| {
session.execute_fluent_project_terminal_value_by_slot(
query,
target_slot,
AggregateKind::Last,
projection.projection_plan().into_expr(),
)
})
.map(|value| value.map(output))
}
/// Explain `last_value_by(field)` routing without executing the terminal.
pub fn explain_last_value_by(
&self,
field: impl AsRef<str>,
) -> Result<ExplainExecutionNodeDescriptor, QueryError>
where
E: EntityValue,
{
self.explain_slot_terminal(field, LastValueBySlotTerminal::new)
}
}