Construct an Arrow array from a partially typed JSON column
(
field: &Field,
json_col: ArrowJsonColumn,
dictionaries: Option<&HashMap<i64, ArrowJsonDictionaryBatch>>,
)
| 346 | |
| 347 | /// Construct an Arrow array from a partially typed JSON column |
| 348 | pub 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(_) => { |
no test coverage detected