From f68756adf49db85d55f7fac49835220c2279cf11 Mon Sep 17 00:00:00 2001 From: pandaplusplus Date: Mon, 5 Jul 2021 08:38:15 +0800 Subject: [PATCH] FEAT(server,tests): impl to_date(timestamp32) DF function Signed-off-by: Jin Mingjian --- crates/datafusion_tests/tests/clickhouse.rs | 28 +++++++++++++++++++++ crates/server/tests/confs/base.conf | 1 + crates/tests_integ/tests/sanity_checks.rs | 22 +++++++++++++--- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/crates/datafusion_tests/tests/clickhouse.rs b/crates/datafusion_tests/tests/clickhouse.rs index 9b5cb575..9e074500 100644 --- a/crates/datafusion_tests/tests/clickhouse.rs +++ b/crates/datafusion_tests/tests/clickhouse.rs @@ -6,6 +6,34 @@ mod tests { use base::datetimes::BaseTimeZone; use datafusion::physical_plan::clickhouse::*; + #[test] + fn test_to_date() { + let a: PrimitiveArray = + vec![Some(0), Some(536457600), None, Some(1609459200)].into(); + + let b = timestamp32_to_date(&a, &Some(BaseTimeZone::default())).unwrap(); + + assert_eq!(0, b.value(0)); + assert_eq!(6209, b.value(1)); // 1987-01-01 + assert_eq!(false, b.is_valid(2)); + assert_eq!(18628, b.value(3)); // 2021-01-01 + } + + #[test] + fn stress_to_date() { + let v: Vec = (0..4096).collect(); + let a: PrimitiveArray = v.into(); + + let ts = ::std::time::Instant::now(); + let mut s = 0; + for _ in 0..100 { + let b = timestamp32_to_date(&a, &Some(BaseTimeZone::default())).unwrap(); + s += b.len() as usize; + } + + println!("ts: {:?}, s: {}", ts.elapsed(), s); + } + #[test] fn test_to_year() { let a: PrimitiveArray = vec![Some(1), None, Some(366)].into(); diff --git a/crates/server/tests/confs/base.conf b/crates/server/tests/confs/base.conf index 8c7933c6..17b3f603 100644 --- a/crates/server/tests/confs/base.conf +++ b/crates/server/tests/confs/base.conf @@ -8,3 +8,4 @@ data_dirs_clickhouse = "" [server] ip_addr = "localhost" port = 9528 +timezone = "Etc/GMT-8" diff --git a/crates/tests_integ/tests/sanity_checks.rs b/crates/tests_integ/tests/sanity_checks.rs index bbe65339..5eaa31e9 100644 --- a/crates/tests_integ/tests/sanity_checks.rs +++ b/crates/tests_integ/tests/sanity_checks.rs @@ -746,7 +746,6 @@ async fn tests_integ_date_cast() -> errors::Result<()> { let mut i = 0; for row in block.iter_rows() { - println!("{:?}", row); let res: DateTime = row.value(0)?.unwrap(); assert_eq!(res.date().naive_utc().to_string(), checks[i]); i += 1; @@ -782,8 +781,10 @@ async fn tests_integ_date_time_functions() -> errors::Result<()> { conn.execute(format!("DROP TABLE IF EXISTS test_tab_date")) .await?; - conn.execute(format!("CREATE TABLE test_tab_date(a Date, b DateTime)")) - .await?; + conn.execute(format!( + "CREATE TABLE test_tab_date(a Date, b DateTime)" + )) + .await?; let data_a = vec![ Utc.ymd(2010, 1, 1), @@ -803,10 +804,21 @@ async fn tests_integ_date_time_functions() -> errors::Result<()> { NaiveDate::from_ymd(2021, 8, 31).and_hms(14, 32, 3), NaiveDate::from_ymd(2021, 6, 27).and_hms(17, 44, 32), ]; + + let data_c = [ + Utc.ymd(2010, 1, 1).and_hms(0, 0, 0), + Utc.ymd(2011, 2, 28).and_hms(0, 0, 0), + Utc.ymd(2012, 2, 29).and_hms(0, 0, 0), + Utc.ymd(2012, 3, 4).and_hms(0, 0, 0), + Utc.ymd(2021, 8, 31).and_hms(0, 0, 0), + Utc.ymd(2021, 6, 27).and_hms(0, 0, 0), + ]; + let data_b: Vec<_> = data_b .into_iter() .map(|b| Utc.from_utc_datetime(&tz.from_local_datetime(&b).unwrap().naive_utc())) .collect(); + let years = vec![2010, 2011, 2012, 2012, 2021, 2021]; let months = vec![1, 2, 2, 3, 8, 6]; let quarters = vec![1, 1, 1, 1, 3, 2]; @@ -834,7 +846,7 @@ async fn tests_integ_date_time_functions() -> errors::Result<()> { toDayOfMonth(a), toDayOfMonth(b), \ toDayOfWeek(a), toDayOfWeek(b), \ toQuarter(a), toQuarter(b), \ - toHour(b), toMinute(b), toSecond(b) \ + toHour(b), toMinute(b), toSecond(b), toDate(b) \ from test_tab_date"; let mut query_result = conn.query(sql).await?; @@ -857,6 +869,7 @@ async fn tests_integ_date_time_functions() -> errors::Result<()> { let hour_b: u8 = row.value(iter.next().unwrap())?.unwrap(); let minute_b: u8 = row.value(iter.next().unwrap())?.unwrap(); let second_b: u8 = row.value(iter.next().unwrap())?.unwrap(); + let date1: DateTime = row.value(iter.next().unwrap())?.unwrap(); assert_eq!(year_a, years[i]); assert_eq!(year_b, years[i]); assert_eq!(month_a, months[i]); @@ -872,6 +885,7 @@ async fn tests_integ_date_time_functions() -> errors::Result<()> { assert_eq!(hour_b, hours[i]); assert_eq!(minute_b, minutes[i]); assert_eq!(second_b, seconds[i]); + assert_eq!(date1, data_c[i]); } } }