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-11533-4dc6bc25f351dd0b8474339ca342d488723f1368 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit d883cb81305ead59f1f1c11cf90ad13686130fed Author: Jeffrey Vo <[email protected]> AuthorDate: Wed Oct 14 11:15:50 2026 +0000 fix: `UnionFields::from_iter` should validate input (#11533) # Which issue does this PR close? <!-- It is not necessary to raise an issue, but it is encouraged if it is a non-trivial enhancement/bug. If this PR relates to an existing issue, but does not fully resolve it, please change the wording below from "Closes #NNN" to "Relates to #NNN" or "Part of #NNN" to avoid false closing an issue. --> - None # Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please try to explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. --> Make it impossible to create an invalid `UnionFields` via safe APIs. This makes it easier for downstream uses of `UnionFields` (in other parts of the codebase), since they can rely on the data being valid, instead of needing to check each time. # What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Make sure `UnionFields::from_iter` validates its input before construction a `UnionFields`, similar to `UnionFields::try_new` # Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests, if so identify these tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. DO NOT simply paste a list of commands ran to verify the code; this is what our CI is for and it pollutes the PR. ONLY explain if testing was included with this PR, or why it wasn't. --> Added new unit test. # Are there any significant user-facing changes? <!-- Significant user-facing changes include: - Breaking public API change - Significant change to existing functionality (e.g. function returns something different) These need to be explicitly called out to ensure we have proper documentation if required and to ensure we follow proper semantic versioning. DO NOT restate the changes of this PR; if a bug was fixed and already explained above, we don't need this to be restated as a user-facing change. --> `UnionFields::from_iter` can now panic on invalid input data. This can be reachable via a `collect()` into a `UnionFields` for example. --- arrow-schema/src/fields.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/arrow-schema/src/fields.rs b/arrow-schema/src/fields.rs index e9362a0148..f7fa588c31 100644 --- a/arrow-schema/src/fields.rs +++ b/arrow-schema/src/fields.rs @@ -309,7 +309,12 @@ impl<'a> IntoIterator for &'a Fields { } } -/// A cheaply cloneable, owned collection of [`FieldRef`] and their corresponding type ids +/// A cheaply cloneable, owned collection of [`FieldRef`] and their corresponding +/// type ids. +/// +/// Invariants: +/// - All type ids are unique +/// - All type ids are non-negative #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(transparent))] @@ -350,8 +355,9 @@ impl UnionFields { /// # Errors /// /// This function returns an error if: + /// - Iterators are not the same length /// - Any type_id appears more than once (duplicate type ids) - /// - The type_ids are duplicated + /// - There is a negative type_id /// /// # Examples /// @@ -615,14 +621,26 @@ impl UnionFields { output.push((field_type_id, from_field.clone())) } } - *self = output.into_iter().collect(); + // TODO: see above comment; can we remove the need to go through try_new + // and just validate in this function ourselves? + let (type_ids, fields): (Vec<i8>, Vec<FieldRef>) = output.into_iter().unzip(); + *self = Self::try_new(type_ids, fields)?; Ok(()) } } impl FromIterator<(i8, FieldRef)> for UnionFields { + /// # Panics + /// + /// This will panic on invalid inputs: + /// - Duplicate type IDs + /// - Negative type IDs + /// + /// Prefer using [`UnionFields::try_new`] for a fallible construction method. fn from_iter<T: IntoIterator<Item = (i8, FieldRef)>>(iter: T) -> Self { - Self(iter.into_iter().collect()) + let iter = iter.into_iter(); + let (type_ids, fields): (Vec<i8>, Vec<FieldRef>) = iter.unzip(); + Self::try_new(type_ids, fields).expect("invalid arguments passed") } } @@ -1002,4 +1020,16 @@ mod tests { assert!(res.is_ok()); assert_eq!(res.unwrap().len(), 3); } + + #[test] + #[should_panic( + expected = "invalid arguments passed: InvalidArgumentError(\"type ids must be non-negative: -1\")" + )] + fn test_union_fields_from_iter_should_validate() { + let fields: Vec<(i8, FieldRef)> = vec![ + (0, Arc::new(Field::new("a", DataType::Int32, true))), + (-1, Arc::new(Field::new("a", DataType::Int32, true))), + ]; + UnionFields::from_iter(fields); + } }
