-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-10818: [Rust] Implement DecimalType
- Loading branch information
Showing
6 changed files
with
179 additions
and
13 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::{convert::TryInto, fmt, ops::Add, str::FromStr}; | ||
|
||
use num::{BigInt, Zero}; | ||
|
||
// Decimal (Numeric) implementation on top of BigInt, it's a common implementation to support all scale/precision | ||
// Decimal (scale, precision) = scale.precision = Decimal(1, 2) = 1.00 | ||
// @todo Implementation of Int32/64 can be implemented soon for performance reason | ||
#[derive(Clone, Eq)] | ||
pub struct DecimalType { | ||
digits: BigInt, | ||
scale: usize, | ||
precision: usize, | ||
} | ||
|
||
impl DecimalType { | ||
pub fn new(digits: BigInt, scale: usize, precision: usize) -> DecimalType { | ||
DecimalType { | ||
digits, | ||
scale, | ||
precision, | ||
} | ||
} | ||
|
||
pub fn from_bytes(bytes: &[u8], scale: usize, precision: usize) -> DecimalType { | ||
let as_array = bytes.try_into(); | ||
match as_array { | ||
Ok(v) if bytes.len() == 16 => DecimalType { | ||
digits: BigInt::from(i128::from_le_bytes(v)), | ||
scale, | ||
precision, | ||
}, | ||
_ => panic!("Unsupported Decimal"), | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
fn rescale(mut self, new_scale: usize, new_precision: usize) -> DecimalType { | ||
if self.digits.is_zero() { | ||
return DecimalType::new(BigInt::zero(), new_scale, new_precision); | ||
} | ||
|
||
unimplemented!(); | ||
} | ||
} | ||
|
||
impl Default for DecimalType { | ||
#[inline] | ||
fn default() -> DecimalType { | ||
Zero::zero() | ||
} | ||
} | ||
|
||
impl Zero for DecimalType { | ||
#[inline] | ||
fn zero() -> DecimalType { | ||
DecimalType::new(BigInt::zero(), 0, 0) | ||
} | ||
|
||
#[inline] | ||
fn is_zero(&self) -> bool { | ||
self.digits.is_zero() | ||
} | ||
} | ||
|
||
impl Add<DecimalType> for DecimalType { | ||
type Output = DecimalType; | ||
|
||
#[inline] | ||
fn add(self, rhs: DecimalType) -> DecimalType { | ||
let mut lhs = self; | ||
|
||
unimplemented!() | ||
} | ||
} | ||
|
||
impl PartialEq<DecimalType> for DecimalType { | ||
#[inline] | ||
fn eq(&self, rhs: &DecimalType) -> bool { | ||
if self.precision > rhs.precision { | ||
unimplemented!() | ||
} else if self.precision < rhs.precision { | ||
unimplemented!() | ||
} else { | ||
self.digits == rhs.digits | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for DecimalType { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl fmt::Debug for DecimalType { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
macro_rules! impl_from { | ||
($TYPE:tt) => { | ||
impl From<$TYPE> for DecimalType { | ||
#[inline] | ||
fn from(n: $TYPE) -> Self { | ||
DecimalType { | ||
digits: BigInt::from(n), | ||
scale: 0, | ||
precision: 0, | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_from!(i128); | ||
impl_from!(i64); | ||
impl_from!(i32); | ||
impl_from!(u128); | ||
impl_from!(u64); | ||
impl_from!(u32); | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum ParseDecimalError { | ||
Empty, | ||
Other(String), | ||
} | ||
|
||
impl FromStr for DecimalType { | ||
type Err = ParseDecimalError; | ||
|
||
fn from_str(s: &str) -> Result<Self, ParseDecimalError> { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
mod test { | ||
#[test] | ||
fn test_from_bytes() { | ||
let values: [u8; 32] = [ | ||
192, 219, 180, 17, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 36, 75, 238, 253, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
]; | ||
} | ||
} |
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
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