Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for array[bool/u8/AppDate/AppDateTime/&str] #210 #211

Open
wants to merge 1 commit into
base: async-await
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@ impl From<String> for Value {
}
}

impl From<Vec<u8>> for Value {
fn from(v: Vec<u8>) -> Value {
Value::String(Arc::new(v))
}
}
// impl From<Vec<u8>> for Value {
// fn from(v: Vec<u8>) -> Value {
// Value::String(Arc::new(v))
// }
// }

impl From<&[u8]> for Value {
fn from(v: &[u8]) -> Value {
Expand All @@ -419,6 +419,15 @@ impl From<Vec<String>> for Value {
}
}

impl From<Vec<&str>> for Value {
fn from(v: Vec<&str>) -> Value {
Value::Array(
SqlType::String.into(),
Arc::new(v.into_iter().map(|v| v.into()).collect())
)
}
}

impl From<Uuid> for Value {
fn from(v: Uuid) -> Value {
let mut buffer = *v.as_bytes();
Expand Down Expand Up @@ -447,6 +456,24 @@ where
}
}

impl From<Vec<AppDateTime>> for Value {
fn from(v: Vec<AppDateTime>) -> Self {
Value::Array(
SqlType::DateTime(DateTimeType::DateTime32).into(),
Arc::new(v.into_iter().map(|a| a.into()).collect()),
)
}
}

impl From<Vec<AppDate>> for Value {
fn from(v: Vec<AppDate>) -> Self {
Value::Array(
SqlType::Date.into(),
Arc::new(v.into_iter().map(|a| a.into()).collect()),
)
}
}

value_from! {
bool: Bool,
u8: UInt8,
Expand All @@ -471,6 +498,7 @@ value_from! {
}

value_array_from! {
u8: UInt8,
u16: UInt16,
u32: UInt32,
u64: UInt64,
Expand All @@ -483,7 +511,9 @@ value_array_from! {
i128: Int128,

f32: Float32,
f64: Float64
f64: Float64,

bool: Bool
}

impl<'a> From<&'a str> for Value {
Expand Down Expand Up @@ -835,6 +865,7 @@ mod test {
let mut block = Block::with_capacity(5);
block
.push(row! {
u8: vec![1_u8, 2, 3],
u16: vec![1_u16, 2, 3],
u32: vec![1_u32, 2, 3],
u64: vec![1_u64, 2, 3],
Expand All @@ -848,6 +879,6 @@ mod test {
.unwrap();

assert_eq!(block.row_count(), 1);
assert_eq!(block.column_count(), 9);
assert_eq!(block.column_count(), 10);
}
}
18 changes: 17 additions & 1 deletion tests/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1673,12 +1673,28 @@ async fn test_array() -> Result<(), Error> {
.column("time64", vec![vec![date_time_value]])
.column("bln", vec![vec![true]]);

let mut block2 = Block::with_capacity(1);
for _i in 0..1 {
block2.push(row! {
u8:vec![31_u8],
u32:vec![42_u32],
f64:vec![42_f64],
text1:vec!["A"],
text2:vec!["B".to_string()],
date:vec![date_value],
time:vec![date_time_value],
time64:vec![date_time_value],
bln:vec![true],
})?;
}

let pool = Pool::new(database_url());

let mut c = pool.get_handle().await?;
c.execute("DROP TABLE IF EXISTS clickhouse_array").await?;
c.execute(ddl).await?;
c.insert("clickhouse_array", block).await?;
c.insert("clickhouse_array", block2).await?;
let block = c.query(query).fetch_all().await?;

let u8_vec: Vec<u8> = block.get(0, "u8")?;
Expand All @@ -1691,7 +1707,7 @@ async fn test_array() -> Result<(), Error> {
let time64_vec: Vec<DateTime<Tz>> = block.get(0, "time64")?;
let bln_vec: Vec<bool> = block.get(0, "bln")?;

assert_eq!(1, block.row_count());
assert_eq!(2, block.row_count());
assert_eq!(vec![41_u8], u8_vec);
assert_eq!(vec![42_u32], u32_vec);
assert_eq!(vec![42_f64], f64_vec);
Expand Down