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

Function array_from_json

arrow-integration-test/src/lib.rs:348–1109  ·  view source on GitHub ↗

Construct an Arrow array from a partially typed JSON column

(
    field: &Field,
    json_col: ArrowJsonColumn,
    dictionaries: Option<&HashMap<i64, ArrowJsonDictionaryBatch>>,
)

Source from the content-addressed store, hash-verified

346
347/// Construct an Arrow array from a partially typed JSON column
348pub fn array_from_json(
349 field: &Field,
350 json_col: ArrowJsonColumn,
351 dictionaries: Option<&HashMap<i64, ArrowJsonDictionaryBatch>>,
352) -> Result<ArrayRef> {
353 match field.data_type() {
354 DataType::Null => Ok(Arc::new(NullArray::new(json_col.count))),
355 DataType::Boolean => {
356 let mut b = BooleanBuilder::with_capacity(json_col.count);
357 for (is_valid, value) in json_col
358 .validity
359 .as_ref()
360 .unwrap()
361 .iter()
362 .zip(json_col.data.unwrap())
363 {
364 match is_valid {
365 1 => b.append_value(value.as_bool().unwrap()),
366 _ => b.append_null(),
367 };
368 }
369 Ok(Arc::new(b.finish()))
370 }
371 DataType::Int8 => {
372 let mut b = Int8Builder::with_capacity(json_col.count);
373 for (is_valid, value) in json_col
374 .validity
375 .as_ref()
376 .unwrap()
377 .iter()
378 .zip(json_col.data.unwrap())
379 {
380 match is_valid {
381 1 => b.append_value(value.as_i64().ok_or_else(|| {
382 ArrowError::JsonError(format!("Unable to get {value:?} as int64"))
383 })? as i8),
384 _ => b.append_null(),
385 };
386 }
387 Ok(Arc::new(b.finish()))
388 }
389 DataType::Int16 => {
390 let mut b = Int16Builder::with_capacity(json_col.count);
391 for (is_valid, value) in json_col
392 .validity
393 .as_ref()
394 .unwrap()
395 .iter()
396 .zip(json_col.data.unwrap())
397 {
398 match is_valid {
399 1 => b.append_value(value.as_i64().unwrap() as i16),
400 _ => b.append_null(),
401 };
402 }
403 Ok(Arc::new(b.finish()))
404 }
405 DataType::Int32 | DataType::Date32 | DataType::Time32(_) => {

Callers 2

record_batch_from_jsonFunction · 0.85

Calls 15

castFunction · 0.85
create_null_bufFunction · 0.85
try_newFunction · 0.85
zipMethod · 0.80
is_stringMethod · 0.80
as_f64Method · 0.80
collectMethod · 0.80
add_child_dataMethod · 0.80
add_bufferMethod · 0.80
to_byte_sliceMethod · 0.80
is_positiveMethod · 0.80

Tested by

no test coverage detected