From 9412d2f9a135893b329a77838e3c65e06f978001 Mon Sep 17 00:00:00 2001 From: everpcpc Date: Fri, 28 Apr 2023 08:58:15 +0800 Subject: [PATCH] fix(driver): better handle null datatype (#100) --- driver/Cargo.toml | 2 +- driver/src/schema.rs | 4 ++-- driver/tests/driver/select_simple.rs | 12 +++++++++++- tests/Makefile | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/driver/Cargo.toml b/driver/Cargo.toml index 693488f44..8747ab8e9 100644 --- a/driver/Cargo.toml +++ b/driver/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "databend-driver" -version = "0.2.20" +version = "0.2.21" edition = "2021" license = "Apache-2.0" description = "Databend Driver for Rust" diff --git a/driver/src/schema.rs b/driver/src/schema.rs index a9fa3590b..3e0a4c136 100644 --- a/driver/src/schema.rs +++ b/driver/src/schema.rs @@ -139,7 +139,7 @@ impl TryFrom<&TypeDesc<'_>> for DataType { fn try_from(desc: &TypeDesc) -> Result { let dt = match desc.name { - "Null" => DataType::Null, + "Null" | "NULL" => DataType::Null, "Boolean" => DataType::Boolean, "String" => DataType::String, "Int8" => DataType::Number(NumberDataType::Int8), @@ -256,7 +256,7 @@ impl TryFrom<&Arc> for Field { ))) } }; - if f.is_nullable() { + if f.is_nullable() && !matches!(dt, DataType::Null) { dt = DataType::Nullable(Box::new(dt)); } Ok(Field { diff --git a/driver/tests/driver/select_simple.rs b/driver/tests/driver/select_simple.rs index 9c7f113fa..8af5cdc80 100644 --- a/driver/tests/driver/select_simple.rs +++ b/driver/tests/driver/select_simple.rs @@ -22,6 +22,16 @@ async fn prepare() -> Box { new_connection(dsn).unwrap() } +#[tokio::test] +async fn select_null() { + let conn = prepare().await; + let row = conn.query_row("select null").await.unwrap(); + assert!(row.is_some()); + let row = row.unwrap(); + let (val,): (Option,) = row.try_into().unwrap(); + assert_eq!(val, None); +} + #[tokio::test] async fn select_string() { let conn = prepare().await; @@ -109,7 +119,7 @@ async fn select_datetime() { } #[tokio::test] -async fn select_null() { +async fn select_nullable() { let conn = prepare().await; let row = conn .query_row("select sum(number) from numbers(0)") diff --git a/tests/Makefile b/tests/Makefile index 2b61877b8..2a737616c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,7 @@ default: run +run: test-core test-driver test-bendsql down + prepare: mkdir -p data/databend @@ -19,8 +21,6 @@ test-bendsql: up cd .. && ./cli/test.sh http cd .. && ./cli/test.sh flight -run: test-core test-driver test-bendsql - down: docker compose down