Skip to content

Commit 0c2a55a

Browse files
committed
Add doc
1 parent e594f4b commit 0c2a55a

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

datafusion/functions-aggregate/src/map_agg.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,10 @@ impl Accumulator for OrderSensitiveMapAggAccumulator {
445445
vec![take(&mut self.keys).into()];
446446
let mut partition_orderings: Vec<VecDeque<Vec<ScalarValue>>> =
447447
vec![take(&mut self.ordering_values).into()];
448-
// Values are carried inside the ordering merge by pairing them with keys
449-
// through a side table; simpler to merge keys+values together below.
450448
let mut partition_values: Vec<VecDeque<ScalarValue>> =
451449
vec![take(&mut self.values).into()];
452450

451+
// Push keys and values from each partition's state into the merge buffers.
453452
for row in 0..map_array.len() {
454453
if map_array.is_null(row) {
455454
continue;
@@ -485,7 +484,6 @@ impl Accumulator for OrderSensitiveMapAggAccumulator {
485484
}
486485

487486
fn state(&mut self) -> Result<Vec<ScalarValue>> {
488-
// Ship the de-duplicated map plus ordering values for re-sorting.
489487
let (keys, values) = self.sorted_deduped()?;
490488
let map_array = build_single_map(keys, values, &self.key_type, &self.value_type)?;
491489
Ok(vec![

datafusion/sqllogictest/test_files/map_agg.slt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ SELECT map_agg(k, v) FROM nullkey;
6060
----
6161
{a: 1, c: 3}
6262

63+
# Window usage: a cumulative frame calls evaluate() once per row, so the map
64+
# grows as the frame expands. Guards against optimizations that consume the
65+
# accumulator state between evaluate() calls.
66+
statement ok
67+
CREATE TABLE win (k VARCHAR, v INT, ts INT) AS VALUES
68+
('a', 1, 10),
69+
('b', 2, 20),
70+
('c', 3, 30);
71+
72+
query ?
73+
SELECT map_agg(k, v) OVER (ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
74+
FROM win;
75+
----
76+
{a: 1}
77+
{a: 1, b: 2}
78+
{a: 1, b: 2, c: 3}
79+
80+
statement ok
81+
DROP TABLE win;
82+
6383
statement ok
6484
DROP TABLE kv;
6585

docs/source/user-guide/sql/aggregate_functions.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ SELECT SUM(x) WITHIN GROUP (ORDER BY x) FROM t;
9191
- [first_value](#first_value)
9292
- [grouping](#grouping)
9393
- [last_value](#last_value)
94+
- [map_agg](#map_agg)
9495
- [max](#max)
9596
- [mean](#mean)
9697
- [median](#median)
@@ -347,6 +348,30 @@ last_value(expression [ORDER BY expression])
347348
+-----------------------------------------------+
348349
```
349350

351+
### `map_agg`
352+
353+
Aggregate key-value pairs from two columns into a single map per group. Pairs with a NULL key are skipped; NULL values are retained. On a duplicate key the first value wins; use ORDER BY to make which value wins deterministic.
354+
355+
```sql
356+
map_agg(key, value [ORDER BY expression])
357+
```
358+
359+
#### Arguments
360+
361+
- **key**: The expression to operate on. Can be a constant, column, or function, and any combination of operators.
362+
- **value**: The expression to operate on. Can be a constant, column, or function, and any combination of operators.
363+
364+
#### Example
365+
366+
```sql
367+
> SELECT map_agg(name, score) FROM scores GROUP BY department;
368+
+-------------------------------+
369+
| map_agg(name, score) |
370+
+-------------------------------+
371+
| {Alice: 95, Bob: 87} |
372+
+-------------------------------+
373+
```
374+
350375
### `max`
351376

352377
Returns the maximum value in the specified column.

0 commit comments

Comments
 (0)