Skip to content

Commit

Permalink
feat: implememnt convert for unsigned int
Browse files Browse the repository at this point in the history
  • Loading branch information
ChobobDev committed Nov 26, 2022
1 parent 784cdcc commit f22f06c
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 12 deletions.
3 changes: 3 additions & 0 deletions core/src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub enum DataType {
Int128,
Uint8,
Uint16,
Uint32,
Uint64,
Uint128,
Float,
Text,
Bytea,
Expand Down
15 changes: 15 additions & 0 deletions core/src/data/bigdecimal_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub trait BigDecimalExt {
fn to_i128(&self) -> Option<i128>;
fn to_u8(&self) -> Option<u8>;
fn to_u16(&self) -> Option<u16>;
fn to_u32(&self) -> Option<u32>;
fn to_u64(&self) -> Option<u64>;
fn to_u128(&self) -> Option<u128>;
fn to_f64(&self) -> Option<f64>;
}

Expand Down Expand Up @@ -44,6 +47,18 @@ impl BigDecimalExt for BigDecimal {
self.is_integer()
.then(|| bigdecimal::ToPrimitive::to_u16(self))?
}
fn to_u32(&self) -> Option<u32> {
self.is_integer()
.then(|| bigdecimal::ToPrimitive::to_u32(self))?
}
fn to_u64(&self) -> Option<u64> {
self.is_integer()
.then(|| bigdecimal::ToPrimitive::to_u64(self))?
}
fn to_u128(&self) -> Option<u128> {
self.is_integer()
.then(|| bigdecimal::ToPrimitive::to_u128(self))?
}
fn to_f64(&self) -> Option<f64> {
bigdecimal::ToPrimitive::to_f64(self)
}
Expand Down
57 changes: 57 additions & 0 deletions core/src/data/value/binary_op/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl PartialEq<Value> for Decimal {
I128(other) => *self == Decimal::from(*other),
U8(other) => *self == Decimal::from(*other),
U16(other) => *self == Decimal::from(*other),
U32(other) => *self == Decimal::from(*other),
F64(other) => Decimal::from_f64_retain(*other)
.map(|x| *self == x)
.unwrap_or(false),
Expand All @@ -37,6 +38,7 @@ impl PartialOrd<Value> for Decimal {
I128(rhs) => self.partial_cmp(&(Decimal::from(rhs))),
U8(rhs) => self.partial_cmp(&(Decimal::from(rhs))),
U16(rhs) => self.partial_cmp(&(Decimal::from(rhs))),
U32(rhs) => self.partial_cmp(&(Decimal::from(rhs))),
F64(rhs) => Decimal::from_f64_retain(rhs)
.map(|x| self.partial_cmp(&x))
.unwrap_or(None),
Expand Down Expand Up @@ -119,6 +121,17 @@ impl TryBinaryOperator for Decimal {
.into()
})
.map(Decimal),
U32(rhs) => lhs
.checked_add(Decimal::from(rhs))
.ok_or_else(|| {
ValueError::BinaryOperationOverflow {
lhs: Decimal(lhs),
rhs: U32(rhs),
operator: NumericBinaryOperator::Add,
}
.into()
})
.map(Decimal),
F64(rhs) => Decimal::from_f64_retain(rhs)
.map(|x| Ok(Decimal(lhs + x)))
.unwrap_or_else(|| Err(ValueError::FloatToDecimalConversionFailure(rhs).into())),
Expand Down Expand Up @@ -213,6 +226,17 @@ impl TryBinaryOperator for Decimal {
.into()
})
.map(Decimal),
U32(rhs) => lhs
.checked_sub(Decimal::from(rhs))
.ok_or_else(|| {
ValueError::BinaryOperationOverflow {
lhs: Decimal(lhs),
rhs: U32(rhs),
operator: NumericBinaryOperator::Subtract,
}
.into()
})
.map(Decimal),
F64(rhs) => Decimal::from_f64_retain(rhs)
.map(|x| Ok(Decimal(lhs - x)))
.unwrap_or_else(|| Err(ValueError::FloatToDecimalConversionFailure(rhs).into())),
Expand Down Expand Up @@ -307,6 +331,17 @@ impl TryBinaryOperator for Decimal {
.into()
})
.map(Decimal),
U32(rhs) => lhs
.checked_mul(Decimal::from(rhs))
.ok_or_else(|| {
ValueError::BinaryOperationOverflow {
lhs: Decimal(lhs),
rhs: U32(rhs),
operator: NumericBinaryOperator::Multiply,
}
.into()
})
.map(Decimal),
F64(rhs) => Decimal::from_f64_retain(rhs)
.map(|x| Ok(Decimal(lhs * x)))
.unwrap_or_else(|| Err(ValueError::FloatToDecimalConversionFailure(rhs).into())),
Expand Down Expand Up @@ -401,6 +436,17 @@ impl TryBinaryOperator for Decimal {
.into()
})
.map(Decimal),
U32(rhs) => lhs
.checked_div(Decimal::from(rhs))
.ok_or_else(|| {
ValueError::BinaryOperationOverflow {
lhs: Decimal(lhs),
rhs: U32(rhs),
operator: NumericBinaryOperator::Divide,
}
.into()
})
.map(Decimal),
F64(rhs) => Decimal::from_f64_retain(rhs)
.map(|x| Ok(Decimal(lhs / x)))
.unwrap_or_else(|| Err(ValueError::FloatToDecimalConversionFailure(rhs).into())),
Expand Down Expand Up @@ -495,6 +541,17 @@ impl TryBinaryOperator for Decimal {
.into()
})
.map(Decimal),
U32(rhs) => lhs
.checked_rem(Decimal::from(rhs))
.ok_or_else(|| {
ValueError::BinaryOperationOverflow {
lhs: Decimal(lhs),
rhs: U32(rhs),
operator: NumericBinaryOperator::Modulo,
}
.into()
})
.map(Decimal),
F64(rhs) => match Decimal::from_f64_retain(rhs) {
Some(x) => lhs
.checked_rem(x)
Expand Down

0 comments on commit f22f06c

Please sign in to comment.