Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unwraps from 'create_primitive_array' #3232

Merged
merged 2 commits into from
Nov 30, 2022
Merged
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
43 changes: 16 additions & 27 deletions arrow-ipc/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn create_array(
read_buffer(buffers.get(buffer_index + 1), data, compression_codec)?,
read_buffer(buffers.get(buffer_index + 2), data, compression_codec)?,
],
);
)?;
node_index += 1;
buffer_index += 3;
array
Expand All @@ -104,7 +104,7 @@ fn create_array(
read_buffer(buffers.get(buffer_index), data, compression_codec)?,
read_buffer(buffers.get(buffer_index + 1), data, compression_codec)?,
],
);
)?;
node_index += 1;
buffer_index += 2;
array
Expand Down Expand Up @@ -305,7 +305,7 @@ fn create_array(
read_buffer(buffers.get(buffer_index), data, compression_codec)?,
read_buffer(buffers.get(buffer_index + 1), data, compression_codec)?,
],
);
)?;
node_index += 1;
buffer_index += 2;
array
Expand Down Expand Up @@ -397,7 +397,7 @@ fn create_primitive_array(
field_node: &crate::FieldNode,
data_type: &DataType,
buffers: &[Buffer],
) -> ArrayRef {
) -> Result<ArrayRef, ArrowError> {
let length = field_node.length() as usize;
let null_buffer = (field_node.null_count() > 0).then_some(buffers[0].clone());
let array_data = match data_type {
Expand All @@ -407,17 +407,15 @@ fn create_primitive_array(
.len(length)
.buffers(buffers[1..3].to_vec())
.null_bit_buffer(null_buffer)
.build()
.unwrap()
.build()?
}
FixedSizeBinary(_) => {
// read 2 buffers: null buffer (optional) and data buffer
ArrayData::builder(data_type.clone())
.len(length)
.add_buffer(buffers[1].clone())
.null_bit_buffer(null_buffer)
.build()
.unwrap()
.build()?
}
Int8
| Int16
Expand All @@ -434,19 +432,16 @@ fn create_primitive_array(
.len(length)
.add_buffer(buffers[1].clone())
.null_bit_buffer(null_buffer)
.build()
.unwrap();
.build()?;
let values = Arc::new(Int64Array::from(data)) as ArrayRef;
// this cast is infallible, the unwrap is safe
let casted = cast(&values, data_type).unwrap();
let casted = cast(&values, data_type)?;
casted.into_data()
} else {
ArrayData::builder(data_type.clone())
.len(length)
.add_buffer(buffers[1].clone())
.null_bit_buffer(null_buffer)
.build()
.unwrap()
.build()?
}
}
Float32 => {
Expand All @@ -456,19 +451,16 @@ fn create_primitive_array(
.len(length)
.add_buffer(buffers[1].clone())
.null_bit_buffer(null_buffer)
.build()
.unwrap();
.build()?;
let values = Arc::new(Float64Array::from(data)) as ArrayRef;
// this cast is infallible, the unwrap is safe
let casted = cast(&values, data_type).unwrap();
let casted = cast(&values, data_type)?;
casted.into_data()
} else {
ArrayData::builder(data_type.clone())
.len(length)
.add_buffer(buffers[1].clone())
.null_bit_buffer(null_buffer)
.build()
.unwrap()
.build()?
}
}
Boolean
Expand All @@ -483,8 +475,7 @@ fn create_primitive_array(
.len(length)
.add_buffer(buffers[1].clone())
.null_bit_buffer(null_buffer)
.build()
.unwrap(),
.build()?,
Interval(IntervalUnit::MonthDayNano) | Decimal128(_, _) => {
let buffer = get_aligned_buffer::<i128>(&buffers[1], length);

Expand All @@ -493,8 +484,7 @@ fn create_primitive_array(
.len(length)
.add_buffer(buffer)
.null_bit_buffer(null_buffer)
.build()
.unwrap()
.build()?
}
Decimal256(_, _) => {
let buffer = get_aligned_buffer::<i256>(&buffers[1], length);
Expand All @@ -504,13 +494,12 @@ fn create_primitive_array(
.len(length)
.add_buffer(buffer)
.null_bit_buffer(null_buffer)
.build()
.unwrap()
.build()?
}
t => unreachable!("Data type {:?} either unsupported or not primitive", t),
};

make_array(array_data)
Ok(make_array(array_data))
}

/// Checks if given `Buffer` is properly aligned with `T`.
Expand Down