Skip to content

Commit

Permalink
feat: impl TryFrom for HashMap and Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed Apr 12, 2024
1 parent d4f8b6b commit 0ee31d7
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 165 deletions.
27 changes: 16 additions & 11 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
[advisories]
version = 2
db-path = "~/.cargo/advisory-db"
db-urls = ["https://github.com/rustsec/advisory-db"]
vulnerability = "deny"
unmaintained = "warn"
yanked = "warn"
notice = "warn"
ignore = [
#"RUSTSEC-0000-0000",
]

[licenses]
unlicensed = "warn"
version = 2
allow = [
"MIT",
"CC0-1.0",
"Apache-2.0 WITH LLVM-exception",
"Apache-2.0",
"BSD-3-Clause",
"Unicode-DFS-2016",
"BSL-1.0",
"CC0-1.0",
"ISC",
"MIT",
"MPL-2.0",
"BSL-1.0",
"Apache-2.0",
"Apache-2.0 WITH LLVM-exception",
"OpenSSL",
"Unicode-DFS-2016",
]

[[licenses.clarify]]
name = "ring"
expression = "MIT AND ISC AND OpenSSL"
license-files = [
{ path = "LICENSE", hash = 0xbd0eed23 }
]

[bans]
Expand Down
161 changes: 92 additions & 69 deletions driver/tests/driver/select_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::assert_eq;
use std::collections::HashMap;

use chrono::{DateTime, NaiveDate, NaiveDateTime};
use databend_driver::{Client, Connection, DecimalSize, NumberValue, Value};
Expand Down Expand Up @@ -180,94 +181,116 @@ async fn select_nullable_u64() {
#[tokio::test]
async fn select_array() {
let conn = prepare().await;
let row = conn
.query_row("select [], [1, 2, 3, 4, 5], [10::Decimal(15,2), 1.1+2.3], [to_binary('xyz')]")

let row1 = conn.query_row("select []").await.unwrap().unwrap();
let (val1,): (Vec<String>,) = row1.try_into().unwrap();
assert_eq!(val1, Vec::<String>::new());

let row2 = conn
.query_row("select [1, 2, 3, 4, 5]")
.await
.unwrap()
.unwrap();
assert!(row.is_some());
let row = row.unwrap();
assert_eq!(
row.values().to_owned(),
vec![
Value::EmptyArray,
Value::Array(vec![
Value::Number(NumberValue::UInt8(1)),
Value::Number(NumberValue::UInt8(2)),
Value::Number(NumberValue::UInt8(3)),
Value::Number(NumberValue::UInt8(4)),
Value::Number(NumberValue::UInt8(5)),
]),
Value::Array(vec![
Value::Number(NumberValue::Decimal128(
1000,
DecimalSize {
precision: 4,
scale: 2
}
)),
Value::Number(NumberValue::Decimal128(
340,
DecimalSize {
precision: 4,
scale: 2
}
))
]),
Value::Array(vec![Value::Binary(vec![120, 121, 122])]),
]
);
let (val2,): (Vec<u8>,) = row2.try_into().unwrap();
assert_eq!(val2, vec![1, 2, 3, 4, 5]);

let row3 = conn
.query_row("select [10::Decimal(15,2), 1.1+2.3]")
.await
.unwrap()
.unwrap();
let (val3,): (Vec<String>,) = row3.try_into().unwrap();
assert_eq!(val3, vec!["10.00".to_string(), "3.40".to_string()]);

let row4 = conn
.query_row("select [to_binary('xyz')]")
.await
.unwrap()
.unwrap();
let (val4,): (Vec<Vec<u8>>,) = row4.try_into().unwrap();
assert_eq!(val4, vec![vec![120, 121, 122]]);
}

#[tokio::test]
async fn select_map() {
let conn = prepare().await;
let row = conn
.query_row("select {}, {'k1':'v1','k2':'v2'}, {'xx':to_date('2020-01-01')}")

let row1 = conn.query_row("select {}").await.unwrap().unwrap();
let (val1,): (HashMap<u8, u8>,) = row1.try_into().unwrap();
assert_eq!(val1, HashMap::new());

let row2 = conn
.query_row("select {'k1':'v1','k2':'v2'}")
.await
.unwrap()
.unwrap();
assert!(row.is_some());
let row = row.unwrap();
let (val2,): (HashMap<String, String>,) = row2.try_into().unwrap();
assert_eq!(
row.values().to_owned(),
val2,
vec![
Value::EmptyMap,
Value::Map(vec![
(
Value::String("k1".to_string()),
Value::String("v1".to_string())
),
(
Value::String("k2".to_string()),
Value::String("v2".to_string())
),
]),
Value::Map(vec![(Value::String("xx".to_string()), Value::Date(18262)),]),
("k1".to_string(), "v1".to_string()),
("k2".to_string(), "v2".to_string())
]
.into_iter()
.collect()
);

let row3 = conn
.query_row("select {'xx':to_date('2020-01-01')}")
.await
.unwrap()
.unwrap();
let (val3,): (HashMap<String, NaiveDate>,) = row3.try_into().unwrap();
assert_eq!(
val3,
vec![(
"xx".to_string(),
NaiveDate::from_ymd_opt(2020, 1, 1).unwrap()
)]
.into_iter()
.collect()
);

let row4 = conn
.query_row("select {1: 'a', 2: 'b'}")
.await
.unwrap()
.unwrap();
let (val4,): (HashMap<u8, String>,) = row4.try_into().unwrap();
assert_eq!(
val4,
vec![(1, "a".to_string()), (2, "b".to_string())]
.into_iter()
.collect()
);
}

#[tokio::test]
async fn select_tuple() {
let conn = prepare().await;
let row = conn.query_row("select (parse_json('[1,2]'), [1,2], true), (st_geometryfromwkt('SRID=4126;POINT(3.0 5.0)'), to_timestamp('2024-10-22 10:11:12'))").await.unwrap();
assert!(row.is_some());
let row = row.unwrap();

let row1 = conn
.query_row("select (parse_json('[1,2]'), [1,2], true)")
.await
.unwrap()
.unwrap();
let (val1,): ((String, Vec<u8>, bool),) = row1.try_into().unwrap();
assert_eq!(val1, ("[1,2]".to_string(), vec![1, 2], true,));

let row2 = conn
.query_row("select (st_geometryfromwkt('SRID=4126;POINT(3.0 5.0)'), to_timestamp('2024-10-22 10:11:12'))")
.await
.unwrap()
.unwrap();
let (val2,): ((String, NaiveDateTime),) = row2.try_into().unwrap();
assert_eq!(
row.values().to_owned(),
vec![
Value::Tuple(vec![
Value::Variant("[1,2]".to_string()),
Value::Array(vec![
Value::Number(NumberValue::UInt8(1)),
Value::Number(NumberValue::UInt8(2)),
]),
Value::Boolean(true),
]),
Value::Tuple(vec![
Value::Geometry("SRID=4126;POINT(3 5)".to_string()),
Value::Timestamp(1729591872000000)
]),
]
val2,
(
"SRID=4126;POINT(3 5)".to_string(),
DateTime::parse_from_rfc3339("2024-10-22T10:11:12Z")
.unwrap()
.naive_utc()
)
);
}

Expand Down
8 changes: 4 additions & 4 deletions sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ flight-sql = ["dep:arrow-array", "dep:arrow-schema", "dep:tonic"]
[dependencies]
databend-client = { workspace = true }

memchr = "2.7.2"
chrono = { version = "0.4.35", default-features = false }
geozero = { version = "0.12.0", features = ["default", "with-wkb"] }
geozero = { version = "0.12", features = ["default", "with-wkb"] }
glob = "0.3"
itertools = "0.12"
jsonb = "0.3"
lexical-core = "0.8.5"
lexical-core = "0.8"
memchr = "2.7"

roaring = { version = "0.10", features = ["serde"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
Expand All @@ -33,11 +33,11 @@ url = { version = "2.5", default-features = false }
arrow = { version = "47.0" }
arrow-array = { version = "47.0", optional = true }
arrow-schema = { version = "47.0", optional = true }
hex = "0.4.3"
tonic = { version = "0.10", default-features = false, features = [
"transport",
"codegen",
"tls",
"tls-webpki-roots",
"prost",
], optional = true }
hex = "0.4.3"
78 changes: 0 additions & 78 deletions sql/src/from_row.rs

This file was deleted.

1 change: 0 additions & 1 deletion sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

mod cursor_ext;
pub mod error;
pub mod from_row;
pub mod rows;
pub mod schema;
pub mod value;
Expand Down
Loading

0 comments on commit 0ee31d7

Please sign in to comment.