This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12071-770393221c3859a3df489372672331aaffa090a7 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit a1e8df8214ee95f9f0aa14c7efaebeefe7ada69d Author: Alfonso Subiotto Marqués <[email protected]> AuthorDate: Fri Nov 6 01:53:03 2026 +0000 perf(arrow-ipc): find dictionary fields without collecting the schema (#12071) # Which issue does this PR close? - Closes apache/arrow-rs#12069 # Rationale for this change For every dictionary batch, `get_dictionary_values` calls `Schema::fields_with_dict_id`, which collects every nested field in the schema into a `Vec` and then keeps only the first match. Reading a stream with many dictionaries pays for that once per dictionary. # What changes are included in this PR? A private helper walks the schema in the same pre-order and returns the first match, without allocating. # Are these changes tested? A new test checks that the helper matches `Schema::fields_with_dict_id(id).first()` across nested types, duplicate IDs, and a `dict_id` on a non-dictionary field. `StreamReader/dictionaries` from #12070, local run (Apple M2 Max, under some load): | distinct values | layout | main | this PR | speedup | | --- | --- | --- | --- | --- | | 4 | flat | 21.3 ms | 4.99 ms | 4.3x | | 4 | list | 76.0 ms | 6.98 ms | 10.9x | | 4096 | flat | 33.0 ms | 16.1 ms | 2.0x | | 4096 | list | 86.5 ms | 18.0 ms | 4.8x | # Are there any significant user-facing changes? No. The implementation and test were written with AI assistance (Claude) and reviewed by me. --------- Co-authored-by: RIchard Baah <[email protected]> --- arrow-ipc/src/reader.rs | 98 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/arrow-ipc/src/reader.rs b/arrow-ipc/src/reader.rs index ea3026319e..83b8b5d85e 100644 --- a/arrow-ipc/src/reader.rs +++ b/arrow-ipc/src/reader.rs @@ -902,6 +902,41 @@ fn update_dictionaries( Ok(()) } +/// Returns the first field with dictionary `id`, in [`Schema::fields_with_dict_id`] order. +/// +/// Unlike that method this stops at the first match without collecting every nested field, +/// which is significant when a stream has many dictionary batches. +fn first_field_with_dict_id(fields: &Fields, id: i64) -> Option<&Field> { + fields + .iter() + .find_map(|field| field_with_dict_id(field, id)) +} + +fn field_with_dict_id(field: &Field, id: i64) -> Option<&Field> { + #[expect(deprecated)] + let dict_id = field.dict_id(); + if dict_id == Some(id) { + return Some(field); + } + child_field_with_dict_id(field.data_type(), id) +} + +fn child_field_with_dict_id(data_type: &DataType, id: i64) -> Option<&Field> { + match data_type { + DataType::Struct(fields) => first_field_with_dict_id(fields, id), + DataType::Union(fields, _) => fields.iter().find_map(|(_, f)| field_with_dict_id(f, id)), + DataType::List(field) + | DataType::LargeList(field) + | DataType::ListView(field) + | DataType::LargeListView(field) + | DataType::FixedSizeList(field, _) + | DataType::Map(field, _) + | DataType::RunEndEncoded(_, field) => field_with_dict_id(field, id), + DataType::Dictionary(_, value_type) => child_field_with_dict_id(value_type, id), + _ => None, + } +} + /// Given a dictionary batch IPC message/body along with the full state of a /// stream including schema, dictionary cache, metadata, and other flags, this /// function will parse the buffer into an array of dictionary values. @@ -915,9 +950,7 @@ fn get_dictionary_values( skip_validation: UnsafeFlag, ) -> Result<ArrayRef, ArrowError> { let id = batch.id(); - #[expect(deprecated)] - let fields_using_this_dictionary = schema.fields_with_dict_id(id); - let first_field = fields_using_this_dictionary.first().ok_or_else(|| { + let first_field = first_field_with_dict_id(schema.fields(), id).ok_or_else(|| { ArrowError::InvalidArgumentError(format!("dictionary id {id} not found in schema")) })?; @@ -3869,6 +3902,65 @@ mod tests { ); } + #[test] + #[expect(deprecated)] + fn test_first_field_with_dict_id_matches_fields_with_dict_id() { + let dict = |name: &str, id: i64| { + let data_type = + DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)); + Arc::new(Field::new_dict(name, data_type, true, id, false)) + }; + let dict_of_list_of_dict = |name: &str, outer_id: i64, inner_id: i64| { + let inner = DataType::List(dict("inner", inner_id)); + let data_type = DataType::Dictionary(Box::new(DataType::Int32), Box::new(inner)); + Field::new_dict(name, data_type, true, outer_id, false) + }; + // Duplicate IDs check that the first match in pre-order wins. + let schema = Schema::new(vec![ + // dict_id is ignored on non-dictionary fields. + Field::new_dict("not_a_dictionary", DataType::Utf8, true, 3, false), + Field::new_struct( + "struct", + vec![dict("struct.a", 1), dict("struct.b", 2)], + true, + ), + Field::new( + "union", + DataType::Union( + UnionFields::try_new(vec![0, 1], vec![dict("union.a", 3), dict("union.b", 1)]) + .unwrap(), + UnionMode::Dense, + ), + true, + ), + Field::new("list", DataType::List(dict("list.item", 4)), true), + Field::new_map( + "map", + "entries", + dict("key", 5), + dict("value", 2), + false, + true, + ), + Field::new( + "ree", + DataType::RunEndEncoded( + Arc::new(Field::new("run_ends", DataType::Int32, false)), + dict("ree.values", 6), + ), + true, + ), + dict_of_list_of_dict("dict_of_list_of_dict", 7, 8), + dict_of_list_of_dict("same_outer_and_inner_id", 9, 9), + ]); + + for id in -1..=10 { + let expected = schema.fields_with_dict_id(id).first().map(|f| f.name()); + let actual = first_field_with_dict_id(schema.fields(), id).map(|f| f.name()); + assert_eq!(actual, expected, "dictionary id {id}"); + } + } + #[test] fn test_same_dict_id_without_preserve() { let batch = RecordBatch::try_new(
