Skip to content

Commit

Permalink
Add UINT16 data type (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChobobDev authored Oct 29, 2022
1 parent fe185da commit 823e8e8
Show file tree
Hide file tree
Showing 25 changed files with 951 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum DataType {
Int,
Int128,
Uint8,
Uint16,
Float,
Text,
Bytea,
Expand Down
5 changes: 5 additions & 0 deletions core/src/data/bigdecimal_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub trait BigDecimalExt {
fn to_i64(&self) -> Option<i64>;
fn to_i128(&self) -> Option<i128>;
fn to_u8(&self) -> Option<u8>;
fn to_u16(&self) -> Option<u16>;
fn to_f64(&self) -> Option<f64>;
}

Expand Down Expand Up @@ -39,6 +40,10 @@ impl BigDecimalExt for BigDecimal {
self.is_integer()
.then(|| bigdecimal::ToPrimitive::to_u8(self))?
}
fn to_u16(&self) -> Option<u16> {
self.is_integer()
.then(|| bigdecimal::ToPrimitive::to_u16(self))?
}
fn to_f64(&self) -> Option<f64> {
bigdecimal::ToPrimitive::to_f64(self)
}
Expand Down
51 changes: 51 additions & 0 deletions core/src/data/interval/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ impl Mul<u8> for Interval {
}
}

impl Mul<u16> for Interval {
type Output = Self;

fn mul(self, rhs: u16) -> Self {
match self {
Interval::Month(v) => Interval::Month(((v as u16) * rhs) as i32),
Interval::Microsecond(v) => Interval::Microsecond(((v as u16) * rhs) as i64),
}
}
}
impl Mul<f64> for Interval {
type Output = Self;

Expand Down Expand Up @@ -128,6 +138,14 @@ impl Mul<Interval> for u8 {
}
}

impl Mul<Interval> for u16 {
type Output = Interval;

fn mul(self, rhs: Interval) -> Interval {
rhs * self
}
}

impl Mul<Interval> for f64 {
type Output = Interval;

Expand Down Expand Up @@ -202,6 +220,17 @@ impl Div<u8> for Interval {
}
}

impl Div<u16> for Interval {
type Output = Self;

fn div(self, rhs: u16) -> Self {
match self {
Interval::Month(v) => Interval::Month(((v as u16) / rhs) as i32),
Interval::Microsecond(v) => Interval::Microsecond(((v as u16) / rhs) as i64),
}
}
}

impl Div<f64> for Interval {
type Output = Self;

Expand Down Expand Up @@ -279,6 +308,16 @@ impl Div<Interval> for u8 {
}
}

impl Div<Interval> for u16 {
type Output = Interval;

fn div(self, rhs: Interval) -> Interval {
match rhs {
Interval::Month(v) => Interval::Month((self / (v as u16)) as i32),
Interval::Microsecond(v) => Interval::Microsecond((self / (v as u16)) as i64),
}
}
}
impl Div<Interval> for f64 {
type Output = Interval;

Expand Down Expand Up @@ -316,6 +355,9 @@ mod tests {
assert_eq!(Month(2) * 3_u8, Month(6));
assert_eq!(2_u8 * Month(3), Month(6));

assert_eq!(Month(2) * 3_u16, Month(6));
assert_eq!(2_u16 * Month(3), Month(6));

assert_eq!(Month(2) * 3.0, Month(6));
assert_eq!(2.0 * Month(3), Month(6));

Expand All @@ -337,6 +379,9 @@ mod tests {
assert_eq!(Month(6) / 3_u8, Month(2));
assert_eq!(6_u8 / Month(2), Month(3));

assert_eq!(Month(6) / 3_u16, Month(2));
assert_eq!(6_u16 / Month(2), Month(3));

assert_eq!(Month(8) / 4.0, Month(2));
assert_eq!(8.0 / Month(4), Month(2));

Expand All @@ -358,6 +403,9 @@ mod tests {
assert_eq!(Microsecond(2) * 3_u8, Microsecond(6));
assert_eq!(2_u8 * Microsecond(3), Microsecond(6));

assert_eq!(Microsecond(2) * 3_u16, Microsecond(6));
assert_eq!(2_u16 * Microsecond(3), Microsecond(6));

assert_eq!(Microsecond(6) / 3_i8, Microsecond(2));
assert_eq!(6_i8 / Microsecond(2), Microsecond(3));

Expand All @@ -375,5 +423,8 @@ mod tests {

assert_eq!(Microsecond(6) / 3_u8, Microsecond(2));
assert_eq!(6_u8 / Microsecond(2), Microsecond(3));

assert_eq!(Microsecond(6) / 3_u16, Microsecond(2));
assert_eq!(6_u16 / Microsecond(2), Microsecond(3));
}
}
18 changes: 18 additions & 0 deletions core/src/data/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum Key {
I128(i128),
U8(u8),
Decimal(Decimal),
U16(u16),
Bool(bool),
Str(String),
Bytea(Vec<u8>),
Expand All @@ -50,6 +51,7 @@ impl PartialOrd for Key {
(Key::I32(l), Key::I32(r)) => Some(l.cmp(r)),
(Key::I64(l), Key::I64(r)) => Some(l.cmp(r)),
(Key::U8(l), Key::U8(r)) => Some(l.cmp(r)),
(Key::U16(l), Key::U16(r)) => Some(l.cmp(r)),
(Key::Decimal(l), Key::Decimal(r)) => Some(l.cmp(r)),
(Key::Bool(l), Key::Bool(r)) => Some(l.cmp(r)),
(Key::Str(l), Key::Str(r)) => Some(l.cmp(r)),
Expand Down Expand Up @@ -78,6 +80,7 @@ impl TryFrom<Value> for Key {
I64(v) => Ok(Key::I64(v)),
I128(v) => Ok(Key::I128(v)),
U8(v) => Ok(Key::U8(v)),
U16(v) => Ok(Key::U16(v)),
Decimal(v) => Ok(Key::Decimal(v)),
Str(v) => Ok(Key::Str(v)),
Bytea(v) => Ok(Key::Bytea(v)),
Expand Down Expand Up @@ -166,6 +169,11 @@ impl Key {
.chain(v.to_be_bytes().iter())
.copied()
.collect::<Vec<_>>(),
Key::U16(v) => [VALUE, 1]
.iter()
.chain(v.to_be_bytes().iter())
.copied()
.collect::<Vec<_>>(),
Key::Decimal(v) => {
let sign = if v.is_sign_positive() { 1 } else { 0 };
let convert = |v: Decimal| {
Expand Down Expand Up @@ -273,6 +281,7 @@ mod tests {
assert_eq!(convert("CAST(11 AS INT32)"), Ok(Key::I32(11)));
assert_eq!(convert("2048"), Ok(Key::I64(2048)));
assert_eq!(convert("CAST(11 AS UINT8)"), Ok(Key::U8(11)));
assert_eq!(convert("CAST(11 AS UINT16)"), Ok(Key::U16(11)));
assert_eq!(
convert("CAST(123.45 AS DECIMAL)"),
Ok(Key::Decimal(Decimal::from_str("123.45").unwrap()))
Expand Down Expand Up @@ -436,6 +445,15 @@ mod tests {
assert_eq!(cmp(&n1, &n4), Ordering::Less);
assert_eq!(cmp(&n3, &n4), Ordering::Equal);

let n1 = U16(0).to_cmp_be_bytes();
let n2 = U16(3).to_cmp_be_bytes();
let n3 = U16(20).to_cmp_be_bytes();
let n4 = U16(20).to_cmp_be_bytes();
assert_eq!(cmp(&n1, &n2), Ordering::Less);
assert_eq!(cmp(&n3, &n2), Ordering::Greater);
assert_eq!(cmp(&n1, &n4), Ordering::Less);
assert_eq!(cmp(&n3, &n4), Ordering::Equal);

let dec = |n| Decimal(rust_decimal::Decimal::from_str(n).unwrap());
let n1 = dec("-1200.345678").to_cmp_be_bytes();
let n2 = dec("-1.01").to_cmp_be_bytes();
Expand Down
Loading

0 comments on commit 823e8e8

Please sign in to comment.