Skip to content

Commit

Permalink
Make the API of fn Decimal:new be consistent with `fn Decimal:try_n…
Browse files Browse the repository at this point in the history
…ew_bytes` and add length bound for `Decimal::raw_value` (#2405)

* add bound

Signed-off-by: remzi <13716567376yh@gmail.com>

* update doc

Signed-off-by: remzi <13716567376yh@gmail.com>

Signed-off-by: remzi <13716567376yh@gmail.com>
  • Loading branch information
HaoYang670 committed Aug 12, 2022
1 parent 0e97491 commit 0c3c686
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 2 additions & 0 deletions arrow/src/array/array_decimal.rs
Expand Up @@ -122,6 +122,8 @@ impl<const BYTE_WIDTH: usize> BasicDecimalArray<BYTE_WIDTH> {
self.raw_value_data_ptr().offset(pos as isize),
Self::VALUE_LENGTH as usize,
)
.try_into()
.unwrap()
};
BasicDecimal::<BYTE_WIDTH>::new(self.precision(), self.scale(), raw_val)
}
Expand Down
11 changes: 5 additions & 6 deletions arrow/src/util/decimal.rs
Expand Up @@ -64,8 +64,7 @@ impl<const BYTE_WIDTH: usize> BasicDecimal<BYTE_WIDTH> {
Self::MAX_PRECISION_SCALE_CONSTRUCTOR_DEFAULT_TYPE.3;

/// Tries to create a decimal value from precision, scale and bytes.
/// If the length of bytes isn't same as the bit width of this decimal,
/// returning an error. The bytes should be stored in little-endian order.
/// The bytes should be stored in little-endian order.
///
/// Safety:
/// This method doesn't validate if the decimal value represented by the bytes
Expand Down Expand Up @@ -114,17 +113,17 @@ impl<const BYTE_WIDTH: usize> BasicDecimal<BYTE_WIDTH> {
/// Creates a decimal value from precision, scale, and bytes.
///
/// Safety:
/// This method doesn't check if the length of bytes is compatible with this decimal.
/// This method doesn't check if the precision and scale are valid.
/// Use `try_new_from_bytes` for safe constructor.
pub fn new(precision: usize, scale: usize, bytes: &[u8]) -> Self {
pub fn new(precision: usize, scale: usize, bytes: &[u8; BYTE_WIDTH]) -> Self {
Self {
precision,
scale,
value: bytes.try_into().unwrap(),
value: *bytes,
}
}
/// Returns the raw bytes of the integer representation of the decimal.
pub fn raw_value(&self) -> &[u8] {
pub fn raw_value(&self) -> &[u8; BYTE_WIDTH] {
&self.value
}

Expand Down

0 comments on commit 0c3c686

Please sign in to comment.