MCPcopy Create free account
hub / github.com/apache/arrow-rs / try_new

Method try_new

arrow-array/src/array/list_array.rs:209–259  ·  view source on GitHub ↗

Create a new [`GenericListArray`] from the provided parts # Errors Errors if `offsets.len() - 1 != nulls.len()` `offsets.last() > values.len()` `!field.is_nullable() && values.is_nullable()` `field.data_type() != values.data_type()`

(
        field: FieldRef,
        offsets: OffsetBuffer<OffsetSize>,
        values: ArrayRef,
        nulls: Option<NullBuffer>,
    )

Source from the content-addressed store, hash-verified

207 /// * `!field.is_nullable() && values.is_nullable()`
208 /// * `field.data_type() != values.data_type()`
209 pub fn try_new(
210 field: FieldRef,
211 offsets: OffsetBuffer<OffsetSize>,
212 values: ArrayRef,
213 nulls: Option<NullBuffer>,
214 ) -> Result<Self, ArrowError> {
215 let len = offsets.len() - 1; // Offsets guaranteed to not be empty
216 let end_offset = offsets.last().unwrap().as_usize();
217 // don't need to check other values of `offsets` because they are checked
218 // during construction of `OffsetBuffer`
219 if end_offset > values.len() {
220 return Err(ArrowError::InvalidArgumentError(format!(
221 "Max offset of {end_offset} exceeds length of values {}",
222 values.len()
223 )));
224 }
225
226 if let Some(n) = nulls.as_ref() {
227 if n.len() != len {
228 return Err(ArrowError::InvalidArgumentError(format!(
229 "Incorrect length of null buffer for {}ListArray, expected {len} got {}",
230 OffsetSize::PREFIX,
231 n.len(),
232 )));
233 }
234 }
235 if !field.is_nullable() && values.is_nullable() {
236 return Err(ArrowError::InvalidArgumentError(format!(
237 "Non-nullable field of {}ListArray {:?} cannot contain nulls",
238 OffsetSize::PREFIX,
239 field.name()
240 )));
241 }
242
243 if field.data_type() != values.data_type() {
244 return Err(ArrowError::InvalidArgumentError(format!(
245 "{}ListArray expected data type {} got {} for {:?}",
246 OffsetSize::PREFIX,
247 field.data_type(),
248 values.data_type(),
249 field.name()
250 )));
251 }
252
253 Ok(Self {
254 data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
255 nulls,
256 values,
257 value_offsets: offsets,
258 })
259 }
260
261 /// Create a new [`GenericListArray`] from the provided parts
262 ///

Callers

nothing calls this directly

Calls 6

as_usizeMethod · 0.80
lenMethod · 0.45
lastMethod · 0.45
as_refMethod · 0.45
is_nullableMethod · 0.45
data_typeMethod · 0.45

Tested by

no test coverage detected