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

Method try_new_impl

arrow-array/src/record_batch.rs:324–394  ·  view source on GitHub ↗

Validate the schema and columns using [`RecordBatchOptions`]. Returns an error if any validation check fails, otherwise returns the created [`Self`]

(
        schema: SchemaRef,
        columns: Vec<ArrayRef>,
        options: &RecordBatchOptions,
    )

Source from the content-addressed store, hash-verified

322 /// Validate the schema and columns using [`RecordBatchOptions`]. Returns an error
323 /// if any validation check fails, otherwise returns the created [`Self`]
324 fn try_new_impl(
325 schema: SchemaRef,
326 columns: Vec<ArrayRef>,
327 options: &RecordBatchOptions,
328 ) -> Result<Self, ArrowError> {
329 // check that number of fields in schema match column length
330 if schema.fields().len() != columns.len() {
331 return Err(ArrowError::InvalidArgumentError(format!(
332 "number of columns({}) must match number of fields({}) in schema",
333 columns.len(),
334 schema.fields().len(),
335 )));
336 }
337
338 let row_count = options
339 .row_count
340 .or_else(|| columns.first().map(|col| col.len()))
341 .ok_or_else(|| {
342 ArrowError::InvalidArgumentError(
343 "must either specify a row count or at least one column".to_string(),
344 )
345 })?;
346
347 for (c, f) in columns.iter().zip(&schema.fields) {
348 if !f.is_nullable() && c.null_count() > 0 {
349 return Err(ArrowError::InvalidArgumentError(format!(
350 "Column '{}' is declared as non-nullable but contains null values",
351 f.name()
352 )));
353 }
354 }
355
356 // check that all columns have the same row count
357 if columns.iter().any(|c| c.len() != row_count) {
358 let err = match options.row_count {
359 Some(_) => "all columns in a record batch must have the specified row count",
360 None => "all columns in a record batch must have the same length",
361 };
362 return Err(ArrowError::InvalidArgumentError(err.to_string()));
363 }
364
365 // function for comparing column type and field type
366 // return true if 2 types are not matched
367 let type_not_match = if options.match_field_names {
368 |(_, (col_type, field_type)): &(usize, (&DataType, &DataType))| col_type != field_type
369 } else {
370 |(_, (col_type, field_type)): &(usize, (&DataType, &DataType))| {
371 !col_type.equals_datatype(field_type)
372 }
373 };
374
375 // check that all columns match the schema
376 let not_match = columns
377 .iter()
378 .zip(schema.fields().iter())
379 .map(|(col, field)| (col.data_type(), field.data_type()))
380 .enumerate()
381 .find(type_not_match);

Callers

nothing calls this directly

Calls 10

firstMethod · 0.80
zipMethod · 0.80
equals_datatypeMethod · 0.80
findMethod · 0.80
lenMethod · 0.45
fieldsMethod · 0.45
iterMethod · 0.45
is_nullableMethod · 0.45
null_countMethod · 0.45
data_typeMethod · 0.45

Tested by

no test coverage detected