MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / dict_agg

Function dict_agg

src/expr/src/relation/func.rs:285–320  ·  view source on GitHub ↗
(datums: I, temp_storage: &'a RowArena, order_by: &[ColumnOrder])

Source from the content-addressed store, hash-verified

283}
284
285fn dict_agg<'a, I>(datums: I, temp_storage: &'a RowArena, order_by: &[ColumnOrder]) -> Datum<'a>
286where
287 I: IntoIterator<Item = Datum<'a>>,
288{
289 let datums = order_aggregate_datums(datums, order_by);
290 temp_storage.make_datum(|packer| {
291 let mut datums: Vec<_> = datums
292 .into_iter()
293 .filter_map(|d| {
294 if d.is_null() {
295 return None;
296 }
297 let mut list = d.unwrap_list().iter();
298 let key = list.next().unwrap();
299 let val = list.next().unwrap();
300 if key.is_null() {
301 // TODO(benesch): this should produce an error, but
302 // aggregate functions cannot presently produce errors.
303 None
304 } else {
305 Some((key.unwrap_str(), val))
306 }
307 })
308 .collect();
309 // datums are ordered by any ORDER BY clause now, and we want to preserve
310 // the last entry for each key, but we also need to present unique and sorted
311 // keys to push_dict. Use sort_by here, which is stable, and so will preserve
312 // the ORDER BY order. Then reverse and dedup to retain the last of each
313 // key. Reverse again so we're back in push_dict order.
314 datums.sort_by_key(|(k, _v)| *k);
315 datums.reverse();
316 datums.dedup_by_key(|(k, _v)| *k);
317 datums.reverse();
318 packer.push_dict(datums);
319 })
320}
321
322/// Assuming datums is a List, sort them by the 2nd through Nth elements
323/// corresponding to order_by, then return the 1st element.

Callers 1

eval_datumsMethod · 0.85

Calls 12

order_aggregate_datumsFunction · 0.85
make_datumMethod · 0.80
unwrap_listMethod · 0.80
unwrapMethod · 0.80
unwrap_strMethod · 0.80
sort_by_keyMethod · 0.80
push_dictMethod · 0.80
collectMethod · 0.45
into_iterMethod · 0.45
is_nullMethod · 0.45
iterMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected