(datums: I, temp_storage: &'a RowArena, order_by: &[ColumnOrder])
| 283 | } |
| 284 | |
| 285 | fn dict_agg<'a, I>(datums: I, temp_storage: &'a RowArena, order_by: &[ColumnOrder]) -> Datum<'a> |
| 286 | where |
| 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. |
no test coverage detected