diff --git a/src/array/list/mutable.rs b/src/array/list/mutable.rs index 6e5ffa2eb19..26bd29476a0 100644 --- a/src/array/list/mutable.rs +++ b/src/array/list/mutable.rs @@ -50,12 +50,16 @@ impl Default for MutableListArray { impl From> for ListArray { fn from(mut other: MutableListArray) -> Self { - ListArray::new( - other.data_type, - other.offsets.into(), - other.values.as_arc(), - other.validity.map(|x| x.into()), - ) + // Safety: + // MutableListArray has monotonically increasing offsets + unsafe { + ListArray::new_unchecked( + other.data_type, + other.offsets.into(), + other.values.as_arc(), + other.validity.map(|x| x.into()), + ) + } } } @@ -209,21 +213,29 @@ impl MutableArray for MutableLis } fn as_box(&mut self) -> Box { - Box::new(ListArray::new( - self.data_type.clone(), - std::mem::take(&mut self.offsets).into(), - self.values.as_arc(), - std::mem::take(&mut self.validity).map(|x| x.into()), - )) + // Safety: + // MutableListArray has monotonically increasing offsets + unsafe { + Box::new(ListArray::new_unchecked( + self.data_type.clone(), + std::mem::take(&mut self.offsets).into(), + self.values.as_arc(), + std::mem::take(&mut self.validity).map(|x| x.into()), + )) + } } fn as_arc(&mut self) -> Arc { - Arc::new(ListArray::new( - self.data_type.clone(), - std::mem::take(&mut self.offsets).into(), - self.values.as_arc(), - std::mem::take(&mut self.validity).map(|x| x.into()), - )) + // Safety: + // MutableListArray has monotonically increasing offsets + unsafe { + Arc::new(ListArray::new_unchecked( + self.data_type.clone(), + std::mem::take(&mut self.offsets).into(), + self.values.as_arc(), + std::mem::take(&mut self.validity).map(|x| x.into()), + )) + } } fn data_type(&self) -> &DataType {