-
Notifications
You must be signed in to change notification settings - Fork 20
Add type-safe accessors for primitive types in Row #86
Changes from 1 commit
abcf257
198a1e3
d09907e
acf66fa
d1aeaa5
910f118
15533c2
e5c9b6e
89b04f9
76d4ab0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ use std::fmt; | |
|
||
use basic::{LogicalType, Type as PhysicalType}; | ||
use data_type::{ByteArray, Int96}; | ||
use errors::ParquetError; | ||
|
||
/// Macro as a shortcut to generate 'not yet implemented' panic error. | ||
macro_rules! nyi { | ||
|
@@ -55,7 +56,59 @@ pub enum Row { | |
Map(Vec<(Row, Row)>) // List of key-value pairs | ||
} | ||
|
||
// Macro to generate type-safe get_xxx methods e.g. get_bool, get_short | ||
macro_rules! row_primitive_accessor { | ||
($METHOD:ident, $VARIANT:ident, $TY:ty) => { | ||
pub fn $METHOD(&self) -> Result<$TY, ParquetError> { | ||
match *self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we use 2-space indent. |
||
Row::$VARIANT(v) => Ok(v), | ||
_ => Err(ParquetError::General(format!("Cannot access {} as {}", self.get_type_name(), stringify!($VARIANT)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking if it would be easier to just print There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we print There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, let's keep type names! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe we can use |
||
} | ||
} | ||
} | ||
} | ||
|
||
impl Row { | ||
/// Get the type name | ||
fn get_type_name(&self) -> &'static str { | ||
match *self { | ||
Row::Null => "Null", | ||
Row::Bool(_) => "Bool", | ||
Row::Byte(_) => "Byte", | ||
Row::Short(_) => "Short", | ||
Row::Int(_) => "Int", | ||
Row::Long(_) => "Long", | ||
Row::Float(_) => "Float", | ||
Row::Double(_) => "Double", | ||
Row::Str(_) => "Str", | ||
Row::Bytes(_) => "Bytes", | ||
Row::Timestamp(_) => "Timestamp", | ||
Row::Group(_) => "Group", | ||
Row::List(_) => "List", | ||
Row::Map(_) => "Map", | ||
} | ||
} | ||
|
||
row_primitive_accessor!(get_bool, Bool, bool); | ||
row_primitive_accessor!(get_byte, Byte, i8); | ||
row_primitive_accessor!(get_short, Short, i16); | ||
row_primitive_accessor!(get_int, Int, i32); | ||
row_primitive_accessor!(get_long, Long, i64); | ||
row_primitive_accessor!(get_float, Float, f32); | ||
row_primitive_accessor!(get_double, Double, f64); | ||
row_primitive_accessor!(get_timestamp, Timestamp, u64); | ||
|
||
/// Type-safe accessor for Str type | ||
pub fn get_string(&self) -> Result<String, ParquetError> { | ||
match *self { | ||
Row::Str(ref v) => Ok(v.clone()), | ||
_ => Err(ParquetError::General(format!( | ||
"Cannot access {} as Str", | ||
self.get_type_name() | ||
))), | ||
} | ||
} | ||
|
||
/// Converts Parquet BOOLEAN type with logical type into `bool` value. | ||
pub fn convert_bool( | ||
_physical_type: PhysicalType, | ||
|
@@ -343,4 +396,17 @@ mod tests { | |
]); | ||
assert_eq!(format!("{}", row), "{1 -> 1.2, 2 -> 4.5, 3 -> 2.3}"); | ||
} | ||
|
||
#[test] | ||
fn test_row_bool_accessors() { | ||
assert_eq!(true, Row::Bool(true).get_bool().unwrap()); | ||
assert_eq!(false, Row::Bool(false).get_bool().unwrap()); | ||
assert_eq!(ParquetError::General("Cannot access Float as Bool".to_string()), Row::Float(1.2).get_bool().unwrap_err()); | ||
} | ||
|
||
#[test] | ||
fn test_row_string_accessors() { | ||
assert_eq!("Hello".to_string(), Row::Str("Hello".to_string()).get_string().unwrap()); | ||
assert_eq!(ParquetError::General("Cannot access Float as Str".to_string()), Row::Float(1.2).get_string().unwrap_err()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: line too long. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can import
errors::Result
and replaceResult<$TY, ParquetError>
withResult<$TY>