From 6af3a3e1e8eb98e0021ca0dea737ec7f7b9958ae Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Sun, 15 Nov 2026 19:26:40 +0530 Subject: [PATCH 1/9] feat: implement map_agg aggregate function --- datafusion/functions-aggregate/src/lib.rs | 3 + datafusion/functions-aggregate/src/map_agg.rs | 1070 +++++++++++++++++ .../functions-aggregate/tests/state_compat.rs | 1 + .../sqllogictest/test_files/map_agg.slt | 313 +++++ .../user-guide/sql/aggregate_functions.md | 25 + 5 files changed, 1412 insertions(+) create mode 100644 datafusion/functions-aggregate/src/map_agg.rs create mode 100644 datafusion/sqllogictest/test_files/map_agg.slt diff --git a/datafusion/functions-aggregate/src/lib.rs b/datafusion/functions-aggregate/src/lib.rs index 376342c3455a6..dd69648c0569c 100644 --- a/datafusion/functions-aggregate/src/lib.rs +++ b/datafusion/functions-aggregate/src/lib.rs @@ -67,6 +67,7 @@ pub mod covariance; pub mod first_last; pub mod grouping; pub mod hyperloglog; +pub mod map_agg; pub mod median; pub mod min_max; pub mod nth_value; @@ -111,6 +112,7 @@ pub mod expr_fn { pub use super::first_last::first_value; pub use super::first_last::last_value; pub use super::grouping::grouping; + pub use super::map_agg::map_agg; pub use super::median::median; pub use super::min_max::max; pub use super::min_max::min; @@ -138,6 +140,7 @@ pub fn all_default_aggregate_functions() -> Vec> { vec![ any_value::any_value_udaf(), array_agg::array_agg_udaf(), + map_agg::map_agg_udaf(), first_last::first_value_udaf(), first_last::last_value_udaf(), covariance::covar_samp_udaf(), diff --git a/datafusion/functions-aggregate/src/map_agg.rs b/datafusion/functions-aggregate/src/map_agg.rs new file mode 100644 index 0000000000000..72356cca1fcce --- /dev/null +++ b/datafusion/functions-aggregate/src/map_agg.rs @@ -0,0 +1,1070 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +//! `MAP_AGG` aggregate implementation: [`MapAgg`] +//! +//! Aggregates key/value pairs into a `Map`, analogous to how `array_agg` +//! aggregates values into a `List`. +//! +//! # Accumulators +//! +//! Two accumulators implement the function, selected in +//! [`MapAgg::accumulator`] by whether the aggregate carries an `ORDER BY`: +//! +//! * [`MapAggAccumulator`] keeps the pairs in input order and serializes its +//! state as a single `Map` column. +//! * [`OrderSensitiveMapAggAccumulator`] additionally records the values of the +//! ordering expressions for every pair. Its state therefore has two columns, +//! the `Map` plus a `List>` that lets partial states from +//! every partition be sorted together at output, so the final +//! entry order stays deterministic under parallelism. +//! +//! [`MapAgg::order_sensitivity`] is [`AggregateOrderSensitivity::SoftRequirement`], +//! so when a plan's input already satisfies the ordering the accumulator is +//! marked through [`AggregateUDFImpl::with_beneficial_ordering`] and the pairs +//! are not sorted again unless partial states are merged. +//! +//! # De-duplication +//! +//! Duplicate keys are removed during unordered updates or after sorting ordered +//! input. Without `ORDER BY`, neither entry order nor the duplicate winner is +//! guaranteed. With `ORDER BY`, the first pair in the requested ordering wins; +//! ties require additional ordering expressions for deterministic selection. +//! +//! # Nulls +//! +//! Both accumulators reject `NULL` keys in `update_batch` with +//! `map key cannot be null`. +//! `NULL` values are ordinary map values and are kept. An empty group +//! evaluates to a `NULL` map, matching `array_agg`. + +use std::collections::HashSet; +use std::mem::{size_of, size_of_val}; +use std::sync::Arc; + +use arrow::array::{ + Array, ArrayRef, AsArray, MapArray, StructArray, UInt64Array, new_empty_array, +}; +use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; +use arrow::compute::{SortOptions, concat, take}; +use arrow::datatypes::{DataType, Field, FieldRef, Fields}; +use arrow::row::{RowConverter, SortField}; + +use datafusion_common::cast::as_map_array; +use datafusion_common::utils::{SingleRowListArrayBuilder, take_function_args}; +use datafusion_common::{ + Result, ScalarValue, assert_eq_or_internal_err, exec_err, internal_err, not_impl_err, +}; +use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; +use datafusion_expr::utils::format_state_name; +use datafusion_expr::{ + Accumulator, AggregateUDFImpl, Documentation, Signature, Volatility, +}; +use datafusion_functions_aggregate_common::order::AggregateOrderSensitivity; +use datafusion_functions_aggregate_common::utils::ordering_fields; +use datafusion_macros::user_doc; +use datafusion_physical_expr_common::sort_expr::LexOrdering; + +make_udaf_expr_and_func!( + MapAgg, + map_agg, + key value, + "Aggregates keys and values into a map", + map_agg_udaf +); + +#[user_doc( + doc_section(label = "General Functions"), + description = "Returns a map created from the key and value expression elements. \ +For each row, the key expression becomes a map key and the value expression becomes the corresponding map value. \ +Without `ORDER BY`, neither entry order nor the value retained for a duplicate key is guaranteed. \ +With `ORDER BY`, the first entry for each key in the requested ordering is kept; ties are not deterministic. \ +NULL keys cause an error, NULL values are retained, and empty input returns NULL. \ +DISTINCT is not supported.", + syntax_example = "map_agg(key, value [ORDER BY expression])", + sql_example = r#"```sql +> SELECT map_agg(k, v ORDER BY k) AS m FROM (VALUES ('a', 1)) t(k, v); ++--------+ +| m | ++--------+ +| {a: 1} | ++--------+ +```"#, + argument( + name = "key", + description = "Expression used as the map key. Can be a column or any valid expression." + ), + argument( + name = "value", + description = "Expression used as the map value. Can be a column or any valid expression." + ) +)] +#[derive(Debug, PartialEq, Eq, Hash)] +/// MAP_AGG aggregate expression +pub struct MapAgg { + /// Accepts two arguments of any type: the key and the value. + signature: Signature, + /// Whether the input is known to arrive already ordered by the `ORDER BY` + /// inside the aggregate. + is_input_pre_ordered: bool, +} + +impl Default for MapAgg { + fn default() -> Self { + Self { + signature: Signature::any(2, Volatility::Immutable), + is_input_pre_ordered: false, + } + } +} + +impl MapAgg { + /// Create a new MAP_AGG aggregate function + pub fn new() -> Self { + Self::default() + } + + /// Build the Arrow `Map` data type for `map_agg(key, value)` + fn map_data_type(key_type: &DataType, value_type: &DataType) -> DataType { + DataType::Map( + Arc::new(Field::new( + "entries", + DataType::Struct(Fields::from(vec![ + Field::new("key", key_type.clone(), false), + Field::new("value", value_type.clone(), true), + ])), + false, + )), + false, + ) + } +} + +impl AggregateUDFImpl for MapAgg { + fn name(&self) -> &str { + "map_agg" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result { + let [key_type, value_type] = take_function_args(self.name(), arg_types)?; + Ok(Self::map_data_type(key_type, value_type)) + } + + fn state_fields(&self, args: StateFieldsArgs) -> Result> { + let map_type = Self::map_data_type( + args.input_fields[0].data_type(), + args.input_fields[1].data_type(), + ); + + let mut fields = vec![ + Field::new( + format_state_name(args.name, "map_agg"), + // Nullable so empty groups can produce a NULL map + map_type, + true, + ) + .into(), + ]; + + if args.ordering_fields.is_empty() { + return Ok(fields); + } + + let orderings = args.ordering_fields.to_vec(); + fields.push( + Field::new_list( + format_state_name(args.name, "map_agg_orderings"), + Field::new_list_field(DataType::Struct(Fields::from(orderings)), true), + false, + ) + .into(), + ); + + Ok(fields) + } + + fn accumulator(&self, acc_args: AccumulatorArgs) -> Result> { + if acc_args.is_distinct { + return not_impl_err!("map_agg does not support DISTINCT"); + } + let [key_field, value_field] = + take_function_args(self.name(), acc_args.expr_fields)?; + let key_type = key_field.data_type().clone(); + let value_type = value_field.data_type().clone(); + + let Some(ordering) = LexOrdering::new(acc_args.order_bys.to_vec()) else { + return MapAggAccumulator::try_new(key_type, value_type) + .map(|acc| Box::new(acc) as _); + }; + + let ordering_dtypes = ordering + .iter() + .map(|e| e.expr.data_type(acc_args.schema)) + .collect::>>()?; + + Ok(Box::new(OrderSensitiveMapAggAccumulator::new( + key_type, + value_type, + ordering_dtypes, + ordering, + self.is_input_pre_ordered, + acc_args.is_reversed, + ))) + } + + fn order_sensitivity(&self) -> AggregateOrderSensitivity { + AggregateOrderSensitivity::SoftRequirement + } + + fn reverse_expr(&self) -> datafusion_expr::ReversedUDAF { + datafusion_expr::ReversedUDAF::NotSupported + } + + fn with_beneficial_ordering( + self: Arc, + beneficial_ordering: bool, + ) -> Result>> { + Ok(Some(Arc::new(Self { + signature: self.signature.clone(), + is_input_pre_ordered: beneficial_ordering, + }))) + } + + fn documentation(&self) -> Option<&Documentation> { + self.doc() + } +} + +/// Accumulates key/value pairs for [`MapAgg`]. +/// +/// Only the first pair for each key is retained, in compact array batches. +#[derive(Debug)] +pub struct MapAggAccumulator { + /// Data type of the map keys. + key_type: DataType, + /// Data type of the map values. + value_type: DataType, + batches: Vec>, + seen: HashSet, +} + +impl MapAggAccumulator { + /// Create a new map_agg accumulator for the given key and value types + pub fn try_new(key_type: DataType, value_type: DataType) -> Result { + Ok(Self { + key_type, + value_type, + batches: vec![], + seen: HashSet::new(), + }) + } +} + +impl Accumulator for MapAggAccumulator { + fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> { + assert_eq_or_internal_err!(values.len(), 2, "map_agg expects (key, value)"); + + let keys = &values[0]; + let vals = &values[1]; + assert_eq_or_internal_err!(keys.len(), vals.len(), "key/value length mismatch"); + if keys.logical_null_count() != 0 { + return exec_err!("map key cannot be null"); + } + let mut indices = Vec::new(); + for row in 0..keys.len() { + if self + .seen + .insert(ScalarValue::try_from_array(keys, row)?.compacted()) + { + indices.push(row as u64); + } + } + if !indices.is_empty() { + let indices = UInt64Array::from(indices); + self.batches.push( + values + .iter() + .map(|array| compact_array(take(array.as_ref(), &indices, None)?)) + .collect::>()?, + ); + } + + Ok(()) + } + fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> { + if states.is_empty() { + return Ok(()); + } + + assert_eq_or_internal_err!(states.len(), 1, "expects single state"); + + let map_arr = as_map_array(states[0].as_ref())?; + for row in 0..map_arr.len() { + // Partial states are nullable so empty groups can produce a NULL map + if map_arr.is_null(row) { + continue; + } + self.update_batch(&map_row_to_arrays(map_arr, row))?; + } + + Ok(()) + } + + fn state(&mut self) -> Result> { + Ok(vec![self.evaluate()?]) + } + + fn evaluate(&mut self) -> Result { + let keys = concat_column(&self.batches, 0, &self.key_type)?; + let values = concat_column(&self.batches, 1, &self.value_type)?; + build_single_map(keys, values) + } + + fn size(&self) -> usize { + // HashSet capacity excludes the empty buckets reserved for probing. + let buckets = if self.seen.capacity() == 0 { + 0 + } else { + self.seen.capacity().next_power_of_two() + }; + size_of_val(self) + batches_size(&self.batches) + self.key_type.size() + - size_of_val(&self.key_type) + + self.value_type.size() + - size_of_val(&self.value_type) + + buckets * (size_of::() + 1) + + self + .seen + .iter() + .map(|s| s.size() - size_of::()) + .sum::() + } +} + +/// Accumulator used when `map_agg` has an `ORDER BY`. Stores the ordering column +/// values alongside each pair so the input can be globally sorted (across +/// partitions). +#[derive(Debug)] +pub struct OrderSensitiveMapAggAccumulator { + /// Data type of the map keys. + key_type: DataType, + /// Data type of the map values. + value_type: DataType, + /// Key, value, and ordering columns, retained as compact batches. + batches: Vec>, + /// Data types of the ordering expressions, used to build the state field. + ordering_dtypes: Vec, + /// The `ORDER BY` requirement inside the aggregate. + ordering_req: LexOrdering, + /// When true the input already satisfies `ordering_req`, so the pairs do + /// not have to be sorted again before de-duplication. + is_input_pre_ordered: bool, + /// Whether to restore the output order after sorting by a reversed requirement. + reverse: bool, +} + +impl OrderSensitiveMapAggAccumulator { + /// Create a new accumulator for `map_agg(key, value ORDER BY ...)`. + /// + /// `ordering_dtypes` must hold one data type per entry of `ordering_req`. + pub fn new( + key_type: DataType, + value_type: DataType, + ordering_dtypes: Vec, + ordering_req: LexOrdering, + is_input_pre_ordered: bool, + reverse: bool, + ) -> Self { + Self { + key_type, + value_type, + batches: Vec::new(), + ordering_dtypes, + ordering_req, + is_input_pre_ordered, + reverse, + } + } + + /// Sort options of each ordering expression, used when sorting the pairs + /// and when merging partial states. + /// + /// For a reversed accumulator these are the already-reversed options; + /// the requested output order is restored separately by + /// reversing the rows in [`Self::sorted_deduped`]. + fn sort_options(&self) -> Vec { + self.ordering_req.iter().map(|s| s.options).collect() + } + + /// Sorts and de-duplicates all columns using the same row indices. + #[allow(clippy::allow_attributes, clippy::mutable_key_type)] // ScalarValue has interior mutability but is intentionally used as hash key + fn sorted_deduped(&self) -> Result> { + let columns = std::iter::once(&self.key_type) + .chain(std::iter::once(&self.value_type)) + .chain(self.ordering_dtypes.iter()) + .enumerate() + .map(|(i, dtype)| concat_column(&self.batches, i, dtype)) + .collect::>>()?; + let mut rows: Vec = (0..columns[0].len()).collect(); + if !self.is_input_pre_ordered { + let converter = RowConverter::new( + self.ordering_dtypes + .iter() + .zip(self.sort_options()) + .map(|(dtype, options)| { + SortField::new_with_options(dtype.clone(), options) + }) + .collect(), + )?; + let ordering_rows = converter.convert_columns(&columns[2..])?; + rows.sort_by(|&a, &b| ordering_rows.row(a).cmp(&ordering_rows.row(b))); + } + if self.reverse { + rows.reverse(); + } + let mut seen = HashSet::new(); + let mut indices = Vec::new(); + for i in rows { + if seen.insert(ScalarValue::try_from_array(&columns[0], i)?) { + indices.push(i as u64); + } + } + let indices = UInt64Array::from(indices); + columns + .iter() + .map(|array| Ok(take(array.as_ref(), &indices, None)?)) + .collect() + } + + /// Builds the `List>` state column from the given + /// ordering values. These must be the de-duplicated ordering values that + /// align with the map state, so both pieces of state describe the same rows. + fn evaluate_orderings(&self, ordering_values: &[ArrayRef]) -> Result { + let fields = ordering_fields(&self.ordering_req, &self.ordering_dtypes); + let struct_array = + StructArray::try_new(Fields::from(fields), ordering_values.to_vec(), None)?; + Ok(SingleRowListArrayBuilder::new(Arc::new(struct_array)).build_list_scalar()) + } +} + +impl Accumulator for OrderSensitiveMapAggAccumulator { + fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> { + assert_eq_or_internal_err!( + values.len(), + 2 + self.ordering_dtypes.len(), + "map_agg column count mismatch" + ); + let keys = &values[0]; + for array in &values[1..] { + assert_eq_or_internal_err!( + keys.len(), + array.len(), + "map_agg column length mismatch" + ); + } + if keys.logical_null_count() != 0 { + return exec_err!("map key cannot be null"); + } + if !keys.is_empty() { + self.batches.push( + values + .iter() + .map(|array| compact_array(Arc::clone(array))) + .collect::>()?, + ); + } + Ok(()) + } + + fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> { + if states.is_empty() { + return Ok(()); + } + if states.len() != 2 { + return exec_err!( + "map_agg ordered merge expects 2 state columns, got {}", + states.len() + ); + } + + let map_array = states[0].as_map(); + let orderings = states[1].as_list::(); + + // Individually ordered partitions are not globally ordered. Sort once + // at output, including when the raw input was marked pre-ordered. + self.is_input_pre_ordered = false; + for row in 0..map_array.len() { + if map_array.is_null(row) { + continue; + } + let mut columns = map_row_to_arrays(map_array, row).to_vec(); + columns.extend_from_slice(orderings.value(row).as_struct().columns()); + self.update_batch(&columns)?; + } + Ok(()) + } + + fn state(&mut self) -> Result> { + let rows = self.sorted_deduped()?; + let orderings = self.evaluate_orderings(&rows[2..])?; + let map = build_single_map(Arc::clone(&rows[0]), Arc::clone(&rows[1]))?; + Ok(vec![map, orderings]) + } + + fn evaluate(&mut self) -> Result { + let rows = self.sorted_deduped()?; + build_single_map(Arc::clone(&rows[0]), Arc::clone(&rows[1])) + } + + fn size(&self) -> usize { + let mut total = + size_of_val(self) + batches_size(&self.batches) + self.key_type.size() + - size_of_val(&self.key_type) + + self.value_type.size() + - size_of_val(&self.value_type); + + // ordering_dtypes: Vec spine plus the heap owned by each DataType. + total += size_of::() * self.ordering_dtypes.capacity(); + for dtype in &self.ordering_dtypes { + total += dtype.size() - size_of_val(dtype); + } + + total + } +} + +/// Builds a single-row `Map` array holding `keys` and `values`. +/// +/// An empty input yields a one-row array that is NULL, so empty groups +/// evaluate to a NULL map just like [`MapAggAccumulator`] does. +fn build_single_map(keys: ArrayRef, values: ArrayRef) -> Result { + let map_type = MapAgg::map_data_type(keys.data_type(), values.data_type()); + let DataType::Map(entries_field, ordered) = &map_type else { + return internal_err!("map_agg expected Map type, got {map_type}"); + }; + let DataType::Struct(entry_fields) = entries_field.data_type() else { + return internal_err!( + "map_agg expected struct entries, got {}", + entries_field.data_type() + ); + }; + + let entries = StructArray::try_new(entry_fields.clone(), vec![keys, values], None)?; + + let Ok(num_entries) = i32::try_from(entries.len()) else { + return internal_err!( + "map_agg produced {} entries which exceeds the map offset range", + entries.len() + ); + }; + let offsets = OffsetBuffer::new(ScalarBuffer::from(vec![0_i32, num_entries])); + let nulls = entries.is_empty().then(|| NullBuffer::from(vec![false])); + + Ok(ScalarValue::Map(Arc::new(MapArray::try_new( + Arc::clone(entries_field), + offsets, + entries, + nulls, + *ordered, + )?))) +} + +fn map_row_to_arrays(map: &MapArray, row: usize) -> [ArrayRef; 2] { + let entries = map.value(row); + [Arc::clone(entries.column(0)), Arc::clone(entries.column(1))] +} + +fn compact_array(array: ArrayRef) -> Result { + // Compact a whole column, including nested view buffers, rather than + // allocating one scalar per value or retaining the input batch's buffers. + let scalar = SingleRowListArrayBuilder::new(array) + .build_list_scalar() + .compacted(); + let ScalarValue::List(list) = scalar else { + return internal_err!("expected a list scalar"); + }; + Ok(Arc::clone(list.values())) +} + +fn concat_column( + batches: &[Vec], + column: usize, + dtype: &DataType, +) -> Result { + if batches.is_empty() { + return Ok(new_empty_array(dtype)); + } + if batches.len() == 1 { + return Ok(Arc::clone(&batches[0][column])); + } + let arrays = batches + .iter() + .map(|batch| batch[column].as_ref()) + .collect::>(); + Ok(concat(&arrays)?) +} + +fn batches_size(batches: &Vec>) -> usize { + batches.capacity() * size_of::>() + + batches + .iter() + .map(|batch| { + batch.capacity() * size_of::() + + batch + .iter() + .map(|array| array.get_array_memory_size()) + .sum::() + }) + .sum::() +} + +#[cfg(test)] +mod tests { + use super::*; + + use arrow::array::{Int32Array, StringArray}; + + fn map_agg_accumulator() -> Result { + MapAggAccumulator::try_new(DataType::Utf8, DataType::Int32) + } + + #[test] + fn map_agg_builds_map_from_batches() -> Result<()> { + let mut acc = map_agg_accumulator()?; + + acc.update_batch(&[ + Arc::new(StringArray::from(vec!["a", "b"])), + Arc::new(Int32Array::from(vec![Some(1), None])), + ])?; + acc.update_batch(&[ + Arc::new(StringArray::from(vec!["c"])), + Arc::new(Int32Array::from(vec![Some(3)])), + ])?; + + let ScalarValue::Map(map) = acc.evaluate()? else { + panic!("expected map scalar"); + }; + assert_eq!(map.len(), 1); + assert!(map.is_valid(0)); + assert_eq!(map.value_offsets(), &[0, 3]); + + let map_keys = map.keys().as_any().downcast_ref::().unwrap(); + assert_eq!(map_keys, &StringArray::from(vec!["a", "b", "c"])); + + let map_values = map.values().as_any().downcast_ref::().unwrap(); + assert_eq!(map_values, &Int32Array::from(vec![Some(1), None, Some(3)])); + + Ok(()) + } + #[test] + fn duplicate_key_handling() -> Result<()> { + let mut acc = map_agg_accumulator()?; + acc.update_batch(&[ + Arc::new(StringArray::from(vec!["a", "b"])), + Arc::new(Int32Array::from(vec![Some(1), Some(23)])), + ])?; + acc.update_batch(&[ + Arc::new(StringArray::from(vec!["a"])), + Arc::new(Int32Array::from(vec![Some(3)])), + ])?; + + let ScalarValue::Map(map) = acc.evaluate()? else { + panic!("expected map scalar"); + }; + // De-duplication is first-wins: the later `a` pair is dropped and the + // surviving entries keep their input order. + assert_eq!(map.len(), 1); + assert!(map.is_valid(0)); + assert_eq!(map.value_offsets(), &[0, 2]); + + let map_keys = map.keys().as_any().downcast_ref::().unwrap(); + assert_eq!(map_keys, &StringArray::from(vec!["a", "b"])); + + let map_values = map.values().as_any().downcast_ref::().unwrap(); + assert_eq!(map_values, &Int32Array::from(vec![Some(1), Some(23)])); + + Ok(()) + } + + #[test] + fn map_agg_empty_group_produces_null_map() -> Result<()> { + let mut acc = map_agg_accumulator()?; + let map_type = + MapAgg::default().return_type(&[DataType::Utf8, DataType::Int32])?; + + acc.update_batch(&[ + Arc::new(StringArray::from(Vec::<&str>::new())), + Arc::new(Int32Array::from(Vec::::new())), + ])?; + + assert_eq!(acc.evaluate()?, ScalarValue::try_new_null(&map_type)?); + Ok(()) + } + + #[test] + fn map_agg_null_key_errors() -> Result<()> { + let mut acc = map_agg_accumulator()?; + let err = acc + .update_batch(&[ + Arc::new(StringArray::from(vec![Some("a"), None])), + Arc::new(Int32Array::from(vec![Some(1), Some(2)])), + ]) + .unwrap_err(); + assert!( + err.to_string().contains("map key cannot be null"), + "unexpected error: {err}" + ); + Ok(()) + } + + #[test] + fn map_agg_state_merge_roundtrip() -> Result<()> { + let mut acc = map_agg_accumulator()?; + acc.update_batch(&[ + Arc::new(StringArray::from(vec!["a", "b"])), + Arc::new(Int32Array::from(vec![1, 2])), + ])?; + acc.update_batch(&[ + Arc::new(StringArray::from(vec!["c"])), + Arc::new(Int32Array::from(vec![3])), + ])?; + + let expected = acc.evaluate()?; + let state = acc.state()?; + assert_eq!(state.len(), 1); + assert_eq!(state[0], expected); + + let state_arrays: Vec = + state.iter().map(|s| s.to_array()).collect::>()?; + let mut merged = map_agg_accumulator()?; + merged.merge_batch(&state_arrays)?; + + // Merging state back in must not change the accumulated map + assert_eq!(merged.evaluate()?, expected); + Ok(()) + } + + #[test] + fn map_agg_update_batch_rejects_mismatched_lengths() { + let mut acc = map_agg_accumulator().unwrap(); + let err = acc + .update_batch(&[ + Arc::new(StringArray::from(vec!["a", "b"])), + Arc::new(Int32Array::from(vec![1])), + ]) + .unwrap_err(); + assert!( + err.to_string().contains("key/value length mismatch"), + "unexpected error: {err}" + ); + } + + #[test] + fn map_agg_name() { + assert_eq!(MapAgg::default().name(), "map_agg"); + } + + #[test] + fn map_agg_return_type_builds_map() { + let map_type = MapAgg::default() + .return_type(&[DataType::Utf8, DataType::Int32]) + .unwrap(); + + let DataType::Map(entries, false) = map_type else { + panic!("expected Map, got {map_type:?}"); + }; + let DataType::Struct(fields) = entries.data_type() else { + panic!("expected entries Struct, got {:?}", entries.data_type()); + }; + assert_eq!(fields.len(), 2); + assert_eq!(fields[0].name(), "key"); + assert_eq!(fields[0].data_type(), &DataType::Utf8); + assert!(!fields[0].is_nullable()); + assert_eq!(fields[1].name(), "value"); + assert_eq!(fields[1].data_type(), &DataType::Int32); + assert!(fields[1].is_nullable()); + } + + #[test] + fn map_agg_udaf_is_registered_singleton() { + let udaf = map_agg_udaf(); + assert_eq!(udaf.name(), "map_agg"); + assert!(Arc::ptr_eq(&udaf, &map_agg_udaf())); + } + + /// Accumulator ordered by an `Int32` `ord` column (ascending, nulls last). + fn ordered_accumulator( + is_input_pre_ordered: bool, + reverse: bool, + ) -> Result { + ordered_accumulator_with_options( + is_input_pre_ordered, + reverse, + SortOptions::new(false, false), + ) + } + + /// Accumulator ordered by an `Int32` `ord` column with the given options. + fn ordered_accumulator_with_options( + is_input_pre_ordered: bool, + reverse: bool, + options: SortOptions, + ) -> Result { + use arrow::datatypes::Schema; + use datafusion_physical_expr::expressions::Column; + use datafusion_physical_expr_common::physical_expr::PhysicalExpr; + use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr; + + let schema = Schema::new(vec![ + Field::new("key", DataType::Utf8, true), + Field::new("value", DataType::Int32, true), + Field::new("ord", DataType::Int32, true), + ]); + let ord_expr = Arc::new( + Column::new_with_schema("ord", &schema).expect("column not in schema"), + ) as Arc; + let ordering = + LexOrdering::new(vec![PhysicalSortExpr::new(ord_expr, options)]).unwrap(); + + Ok(OrderSensitiveMapAggAccumulator::new( + DataType::Utf8, + DataType::Int32, + vec![DataType::Int32], + ordering, + is_input_pre_ordered, + reverse, + )) + } + + /// `(key, value, ord)` input batch for [`ordered_accumulator`]. + fn ordered_batch(keys: &[&str], values: &[i32], ord: &[i32]) -> Vec { + vec![ + Arc::new(StringArray::from(keys.to_vec())), + Arc::new(Int32Array::from(values.to_vec())), + Arc::new(Int32Array::from(ord.to_vec())), + ] + } + + #[test] + fn ordered_map_agg_sorts_and_keeps_first_pair_per_key() -> Result<()> { + let mut acc = ordered_accumulator(false, false)?; + acc.update_batch(&ordered_batch(&["a", "b", "a"], &[10, 20, 30], &[3, 1, 2]))?; + + // State carries the map plus the parallel ordering column. + assert_eq!(acc.state()?.len(), 2); + + let ScalarValue::Map(map) = acc.evaluate()? else { + panic!("expected map scalar"); + }; + // Ordered by `ord`, so `b` (ord 1) comes first; of the two `a` pairs the + // one with the smaller `ord` wins the de-duplication. + assert_eq!(map.value_offsets(), &[0, 2]); + let map_keys = map.keys().as_any().downcast_ref::().unwrap(); + assert_eq!(map_keys, &StringArray::from(vec!["b", "a"])); + let map_values = map.values().as_any().downcast_ref::().unwrap(); + assert_eq!(map_values, &Int32Array::from(vec![20, 30])); + + Ok(()) + } + + #[test] + fn ordered_map_agg_merges_partition_states_in_ordering() -> Result<()> { + let mut lhs = ordered_accumulator(true, false)?; + lhs.update_batch(&ordered_batch(&["a", "c"], &[1, 3], &[1, 3]))?; + + let mut rhs = ordered_accumulator(false, false)?; + rhs.update_batch(&ordered_batch(&["b", "a"], &[2, 4], &[2, 4]))?; + let state = rhs + .state()? + .iter() + .map(ScalarValue::to_array) + .collect::>>()?; + lhs.merge_batch(&state)?; + + let ScalarValue::Map(map) = lhs.evaluate()? else { + panic!("expected map scalar"); + }; + // Both partitions interleave on `ord`: a(1), b(2), c(3), a(4) -- and + // the trailing `a` loses the de-duplication to the earlier one. + assert_eq!(map.value_offsets(), &[0, 3]); + let map_keys = map.keys().as_any().downcast_ref::().unwrap(); + assert_eq!(map_keys, &StringArray::from(vec!["a", "b", "c"])); + let map_values = map.values().as_any().downcast_ref::().unwrap(); + assert_eq!(map_values, &Int32Array::from(vec![1, 2, 3])); + + Ok(()) + } + + #[test] + fn ordered_map_agg_reverses_preordered_input_before_deduplication() -> Result<()> { + // Reversed accumulator arguments carry reversed ordering options. + let mut acc = + ordered_accumulator_with_options(true, true, SortOptions::new(true, false))?; + acc.update_batch(&ordered_batch(&["a", "a", "b"], &[20, 10, 5], &[20, 10, 5]))?; + + let ScalarValue::Map(map) = acc.evaluate()? else { + panic!("expected map scalar"); + }; + let map_keys = map.keys().as_any().downcast_ref::().unwrap(); + let map_values = map.values().as_any().downcast_ref::().unwrap(); + assert_eq!(map_keys, &StringArray::from(vec!["b", "a"])); + assert_eq!(map_values, &Int32Array::from(vec![5, 10])); + + Ok(()) + } + + #[test] + fn ordered_map_agg_reverses_unordered_input_before_deduplication() -> Result<()> { + // A reversed aggregate can receive the reversed ordering options + // without pre-ordered input, for example in a Final aggregate. + let mut acc = + ordered_accumulator_with_options(false, true, SortOptions::new(true, false))?; + acc.update_batch(&ordered_batch(&["a", "b", "a"], &[20, 5, 10], &[20, 5, 10]))?; + + let ScalarValue::Map(map) = acc.evaluate()? else { + panic!("expected map scalar"); + }; + // The requested order is ascending, so `b` (5) comes first and the + // earlier `a` pair (10) wins over the later one (20). + let map_keys = map.keys().as_any().downcast_ref::().unwrap(); + let map_values = map.values().as_any().downcast_ref::().unwrap(); + assert_eq!(map_keys, &StringArray::from(vec!["b", "a"])); + assert_eq!(map_values, &Int32Array::from(vec![5, 10])); + + Ok(()) + } + + #[test] + fn ordered_map_agg_reversed_partial_states_merge_before_deduplication() -> Result<()> + { + let options = SortOptions::new(true, false); + + let mut lhs = ordered_accumulator_with_options(true, true, options)?; + lhs.update_batch(&ordered_batch(&["a"], &[20], &[20]))?; + let lhs_state = lhs + .state()? + .iter() + .map(ScalarValue::to_array) + .collect::>>()?; + + let mut rhs = ordered_accumulator_with_options(true, true, options)?; + rhs.update_batch(&ordered_batch(&["a", "b"], &[10, 5], &[10, 5]))?; + let rhs_state = rhs + .state()? + .iter() + .map(ScalarValue::to_array) + .collect::>>()?; + + // The final stage is reversed but not pre-ordered. + let mut final_acc = ordered_accumulator_with_options(false, true, options)?; + final_acc.merge_batch(&lhs_state)?; + final_acc.merge_batch(&rhs_state)?; + + let ScalarValue::Map(map) = final_acc.evaluate()? else { + panic!("expected map scalar"); + }; + let map_keys = map.keys().as_any().downcast_ref::().unwrap(); + let map_values = map.values().as_any().downcast_ref::().unwrap(); + assert_eq!(map_keys, &StringArray::from(vec!["b", "a"])); + assert_eq!(map_values, &Int32Array::from(vec![5, 10])); + + Ok(()) + } + + #[test] + fn ordered_map_agg_null_key_errors() -> Result<()> { + let mut acc = ordered_accumulator(false, false)?; + let err = acc + .update_batch(&[ + Arc::new(StringArray::from(vec![Some("a"), None])), + Arc::new(Int32Array::from(vec![1, 2])), + Arc::new(Int32Array::from(vec![1, 2])), + ]) + .unwrap_err(); + assert!(err.to_string().contains("map key cannot be null")); + assert!(acc.evaluate()?.is_null()); + Ok(()) + } + + #[test] + fn map_agg_dictionary_null_keys() -> Result<()> { + use arrow::array::DictionaryArray; + use arrow::datatypes::Int32Type; + + let keys: ArrayRef = Arc::new(DictionaryArray::::try_new( + Int32Array::from(vec![0, 1]), + Arc::new(StringArray::from(vec![Some("a"), None])), + )?); + assert_eq!(keys.null_count(), 0); + for ordered in [false, true] { + let mut acc: Box = if ordered { + let mut acc = ordered_accumulator(false, false)?; + acc.key_type = keys.data_type().clone(); + Box::new(acc) + } else { + Box::new(MapAggAccumulator::try_new( + keys.data_type().clone(), + DataType::Int32, + )?) + }; + let mut values = vec![ + Arc::clone(&keys), + Arc::new(Int32Array::from(vec![1, 2])) as ArrayRef, + ]; + if ordered { + values.push(Arc::clone(&values[1])); + } + let err = acc.update_batch(&values).unwrap_err(); + assert!(err.to_string().contains("map key cannot be null")); + assert!(acc.evaluate()?.is_null()); + } + Ok(()) + } + + #[test] + fn map_agg_duplicate_payloads_are_not_retained() -> Result<()> { + let mut acc = map_agg_accumulator()?; + let batch = [ + Arc::new(StringArray::from(vec!["a"])) as ArrayRef, + Arc::new(Int32Array::from(vec![1])) as ArrayRef, + ]; + acc.update_batch(&batch)?; + let size = acc.size(); + let expected = acc.evaluate()?; + for _ in 0..10 { + acc.update_batch(&batch)?; + acc.merge_batch(&[expected.to_array()?])?; + assert_eq!(acc.evaluate()?, expected); + assert_eq!(acc.size(), size); + } + Ok(()) + } + + #[test] + fn ordered_map_agg_empty_state_roundtrip() -> Result<()> { + let mut acc = ordered_accumulator(false, false)?; + let expected = acc.evaluate()?; + assert_eq!( + expected, + ScalarValue::try_new_null(&MapAgg::map_data_type( + &DataType::Utf8, + &DataType::Int32, + ))? + ); + let state = acc + .state()? + .iter() + .map(ScalarValue::to_array) + .collect::>>()?; + acc.merge_batch(&state)?; + assert_eq!(acc.evaluate()?, expected); + acc.update_batch(&ordered_batch(&["a"], &[1], &[1]))?; + acc.merge_batch(&state)?; + assert_eq!(acc.evaluate()?, acc.state()?[0]); + Ok(()) + } +} diff --git a/datafusion/functions-aggregate/tests/state_compat.rs b/datafusion/functions-aggregate/tests/state_compat.rs index 6e8033fc1ec48..e38445b8bf603 100644 --- a/datafusion/functions-aggregate/tests/state_compat.rs +++ b/datafusion/functions-aggregate/tests/state_compat.rs @@ -47,6 +47,7 @@ const NOT_EXERCISED: &[(&str, Reason)] = &[ ("covar_pop", Reason::NoGroupsAccumulator), ("covar_samp", Reason::NoGroupsAccumulator), ("grouping", Reason::NoAccumulator), + ("map_agg", Reason::NoGroupsAccumulator), ("nth_value", Reason::NoGroupsAccumulator), ("regr_avgx", Reason::NoGroupsAccumulator), ("regr_avgy", Reason::NoGroupsAccumulator), diff --git a/datafusion/sqllogictest/test_files/map_agg.slt b/datafusion/sqllogictest/test_files/map_agg.slt new file mode 100644 index 0000000000000..6aa2c5315a958 --- /dev/null +++ b/datafusion/sqllogictest/test_files/map_agg.slt @@ -0,0 +1,313 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +####### +# Tests for the map_agg aggregate function. +####### + +# A map is built from all input rows. +query ? +SELECT map_agg(v, m ORDER BY v) FROM (VALUES (1, 10), (2, 20), (3, 30), (4, 40)) t(v, m) +---- +{1: 10, 2: 20, 3: 30, 4: 40} + +# NULL values are kept in the map. +query ? +SELECT map_agg(v, m ORDER BY v) FROM (VALUES (1, 10), (2, NULL), (3, 30)) t(v, m) +---- +{1: 10, 2: NULL, 3: 30} + +# Null keys cannot be represented in an Arrow map. +statement error DataFusion error: Execution error: map key cannot be null +SELECT map_agg(v, m) FROM (VALUES (1, 10), (NULL, 20)) t(v, m) + +# Empty input produces a NULL map, like array_agg. +query ? +SELECT map_agg(v, m) FROM (VALUES (1, 10)) t(v, m) WHERE false +---- +NULL + +# Each group collects its own key/value pairs into one map. +query T? +SELECT k, map_agg(v, m ORDER BY v) +FROM (VALUES + ('a', 1, 10), + ('a', 2, 20), + ('b', 3, 30) +) t(k, v, m) +GROUP BY k +ORDER BY k +---- +a {1: 10, 2: 20} +b {3: 30} + +# duplicate group keys: each group keeps only its first pair per key +query T? +SELECT k, map_agg(k,v ORDER BY v) +FROM (VALUES + ('a', 1), + ('a', 2), + ('b', 3) +) t(k, v ) +GROUP BY k +ORDER BY k +---- +a {a: 1} +b {b: 3} + +# ORDER BY inside the aggregate controls the map entry order +query ? +SELECT map_agg(k, v ORDER BY v) +FROM (VALUES + ('a', 3), + ('b', 1), + ('c', 2) +) t(k, v) +---- +{b: 1, c: 2, a: 3} + +# ORDER BY DESC reverses the entry order. +query ? +SELECT map_agg(k, v ORDER BY v DESC) +FROM (VALUES ('a', 3), ('b', 1), ('c', 2)) t(k, v) +---- +{a: 3, c: 2, b: 1} + +# Ordering by the key puts the entries in key order. +query ? +SELECT map_agg(k, v ORDER BY k) +FROM (VALUES ('c', 3), ('a', 1), ('b', 2)) t(k, v) +---- +{a: 1, b: 2, c: 3} + +# Multiple ordering expressions are applied left to right. +query ? +SELECT map_agg(k, v ORDER BY g, v DESC) +FROM (VALUES + ('a', 1, 'x'), + ('b', 5, 'x'), + ('c', 3, 'y') +) t(k, v, g) +---- +{b: 5, a: 1, c: 3} + +# The ordering may reference a column that is neither the key nor the value. +query ? +SELECT map_agg(k, v ORDER BY o) +FROM (VALUES ('a', 10, 3), ('b', 20, 1), ('c', 30, 2)) t(k, v, o) +---- +{b: 20, c: 30, a: 10} + +# With an ORDER BY the pairs are sorted before de-duplication, so the pair that +# sorts first wins -- unlike the unordered accumulator, which keeps the first +# input row. +query ? +SELECT map_agg(k, v ORDER BY v) +FROM (VALUES ('a', 20), ('b', 5), ('a', 10)) t(k, v) +---- +{b: 5, a: 10} + +# Reverse-preordered input must still use the first pair in the requested +# ordering, not the first physical row. +query ? +SELECT map_agg(k, v ORDER BY v) +FROM ( + SELECT * FROM (VALUES ('a', 20), ('b', 5), ('a', 10)) t(k, v) + ORDER BY v DESC +) t +---- +{b: 5, a: 10} + +# The default null ordering is NULLS LAST for ascending order, so NULL ordering +# values sort to the end. +query ? +SELECT map_agg(k, v ORDER BY v) +FROM (VALUES ('a', 1), ('b', NULL), ('c', 2)) t(k, v) +---- +{a: 1, c: 2, b: NULL} + +# NULLS FIRST moves them to the front. +query ? +SELECT map_agg(k, v ORDER BY v NULLS FIRST) +FROM (VALUES ('a', 1), ('b', NULL), ('c', 2)) t(k, v) +---- +{b: NULL, a: 1, c: 2} + +# Ordered and unordered aggregates both reject NULL keys. +statement error map key cannot be null +SELECT map_agg(k, v ORDER BY v) FROM (VALUES ('a', 1), (NULL, 2)) t(k, v) + +# Empty input produces a NULL map even when an ORDER BY is present. +query ? +SELECT map_agg(k, v ORDER BY v) FROM (VALUES ('a', 1)) t(k, v) WHERE false +---- +NULL + +# The ORDER BY is applied independently within each group. +query T? +SELECT g, map_agg(k, v ORDER BY v DESC) +FROM (VALUES + ('a', 'p', 1), + ('a', 'q', 3), + ('b', 'r', 2) +) t(g, k, v) +GROUP BY g +ORDER BY g +---- +a {q: 3, p: 1} +b {r: 2} + +# Partial states from every partition are merged on the ordering, so the entry +# order is deterministic under the default parallelism -- in contrast to the +# unordered accumulator, whose entry order depends on which partial state +# finishes first. +query ? +SELECT map_agg(k, v ORDER BY v) +FROM (VALUES + ('a', 1), ('b', 2), ('c', 3), ('d', 4), + ('e', 5), ('f', 6), ('g', 7), ('h', 8) +) t(k, v) +---- +{a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8} + +# Multiple batches: with batch_size = 1 every row arrives in its own batch. +# A single partition keeps the input order, so the entries stay in row order. +statement ok +set datafusion.execution.batch_size = 1 + +statement ok +set datafusion.execution.target_partitions = 1 + +query ? +SELECT map_agg(v, m) FROM (VALUES (1, 10), (2, NULL), (3, 30)) t(v, m) +---- +{1: 10, 2: NULL, 3: 30} + +# The ordered accumulator keeps its ordering when every row arrives in its own +# batch. +query ? +SELECT map_agg(k, v ORDER BY v) +FROM (VALUES ('a', 3), ('b', 1), ('c', 2)) t(k, v) +---- +{b: 1, c: 2, a: 3} + +# The SLT runner sets target_partitions to 4 instead of using the default, so +# set it back explicitly rather than resetting. +statement ok +set datafusion.execution.target_partitions = 4 + +# With the default parallelism the entry order depends on which partial state +# finishes first, so assert the entry count and the sorted keys instead. +query I +SELECT cardinality(map_agg(v, m)) FROM (VALUES (1, 10), (2, 20), (3, 30), (4, 40)) t(v, m) +---- +4 + +query T +SELECT array_to_string(array_sort(map_keys(map_agg(v, m))), ',') FROM (VALUES (1, 10), (2, 20), (3, 30), (4, 40)) t(v, m) +---- +1,2,3,4 + +statement ok +reset datafusion.execution.batch_size + +# DISTINCT must not be silently ignored, with or without aggregate ordering. +statement error map_agg does not support DISTINCT +SELECT map_agg(DISTINCT k, v) FROM (VALUES ('a', 1), ('a', 2)) t(k, v) + +statement error map_agg does not support DISTINCT +SELECT map_agg(DISTINCT k, v ORDER BY v) FROM (VALUES ('a', 1), ('a', 2)) t(k, v) + +# UNION ALL supplies multiple input partitions. Reverse-sorted input and +# duplicates across partitions must still select the first requested value. +statement ok +CREATE VIEW map_partitions AS +SELECT * FROM (VALUES (1, 'a', 20), (2, 'c', 30)) t(g, k, v) +UNION ALL +SELECT * FROM (VALUES (1, 'a', 10), (1, 'b', 5), (2, 'c', 3)) t(g, k, v) + +query TT +EXPLAIN SELECT g, map_agg(k, v ORDER BY v) +FROM (SELECT * FROM map_partitions ORDER BY v DESC) t +GROUP BY g ORDER BY g +---- +logical_plan +01)Sort: t.g ASC NULLS LAST +02)--Aggregate: groupBy=[[t.g]], aggr=[[map_agg(t.k, t.v) ORDER BY [t.v ASC NULLS LAST]]] +03)----SubqueryAlias: t +04)------SubqueryAlias: map_partitions +05)--------Union +06)----------SubqueryAlias: t +07)------------Projection: column1 AS g, column2 AS k, column3 AS v +08)--------------Values: (Int64(1), Utf8("a"), Int64(20)), (Int64(2), Utf8("c"), Int64(30)) +09)----------SubqueryAlias: t +10)------------Projection: column1 AS g, column2 AS k, column3 AS v +11)--------------Values: (Int64(1), Utf8("a"), Int64(10)), (Int64(1), Utf8("b"), Int64(5)), (Int64(2), Utf8("c"), Int64(3)) +physical_plan +01)SortPreservingMergeExec: [g@0 ASC NULLS LAST] +02)--SortExec: expr=[g@0 ASC NULLS LAST], preserve_partitioning=[true] +03)----AggregateExec: mode=FinalPartitioned, gby=[g@0 as g], aggr=[map_agg(t.k,t.v) ORDER BY [t.v ASC NULLS LAST]] +04)------RepartitionExec: partitioning=Hash([g@0], 4), input_partitions=2 +05)--------AggregateExec: mode=Partial, gby=[g@0 as g], aggr=[map_agg(t.k,t.v) ORDER BY [t.v ASC NULLS LAST]] +06)----------UnionExec +07)------------ProjectionExec: expr=[column1@0 as g, column2@1 as k, column3@2 as v] +08)--------------DataSourceExec: partitions=1, partition_sizes=[1] +09)------------ProjectionExec: expr=[column1@0 as g, column2@1 as k, column3@2 as v] +10)--------------DataSourceExec: partitions=1, partition_sizes=[1] + +query I? +SELECT g, map_agg(k, v ORDER BY v) +FROM (SELECT * FROM map_partitions ORDER BY v DESC) t +GROUP BY g ORDER BY g +---- +1 {b: 5, a: 10} +2 {c: 3} + +# Nested values, including NULL list elements, survive partial-state merging. +query I? +SELECT g, map_agg(k, [v, NULL] ORDER BY v) +FROM map_partitions GROUP BY g ORDER BY g +---- +1 {b: [5, NULL], a: [10, NULL]} +2 {c: [3, NULL]} + +query ? +SELECT map_agg(k, named_struct('x', v) ORDER BY v) +FROM (VALUES ('a', 2), ('b', 1)) t(k, v) +---- +{b: {x: 1}, a: {x: 2}} + +# Unordered aggregation also preserves nested values through merging. +query ? +SELECT map_agg(k, [v, NULL]) +FROM (SELECT 'a' AS k, 1 AS v UNION ALL SELECT 'a' AS k, 1 AS v) t +---- +{a: [1, NULL]} + +# Full-partition windows over reverse-sorted input must not reverse the +# duplicate winner when there is no ORDER BY inside the aggregate. +query I? +SELECT v, map_agg(k, v) OVER ( + ORDER BY v ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING +) FROM (SELECT * FROM (VALUES ('a', 20), ('b', 5), ('a', 10)) t(k, v) + ORDER BY v DESC) t +ORDER BY v +---- +5 {b: 5, a: 10} +10 {b: 5, a: 10} +20 {b: 5, a: 10} + +# A growing window must support repeated evaluation without consuming state. +query I? +SELECT v, map_agg(k, v) OVER ( + ORDER BY v ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW +) FROM (VALUES ('a', 20), ('b', 5), ('a', 10)) t(k, v) +ORDER BY v +---- +5 {b: 5} +10 {b: 5, a: 10} +20 {b: 5, a: 10} + +statement ok +DROP VIEW map_partitions diff --git a/docs/source/user-guide/sql/aggregate_functions.md b/docs/source/user-guide/sql/aggregate_functions.md index 292454949681d..6cc3e6333db31 100644 --- a/docs/source/user-guide/sql/aggregate_functions.md +++ b/docs/source/user-guide/sql/aggregate_functions.md @@ -79,6 +79,7 @@ SELECT SUM(x) WITHIN GROUP (ORDER BY x) FROM t; - [first_value](#first_value) - [grouping](#grouping) - [last_value](#last_value) +- [map_agg](#map_agg) - [max](#max) - [mean](#mean) - [median](#median) @@ -358,6 +359,30 @@ last_value(expression [ORDER BY expression]) +-----------------------------------------------+ ``` +### `map_agg` + +Returns a map created from the key and value expression elements. For each row, the key expression becomes a map key and the value expression becomes the corresponding map value. Without `ORDER BY`, neither entry order nor the value retained for a duplicate key is guaranteed. With `ORDER BY`, the first entry for each key in the requested ordering is kept; ties are not deterministic. NULL keys cause an error, NULL values are retained, and empty input returns NULL. DISTINCT is not supported. + +```sql +map_agg(key, value [ORDER BY expression]) +``` + +#### Arguments + +- **key**: Expression used as the map key. Can be a column or any valid expression. +- **value**: Expression used as the map value. Can be a column or any valid expression. + +#### Example + +```sql +> SELECT map_agg(k, v ORDER BY k) AS m FROM (VALUES ('a', 1)) t(k, v); ++--------+ +| m | ++--------+ +| {a: 1} | ++--------+ +``` + ### `max` Returns the maximum value in the specified column. From b816947e138131cce76b8401f1d5a147ad20cc44 Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Mon, 23 Nov 2026 21:31:07 +0530 Subject: [PATCH 2/9] order fix --- datafusion/functions-aggregate/src/map_agg.rs | 1 + .../src/update_aggr_exprs.rs | 22 ++++++++++++------- .../sqllogictest/test_files/map_agg.slt | 18 +++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/datafusion/functions-aggregate/src/map_agg.rs b/datafusion/functions-aggregate/src/map_agg.rs index 72356cca1fcce..50bb90aa3cffc 100644 --- a/datafusion/functions-aggregate/src/map_agg.rs +++ b/datafusion/functions-aggregate/src/map_agg.rs @@ -222,6 +222,7 @@ impl AggregateUDFImpl for MapAgg { } fn reverse_expr(&self) -> datafusion_expr::ReversedUDAF { + // Not Supported because order matters for first-wins semantics for duplicate keys datafusion_expr::ReversedUDAF::NotSupported } diff --git a/datafusion/physical-optimizer/src/update_aggr_exprs.rs b/datafusion/physical-optimizer/src/update_aggr_exprs.rs index 70c4ec1694e76..2b0ea7bee9b82 100644 --- a/datafusion/physical-optimizer/src/update_aggr_exprs.rs +++ b/datafusion/physical-optimizer/src/update_aggr_exprs.rs @@ -180,15 +180,21 @@ fn try_convert_aggregate_if_better( .map(|e| e.reverse().into()) .collect::>(), ))? { - // Converting to reverse enables more efficient execution - // given the existing ordering (if possible): - aggr_expr - .reverse_expr() - .map(Arc::new) - .unwrap_or(aggr_expr) + + + + // Use reversed ordering only if the aggregate supports reversal. + // Otherwise, fall back to sorting within the aggregate. + match aggr_expr.reverse_expr() { + Some(reversed) => Arc::new(reversed) .with_beneficial_ordering(true)? - .map(Arc::new) - } else { + .map(Arc::new), + + None => aggr_expr + .with_beneficial_ordering(false)? + .map(Arc::new), + } + } else { // There is no beneficial ordering present -- aggregation // will still work albeit in a less efficient mode. aggr_expr.with_beneficial_ordering(false)?.map(Arc::new) diff --git a/datafusion/sqllogictest/test_files/map_agg.slt b/datafusion/sqllogictest/test_files/map_agg.slt index 6aa2c5315a958..afaed959bd881 100644 --- a/datafusion/sqllogictest/test_files/map_agg.slt +++ b/datafusion/sqllogictest/test_files/map_agg.slt @@ -311,3 +311,21 @@ ORDER BY v statement ok DROP VIEW map_partitions + +# Assumes sorted for sorted values in a opposite order +statement ok +SET datafusion.execution.target_partitions = 1; + +query ? +SELECT map_agg(k, v ORDER BY v ASC NULLS LAST) +FROM ( + SELECT * + FROM (VALUES ('a', 20), ('b', 5), ('a', 10), ('z', 0)) t(k, v) + ORDER BY v DESC NULLS FIRST + LIMIT 3 +) s; +---- +{b: 5, a: 5} + +statement ok +set datafusion.execution.target_partitions = 4 From 6b84b2440a086f7e5909b88f4686f110b38db5f6 Mon Sep 17 00:00:00 2001 From: Rishabh Bhardwaj <140598487+rizzabhdwaj@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:14:25 +0530 Subject: [PATCH 3/9] Update map_agg.slt --- datafusion/sqllogictest/test_files/map_agg.slt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datafusion/sqllogictest/test_files/map_agg.slt b/datafusion/sqllogictest/test_files/map_agg.slt index afaed959bd881..b50bddd41acd0 100644 --- a/datafusion/sqllogictest/test_files/map_agg.slt +++ b/datafusion/sqllogictest/test_files/map_agg.slt @@ -325,7 +325,7 @@ FROM ( LIMIT 3 ) s; ---- -{b: 5, a: 5} +{b: 5, a: 10} statement ok set datafusion.execution.target_partitions = 4 From 2a0e34efb19ef8a23a7ea29c881e76f2dc8c86ef Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Mon, 23 Nov 2026 22:26:06 +0530 Subject: [PATCH 4/9] normalize arrayref --- datafusion/functions-aggregate/src/map_agg.rs | 29 +++++++++++-------- .../sqllogictest/test_files/map_agg.slt | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/datafusion/functions-aggregate/src/map_agg.rs b/datafusion/functions-aggregate/src/map_agg.rs index 50bb90aa3cffc..f07a786d3d39b 100644 --- a/datafusion/functions-aggregate/src/map_agg.rs +++ b/datafusion/functions-aggregate/src/map_agg.rs @@ -47,7 +47,7 @@ use arrow::array::{ Array, ArrayRef, AsArray, MapArray, StructArray, UInt64Array, new_empty_array, }; use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; -use arrow::compute::{SortOptions, concat, take}; +use arrow::compute::{SortOptions, cast, concat, take}; use arrow::datatypes::{DataType, Field, FieldRef, Fields}; use arrow::row::{RowConverter, SortField}; @@ -222,7 +222,7 @@ impl AggregateUDFImpl for MapAgg { } fn reverse_expr(&self) -> datafusion_expr::ReversedUDAF { - // Not Supported because order matters for first-wins semantics for duplicate keys + // Not Supported because order matters for aggregate first-wins semantics for duplicate keys datafusion_expr::ReversedUDAF::NotSupported } @@ -270,8 +270,9 @@ impl Accumulator for MapAggAccumulator { fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> { assert_eq_or_internal_err!(values.len(), 2, "map_agg expects (key, value)"); - let keys = &values[0]; - let vals = &values[1]; + let keys = cast(&values[0], &self.key_type)?; + let vals = cast(&values[1], &self.value_type)?; + assert_eq_or_internal_err!(keys.len(), vals.len(), "key/value length mismatch"); if keys.logical_null_count() != 0 { return exec_err!("map key cannot be null"); @@ -280,7 +281,7 @@ impl Accumulator for MapAggAccumulator { for row in 0..keys.len() { if self .seen - .insert(ScalarValue::try_from_array(keys, row)?.compacted()) + .insert(ScalarValue::try_from_array(&keys, row)?.compacted()) { indices.push(row as u64); } @@ -459,7 +460,9 @@ impl Accumulator for OrderSensitiveMapAggAccumulator { 2 + self.ordering_dtypes.len(), "map_agg column count mismatch" ); - let keys = &values[0]; + let keys = cast(&values[0], &self.key_type)?; + let vals = cast(values[1].as_ref(), &self.value_type)?; + for array in &values[1..] { assert_eq_or_internal_err!( keys.len(), @@ -471,12 +474,14 @@ impl Accumulator for OrderSensitiveMapAggAccumulator { return exec_err!("map key cannot be null"); } if !keys.is_empty() { - self.batches.push( - values - .iter() - .map(|array| compact_array(Arc::clone(array))) - .collect::>()?, - ); + let mut batch = Vec::with_capacity(values.len()); + + batch.push(compact_array(keys)?); + batch.push(compact_array(vals)?); + for array in &values[2..] { + batch.push(compact_array(Arc::clone(array))?); + } + self.batches.push(batch); } Ok(()) } diff --git a/datafusion/sqllogictest/test_files/map_agg.slt b/datafusion/sqllogictest/test_files/map_agg.slt index afaed959bd881..b50bddd41acd0 100644 --- a/datafusion/sqllogictest/test_files/map_agg.slt +++ b/datafusion/sqllogictest/test_files/map_agg.slt @@ -325,7 +325,7 @@ FROM ( LIMIT 3 ) s; ---- -{b: 5, a: 5} +{b: 5, a: 10} statement ok set datafusion.execution.target_partitions = 4 From 9435d0fd9fd193d16a8f402274b9abf7feb7b431 Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Mon, 23 Nov 2026 22:35:11 +0530 Subject: [PATCH 5/9] share map_arr compact_payload --- datafusion/functions-aggregate/src/array_agg.rs | 2 +- datafusion/functions-aggregate/src/map_agg.rs | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/datafusion/functions-aggregate/src/array_agg.rs b/datafusion/functions-aggregate/src/array_agg.rs index 36d00524a87f2..180fed86b18a3 100644 --- a/datafusion/functions-aggregate/src/array_agg.rs +++ b/datafusion/functions-aggregate/src/array_agg.rs @@ -2043,7 +2043,7 @@ impl OrderSensitiveArrayAggAccumulator { /// Dictionary GC removes unused values without decoding the dictionary, while /// view GC copies only referenced bytes. Recurse into children so nested payloads /// cannot retain the source buffers either. -fn compact_payload(array: ArrayRef) -> Result { +pub(crate) fn compact_payload(array: ArrayRef) -> Result { match array.data_type() { DataType::Utf8View => Ok(Arc::new(array.as_string_view().gc())), DataType::BinaryView => Ok(Arc::new(array.as_binary_view().gc())), diff --git a/datafusion/functions-aggregate/src/map_agg.rs b/datafusion/functions-aggregate/src/map_agg.rs index f07a786d3d39b..9eba459fe6e11 100644 --- a/datafusion/functions-aggregate/src/map_agg.rs +++ b/datafusion/functions-aggregate/src/map_agg.rs @@ -51,6 +51,7 @@ use arrow::compute::{SortOptions, cast, concat, take}; use arrow::datatypes::{DataType, Field, FieldRef, Fields}; use arrow::row::{RowConverter, SortField}; +use crate::array_agg::compact_payload; use datafusion_common::cast::as_map_array; use datafusion_common::utils::{SingleRowListArrayBuilder, take_function_args}; use datafusion_common::{ @@ -65,7 +66,6 @@ use datafusion_functions_aggregate_common::order::AggregateOrderSensitivity; use datafusion_functions_aggregate_common::utils::ordering_fields; use datafusion_macros::user_doc; use datafusion_physical_expr_common::sort_expr::LexOrdering; - make_udaf_expr_and_func!( MapAgg, map_agg, @@ -585,15 +585,7 @@ fn map_row_to_arrays(map: &MapArray, row: usize) -> [ArrayRef; 2] { } fn compact_array(array: ArrayRef) -> Result { - // Compact a whole column, including nested view buffers, rather than - // allocating one scalar per value or retaining the input batch's buffers. - let scalar = SingleRowListArrayBuilder::new(array) - .build_list_scalar() - .compacted(); - let ScalarValue::List(list) = scalar else { - return internal_err!("expected a list scalar"); - }; - Ok(Arc::clone(list.values())) + compact_payload(array) } fn concat_column( From 107ec20a39735b1b0b9b7d4366a033d1eab62dd7 Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Mon, 23 Nov 2026 22:53:26 +0530 Subject: [PATCH 6/9] test --- datafusion/functions-aggregate/src/map_agg.rs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/datafusion/functions-aggregate/src/map_agg.rs b/datafusion/functions-aggregate/src/map_agg.rs index 9eba459fe6e11..22a42f443b6a0 100644 --- a/datafusion/functions-aggregate/src/map_agg.rs +++ b/datafusion/functions-aggregate/src/map_agg.rs @@ -1065,4 +1065,47 @@ mod tests { assert_eq!(acc.evaluate()?, acc.state()?[0]); Ok(()) } + + #[test] + fn map_agg_compacts_dictionary_payloads() -> Result<()> { + use arrow::array::{DictionaryArray, StringArray}; + use arrow::datatypes::Int32Type; + + for n in [10, 10_000] { + let values = StringArray::from_iter_values( + (0..n).map(|i| format!("unused_value_{i:05}")), + ); + let dictionary: ArrayRef = Arc::new(DictionaryArray::::try_new( + Int32Array::from_iter_values(0..n as i32), + Arc::new(values), + )?); + // Retain only the last row so compaction must remap its key and + // drop the rest of the dictionary. + let value = dictionary.slice(n - 1, 1); + let keys = Arc::new(Int32Array::from(vec![42])) as ArrayRef; + + let mut accumulator = MapAggAccumulator::try_new( + DataType::Int32, + dictionary.data_type().clone(), + )?; + accumulator.update_batch(&[keys, Arc::clone(&value)])?; + + let size = accumulator.size(); + assert!( + size < 2_000, + "dictionary compaction retained unused values: \ + {size} bytes for {n} entries" + ); + + let ScalarValue::Map(map) = accumulator.evaluate()? else { + panic!("expected map scalar"); + }; + assert_eq!( + ScalarValue::try_from_array(map.values(), 0)?, + ScalarValue::try_from_array(&value, 0)? + ); + } + + Ok(()) + } } From 084782cf3b52745340246e0ded2cc501ae60b587 Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Fri, 27 Nov 2026 13:22:06 +0530 Subject: [PATCH 7/9] fix as per feedback --- datafusion/functions-aggregate/src/map_agg.rs | 156 ++++++++++++++++-- 1 file changed, 146 insertions(+), 10 deletions(-) diff --git a/datafusion/functions-aggregate/src/map_agg.rs b/datafusion/functions-aggregate/src/map_agg.rs index 22a42f443b6a0..2e5d0c6b97f33 100644 --- a/datafusion/functions-aggregate/src/map_agg.rs +++ b/datafusion/functions-aggregate/src/map_agg.rs @@ -44,7 +44,8 @@ use std::mem::{size_of, size_of_val}; use std::sync::Arc; use arrow::array::{ - Array, ArrayRef, AsArray, MapArray, StructArray, UInt64Array, new_empty_array, + Array, ArrayRef, AsArray, MapArray, StructArray, UInt64Array, make_array, + new_empty_array, }; use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; use arrow::compute::{SortOptions, cast, concat, take}; @@ -53,6 +54,7 @@ use arrow::row::{RowConverter, SortField}; use crate::array_agg::compact_payload; use datafusion_common::cast::as_map_array; +use datafusion_common::scalar::copy_array_data; use datafusion_common::utils::{SingleRowListArrayBuilder, take_function_args}; use datafusion_common::{ Result, ScalarValue, assert_eq_or_internal_err, exec_err, internal_err, not_impl_err, @@ -289,8 +291,8 @@ impl Accumulator for MapAggAccumulator { if !indices.is_empty() { let indices = UInt64Array::from(indices); self.batches.push( - values - .iter() + [keys, vals] + .into_iter() .map(|array| compact_array(take(array.as_ref(), &indices, None)?)) .collect::>()?, ); @@ -474,12 +476,15 @@ impl Accumulator for OrderSensitiveMapAggAccumulator { return exec_err!("map key cannot be null"); } if !keys.is_empty() { + // Compaction alone leaves ordinary slices sharing the source buffers. + let copy_and_compact = |array: ArrayRef| { + compact_payload(make_array(copy_array_data(&array.to_data()))) + }; let mut batch = Vec::with_capacity(values.len()); - - batch.push(compact_array(keys)?); - batch.push(compact_array(vals)?); - for array in &values[2..] { - batch.push(compact_array(Arc::clone(array))?); + batch.push(copy_and_compact(keys)?); + batch.push(copy_and_compact(vals)?); + for (array, dtype) in values[2..].iter().zip(&self.ordering_dtypes) { + batch.push(copy_and_compact(cast(array, dtype)?)?); } self.batches.push(batch); } @@ -624,7 +629,8 @@ fn batches_size(batches: &Vec>) -> usize { mod tests { use super::*; - use arrow::array::{Int32Array, StringArray}; + use arrow::array::{Int32Array, RecordBatch, StringArray}; + use arrow::datatypes::Schema; fn map_agg_accumulator() -> Result { MapAggAccumulator::try_new(DataType::Utf8, DataType::Int32) @@ -658,6 +664,48 @@ mod tests { Ok(()) } + + #[test] + fn map_agg_output_and_state_match_declared_schema() -> Result<()> { + let key_type = DataType::Utf8; + let value_type = + DataType::Struct(Fields::from(vec![Field::new("n", DataType::Int32, true)])); + let agg = MapAgg::default(); + let return_field = Arc::new(Field::new( + "map_agg", + agg.return_type(&[key_type.clone(), value_type.clone()])?, + true, + )); + let state_fields = agg.state_fields(StateFieldsArgs { + name: agg.name(), + input_fields: &[ + Arc::new(Field::new("key", key_type.clone(), false)), + Arc::new(Field::new("value", value_type.clone(), true)), + ], + return_field: Arc::clone(&return_field), + ordering_fields: &[], + is_distinct: false, + })?; + let mut acc = MapAggAccumulator::try_new(key_type, value_type)?; + let values = StructArray::from(vec![( + Arc::new(Field::new("n", DataType::Int32, false)), + Arc::new(Int32Array::from(vec![1])) as ArrayRef, + )]); + acc.update_batch(&[Arc::new(StringArray::from(vec!["a"])), Arc::new(values)])?; + + RecordBatch::try_new( + Arc::new(Schema::new(vec![return_field])), + vec![acc.evaluate()?.to_array()?], + )?; + let state = acc + .state()? + .iter() + .map(ScalarValue::to_array) + .collect::>>()?; + RecordBatch::try_new(Arc::new(Schema::new(state_fields)), state)?; + Ok(()) + } + #[test] fn duplicate_key_handling() -> Result<()> { let mut acc = map_agg_accumulator()?; @@ -812,7 +860,6 @@ mod tests { reverse: bool, options: SortOptions, ) -> Result { - use arrow::datatypes::Schema; use datafusion_physical_expr::expressions::Column; use datafusion_physical_expr_common::physical_expr::PhysicalExpr; use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr; @@ -869,6 +916,95 @@ mod tests { Ok(()) } + fn assert_ordered_map_agg_normalizes_ordering_type( + is_input_pre_ordered: bool, + ) -> Result<()> { + let mut acc = ordered_accumulator(is_input_pre_ordered, false)?; + let declared_field = Arc::new(Field::new("n", DataType::Int32, true)); + acc.ordering_dtypes[0] = + DataType::Struct(Fields::from(vec![declared_field.clone()])); + let (keys, values, order) = if is_input_pre_ordered { + (["a", "b"], [10, 20], [1, 2]) + } else { + (["b", "a"], [20, 10], [2, 1]) + }; + let order = StructArray::from(vec![( + Arc::new(Field::new("n", DataType::Int32, false)), + Arc::new(Int32Array::from(order.to_vec())) as ArrayRef, + )]); + acc.update_batch(&[ + Arc::new(StringArray::from(keys.to_vec())), + Arc::new(Int32Array::from(values.to_vec())), + Arc::new(order), + ])?; + + let result = acc.evaluate()?; + let ScalarValue::Map(map) = &result else { + panic!("expected map scalar"); + }; + assert_eq!( + map.keys().as_string::(), + &StringArray::from(vec!["a", "b"]) + ); + assert_eq!(map.values().as_primitive(), &Int32Array::from(vec![10, 20])); + + let state = acc.state()?; + assert_eq!(state[0], result); + let orderings = state[1].to_array()?; + let entries = orderings.as_list::().value(0); + let expected = StructArray::from(vec![( + declared_field, + Arc::new(Int32Array::from(vec![1, 2])) as ArrayRef, + )]); + assert_eq!(entries.as_struct().column(0).as_struct(), &expected); + Ok(()) + } + + #[test] + fn ordered_map_agg_normalizes_presorted_ordering_type() -> Result<()> { + assert_ordered_map_agg_normalizes_ordering_type(true) + } + + #[test] + fn ordered_map_agg_normalizes_unsorted_ordering_type() -> Result<()> { + assert_ordered_map_agg_normalizes_ordering_type(false) + } + + #[test] + fn ordered_map_agg_detaches_input_slices() -> Result<()> { + let keys = StringArray::from_iter_values((0..10_000).map(|i| format!("key-{i}"))); + let values = Int32Array::from_iter_values(0..10_000); + let orderings = Int32Array::from_iter_values((0..10_000).rev()); + let mut acc = ordered_accumulator(false, false)?; + acc.update_batch(&[ + Arc::new(keys.slice(3, 1)), + Arc::new(values.slice(3, 1)), + Arc::new(orderings.slice(3, 1)), + ])?; + let size = acc.size(); + assert!(size < 4_000, "one retained row uses {size} bytes"); + + let result = acc.evaluate()?; + let ScalarValue::Map(map) = &result else { + panic!("expected map scalar"); + }; + assert_eq!( + map.keys().as_string::(), + &StringArray::from(vec!["key-3"]) + ); + assert_eq!(map.values().as_primitive(), &Int32Array::from(vec![3])); + + let state = acc.state()?; + assert_eq!(state[0], result); + let orderings = state[1].to_array()?; + let entries = orderings.as_list::().value(0); + assert_eq!( + entries.as_struct().column(0).as_primitive(), + &Int32Array::from(vec![9996]) + ); + Ok(()) + } + #[test] fn ordered_map_agg_merges_partition_states_in_ordering() -> Result<()> { let mut lhs = ordered_accumulator(true, false)?; From 3163c782ce8c071db0bdd0ff2c0cb416d31c010b Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Mon, 30 Nov 2026 11:15:31 +0530 Subject: [PATCH 8/9] fix: remove trailing whitespace in update_aggr_exprs --- datafusion/physical-optimizer/src/update_aggr_exprs.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/datafusion/physical-optimizer/src/update_aggr_exprs.rs b/datafusion/physical-optimizer/src/update_aggr_exprs.rs index 2b0ea7bee9b82..f9f4fa0d9cf10 100644 --- a/datafusion/physical-optimizer/src/update_aggr_exprs.rs +++ b/datafusion/physical-optimizer/src/update_aggr_exprs.rs @@ -181,8 +181,6 @@ fn try_convert_aggregate_if_better( .collect::>(), ))? { - - // Use reversed ordering only if the aggregate supports reversal. // Otherwise, fall back to sorting within the aggregate. match aggr_expr.reverse_expr() { From 5fabbe670448f4f294d4806cb90b885156a134de Mon Sep 17 00:00:00 2001 From: rishabh1244 <1244rishabh@gmail.com> Date: Thu, 3 Dec 2026 15:01:52 +0530 Subject: [PATCH 9/9] fix: compact MAP_AGG list views and declare DISTINCT unsupported --- datafusion/functions-aggregate/src/map_agg.rs | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/datafusion/functions-aggregate/src/map_agg.rs b/datafusion/functions-aggregate/src/map_agg.rs index 2e5d0c6b97f33..e1be6ef650ae3 100644 --- a/datafusion/functions-aggregate/src/map_agg.rs +++ b/datafusion/functions-aggregate/src/map_agg.rs @@ -44,8 +44,8 @@ use std::mem::{size_of, size_of_val}; use std::sync::Arc; use arrow::array::{ - Array, ArrayRef, AsArray, MapArray, StructArray, UInt64Array, make_array, - new_empty_array, + Array, ArrayRef, AsArray, MapArray, MutableArrayData, StructArray, UInt64Array, + make_array, new_empty_array, }; use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; use arrow::compute::{SortOptions, cast, concat, take}; @@ -62,7 +62,7 @@ use datafusion_common::{ use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::utils::format_state_name; use datafusion_expr::{ - Accumulator, AggregateUDFImpl, Documentation, Signature, Volatility, + Accumulator, AggregateUDFImpl, DistinctHandling, Documentation, Signature, Volatility, }; use datafusion_functions_aggregate_common::order::AggregateOrderSensitivity; use datafusion_functions_aggregate_common::utils::ordering_fields; @@ -190,6 +190,10 @@ impl AggregateUDFImpl for MapAgg { Ok(fields) } + fn distinct_handling(&self) -> DistinctHandling { + DistinctHandling::Unsupported + } + fn accumulator(&self, acc_args: AccumulatorArgs) -> Result> { if acc_args.is_distinct { return not_impl_err!("map_agg does not support DISTINCT"); @@ -590,6 +594,16 @@ fn map_row_to_arrays(map: &MapArray, row: usize) -> [ArrayRef; 2] { } fn compact_array(array: ArrayRef) -> Result { + // Arrow's take copies the list views but still shares the entire child array. + if matches!( + array.data_type(), + DataType::ListView(_) | DataType::LargeListView(_) + ) { + let data = array.to_data(); + let mut copy = MutableArrayData::new(vec![&data], false, data.len()); + copy.try_extend(0, 0, data.len())?; + return compact_payload(make_array(copy.freeze())); + } compact_payload(array) } @@ -629,7 +643,7 @@ fn batches_size(batches: &Vec>) -> usize { mod tests { use super::*; - use arrow::array::{Int32Array, RecordBatch, StringArray}; + use arrow::array::{Int32Array, ListViewArray, RecordBatch, StringArray}; use arrow::datatypes::Schema; fn map_agg_accumulator() -> Result { @@ -922,7 +936,7 @@ mod tests { let mut acc = ordered_accumulator(is_input_pre_ordered, false)?; let declared_field = Arc::new(Field::new("n", DataType::Int32, true)); acc.ordering_dtypes[0] = - DataType::Struct(Fields::from(vec![declared_field.clone()])); + DataType::Struct(Fields::from(vec![Arc::clone(&declared_field)])); let (keys, values, order) = if is_input_pre_ordered { (["a", "b"], [10, 20], [1, 2]) } else { @@ -1202,6 +1216,36 @@ mod tests { Ok(()) } + #[test] + fn map_agg_compacts_list_view_payloads() -> Result<()> { + let values = ListViewArray::try_new( + Arc::new(Field::new_list_field(DataType::Int32, true)), + vec![0, 9_998].into(), + vec![9_998, 2].into(), + Arc::new(Int32Array::from_iter_values(0..10_000)), + None, + )?; + let mut acc = + MapAggAccumulator::try_new(DataType::Int32, values.data_type().clone())?; + acc.update_batch(&[ + Arc::new(Int32Array::from(vec![42])), + Arc::new(values.slice(1, 1)), + ])?; + + let size = acc.size(); + assert!(size < 4_000, "one retained row uses {size} bytes"); + let ScalarValue::Map(map) = acc.evaluate()? else { + panic!("expected map scalar"); + }; + let lists = map.values().as_list_view::(); + assert_eq!(lists.len(), 1); + assert_eq!( + lists.value(0).as_primitive(), + &Int32Array::from(vec![9_998, 9_999]) + ); + Ok(()) + } + #[test] fn map_agg_compacts_dictionary_payloads() -> Result<()> { use arrow::array::{DictionaryArray, StringArray};