Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Added API for Box<dyn Array>
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Jul 26, 2022
1 parent b2f3a4f commit b0cd5e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
26 changes: 21 additions & 5 deletions src/io/orc/read/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! APIs to read from [ORC format](https://orc.apache.org).
use std::io::{Read, Seek, SeekFrom};

use crate::array::{BooleanArray, Float32Array, Int32Array, Int64Array};
use crate::array::{Array, BooleanArray, Float32Array, Int32Array, Int64Array};
use crate::bitmap::{Bitmap, MutableBitmap};
use crate::datatypes::{DataType, Field, Schema};
use crate::error::Error;
Expand Down Expand Up @@ -102,7 +102,7 @@ fn deserialize_validity(
}

/// Deserializes column `column` from `stripe`, assumed to represent a f32
pub fn deserialize_f32(
fn deserialize_f32(
data_type: DataType,
stripe: &Stripe,
column: usize,
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn deserialize_f32(
}

/// Deserializes column `column` from `stripe`, assumed to represent a boolean array
pub fn deserialize_bool(
fn deserialize_bool(
data_type: DataType,
stripe: &Stripe,
column: usize,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<'a> Iterator for IntIter<'a> {
}

/// Deserializes column `column` from `stripe`, assumed to represent a boolean array
pub fn deserialize_i64(
fn deserialize_i64(
data_type: DataType,
stripe: &Stripe,
column: usize,
Expand Down Expand Up @@ -266,7 +266,7 @@ pub fn deserialize_i64(
}

/// Deserializes column `column` from `stripe`, assumed to represent a boolean array
pub fn deserialize_i32(
fn deserialize_i32(
data_type: DataType,
stripe: &Stripe,
column: usize,
Expand Down Expand Up @@ -317,3 +317,19 @@ pub fn deserialize_i32(

Int32Array::try_new(data_type, values.into(), validity)
}

/// Deserializes column `column` from `stripe`, assumed
/// to represent an array of `data_type`.
pub fn deserialize(
data_type: DataType,
stripe: &Stripe,
column: usize,
) -> Result<Box<dyn Array>, Error> {
match data_type {
DataType::Boolean => deserialize_bool(data_type, stripe, column).map(|x| x.boxed()),
DataType::Int32 => deserialize_i32(data_type, stripe, column).map(|x| x.boxed()),
DataType::Int64 => deserialize_i64(data_type, stripe, column).map(|x| x.boxed()),
DataType::Float32 => deserialize_f32(data_type, stripe, column).map(|x| x.boxed()),
dt => return Err(Error::nyi(format!("Reading {dt:?} from ORC"))),
}
}
30 changes: 12 additions & 18 deletions tests/it/io/orc/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ fn float32() -> Result<(), Error> {
let (ps, footer, _) = format::read::read_metadata(&mut reader)?;
let stripe = read::read_stripe(&mut reader, footer.stripes[0].clone(), ps.compression())?;

let array = read::deserialize_f32(DataType::Float32, &stripe, 1)?;
assert_eq!(
array,
Float32Array::from([Some(1.0), Some(2.0), None, Some(4.0), Some(5.0)])
read::deserialize(DataType::Float32, &stripe, 1)?,
Float32Array::from([Some(1.0), Some(2.0), None, Some(4.0), Some(5.0)]).boxed()
);

let array = read::deserialize_f32(DataType::Float32, &stripe, 2)?;
assert_eq!(
array,
Float32Array::from([Some(1.0), Some(2.0), Some(3.0), Some(4.0), Some(5.0)])
read::deserialize(DataType::Float32, &stripe, 2)?,
Float32Array::from([Some(1.0), Some(2.0), Some(3.0), Some(4.0), Some(5.0)]).boxed()
);
Ok(())
}
Expand All @@ -39,16 +37,14 @@ fn boolean() -> Result<(), Error> {
let (ps, footer, _) = format::read::read_metadata(&mut reader)?;
let stripe = read::read_stripe(&mut reader, footer.stripes[0].clone(), ps.compression())?;

let array = read::deserialize_bool(DataType::Boolean, &stripe, 3)?;
assert_eq!(
array,
BooleanArray::from([Some(true), Some(false), None, Some(true), Some(false)])
read::deserialize(DataType::Boolean, &stripe, 3)?,
BooleanArray::from([Some(true), Some(false), None, Some(true), Some(false)]).boxed()
);

let array = read::deserialize_bool(DataType::Boolean, &stripe, 4)?;
assert_eq!(
array,
BooleanArray::from([Some(true), Some(false), Some(true), Some(true), Some(false)])
read::deserialize(DataType::Boolean, &stripe, 4)?,
BooleanArray::from([Some(true), Some(false), Some(true), Some(true), Some(false)]).boxed()
);
Ok(())
}
Expand All @@ -59,16 +55,14 @@ fn int() -> Result<(), Error> {
let (ps, footer, _) = format::read::read_metadata(&mut reader)?;
let stripe = read::read_stripe(&mut reader, footer.stripes[0].clone(), ps.compression())?;

let array = read::deserialize_i32(DataType::Int32, &stripe, 5)?;
assert_eq!(
array,
Int32Array::from([Some(5), Some(-5), None, Some(5), Some(5)])
read::deserialize(DataType::Int32, &stripe, 5)?,
Int32Array::from([Some(5), Some(-5), None, Some(5), Some(5)]).boxed()
);

let array = read::deserialize_i32(DataType::Int32, &stripe, 6)?;
assert_eq!(
array,
Int32Array::from([Some(5), Some(-5), Some(1), Some(5), Some(5)])
read::deserialize(DataType::Int32, &stripe, 6)?,
Int32Array::from([Some(5), Some(-5), Some(1), Some(5), Some(5)]).boxed()
);
Ok(())
}

0 comments on commit b0cd5e5

Please sign in to comment.