Extract a Datum from an Arrow RecordBatch column at the given row index.
(
batch: &RecordBatch,
row_idx: usize,
col_idx: usize,
data_type: &DataType,
)
| 646 | |
| 647 | /// Extract a Datum from an Arrow RecordBatch column at the given row index. |
| 648 | pub fn extract_datum_from_arrow( |
| 649 | batch: &RecordBatch, |
| 650 | row_idx: usize, |
| 651 | col_idx: usize, |
| 652 | data_type: &DataType, |
| 653 | ) -> crate::Result<Option<Datum>> { |
| 654 | use arrow_array::Array; |
| 655 | |
| 656 | let col = batch.column(col_idx); |
| 657 | if col.is_null(row_idx) { |
| 658 | return Ok(None); |
| 659 | } |
| 660 | |
| 661 | let datum = match data_type { |
| 662 | DataType::Boolean(_) => { |
| 663 | let arr = col |
| 664 | .as_any() |
| 665 | .downcast_ref::<arrow_array::BooleanArray>() |
| 666 | .ok_or_else(|| type_mismatch_err("Boolean", col_idx))?; |
| 667 | Datum::Bool(arr.value(row_idx)) |
| 668 | } |
| 669 | DataType::TinyInt(_) => { |
| 670 | let arr = col |
| 671 | .as_any() |
| 672 | .downcast_ref::<arrow_array::Int8Array>() |
| 673 | .ok_or_else(|| type_mismatch_err("TinyInt", col_idx))?; |
| 674 | Datum::TinyInt(arr.value(row_idx)) |
| 675 | } |
| 676 | DataType::SmallInt(_) => { |
| 677 | let arr = col |
| 678 | .as_any() |
| 679 | .downcast_ref::<arrow_array::Int16Array>() |
| 680 | .ok_or_else(|| type_mismatch_err("SmallInt", col_idx))?; |
| 681 | Datum::SmallInt(arr.value(row_idx)) |
| 682 | } |
| 683 | DataType::Int(_) => { |
| 684 | let arr = col |
| 685 | .as_any() |
| 686 | .downcast_ref::<arrow_array::Int32Array>() |
| 687 | .ok_or_else(|| type_mismatch_err("Int", col_idx))?; |
| 688 | Datum::Int(arr.value(row_idx)) |
| 689 | } |
| 690 | DataType::BigInt(_) => { |
| 691 | let arr = col |
| 692 | .as_any() |
| 693 | .downcast_ref::<arrow_array::Int64Array>() |
| 694 | .ok_or_else(|| type_mismatch_err("BigInt", col_idx))?; |
| 695 | Datum::Long(arr.value(row_idx)) |
| 696 | } |
| 697 | DataType::Float(_) => { |
| 698 | let arr = col |
| 699 | .as_any() |
| 700 | .downcast_ref::<arrow_array::Float32Array>() |
| 701 | .ok_or_else(|| type_mismatch_err("Float", col_idx))?; |
| 702 | Datum::Float(arr.value(row_idx)) |
| 703 | } |
| 704 | DataType::Double(_) => { |
| 705 | let arr = col |
no test coverage detected