Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 91 additions & 7 deletions parquet/src/record/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ impl<'a> Iterator for RowColumnIter<'a> {

/// Trait for type-safe convenient access to fields within a Row.
pub trait RowAccessor {
/// Check if the field at the index is null.
fn is_null(&self, i: usize) -> Result<bool>;
/// Try to get a boolean value at the given index.
fn get_bool(&self, i: usize) -> Result<bool>;
/// Try to get a byte value at the given index.
Expand Down Expand Up @@ -210,11 +212,12 @@ pub trait RowFormatter {
macro_rules! row_primitive_accessor {
($METHOD:ident, $VARIANT:ident, $TY:ty) => {
fn $METHOD(&self, i: usize) -> Result<$TY> {
match self.fields[i].1 {
Field::$VARIANT(v) => Ok(v),
match self.fields.get(i) {
Some((_, Field::$VARIANT(v))) => Ok(*v),
None => Err(ParquetError::IndexOutOfBound(i, self.fields.len())),
_ => Err(general_err!(
"Cannot access {} as {}",
self.fields[i].1.get_type_name(),
self.fields[i].1.get_type_name(), // Safe access as None is
stringify!($VARIANT)
)),
}
Expand All @@ -227,11 +230,13 @@ macro_rules! row_primitive_accessor {
macro_rules! row_complex_accessor {
($METHOD:ident, $VARIANT:ident, $TY:ty) => {
fn $METHOD(&self, i: usize) -> Result<&$TY> {
match self.fields[i].1 {
Field::$VARIANT(ref v) => Ok(v),
match self.fields.get(i) {
Some((_, Field::$VARIANT(v))) => Ok(v),
None => Err(ParquetError::IndexOutOfBound(i, self.fields.len())),
_ => Err(general_err!(
"Cannot access {} as {}",
self.fields[i].1.get_type_name(),
self.fields[i].1.get_type_name(), // Safe access as None is
// just checked.
stringify!($VARIANT)
)),
}
Expand All @@ -242,11 +247,23 @@ macro_rules! row_complex_accessor {
impl RowFormatter for Row {
/// Get Display reference for a given field.
fn fmt(&self, i: usize) -> &dyn fmt::Display {
&self.fields[i].1
if let Some((_, v)) = self.fields.get(i) {
v
} else {
&"<IndexOutOfBound>"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if this should be something different so if you have another recommendation feel free to weigh in.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems fine to me

}
}
}

impl RowAccessor for Row {
fn is_null(&self, i: usize) -> Result<bool> {
match self.fields.get(i) {
Some((_, Field::Null)) => Ok(true),
None => Err(ParquetError::IndexOutOfBound(i, self.len())),
_ => Ok(false),
}
}

row_primitive_accessor!(get_bool, Bool, bool);

row_primitive_accessor!(get_byte, Byte, i8);
Expand Down Expand Up @@ -1651,6 +1668,8 @@ mod tests {
("p".to_string(), Field::Float16(f16::from_f32(9.1))),
]);

assert!(row.is_null(0).unwrap());
assert!(!row.is_null(1).unwrap());
assert!(!row.get_bool(1).unwrap());
assert_eq!(3, row.get_byte(2).unwrap());
assert_eq!(4, row.get_short(3).unwrap());
Expand All @@ -1666,6 +1685,71 @@ mod tests {
assert_eq!(5, row.get_bytes(13).unwrap().len());
assert_eq!(7, row.get_decimal(14).unwrap().precision());
assert!((f16::from_f32(9.1) - row.get_float16(15).unwrap()).abs() < f16::EPSILON);

assert!(matches!(
row.is_null(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_bool(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_byte(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_short(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_int(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_long(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_ubyte(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_ushort(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_uint(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_ulong(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_float(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_double(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_string(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_bytes(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_decimal(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
assert!(matches!(
row.get_float16(16).unwrap_err(),
ParquetError::IndexOutOfBound(16, 16),
));
}

#[test]
Expand Down
Loading