Jefffrey commented on code in PR #12403:
URL: https://github.com/apache/arrow-rs/pull/12403#discussion_r4666099883
##########
arrow-array/src/builder/map_builder.rs:
##########
@@ -183,23 +185,40 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> {
/// Append `n` nulls to this [`MapBuilder`]
///
/// Returns an error if the key and values builders are in an inconsistent
state,
- /// or if the number of entries exceeds `i32::MAX`.
+ /// the next offset would be less than the last offset, or the number of
entries
+ /// exceeds `i32::MAX`. A rejected append leaves the builder unchanged.
#[inline]
pub fn append_nulls(&mut self, n: usize) -> Result<(), ArrowError> {
self.validate_equal_lengths()?;
let offset = self.next_offset()?;
+ self.ensure_offset_not_decreasing(offset)?;
self.offsets_builder.extend(std::iter::repeat_n(offset, n));
self.null_buffer_builder.append_n_nulls(n);
Ok(())
}
+ /// `keys().finish()` resets the child length, so the next offset can be
smaller
+ /// than one already recorded. Reject that before either buffer is updated.
+ #[inline]
+ fn ensure_offset_not_decreasing(&self, offset: i32) -> Result<(),
ArrowError> {
+ if let Some(&last) = self.offsets_builder.last()
+ && offset < last
+ {
+ return Err(ArrowError::InvalidArgumentError(format!(
+ "Cannot append to a map builder when the next offset {offset}
is less than the last offset {last}"
Review Comment:
this might be a bit of a confusing message, given we manage the offsets
internally; maybe we can word it like `builder reached invalid state: values
truncated unexpectedly` or something similar
##########
arrow-array/src/builder/map_builder.rs:
##########
@@ -170,11 +170,13 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> {
/// Finish the current map array slot
///
/// Returns an error if the key and values builders are in an inconsistent
state,
- /// or if the number of entries exceeds `i32::MAX`.
+ /// the next offset would be less than the last offset, or the number of
entries
Review Comment:
can we turn this into a dot point list, now that theres many items
(same goes for below where applicable)
##########
arrow-array/src/builder/map_builder.rs:
##########
@@ -254,6 +274,9 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> {
"Keys array must have no null values, found {} null value(s)",
keys_arr.null_count()
);
+ // Checked before `build_unchecked`, which does not validate offsets.
+ // A longer child is allowed: those entries are not part of a map slot
yet.
+ assert_map_offsets(&offset_buffer, keys_arr.len(), values_arr.len());
Review Comment:
is it possible to safely ignore this; the idea of using unchecked build was
to skip this validation, so if we're gonna handroll the validation it calls
into question why we arent just building with validation
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]