Skip to content

Commit

Permalink
* support INTERVAL && TIMESTAMPTZ type
Browse files Browse the repository at this point in the history
Signed-off-by: Zejiong Dong <[email protected]>
  • Loading branch information
ZENOTME committed Oct 22, 2022
1 parent 143e5b2 commit 229a726
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 39 deletions.
1 change: 1 addition & 0 deletions sqllogictest-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ humantime = "2"
itertools = "0.10"
log = "0.4"
postgres-types = { version = "0.2.3", features = ["derive", "with-chrono-0_4"] }
pg_interval = "0.4"
quick-junit = { version = "0.2" }
rand = "0.8"
rust_decimal = { version = "1.7.0", features = ["tokio-pg"] }
Expand Down
107 changes: 69 additions & 38 deletions sqllogictest-bin/src/engines/postgres_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use anyhow::Context;
use async_trait::async_trait;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use pg_interval::Interval;
use postgres_types::Type;
use rust_decimal::Decimal;
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -113,8 +114,46 @@ impl sqllogictest::AsyncDB for PostgresExtended {
if idx != 0 {
write!(output, " ").unwrap();
}

match column.type_().clone() {
Type::INT2 => {
single_process!(row, output, idx, i16);
}
Type::INT4 => {
single_process!(row, output, idx, i32);
}
Type::INT8 => {
single_process!(row, output, idx, i64);
}
Type::NUMERIC => {
single_process!(row, output, idx, Decimal);
}
Type::DATE => {
single_process!(row, output, idx, NaiveDate);
}
Type::TIME => {
single_process!(row, output, idx, NaiveTime);
}
Type::TIMESTAMP => {
single_process!(row, output, idx, NaiveDateTime);
}
Type::INT2_ARRAY => {
array_process!(row, output, idx, i16);
}
Type::INT4_ARRAY => {
array_process!(row, output, idx, i32);
}
Type::INT8_ARRAY => {
array_process!(row, output, idx, i64);
}
Type::FLOAT4_ARRAY => {
array_process!(row, output, idx, f32);
}
Type::FLOAT8_ARRAY => {
array_process!(row, output, idx, f64);
}
Type::NUMERIC_ARRAY => {
array_process!(row, output, idx, Decimal);
}
Type::VARCHAR | Type::TEXT => {
let value: Option<&str> = row.get(idx);
match value {
Expand All @@ -130,15 +169,6 @@ impl sqllogictest::AsyncDB for PostgresExtended {
}
}
}
Type::INT2 => {
single_process!(row, output, idx, i16);
}
Type::INT4 => {
single_process!(row, output, idx, i32);
}
Type::INT8 => {
single_process!(row, output, idx, i64);
}
Type::BOOL => {
let value: Option<bool> = row.get(idx);
match value {
Expand Down Expand Up @@ -188,35 +218,36 @@ impl sqllogictest::AsyncDB for PostgresExtended {
}
}
}
Type::NUMERIC => {
single_process!(row, output, idx, Decimal);
}
Type::TIMESTAMP => {
single_process!(row, output, idx, NaiveDateTime);
}
Type::DATE => {
single_process!(row, output, idx, NaiveDate);
}
Type::TIME => {
single_process!(row, output, idx, NaiveTime);
}
Type::INT2_ARRAY => {
array_process!(row, output, idx, i16);
}
Type::INT4_ARRAY => {
array_process!(row, output, idx, i32);
}
Type::INT8_ARRAY => {
array_process!(row, output, idx, i64);
}
Type::FLOAT4_ARRAY => {
array_process!(row, output, idx, f32);
}
Type::FLOAT8_ARRAY => {
array_process!(row, output, idx, f64);
// INTERVAL will be converted as a unified format: " xx days xx:xx:xx ".
// INTERVAL format of test case should also be written as the same format "
// xx days xx:xx:xx ".
Type::INTERVAL => {
let value: Option<Interval> = row.get(idx);
match value {
Some(value) => {
let days: i64 = (value.days + 31 * value.months).into();
let seconds = Interval::new(0, 0, value.microseconds);
write!(output, "{} days {}", days, seconds.to_postgres())
.unwrap();
}
None => {
write!(output, "NULL").unwrap();
}
}
}
Type::NUMERIC_ARRAY => {
array_process!(row, output, idx, Decimal);
// TIMESTAMP will be converted as a timestamp without time zone format: "
// xxxx-xx-xx xx:xx:xx ". TIMESTAMP format of test
// case should also be written as the same format " xxxx-xx-xx xx:xx:xx ".
Type::TIMESTAMPTZ => {
let value: Option<chrono::DateTime<chrono::Utc>> = row.get(idx);
match value {
Some(value) => {
write!(output, "{}", value.naive_utc()).unwrap();
}
None => {
write!(output, "NULL").unwrap();
}
}
}
_ => {
todo!("Don't support {} type now.", column.type_().name())
Expand Down
3 changes: 2 additions & 1 deletion sqllogictest/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::time::Duration;
use std::vec;

use async_trait::async_trait;
use futures::{executor::block_on, stream, Future, StreamExt};
use futures::executor::block_on;
use futures::{stream, Future, StreamExt};
use itertools::Itertools;
use tempfile::{tempdir, TempDir};

Expand Down

0 comments on commit 229a726

Please sign in to comment.