Skip to content

Commit

Permalink
add u128 and i128
Browse files Browse the repository at this point in the history
  • Loading branch information
gusinacio committed Sep 18, 2023
1 parent 73d39ba commit 45420fb
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ clickhouse-rs = "*"
* Decimal(P, S)
* Float32, Float64
* String, FixedString(N)
* UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64
* UInt8, UInt16, UInt32, UInt64, UInt128, Int8, Int16, Int32, Int64, Int128
* Nullable(T)
* Array(UInt/Int/Float/String/Date/DateTime)
* SimpleAggregateFunction(F, T)
Expand Down
4 changes: 3 additions & 1 deletion src/types/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ sliceable! {
u16: UInt16,
u32: UInt32,
u64: UInt64,
u128: UInt128,

i8: Int8,
i16: Int16,
i32: Int32,
i64: Int64
i64: Int64,
i128: Int128
}

/// Represents Clickhouse Block
Expand Down
2 changes: 2 additions & 0 deletions src/types/column/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ impl dyn ColumnData {
SqlType::UInt16 => W::wrap(VectorColumnData::<u16>::with_capacity(capacity)),
SqlType::UInt32 => W::wrap(VectorColumnData::<u32>::with_capacity(capacity)),
SqlType::UInt64 => W::wrap(VectorColumnData::<u64>::with_capacity(capacity)),
SqlType::UInt128 => W::wrap(VectorColumnData::<u128>::with_capacity(capacity)),
SqlType::Int8 => W::wrap(VectorColumnData::<i8>::with_capacity(capacity)),
SqlType::Int16 => W::wrap(VectorColumnData::<i16>::with_capacity(capacity)),
SqlType::Int32 => W::wrap(VectorColumnData::<i32>::with_capacity(capacity)),
SqlType::Int64 => W::wrap(VectorColumnData::<i64>::with_capacity(capacity)),
SqlType::Int128 => W::wrap(VectorColumnData::<i128>::with_capacity(capacity)),
SqlType::String => W::wrap(StringColumnData::with_capacity(capacity)),
SqlType::FixedString(len) => {
W::wrap(FixedStringColumnData::with_capacity(capacity, len))
Expand Down
2 changes: 2 additions & 0 deletions src/types/column/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ where
Value::UInt16(x) => ValueRef::UInt16(x),
Value::UInt32(x) => ValueRef::UInt32(x),
Value::UInt64(x) => ValueRef::UInt64(x),
Value::UInt128(x) => ValueRef::UInt128(x),

Value::Int8(x) => ValueRef::Int8(x),
Value::Int16(x) => ValueRef::Int16(x),
Value::Int32(x) => ValueRef::Int32(x),
Value::Int64(x) => ValueRef::Int64(x),
Value::Int128(x) => ValueRef::Int128(x),

Value::Float32(x) => ValueRef::Float32(x),
Value::Float64(x) => ValueRef::Float64(x),
Expand Down
4 changes: 4 additions & 0 deletions src/types/from_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ from_sql_vec_impl! {
i16: Int16,
i32: Int32,
i64: Int64,
i128: Int128,

u16: UInt16,
u32: UInt32,
u64: UInt64,
u128: UInt128,

f32: Float32,
f64: Float64
Expand Down Expand Up @@ -340,11 +342,13 @@ from_sql_impl! {
u16: UInt16,
u32: UInt32,
u64: UInt64,
u128: UInt128,

i8: Int8,
i16: Int16,
i32: Int32,
i64: Int64,
i128: Int128,

f32: Float32,
f64: Float64
Expand Down
50 changes: 50 additions & 0 deletions src/types/marshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ impl Marshal for u64 {
}
}

impl Marshal for u128 {
fn marshal(&self, scratch: &mut [u8]) {
scratch[0] = *self as u8;
scratch[1] = (self >> 8) as u8;
scratch[2] = (self >> 16) as u8;
scratch[3] = (self >> 24) as u8;

scratch[4] = (self >> 32) as u8;
scratch[5] = (self >> 40) as u8;
scratch[6] = (self >> 48) as u8;
scratch[7] = (self >> 56) as u8;


scratch[8] = (self >> 64) as u8;
scratch[9] = (self >> 72) as u8;
scratch[10] = (self >> 80) as u8;
scratch[11] = (self >> 88) as u8;

scratch[12] = (self >> 96) as u8;
scratch[13] = (self >> 104) as u8;
scratch[14] = (self >> 112) as u8;
scratch[15] = (self >> 120) as u8;
}
}

impl Marshal for i8 {
fn marshal(&self, scratch: &mut [u8]) {
scratch[0] = *self as u8;
Expand Down Expand Up @@ -74,6 +99,31 @@ impl Marshal for i64 {
}
}

impl Marshal for i128 {
fn marshal(&self, scratch: &mut [u8]) {
scratch[0] = *self as u8;
scratch[1] = (self >> 8) as u8;
scratch[2] = (self >> 16) as u8;
scratch[3] = (self >> 24) as u8;

scratch[4] = (self >> 32) as u8;
scratch[5] = (self >> 40) as u8;
scratch[6] = (self >> 48) as u8;
scratch[7] = (self >> 56) as u8;


scratch[8] = (self >> 64) as u8;
scratch[9] = (self >> 72) as u8;
scratch[10] = (self >> 80) as u8;
scratch[11] = (self >> 88) as u8;

scratch[12] = (self >> 96) as u8;
scratch[13] = (self >> 104) as u8;
scratch[14] = (self >> 112) as u8;
scratch[15] = (self >> 120) as u8;
}
}

impl Marshal for f32 {
fn marshal(&self, scratch: &mut [u8]) {
let bits = self.to_bits();
Expand Down
6 changes: 6 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ has_sql_type! {
u16: SqlType::UInt16,
u32: SqlType::UInt32,
u64: SqlType::UInt64,
u128: SqlType::UInt128,
i8: SqlType::Int8,
i16: SqlType::Int16,
i32: SqlType::Int32,
i64: SqlType::Int64,
i128: SqlType::Int128,
&str: SqlType::String,
String: SqlType::String,
f32: SqlType::Float32,
Expand Down Expand Up @@ -306,10 +308,12 @@ pub enum SqlType {
UInt16,
UInt32,
UInt64,
UInt128,
Int8,
Int16,
Int32,
Int64,
Int128,
String,
FixedString(usize),
Float32,
Expand Down Expand Up @@ -372,10 +376,12 @@ impl SqlType {
SqlType::UInt16 => "UInt16".into(),
SqlType::UInt32 => "UInt32".into(),
SqlType::UInt64 => "UInt64".into(),
SqlType::UInt128 => "UInt128".into(),
SqlType::Int8 => "Int8".into(),
SqlType::Int16 => "Int16".into(),
SqlType::Int32 => "Int32".into(),
SqlType::Int64 => "Int64".into(),
SqlType::Int128 => "Int128".into(),
SqlType::String => "String".into(),
SqlType::FixedString(str_len) => format!("FixedString({})", str_len).into(),
SqlType::Float32 => "Float32".into(),
Expand Down
24 changes: 24 additions & 0 deletions src/types/stat_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ impl StatBuffer for u64 {
}
}

impl StatBuffer for u128 {
type Buffer = [u8; 16];

fn buffer() -> Self::Buffer {
[0; 16]
}

fn sql_type() -> SqlType {
SqlType::UInt128
}
}

impl StatBuffer for i8 {
type Buffer = [u8; 1];

Expand Down Expand Up @@ -102,6 +114,18 @@ impl StatBuffer for i64 {
}
}

impl StatBuffer for i128 {
type Buffer = [u8; 16];

fn buffer() -> Self::Buffer {
[0; 16]
}

fn sql_type() -> SqlType {
SqlType::Int128
}
}

impl StatBuffer for f32 {
type Buffer = [u8; 4];

Expand Down
42 changes: 42 additions & 0 deletions src/types/unmarshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ impl Unmarshal<u64> for u64 {
}
}

impl Unmarshal<u128> for u128 {
fn unmarshal(scratch: &[u8]) -> Self {
Self::from(scratch[0])
| Self::from(scratch[1]) << 8
| Self::from(scratch[2]) << 16
| Self::from(scratch[3]) << 24
| Self::from(scratch[4]) << 32
| Self::from(scratch[5]) << 40
| Self::from(scratch[6]) << 48
| Self::from(scratch[7]) << 56
| Self::from(scratch[8]) << 64
| Self::from(scratch[9]) << 72
| Self::from(scratch[10]) << 80
| Self::from(scratch[11]) << 88
| Self::from(scratch[12]) << 96
| Self::from(scratch[13]) << 104
| Self::from(scratch[14]) << 112
| Self::from(scratch[15]) << 120
}
}

impl Unmarshal<i8> for i8 {
fn unmarshal(scratch: &[u8]) -> Self {
scratch[0] as Self
Expand Down Expand Up @@ -70,6 +91,27 @@ impl Unmarshal<i64> for i64 {
}
}

impl Unmarshal<i128> for i128 {
fn unmarshal(scratch: &[u8]) -> Self {
Self::from(scratch[0])
| Self::from(scratch[1]) << 8
| Self::from(scratch[2]) << 16
| Self::from(scratch[3]) << 24
| Self::from(scratch[4]) << 32
| Self::from(scratch[5]) << 40
| Self::from(scratch[6]) << 48
| Self::from(scratch[7]) << 56
| Self::from(scratch[8]) << 64
| Self::from(scratch[9]) << 72
| Self::from(scratch[10]) << 80
| Self::from(scratch[11]) << 88
| Self::from(scratch[12]) << 96
| Self::from(scratch[13]) << 104
| Self::from(scratch[14]) << 112
| Self::from(scratch[15]) << 120
}
}

impl Unmarshal<f32> for f32 {
fn unmarshal(scratch: &[u8]) -> Self {
let bits = u32::from(scratch[0])
Expand Down
Loading

0 comments on commit 45420fb

Please sign in to comment.