Skip to content

Commit

Permalink
FEAT(server,tests): impl to_date(timestamp32) DF function
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Mingjian <jin.phd@gmail.com>
  • Loading branch information
pandaplusplus authored and jinmingjian committed Jul 5, 2021
1 parent b311c8a commit f68756a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
28 changes: 28 additions & 0 deletions crates/datafusion_tests/tests/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ mod tests {
use base::datetimes::BaseTimeZone;
use datafusion::physical_plan::clickhouse::*;

#[test]
fn test_to_date() {
let a: PrimitiveArray<Timestamp32Type> =
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<i32> = (0..4096).collect();
let a: PrimitiveArray<Timestamp32Type> = 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<Date16Type> = vec![Some(1), None, Some(366)].into();
Expand Down
1 change: 1 addition & 0 deletions crates/server/tests/confs/base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ data_dirs_clickhouse = ""
[server]
ip_addr = "localhost"
port = 9528
timezone = "Etc/GMT-8"
22 changes: 18 additions & 4 deletions crates/tests_integ/tests/sanity_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utc> = row.value(0)?.unwrap();
assert_eq!(res.date().naive_utc().to_string(), checks[i]);
i += 1;
Expand Down Expand Up @@ -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),
Expand All @@ -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];
Expand Down Expand Up @@ -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?;

Expand All @@ -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<Utc> = row.value(iter.next().unwrap())?.unwrap();
assert_eq!(year_a, years[i]);
assert_eq!(year_b, years[i]);
assert_eq!(month_a, months[i]);
Expand All @@ -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]);
}
}
}
Expand Down

0 comments on commit f68756a

Please sign in to comment.