Apache DataFusion SQL Query Engine
perf: stop dictionary work growing with the group count (#26703)
Jay Zhanbc1be2c5a07c
diff --git a/Cargo.lock b/Cargo.lockindex c835ec6439..7f8b942657 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1912,6 +1912,7 @@ dependencies = ["arrow","arrow-ipc","arrow-schema",+ "arrow-select","chrono","criterion","foldhash 0.2.0",diff --git a/clippy.toml b/clippy.tomlindex 0d7b97d932..af754ce4f8 100644--- a/clippy.toml+++ b/clippy.toml@@ -5,6 +5,8 @@disallowed-methods = [{ path = "tokio::task::spawn", reason = "To provide cancel-safety, use `SpawnedTask::spawn` instead (https://github.com/apache/datafusion/issues/6513)" },{ path = "tokio::task::spawn_blocking", reason = "To provide cancel-safety, use `SpawnedTask::spawn_blocking` instead (https://github.com/apache/datafusion/issues/6513)" },+ { path = "arrow_array::array::AnyDictionaryArray::with_values", reason = "Running code on every dictionary value can cost far more than the batch has rows; use `datafusion_common::utils::map_dictionary_values`" },+ { path = "arrow_array::array::DictionaryArray::with_values", reason = "Running code on every dictionary value can cost far more than the batch has rows; use `datafusion_common::utils::map_dictionary_values`" },{ path = "std::vec::Vec::reserve", reason = "Use `Vec::try_reserve` so allocation failures can be reported instead of panicking", replacement = "try_reserve" },]diff --git a/datafusion/common/Cargo.toml b/datafusion/common/Cargo.tomlindex 7a73d10852..998ffa90c9 100644--- a/datafusion/common/Cargo.toml+++ b/datafusion/common/Cargo.toml@@ -59,6 +59,7 @@ name = "record_batch_memory"arrow = { workspace = true }arrow-ipc = { workspace = true }arrow-schema = { workspace = true, features = ["canonical_extension_types"] }+arrow-select = { workspace = true }chrono = { workspace = true }foldhash = "0.2"half = { workspace = true }diff --git a/datafusion/common/benches/with_hashes.rs b/datafusion/common/benches/with_hashes.rsindex 7fc4a29fc5..69f77458a2 100644--- a/datafusion/common/benches/with_hashes.rs+++ b/datafusion/common/benches/with_hashes.rs@@ -7,7 +7,7 @@use arrow::array::{Array, ArrayRef, ArrowPrimitiveType, DictionaryArray, FixedSizeListArray,GenericStringArray, Int32Array, Int64Array, ListArray, ListViewArray, MapArray,- NullArray, NullBufferBuilder, OffsetSizeTrait, PrimitiveArray, RunArray,+ NullArray, NullBufferBuilder, OffsetSizeTrait, PrimitiveArray, RunArray, StringArray,StringViewArray, StructArray, UnionArray, make_array,};use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer};@@ -394,6 +394,22 @@ fn sliced_array_benchmark(c: &mut Criterion) {);}}++ // Sliced DictionaryArray with one value per row of the full array, like the+ // group keys an aggregation emits in `batch_size` slices. Hashing reduces+ // it to the referenced values only above 64 values per row, so 1/8 keeps+ // the full dictionary and 1/128 doesn't.+ for ratio in [8, 128] {+ let total_rows = BATCH_SIZE * ratio;+ let full_array = distinct_dictionary_array(total_rows);+ let sliced = full_array.slice(BATCH_SIZE * (ratio / 2), BATCH_SIZE);+ c.bench_function(+ &format!("dictionary_sliced: 1/{ratio} of {total_rows} values"),+ |b| {+ do_hash_test_with_len(b, std::slice::from_ref(&sliced), BATCH_SIZE);+ },+ );+ }}fn do_hash_test_with_len(b: &mut Bencher, arrays: &[ArrayRef], expected_len: usize) {@@ -407,6 +423,14 @@ fn do_hash_test_with_len(b: &mut Bencher, arrays: &[ArrayRef], expected_len: usi});}+/// A `Dictionary(Int32, Utf8)` with a distinct value for every row+fn distinct_dictionary_array(num_rows: usize) -> ArrayRef {+ let values =+ StringArray::from_iter_values((0..num_rows).map(|i| format!("group_{i}")));+ let keys = Int32Array::from_iter_values(0..num_rows as i32);+ Arc::new(DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values)).unwrap())+}+fn list_array(num_rows: usize) -> ArrayRef {let mut rng = make_rng();let elements_per_row = 5;diff --git a/datafusion/common/src/hash_utils.rs b/datafusion/common/src/hash_utils.rsindex 3a4ced2eaa..96d154f98a 100644--- a/datafusion/common/src/hash_utils.rs+++ b/datafusion/common/src/hash_utils.rs@@ -4,6 +4,8 @@//! Functionality used both on logical and physical plans+#[cfg(not(feature = "force_hash_collisions"))]+use crate::utils::compact_oversized_dictionary;use arrow::array::types::{IntervalDayTime, IntervalMonthDayNano};use arrow::array::*;#[cfg(not(feature = "force_hash_collisions"))]@@ -656,7 +658,11 @@ fn hash_dictionary<K: ArrowDictionaryKeyType>() -> Result<()> {// Hash each dictionary value once, and then use that computed// hash for each key value to avoid a potentially expensive- // redundant hashing for large dictionary elements (e.g. strings)+ // redundant hashing for large dictionary elements (e.g. strings).+ // A dictionary with many more values than rows (e.g. a slice) is first+ // reduced to the values its rows reference.+ let compacted = compact_oversized_dictionary(array)?;+ let array = compacted.as_ref().unwrap_or(array);let dict_values = array.values();let mut dict_hashes = vec![0; dict_values.len()];create_hashes([dict_values], random_state, &mut dict_hashes)?;@@ -671,6 +677,8 @@ fn hash_dictionary_with_child_hashing<K: ArrowDictionaryKeyType>(hashes_buffer: &mut [u64],multi_col: bool,) -> Result<()> {+ let compacted = compact_oversized_dictionary(array)?;+ let array = compacted.as_ref().unwrap_or(array);let dict_values = array.values();let mut dict_hashes = vec![0; dict_values.len()];child_hashing.create_hashes([dict_values], &mut dict_hashes)?;@@ -1836,6 +1844,91 @@ mod tests {assert_ne!(dict_hashes[0], dict_hashes[2]);}+ #[test]+ // Tests actual values of hashes, which are different if forcing collisions+ #[cfg(not(feature = "force_hash_collisions"))]+ fn create_hashes_for_dict_with_more_rows_than_the_key_type_holds() {+ fn check<K: ArrowDictionaryKeyType>(distinct_keys: usize) {+ let values: ArrayRef = Arc::new(StringArray::from_iter_values(+ (0..20_000).map(|i| format!("v{i}")),+ ));+ let keys: ScalarBuffer<K::Native> = (0..300)+ .map(|i| K::Native::usize_as(i % distinct_keys))+ .collect();+ let keys = PrimitiveArray::<K>::new(keys, None);+ let dict: ArrayRef =+ Arc::new(DictionaryArray::try_new(keys, values).unwrap());+ let decoded = arrow::compute::cast(&dict, &DataType::Utf8).unwrap();+ let random_state = RandomState::with_seed(0);++ let mut dict_hashes = vec![0; dict.len()];+ create_hashes([&dict], &random_state, &mut dict_hashes).unwrap();+ let mut decoded_hashes = vec![0; dict.len()];+ create_hashes([&decoded], &random_state, &mut decoded_hashes).unwrap();+ assert_eq!(dict_hashes, decoded_hashes);+ }+ check::<UInt8Type>(250);+ check::<Int8Type>(120);+ }++ #[test]+ // Tests actual values of hashes, which are different if forcing collisions+ #[cfg(not(feature = "force_hash_collisions"))]+ fn create_hashes_for_dict_does_not_copy_a_value_per_row() {+ // 64 copies of this value wouldn't fit in `i32` offsets+ let long = "x".repeat(34 * 1024 * 1024);+ let values: ArrayRef = Arc::new(StringArray::from_iter_values(+ (0..5000).map(|i| if i == 0 { long.as_str() } else { "short" }),+ ));+ let dict: ArrayRef = Arc::new(+ DictionaryArray::try_new(Int32Array::from(vec![0; 64]), values).unwrap(),+ );+ let single: ArrayRef = Arc::new(StringArray::from(vec![long.as_str()]));+ let random_state = RandomState::with_seed(0);++ let mut dict_hashes = vec![0; dict.len()];+ create_hashes([&dict], &random_state, &mut dict_hashes).unwrap();+ let mut single_hash = vec![0; 1];+ create_hashes([&single], &random_state, &mut single_hash).unwrap();+ assert_eq!(dict_hashes, vec![single_hash[0]; 64]);+ }++ #[test]+ // Tests actual values of hashes, which are different if forcing collisions+ #[cfg(not(feature = "force_hash_collisions"))]+ fn create_hashes_for_dict_slices_with_more_values_than_rows() {+ use crate::utils::MAX_DICTIONARY_VALUES_PER_ROW;++ let values: ArrayRef = Arc::new(StringArray::from_iter(+ (0..200).map(|i| (i != 2).then(|| format!("v{i}"))),+ ));+ let keys = Int8Array::from_iter((0..48).map(|i| (i % 7 != 1).then_some(i)));+ let dict: ArrayRef = Arc::new(DictionaryArray::try_new(keys, values).unwrap());+ let ints: ArrayRef = Arc::new(Int32Array::from_iter_values(0..48));+ let random_state = RandomState::with_seed(0);++ let mut single = vec![0; dict.len()];+ create_hashes([&dict], &random_state, &mut single).unwrap();+ let mut multi = vec![0; dict.len()];+ create_hashes([&ints, &dict], &random_state, &mut multi).unwrap();++ for (offset, len) in [(0, 2), (1, 2), (8, 1), (46, 2)] {+ let slice = dict.slice(offset, len);+ assert!(+ slice.as_any_dictionary().values().len()+ > MAX_DICTIONARY_VALUES_PER_ROW * len+ );+ let mut hashes = vec![0; len];+ create_hashes([&slice], &random_state, &mut hashes).unwrap();+ assert_eq!(hashes, single[offset..offset + len]);++ let ints = ints.slice(offset, len);+ let mut hashes = vec![0; len];+ create_hashes([&ints, &slice], &random_state, &mut hashes).unwrap();+ assert_eq!(hashes, multi[offset..offset + len]);+ }+ }+#[test]// Tests actual values of hashes, which are different if forcing collisions#[cfg(not(feature = "force_hash_collisions"))]diff --git a/datafusion/common/src/utils/mod.rs b/datafusion/common/src/utils/mod.rsindex 355db5c742..e09d703d91 100644--- a/datafusion/common/src/utils/mod.rs+++ b/datafusion/common/src/utils/mod.rs@@ -13,30 +13,35 @@ pub mod string_utils;use crate::assert_or_internal_err;use crate::error::{- _exec_datafusion_err, _exec_err, _internal_datafusion_err, _plan_datafusion_err,+ _exec_datafusion_err, _exec_err, _internal_datafusion_err, _internal_err,+ _plan_datafusion_err,};+use crate::hash_utils::RandomState;use crate::{Result, ScalarValue};use arrow::array::{Array, ArrayRef, FixedSizeListArray, LargeListArray, ListArray, OffsetSizeTrait,cast::AsArray,};use arrow::array::{- ArrowPrimitiveType, BooleanArray, Datum, GenericListArray, Int32Array, Int64Array,- MutableArrayData, PrimitiveArray, layout, make_array,+ ArrowPrimitiveType, BooleanArray, Datum, DictionaryArray, GenericListArray,+ Int32Array, Int64Array, MutableArrayData, PrimitiveArray, layout, make_array,};use arrow::array::{LargeListViewArray, ListViewArray};use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer};use arrow::compute::kernels::cmp::eq;use arrow::compute::kernels::length::length;-use arrow::compute::{SortColumn, SortOptions, nullif, partition};+use arrow::compute::{SortColumn, SortOptions, nullif, partition, take};use arrow::datatypes::{- ArrowNativeType, DataType, Field, Int32Type, Int64Type, Metadata, SchemaRef,+ ArrowDictionaryKeyType, ArrowNativeType, DataType, Field, Int32Type, Int64Type,+ Metadata, SchemaRef,};+use arrow::downcast_dictionary_array;+use arrow_select::dictionary::garbage_collect_any_dictionary;#[cfg(feature = "sql")]use sqlparser::{ast::Ident, dialect::GenericDialect, parser::Parser};use std::borrow::{Borrow, Cow};use std::cmp::{Ordering, min};-use std::collections::HashSet;+use std::collections::{HashMap, HashSet};use std::iter::repeat_n;use std::num::NonZero;use std::ops::Range;@@ -403,6 +408,92 @@ pub fn split_vec_min_alloc<T>(vec: &mut Vec<T>, n: usize) -> Vec<T> {}}+/// Applies `f` to the values of the dictionary `array` and keeps its keys.+///+/// Slices, filters and repartitioned batches keep the whole values array of+/// the dictionary they came from, so running `f` on every value can cost far+/// more than the batch has rows. `f` never runs on more than+/// [`MAX_DICTIONARY_VALUES_PER_ROW`] values per row: past that, it runs on the+/// values the rows reference instead.+///+/// `f` can also see values that no row references. If it fails, it runs once+/// more on only the referenced values, so it fails only when running it on+/// each row would.+pub fn map_dictionary_values<F>(array: &ArrayRef, mut f: F) -> Result<ArrayRef>+where+ F: FnMut(&ArrayRef) -> Result<ArrayRef>,+{+ let compacted = downcast_dictionary_array! {+ array => compact_oversized_dictionary(array)?.map(|d| Arc::new(d) as ArrayRef),+ dt => return _internal_err!("expected a dictionary array, got {dt}"),+ };+ let mut dictionary = compacted.unwrap_or_else(|| Arc::clone(array));+ let values = match f(dictionary.as_any_dictionary().values()) {+ Ok(values) => values,+ Err(e) => {+ let referenced =+ garbage_collect_any_dictionary(dictionary.as_any_dictionary())?;+ if referenced.as_any_dictionary().values().len()+ == dictionary.as_any_dictionary().values().len()+ {+ return Err(e);+ }+ dictionary = referenced;+ f(dictionary.as_any_dictionary().values())?+ }+ };+ #[expect(clippy::disallowed_methods)] // the values are bounded above+ Ok(dictionary.as_any_dictionary().with_values(values))+}++/// The most dictionary values per row that code processing every value of a+/// dictionary should handle. Past this, it first reduces the dictionary to the+/// values its rows reference. This is a bound, not a tuned break-even: too high+/// only gives up an improvement, while too low can make cheap per-value work+/// slower than processing every value.+pub const MAX_DICTIONARY_VALUES_PER_ROW: usize = 64;++/// Returns `None` when `array` has at most [`MAX_DICTIONARY_VALUES_PER_ROW`]+/// values per row, and otherwise [`compact_dictionary`] of it.+pub(crate) fn compact_oversized_dictionary<K: ArrowDictionaryKeyType>(+ array: &DictionaryArray<K>,+) -> Result<Option<DictionaryArray<K>>> {+ if array.values().len() <= MAX_DICTIONARY_VALUES_PER_ROW * array.len() {+ return Ok(None);+ }+ compact_dictionary(array).map(Some)+}++/// Returns a copy of `array` that holds each value its rows reference once.+/// This costs O(rows), and work over its values never covers more than the+/// values of `array`, however long they are or often they repeat.+pub fn compact_dictionary<K: ArrowDictionaryKeyType>(+ array: &DictionaryArray<K>,+) -> Result<DictionaryArray<K>> {+ let keys = array.keys();+ let mut new_keys =+ HashMap::with_capacity_and_hasher(keys.len(), RandomState::default());+ let mut referenced = Vec::with_capacity(keys.len());+ let remapped: ScalarBuffer<K::Native> = keys+ .iter()+ .map(|key| match key {+ Some(key) => *new_keys.entry(key.as_usize()).or_insert_with(|| {+ referenced.push(key);+ // There are at most as many distinct keys as `K` can hold+ K::Native::usize_as(referenced.len() - 1)+ }),+ None => K::Native::default(),+ })+ .collect();+ let values = take(+ array.values().as_ref(),+ &PrimitiveArray::<K>::from_iter_values(referenced),+ None,+ )?;+ let keys = PrimitiveArray::<K>::new(remapped, keys.nulls().cloned());+ Ok(DictionaryArray::try_new(keys, values)?)+}+#[cfg(test)]mod split_vec_min_alloc_tests {use super::split_vec_min_alloc;@@ -1485,14 +1576,15 @@ pub fn apply_parent_nulls(#[cfg(test)]mod tests {+ use std::cell::Cell;use std::collections::HashMap;use std::sync::Arc;use super::*;use crate::ScalarValue::Null;use arrow::{- array::{Float64Array, Int32Array, NullArray},- datatypes::Int32Type,+ array::{Float64Array, Int32Array, NullArray, StringArray},+ datatypes::{Int8Type, Int32Type, UInt8Type},};#[cfg(feature = "sql")]use sqlparser::ast::Ident;@@ -2090,4 +2182,156 @@ mod tests {fsl_values_row_number(-1, 2).unwrap_err();fsl_values_row_number(-1, 0).unwrap_err();}++ #[test]+ fn map_dictionary_values_bounds_values_per_row() -> Result<()> {+ let strings = (0..100)+ .map(|i| (i % 5 != 1).then(|| "x".repeat(i)))+ .collect::<Vec<_>>();+ let dict: DictionaryArray<Int32Type> =+ strings.iter().map(|s| s.as_deref()).collect();+ let dict: ArrayRef = Arc::new(dict);+ for (offset, len) in [(0, 100), (1, 1), (6, 1), (2, 1)] {+ let slice = dict.slice(offset, len);+ let mut seen = 0;+ let mapped = map_dictionary_values(&slice, |values| {+ seen = values.len();+ Ok(length(values)?)+ })?;+ assert!(+ seen <= MAX_DICTIONARY_VALUES_PER_ROW * len,+ "{seen} values for {len} rows"+ );+ let expected = length(&arrow::compute::cast(&slice, &DataType::Utf8)?)?;+ assert_eq!(&arrow::compute::cast(&mapped, &DataType::Int32)?, &expected);+ }+ Ok(())+ }++ #[test]+ fn map_dictionary_values_fails_only_on_referenced_values() -> Result<()> {+ let calls = Cell::new(0);+ let fail_on_bad = |values: &ArrayRef| {+ calls.set(calls.get() + 1);+ if values.as_string::<i32>().iter().any(|v| v == Some("bad")) {+ return _exec_err!("bad value");+ }+ Ok(length(values)?)+ };+ let values: ArrayRef =+ Arc::new(StringArray::from(vec!["ok", "bad", "fine", "bad"]));++ // "bad" at 1 is behind null keys only, and at 3 has no key. There are+ // more rows than Int8 keys can index, so the retry can't give each row+ // its own key.+ let keys = (0..200)+ .map(|i| [1, 0, 2][i % 3])+ .collect::<ScalarBuffer<i8>>();+ let nulls = NullBuffer::from_iter((0..200).map(|i| i % 3 != 0));+ let keys = PrimitiveArray::<Int8Type>::new(keys, Some(nulls));+ let dict: ArrayRef =+ Arc::new(DictionaryArray::try_new(keys, Arc::clone(&values))?);+ let mapped = map_dictionary_values(&dict, fail_on_bad)?;+ assert_eq!(+ mapped.data_type(),+ &DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32))+ );+ let expected = length(&arrow::compute::cast(&dict, &DataType::Utf8)?)?;+ assert_eq!(&arrow::compute::cast(&mapped, &DataType::Int32)?, &expected);+ assert_eq!(calls.get(), 2);++ // A row references "bad", so `f` must still fail, without a retry when+ // every value is referenced+ calls.set(0);+ let keys = PrimitiveArray::<Int8Type>::from(vec![0, 1, 2, 3]);+ let dict: ArrayRef = Arc::new(DictionaryArray::try_new(keys, values)?);+ map_dictionary_values(&dict, fail_on_bad).unwrap_err();+ assert_eq!(calls.get(), 1);+ Ok(())+ }++ #[test]+ fn map_dictionary_values_runs_on_each_referenced_value_once() -> Result<()> {+ let values: ArrayRef = Arc::new(StringArray::from_iter_values(+ (0..1000).map(|i| "x".repeat(i % 7)),+ ));+ let keys = Int32Array::from(vec![+ Some(7),+ Some(900),+ None,+ Some(7),+ Some(42),+ Some(900),+ Some(7),+ None,+ ]);+ let dict: ArrayRef = Arc::new(DictionaryArray::try_new(keys, values)?);+ let mut seen = 0;+ let mapped = map_dictionary_values(&dict, |values| {+ seen = values.len();+ Ok(length(values)?)+ })?;+ assert_eq!(seen, 3);+ let expected = length(&arrow::compute::cast(&dict, &DataType::Utf8)?)?;+ assert_eq!(&arrow::compute::cast(&mapped, &DataType::Int32)?, &expected);++ // No row references a value+ let all_null = Arc::new(DictionaryArray::<Int32Type>::new(+ Int32Array::from(vec![None; 8]),+ Arc::clone(dict.as_any_dictionary().values()),+ )) as ArrayRef;+ let mapped = map_dictionary_values(&all_null, |values| {+ seen = values.len();+ Ok(length(values)?)+ })?;+ assert_eq!(seen, 0);+ assert_eq!(mapped.null_count(), 8);+ Ok(())+ }++ // Arrow doesn't require the key type to be able to index every value, so+ // the row count can exceed what the key type holds.+ #[test]+ fn map_dictionary_values_with_more_rows_than_the_key_type_holds() -> Result<()> {+ fn check<K: ArrowDictionaryKeyType>(distinct_keys: usize) -> Result<()> {+ let values: ArrayRef = Arc::new(StringArray::from_iter_values(+ (0..20_000).map(|i| format!("v{i}")),+ ));+ let keys: ScalarBuffer<K::Native> = (0..300)+ .map(|i| K::Native::usize_as(i % distinct_keys))+ .collect();+ let keys = PrimitiveArray::<K>::new(keys, None);+ let dict: ArrayRef = Arc::new(DictionaryArray::try_new(keys, values)?);+ let mapped = map_dictionary_values(&dict, |values| Ok(Arc::clone(values)))?;+ assert_eq!(+ &arrow::compute::cast(&mapped, &DataType::Utf8)?,+ &arrow::compute::cast(&dict, &DataType::Utf8)?,+ );+ Ok(())+ }+ check::<UInt8Type>(250)?;+ check::<Int8Type>(120)+ }++ #[test]+ fn map_dictionary_values_does_not_copy_a_value_per_row() -> Result<()> {+ // 64 copies of this value wouldn't fit in `i32` offsets+ const LONG: usize = 34 * 1024 * 1024;+ let long = "x".repeat(LONG);+ let values: ArrayRef = Arc::new(StringArray::from_iter_values(+ (0..5000).map(|i| if i == 0 { long.as_str() } else { "short" }),+ ));+ let dict: ArrayRef = Arc::new(DictionaryArray::try_new(+ Int32Array::from(vec![0; 64]),+ values,+ )?);+ let mapped = map_dictionary_values(&dict, |values| Ok(length(values)?))?;+ assert_eq!(+ arrow::compute::cast(&mapped, &DataType::Int32)?+ .as_primitive::<Int32Type>()+ .values(),+ &[LONG as i32; 64]+ );+ Ok(())+ }}diff --git a/datafusion/functions-aggregate/src/array_agg.rs b/datafusion/functions-aggregate/src/array_agg.rsindex 6663c63b51..36d00524a8 100644--- a/datafusion/functions-aggregate/src/array_agg.rs+++ b/datafusion/functions-aggregate/src/array_agg.rs@@ -23,6 +23,7 @@ use arrow_select::dictionary::garbage_collect_any_dictionary;use datafusion_common::cast::as_list_array;use datafusion_common::hash_utils::{RandomState, create_hashes};use datafusion_common::scalar::copy_array_data;+use datafusion_common::utils::map_dictionary_values;use datafusion_common::utils::memory::estimate_memory_size;use datafusion_common::utils::proxy::HashTableAllocExt;use datafusion_common::utils::{SingleRowListArrayBuilder, offset_span};@@ -2048,11 +2049,11 @@ fn compact_payload(array: ArrayRef) -> Result<ArrayRef> {DataType::BinaryView => Ok(Arc::new(array.as_binary_view().gc())),DataType::Dictionary(_, _) => {let array = garbage_collect_any_dictionary(array.as_any_dictionary())?;- let dictionary = array.as_any_dictionary();// GC can return the original values if every entry is referenced.// Detach offset slices as well as any nested view/dictionary buffers.- let values = make_array(copy_array_data(&dictionary.values().to_data()));- Ok(dictionary.with_values(compact_payload(values)?))+ map_dictionary_values(&array, |values| {+ compact_payload(make_array(copy_array_data(&values.to_data())))+ })}data_type if data_type.is_nested() => {let data = array.to_data();diff --git a/datafusion/functions/src/core/getfield.rs b/datafusion/functions/src/core/getfield.rsindex d4f304cd32..1fc5629e6c 100644--- a/datafusion/functions/src/core/getfield.rs+++ b/datafusion/functions/src/core/getfield.rs@@ -108,6 +108,8 @@ fn extract_single_field(base: ColumnarValue, name: ScalarValue) -> Result<Column)})?;let field_col = apply_parent_nulls(field_col, values_struct.nulls())?;+ #[expect(clippy::disallowed_methods)]+ // picks a child column, no per-value workOk(ColumnarValue::Array(dict.with_values(field_col)))}(DataType::Map(_, _), key, _) => {diff --git a/datafusion/functions/src/regex/regexpreplace.rs b/datafusion/functions/src/regex/regexpreplace.rsindex fce153e9f8..d2151891ce 100644--- a/datafusion/functions/src/regex/regexpreplace.rs+++ b/datafusion/functions/src/regex/regexpreplace.rs@@ -21,7 +21,7 @@ use datafusion_common::cast::{};use datafusion_common::exec_err;use datafusion_common::plan_err;-use datafusion_common::utils::offset_span_len;+use datafusion_common::utils::{map_dictionary_values, offset_span_len};use datafusion_common::{Result, cast::as_generic_string_array, internal_err};use datafusion_expr::ColumnarValue;use datafusion_expr::function::Hint;@@ -175,18 +175,21 @@ impl ScalarUDFImpl for RegexpReplaceFunc {{if preserve_dictionary {let compacted;- let mut dict = array.as_any_dictionary();- if dict.values().len() > dict.keys().len() {+ let mut dict = array;+ if array.as_any_dictionary().values().len() > array.len() {// Filtered batches and shared Parquet dictionaries can have more// values than rows, so drop unreferenced values first.- compacted = garbage_collect_any_dictionary(dict)?;- dict = compacted.as_any_dictionary();+ compacted =+ garbage_collect_any_dictionary(array.as_any_dictionary())?;+ dict = &compacted;}- let mut inner: Vec<ColumnarValue> = Vec::with_capacity(args.len());- inner.push(ColumnarValue::Array(Arc::clone(dict.values())));- inner.extend_from_slice(&args[1..]);- let values_out = regexp_replace_func(&inner)?;- return Ok(ColumnarValue::Array(dict.with_values(values_out)));+ let result = map_dictionary_values(dict, |values| {+ let mut inner: Vec<ColumnarValue> = Vec::with_capacity(args.len());+ inner.push(ColumnarValue::Array(Arc::clone(values)));+ inner.extend_from_slice(&args[1..]);+ regexp_replace_func(&inner)+ })?;+ return Ok(ColumnarValue::Array(result));}args[0] = ColumnarValue::Array(arrow::compute::cast(array, &value_type)?);}diff --git a/datafusion/functions/src/string/ascii.rs b/datafusion/functions/src/string/ascii.rsindex 13c859ea7c..665893b606 100644--- a/datafusion/functions/src/string/ascii.rs+++ b/datafusion/functions/src/string/ascii.rs@@ -7,6 +7,7 @@ use arrow::array::{ArrayRef, AsArray, Int32Array, StringArrayType};use arrow::datatypes::DataType;use arrow::error::ArrowError;use datafusion_common::types::logical_string;+use datafusion_common::utils::map_dictionary_values;use datafusion_common::utils::take_function_args;use datafusion_common::{Result, ScalarValue, internal_err};use datafusion_expr::{@@ -177,9 +178,7 @@ pub fn ascii(args: &[ArrayRef]) -> Result<ArrayRef> {Ok(calculate_ascii(&string_array)?)}DataType::Dictionary(_, _) => {- let dictionary = args[0].as_any_dictionary();- let converted = ascii(&[Arc::clone(dictionary.values())])?;- Ok(dictionary.with_values(converted))+ map_dictionary_values(&args[0], |values| ascii(&[Arc::clone(values)]))}_ => internal_err!("Unsupported data type"),}diff --git a/datafusion/functions/src/string/btrim.rs b/datafusion/functions/src/string/btrim.rsindex ec73ae1db7..38dbb7ef50 100644--- a/datafusion/functions/src/string/btrim.rs+++ b/datafusion/functions/src/string/btrim.rs@@ -4,9 +4,10 @@use crate::string::common::*;use crate::utils::make_scalar_function;-use arrow::array::{ArrayRef, AsArray};+use arrow::array::ArrayRef;use arrow::datatypes::DataType;use datafusion_common::types::logical_string;+use datafusion_common::utils::map_dictionary_values;use datafusion_common::{Result, exec_err};use datafusion_expr::function::Hint;use datafusion_expr::{@@ -30,9 +31,7 @@ fn btrim(args: &[ArrayRef]) -> Result<ArrayRef> {DataType::LargeUtf8 => general_trim::<i64, TrimBoth>(&args, false),DataType::Utf8View => general_trim::<i32, TrimBoth>(&args, true),DataType::Dictionary(_, _) => {- let dictionary = args[0].as_any_dictionary();- let trimmed = btrim(&[Arc::clone(dictionary.values())])?;- Ok(dictionary.with_values(trimmed))+ map_dictionary_values(&args[0], |values| btrim(&[Arc::clone(values)]))}other => exec_err!("Unsupported data type {other:?} for function btrim, expected Utf8, LargeUtf8 or Utf8View."diff --git a/datafusion/functions/src/string/common.rs b/datafusion/functions/src/string/common.rsindex 9f22c12226..32dc9b94c1 100644--- a/datafusion/functions/src/string/common.rs+++ b/datafusion/functions/src/string/common.rs@@ -11,13 +11,14 @@ use crate::strings::{StringViewArrayBuilder, substr_view,};use arrow::array::{- Array, ArrayRef, AsArray, GenericStringArray, NullBufferBuilder, OffsetSizeTrait,+ Array, ArrayRef, GenericStringArray, NullBufferBuilder, OffsetSizeTrait,StringViewArray, new_null_array,};use arrow::buffer::{Buffer, NullBuffer, OffsetBuffer, ScalarBuffer};use arrow::datatypes::DataType;use datafusion_common::Result;use datafusion_common::cast::{as_generic_string_array, as_string_view_array};+use datafusion_common::utils::map_dictionary_values;use datafusion_common::utils::{offset_span, offset_span_len};use datafusion_common::{ScalarValue, exec_err};use datafusion_expr::ColumnarValue;@@ -528,9 +529,9 @@ fn case_conversion_dictionary(lower: bool,name: &str,) -> Result<ArrayRef> {- let dictionary = array.as_any_dictionary();- let converted = case_conversion_columnar_array(dictionary.values(), lower, name)?;- Ok(dictionary.with_values(converted))+ map_dictionary_values(array, |values| {+ case_conversion_columnar_array(values, lower, name)+ })}fn case_conversion_array<O: OffsetSizeTrait>(diff --git a/datafusion/functions/src/string/ltrim.rs b/datafusion/functions/src/string/ltrim.rsindex 9c635cc8dc..1f6f21bee5 100644--- a/datafusion/functions/src/string/ltrim.rs+++ b/datafusion/functions/src/string/ltrim.rs@@ -2,13 +2,14 @@// 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-use arrow::array::{ArrayRef, AsArray};+use arrow::array::ArrayRef;use arrow::datatypes::DataType;use std::sync::Arc;use crate::string::common::*;use crate::utils::make_scalar_function;use datafusion_common::types::logical_string;+use datafusion_common::utils::map_dictionary_values;use datafusion_common::{Result, exec_err};use datafusion_expr::function::Hint;use datafusion_expr::{@@ -31,9 +32,7 @@ fn ltrim(args: &[ArrayRef]) -> Result<ArrayRef> {DataType::LargeUtf8 => general_trim::<i64, TrimLeft>(&args, false),DataType::Utf8View => general_trim::<i32, TrimLeft>(&args, true),DataType::Dictionary(_, _) => {- let dictionary = args[0].as_any_dictionary();- let trimmed = ltrim(&[Arc::clone(dictionary.values())])?;- Ok(dictionary.with_values(trimmed))+ map_dictionary_values(&args[0], |values| ltrim(&[Arc::clone(values)]))}other => exec_err!("Unsupported data type {other:?} for function ltrim, expected Utf8, LargeUtf8 or Utf8View."diff --git a/datafusion/functions/src/string/rtrim.rs b/datafusion/functions/src/string/rtrim.rsindex a93bd592c5..830f6a3c6f 100644--- a/datafusion/functions/src/string/rtrim.rs+++ b/datafusion/functions/src/string/rtrim.rs@@ -2,13 +2,14 @@// 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-use arrow::array::{ArrayRef, AsArray};+use arrow::array::ArrayRef;use arrow::datatypes::DataType;use std::sync::Arc;use crate::string::common::*;use crate::utils::make_scalar_function;use datafusion_common::types::logical_string;+use datafusion_common::utils::map_dictionary_values;use datafusion_common::{Result, exec_err};use datafusion_expr::function::Hint;use datafusion_expr::{@@ -31,9 +32,7 @@ fn rtrim(args: &[ArrayRef]) -> Result<ArrayRef> {DataType::LargeUtf8 => general_trim::<i64, TrimRight>(&args, false),DataType::Utf8View => general_trim::<i32, TrimRight>(&args, true),DataType::Dictionary(_, _) => {- let dictionary = args[0].as_any_dictionary();- let trimmed = rtrim(&[Arc::clone(dictionary.values())])?;- Ok(dictionary.with_values(trimmed))+ map_dictionary_values(&args[0], |values| rtrim(&[Arc::clone(values)]))}other => exec_err!("Unsupported data type {other:?} for function rtrim, expected Utf8, LargeUtf8 or Utf8View."diff --git a/datafusion/functions/src/unicode/character_length.rs b/datafusion/functions/src/unicode/character_length.rsindex 01d4df22d6..50b3b63426 100644--- a/datafusion/functions/src/unicode/character_length.rs+++ b/datafusion/functions/src/unicode/character_length.rs@@ -12,6 +12,7 @@ use arrow::array::{use arrow::datatypes::{ArrowNativeType, DataType, Int32Type, Int64Type};use datafusion_common::Result;use datafusion_common::types::{NativeType, logical_string};+use datafusion_common::utils::map_dictionary_values;use datafusion_expr::{Coercion, ColumnarValue, Documentation, EncodingPreservation, ScalarFunctionArgs,ScalarUDFImpl, Signature, TypeSignatureClass, Volatility,@@ -111,11 +112,9 @@ fn character_length(args: &[ArrayRef]) -> Result<ArrayRef> {let string_array = args[0].as_string_view();character_length_general::<Int32Type, _>(&string_array)}- DataType::Dictionary(_, _) => {- let dictionary = args[0].as_any_dictionary();- let converted = character_length(&[Arc::clone(dictionary.values())])?;- Ok(dictionary.with_values(converted))- }+ DataType::Dictionary(_, _) => map_dictionary_values(&args[0], |values| {+ character_length(&[Arc::clone(values)])+ }),_ => unreachable!("CharacterLengthFunc"),}}diff --git a/datafusion/functions/src/unicode/initcap.rs b/datafusion/functions/src/unicode/initcap.rsindex 90fd580ae7..6d992b8edb 100644--- a/datafusion/functions/src/unicode/initcap.rs+++ b/datafusion/functions/src/unicode/initcap.rs@@ -4,13 +4,14 @@use std::sync::Arc;-use arrow::array::{Array, ArrayRef, AsArray, GenericStringArray, OffsetSizeTrait};+use arrow::array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait};use arrow::buffer::Buffer;use arrow::datatypes::DataType;use crate::strings::{GenericStringArrayBuilder, StringViewArrayBuilder};use datafusion_common::cast::{as_generic_string_array, as_string_view_array};use datafusion_common::types::logical_string;+use datafusion_common::utils::map_dictionary_values;use datafusion_common::utils::offset_span_len;use datafusion_common::{Result, ScalarValue, exec_err};use datafusion_expr::{@@ -128,11 +129,7 @@ fn initcap_array(array: &ArrayRef) -> Result<ArrayRef> {DataType::Utf8 => initcap::<i32>(&[Arc::clone(array)]),DataType::LargeUtf8 => initcap::<i64>(&[Arc::clone(array)]),DataType::Utf8View => initcap_utf8view(&[Arc::clone(array)]),- DataType::Dictionary(_, _) => {- let dictionary = array.as_any_dictionary();- let converted = initcap_array(dictionary.values())?;- Ok(dictionary.with_values(converted))- }+ DataType::Dictionary(_, _) => map_dictionary_values(array, initcap_array),other => {exec_err!("Unsupported data type {other:?} for function `initcap`")}diff --git a/datafusion/functions/src/unicode/reverse.rs b/datafusion/functions/src/unicode/reverse.rsindex 0b2861cb84..697a2179b3 100644--- a/datafusion/functions/src/unicode/reverse.rs+++ b/datafusion/functions/src/unicode/reverse.rs@@ -13,6 +13,7 @@ use arrow::array::{Array, ArrayRef, AsArray, StringArrayType};use arrow::datatypes::DataType;use datafusion_common::Result;use datafusion_common::types::logical_string;+use datafusion_common::utils::map_dictionary_values;use datafusion_common::utils::offset_span_len;use datafusion_expr::{Coercion, ColumnarValue, Documentation, EncodingPreservation, ScalarFunctionArgs,@@ -115,9 +116,7 @@ fn reverse(args: &[ArrayRef]) -> Result<ArrayRef> {StringViewArrayBuilder::with_capacity(len),),DataType::Dictionary(_, _) => {- let dictionary = args[0].as_any_dictionary();- let converted = reverse(&[Arc::clone(dictionary.values())])?;- Ok(dictionary.with_values(converted))+ map_dictionary_values(&args[0], |values| reverse(&[Arc::clone(values)]))}_ => unreachable!("Reverse can only be applied to Utf8View, Utf8 and LargeUtf8 types"diff --git a/datafusion/functions/src/unicode/substr.rs b/datafusion/functions/src/unicode/substr.rsindex 1a57e883ad..dcd40b7416 100644--- a/datafusion/functions/src/unicode/substr.rs+++ b/datafusion/functions/src/unicode/substr.rs@@ -18,6 +18,7 @@ use datafusion_common::cast::as_int64_array;use datafusion_common::types::{NativeType, logical_int32, logical_int64, logical_string,};+use datafusion_common::utils::map_dictionary_values;use datafusion_common::{Result, exec_err};use datafusion_expr::{Coercion, ColumnarValue, Documentation, EncodingPreservation, ScalarFunctionArgs,@@ -137,17 +138,13 @@ impl ScalarUDFImpl for SubstrFunc {.all(|v| matches!(v, ColumnarValue::Scalar(_))){// Fast path: substr the N_distinct values, reuse the keys.- let dict = array.as_any_dictionary();- let values = Arc::clone(dict.values());- let mut inner = Vec::with_capacity(args.args.len());- inner.push(ColumnarValue::Array(values));- inner.extend_from_slice(&args.args[1..]);- // Values that no valid row references can still fail (e.g. a- // negative length), so fall back to the row-wise path on error.- if let Ok(new_values) = make_scalar_function(substr, vec![])(&inner) {- let new_values = new_values.into_array(dict.values().len())?;- return Ok(ColumnarValue::Array(dict.with_values(new_values)));- }+ let result = map_dictionary_values(array, |values| {+ let mut inner = Vec::with_capacity(args.args.len());+ inner.push(ColumnarValue::Array(Arc::clone(values)));+ inner.extend_from_slice(&args.args[1..]);+ make_scalar_function(substr, vec![])(&inner)?.into_array(values.len())+ })?;+ return Ok(ColumnarValue::Array(result));}make_scalar_function(substr, vec![])(&args.args)diff --git a/datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rs b/datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rsindex 0c7d5f123a..dcee4fc334 100644--- a/datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rs+++ b/datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rs@@ -12,7 +12,9 @@ use arrow::array::{};use arrow::compute::{filter_record_batch, prep_null_mask_filter};use arrow::datatypes::SchemaRef;+use arrow::downcast_dictionary_array;use arrow::record_batch::RecordBatch;+use datafusion_common::utils::compact_dictionary;use datafusion_common::{Result, internal_err};use datafusion_execution::memory_pool::proxy::VecAllocExt;use datafusion_expr::{AggregateMetrics, EmitTo, GroupsAccumulator};@@ -318,7 +320,7 @@ impl<AggrMode> AggregateHashTable<AggrMode> {}};- let batch = output.next_batch(batch_size);+ let batch = output.next_batch(batch_size)?;if output.is_exhausted() {self.state = AggregateHashTableState::Done;} else {@@ -641,16 +643,19 @@ impl MaterializedAggregateOutput {Self { batch, offset: 0 }}- pub(super) fn next_batch(&mut self, batch_size: usize) -> Option<RecordBatch> {+ pub(super) fn next_batch(+ &mut self,+ batch_size: usize,+ ) -> Result<Option<RecordBatch>> {debug_assert!(batch_size > 0);if self.is_exhausted() {- return None;+ return Ok(None);}let length = batch_size.min(self.batch.num_rows() - self.offset);let batch = self.batch.slice(self.offset, length);self.offset += length;- Some(batch)+ compact_dictionaries(batch).map(Some)}pub(super) fn is_exhausted(&self) -> bool {@@ -662,6 +667,36 @@ impl MaterializedAggregateOutput {}}+/// Gives each dictionary column of an emitted slice only the values its rows+/// reference, when it has more values than rows. A slice otherwise keeps the+/// values of every group, so work after this operator would grow with the group+/// count. Each group is emitted once, so this costs O(rows) overall.+fn compact_dictionaries(batch: RecordBatch) -> Result<RecordBatch> {+ let rows = batch.num_rows();+ let oversized = |column: &ArrayRef| {+ column+ .as_any_dictionary_opt()+ .is_some_and(|dictionary| dictionary.values().len() > rows)+ };+ if !batch.columns().iter().any(oversized) {+ return Ok(batch);+ }+ let columns = batch+ .columns()+ .iter()+ .map(|column| {+ if !oversized(column) {+ return Ok(Arc::clone(column));+ }+ downcast_dictionary_array! {+ column => Ok(Arc::new(compact_dictionary(column)?) as ArrayRef),+ dt => internal_err!("expected a dictionary array, got {dt}"),+ }+ })+ .collect::<Result<Vec<_>>>()?;+ Ok(RecordBatch::try_new(batch.schema(), columns)?)+}+/// Compacts row-aligned group indices using an aggregate filter.////// Returns `None` when every row is selected so callers can reuse the input@@ -919,8 +954,16 @@ impl AggregateHashTableState {mod tests {use std::sync::Arc;- use arrow::array::{Array, BooleanArray, Int32Array, Int64Array};- use arrow::datatypes::{DataType, Field, Schema};+ use arrow::array::{+ Array, BooleanArray, DictionaryArray, Int32Array, Int64Array, PrimitiveArray,+ StringArray,+ };+ use arrow::buffer::ScalarBuffer;+ use arrow::compute::cast;+ use arrow::datatypes::{+ ArrowDictionaryKeyType, ArrowNativeType, DataType, Field, Int8Type, Schema,+ UInt8Type,+ };use datafusion_functions_aggregate::sum::sum_udaf;use datafusion_physical_expr::aggregate::AggregateExprBuilder;use datafusion_physical_expr::expressions::Column;@@ -963,15 +1006,88 @@ mod tests {)?;let mut output = MaterializedAggregateOutput::new(batch);- assert_eq!(int32_values(&output.next_batch(2).unwrap(), 0), vec![1, 2]);- assert_eq!(int32_values(&output.next_batch(2).unwrap(), 0), vec![3, 4]);- assert_eq!(int32_values(&output.next_batch(2).unwrap(), 0), vec![5]);- assert!(output.next_batch(2).is_none());+ assert_eq!(int32_values(&output.next_batch(2)?.unwrap(), 0), vec![1, 2]);+ assert_eq!(int32_values(&output.next_batch(2)?.unwrap(), 0), vec![3, 4]);+ assert_eq!(int32_values(&output.next_batch(2)?.unwrap(), 0), vec![5]);+ assert!(output.next_batch(2)?.is_none());assert!(output.is_exhausted());Ok(())}+ fn dictionary_batch<K: ArrowDictionaryKeyType>(+ keys: PrimitiveArray<K>,+ num_values: usize,+ ) -> Result<RecordBatch> {+ let values: ArrayRef = Arc::new(StringArray::from_iter_values(+ (0..num_values).map(|i| format!("v{i}")),+ ));+ let dictionary: ArrayRef = Arc::new(DictionaryArray::try_new(keys, values)?);+ let schema = Arc::new(Schema::new(vec![Field::new(+ "group_col",+ dictionary.data_type().clone(),+ true,+ )]));+ Ok(RecordBatch::try_new(schema, vec![dictionary])?)+ }++ fn num_dictionary_values(batch: &RecordBatch) -> usize {+ batch.column(0).as_any_dictionary().values().len()+ }++ fn decoded(batch: &RecordBatch) -> Result<ArrayRef> {+ Ok(cast(batch.column(0), &DataType::Utf8)?)+ }++ #[test]+ fn next_batch_gives_a_slice_only_the_dictionary_values_it_references() -> Result<()> {+ // Multi-column group keys repeat dictionary keys, and a group can be NULL+ let keys =+ Int32Array::from(vec![Some(5), Some(5), None, Some(7), Some(1), Some(9)]);+ let batch = dictionary_batch(keys, 20)?;+ let mut output = MaterializedAggregateOutput::new(batch.clone());++ let first = output.next_batch(4)?.unwrap();+ assert_eq!(num_dictionary_values(&first), 2);+ assert_eq!(&decoded(&first)?, &decoded(&batch.slice(0, 4))?);++ let second = output.next_batch(4)?.unwrap();+ assert_eq!(num_dictionary_values(&second), 2);+ assert_eq!(&decoded(&second)?, &decoded(&batch.slice(4, 2))?);+ Ok(())+ }++ #[test]+ fn next_batch_keeps_a_dictionary_with_no_more_values_than_rows() -> Result<()> {+ let batch = dictionary_batch(Int32Array::from(vec![0, 1, 2, 3]), 4)?;+ let mut output = MaterializedAggregateOutput::new(batch.clone());+ let slice = output.next_batch(4)?.unwrap();+ assert!(Arc::ptr_eq(+ slice.column(0).as_any_dictionary().values(),+ batch.column(0).as_any_dictionary().values(),+ ));+ Ok(())+ }++ // Arrow doesn't require the key type to be able to index every value, so+ // a slice can have more rows than the key type holds.+ #[test]+ fn next_batch_with_more_rows_than_the_key_type_holds() -> Result<()> {+ fn check<K: ArrowDictionaryKeyType>(distinct_keys: usize) -> Result<()> {+ let keys: ScalarBuffer<K::Native> = (0..300)+ .map(|i| K::Native::usize_as(i % distinct_keys))+ .collect();+ let batch = dictionary_batch(PrimitiveArray::<K>::new(keys, None), 1000)?;+ let mut output = MaterializedAggregateOutput::new(batch.clone());+ let slice = output.next_batch(300)?.unwrap();+ assert_eq!(num_dictionary_values(&slice), distinct_keys);+ assert_eq!(&decoded(&slice)?, &decoded(&batch)?);+ Ok(())+ }+ check::<UInt8Type>(250)?;+ check::<Int8Type>(120)+ }+#[test]fn convert_to_state_preserves_rows_and_metrics() -> Result<()> {let schema = Arc::new(Schema::new(vec![diff --git a/datafusion/spark/src/function/math/hex.rs b/datafusion/spark/src/function/math/hex.rsindex e6f450ecdc..fd35175c0f 100644--- a/datafusion/spark/src/function/math/hex.rs+++ b/datafusion/spark/src/function/math/hex.rs@@ -6,16 +6,14 @@ use std::str::from_utf8_unchecked;use std::sync::Arc;use arrow::array::{Array, ArrayAccessor, ArrayRef, StringArray, StringBuilder};+use arrow::array::{as_largestring_array, as_string_array};use arrow::buffer::{Buffer, OffsetBuffer};use arrow::datatypes::DataType;-use arrow::{- array::{as_dictionary_array, as_largestring_array, as_string_array},- datatypes::Int32Type,-};use datafusion_common::cast::as_large_binary_array;use datafusion_common::cast::as_string_view_array;use datafusion_common::types::{NativeType, logical_int64, logical_string};use datafusion_common::utils::hex::{HexCase, ToHex, encode_bytes_into};+use datafusion_common::utils::map_dictionary_values;use datafusion_common::utils::take_function_args;use datafusion_common::{DataFusionError,@@ -258,48 +256,45 @@ pub fn compute_hex();}- let dict = as_dictionary_array::<Int32Type>(&array);- let dict_values = dict.values();-- let encoded_values = match dict_values.data_type() {- DataType::Int64 => {- let arr = as_int64_array(dict_values)?;- hex_encode_int64(arr.iter(), arr.len())?- }- DataType::Utf8 => {- let arr = as_string_array(dict_values);- hex_encode_bytes(&arr, lowercase)?- }- DataType::LargeUtf8 => {- let arr = as_largestring_array(dict_values);- hex_encode_bytes(&arr, lowercase)?- }- DataType::Utf8View => {- let arr = as_string_view_array(dict_values)?;- hex_encode_bytes(&arr, lowercase)?- }- DataType::Binary => {- let arr = as_binary_array(dict_values)?;- hex_encode_bytes(&arr, lowercase)?- }- DataType::LargeBinary => {- let arr = as_large_binary_array(dict_values)?;- hex_encode_bytes(&arr, lowercase)?- }- DataType::FixedSizeBinary(_) => {- let arr = as_fixed_size_binary_array(dict_values)?;- hex_encode_bytes(&arr, lowercase)?- }- _ => {- return exec_err!(- "hex got an unexpected argument type: {}",- dict_values.data_type()- );- }- };-- let new_dict = dict.with_values(encoded_values);- Ok(ColumnarValue::Array(Arc::new(new_dict)))+ let encoded = map_dictionary_values(array, |dict_values| {+ Ok(match dict_values.data_type() {+ DataType::Int64 => {+ let arr = as_int64_array(dict_values)?;+ hex_encode_int64(arr.iter(), arr.len())?+ }+ DataType::Utf8 => {+ let arr = as_string_array(dict_values);+ hex_encode_bytes(&arr, lowercase)?+ }+ DataType::LargeUtf8 => {+ let arr = as_largestring_array(dict_values);+ hex_encode_bytes(&arr, lowercase)?+ }+ DataType::Utf8View => {+ let arr = as_string_view_array(dict_values)?;+ hex_encode_bytes(&arr, lowercase)?+ }+ DataType::Binary => {+ let arr = as_binary_array(dict_values)?;+ hex_encode_bytes(&arr, lowercase)?+ }+ DataType::LargeBinary => {+ let arr = as_large_binary_array(dict_values)?;+ hex_encode_bytes(&arr, lowercase)?+ }+ DataType::FixedSizeBinary(_) => {+ let arr = as_fixed_size_binary_array(dict_values)?;+ hex_encode_bytes(&arr, lowercase)?+ }+ _ => {+ return exec_err!(+ "hex got an unexpected argument type: {}",+ dict_values.data_type()+ );+ }+ })+ })?;+ Ok(ColumnarValue::Array(encoded))}_ => exec_err!("hex got an unexpected argument type: {}", array.data_type()),},diff --git a/datafusion/sqllogictest/test_files/dictionary.slt b/datafusion/sqllogictest/test_files/dictionary.sltindex ca87203d1d..5809e39535 100644--- a/datafusion/sqllogictest/test_files/dictionary.slt+++ b/datafusion/sqllogictest/test_files/dictionary.slt@@ -714,3 +714,92 @@ NULLstatement okDROP TABLE dict_nulls_order;++# Aggregate output is emitted in `batch_size` slices of one array that holds+# every group's dictionary value. Hashing the slices and running string+# functions on them must give the same results as on the decoded values.+statement ok+set datafusion.execution.batch_size = 2;++statement ok+set datafusion.execution.target_partitions = 2;++statement ok+create table padded_groups as+select arrow_cast(+ case when value % 13 = 0 then null else ' Grp' || (value % 400) || ' ' end,+ 'Dictionary(Int32, Utf8)'+) as p+from generate_series(1, 1000);++query I+select count(*) from (select p from padded_groups group by p);+----+401++# Each function on the grouped dictionary must match the same function on the+# decoded value. These are projected, not filtered on: a filter on the group+# key is pushed below the aggregate, where a batch has very few values.+query B+select distinct+ lower(p) is distinct from lower(u)+ or upper(p) is distinct from upper(u)+ or reverse(p) is distinct from reverse(u)+ or character_length(p) is distinct from character_length(u)+ or ascii(p) is distinct from ascii(u)+ or initcap(p) is distinct from initcap(u)+ or btrim(p) is distinct from btrim(u)+ or ltrim(p) is distinct from ltrim(u)+ or rtrim(p) is distinct from rtrim(u)+from (+ select p, arrow_cast(p, 'Utf8') as u+ from (select p from padded_groups group by p)+);+----+false++query T+select distinct arrow_typeof(lower(p)) from (select p from padded_groups group by p);+----+Dictionary(Int32, Utf8)++statement ok+drop table padded_groups;++# A filter keeps the whole dictionary, so these batches have many more+# dictionary values than rows.+statement ok+set datafusion.execution.batch_size = 8192;++statement ok+create table filtered_groups as+select value as id, arrow_cast(' Grp' || (value % 400) || ' ', 'Dictionary(Int32, Utf8)') as p+from generate_series(1, 1000);++query B+select distinct+ lower(p) is distinct from lower(u)+ or upper(p) is distinct from upper(u)+ or reverse(p) is distinct from reverse(u)+ or character_length(p) is distinct from character_length(u)+ or ascii(p) is distinct from ascii(u)+ or initcap(p) is distinct from initcap(u)+ or btrim(p) is distinct from btrim(u)+ or ltrim(p) is distinct from ltrim(u)+ or rtrim(p) is distinct from rtrim(u)+from (+ select p, arrow_cast(p, 'Utf8') as u from filtered_groups where id % 200 = 0+);+----+false++statement ok+drop table filtered_groups;++# The SLT runner sets `target_partitions` to 4 instead of using the default, so+# reset it explicitly.+statement ok+set datafusion.execution.target_partitions = 4;++statement ok+reset datafusion.execution.batch_size;