-
Notifications
You must be signed in to change notification settings - Fork 784
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
Add support of converting FixedSizeBinaryArray
to DecimalArray
#2041
Merged
tustvold
merged 4 commits into
apache:master
from
HaoYang670:convert_fixed_size_binary_to_decimal
Jul 15, 2022
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,10 +20,10 @@ use std::convert::From; | |
use std::fmt; | ||
use std::{any::Any, iter::FromIterator}; | ||
|
||
use super::BooleanBufferBuilder; | ||
use super::{ | ||
array::print_long_array, raw_pointer::RawPtrBox, Array, ArrayData, FixedSizeListArray, | ||
}; | ||
use super::{BooleanBufferBuilder, FixedSizeBinaryArray}; | ||
pub use crate::array::DecimalIter; | ||
use crate::buffer::Buffer; | ||
use crate::datatypes::DataType; | ||
|
@@ -148,6 +148,32 @@ pub trait BasicDecimalArray<T: BasicDecimal, U: From<ArrayData>>: | |
self.value(row).to_string() | ||
} | ||
|
||
/// Build a decimal array from [`FixedSizeBinaryArray`]. | ||
/// | ||
/// NB: This function does not validate that each value is in the permissible | ||
/// range for a decimal | ||
fn from_fixed_size_binary_array( | ||
v: FixedSizeBinaryArray, | ||
precision: usize, | ||
scale: usize, | ||
) -> U { | ||
assert!( | ||
v.value_length() == Self::VALUE_LENGTH, | ||
"Value length of the array ({}) must equal to the byte width of the decimal ({})", | ||
v.value_length(), | ||
Self::VALUE_LENGTH, | ||
); | ||
|
||
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. You could use the new APIs for this
Saves some clones and is slightly less verbose |
||
let builder = ArrayData::builder(DataType::Decimal(precision, scale)) | ||
.len(v.len()) | ||
.add_buffer(v.value_data()) | ||
.null_bit_buffer(v.data_ref().null_buffer().cloned()) | ||
.offset(v.offset()); | ||
|
||
let array_data = unsafe { builder.build_unchecked() }; | ||
U::from(array_data) | ||
} | ||
|
||
fn from_fixed_size_list_array( | ||
v: FixedSizeListArray, | ||
precision: usize, | ||
|
@@ -646,6 +672,42 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
fn test_decimal_array_from_fixed_size_binary() { | ||
let value_data = ArrayData::builder(DataType::FixedSizeBinary(16)) | ||
.offset(1) | ||
.len(3) | ||
.add_buffer(Buffer::from_slice_ref(&[99999_i128, 2, 34, 560])) | ||
.null_bit_buffer(Some(Buffer::from_slice_ref(&[0b1010]))) | ||
.build() | ||
.unwrap(); | ||
|
||
let binary_array = FixedSizeBinaryArray::from(value_data); | ||
let decimal = DecimalArray::from_fixed_size_binary_array(binary_array, 38, 1); | ||
|
||
assert_eq!(decimal.len(), 3); | ||
assert_eq!(decimal.value_as_string(0), "0.2".to_string()); | ||
assert!(decimal.is_null(1)); | ||
assert_eq!(decimal.value_as_string(2), "56.0".to_string()); | ||
} | ||
|
||
#[test] | ||
#[should_panic( | ||
expected = "Value length of the array (8) must equal to the byte width of the decimal (16)" | ||
)] | ||
fn test_decimal_array_from_fixed_size_binary_wrong_length() { | ||
let value_data = ArrayData::builder(DataType::FixedSizeBinary(8)) | ||
.offset(1) | ||
.len(3) | ||
.add_buffer(Buffer::from_slice_ref(&[99999_i64, 2, 34, 560])) | ||
.null_bit_buffer(Some(Buffer::from_slice_ref(&[0b1010]))) | ||
.build() | ||
.unwrap(); | ||
|
||
let binary_array = FixedSizeBinaryArray::from(value_data); | ||
let _ = DecimalArray::from_fixed_size_binary_array(binary_array, 38, 1); | ||
} | ||
|
||
#[test] | ||
fn test_decimal_array_from_fixed_size_list() { | ||
let value_data = ArrayData::builder(DataType::UInt8) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
But from this message, this function is just used to convert to the decimal array.
@HaoYang670