From 97f727bac37924477d43e139ff003c07980cb791 Mon Sep 17 00:00:00 2001 From: Wei-Ting Kuo Date: Wed, 5 Oct 2022 05:03:46 +0800 Subject: [PATCH 1/2] raise error for timetz --- datafusion/core/tests/sql/timestamp.rs | 75 ++++++++++++++++++++++++++ datafusion/sql/src/planner.rs | 14 ++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/datafusion/core/tests/sql/timestamp.rs b/datafusion/core/tests/sql/timestamp.rs index 29119792531..fb346d23682 100644 --- a/datafusion/core/tests/sql/timestamp.rs +++ b/datafusion/core/tests/sql/timestamp.rs @@ -1554,3 +1554,78 @@ async fn cast_timestamp_to_timestamptz() -> Result<()> { Ok(()) } + +#[tokio::test] +async fn test_cast_to_time() -> Result<()> { + let ctx = SessionContext::new(); + let sql = "SELECT 0::TIME"; + let actual = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+----------+", + "| Int64(0) |", + "+----------+", + "| 00:00:00 |", + "+----------+", + ]; + assert_batches_eq!(expected, &actual); + + Ok(()) +} + +#[tokio::test] +async fn test_cast_to_time_with_time_zone_should_not_work() -> Result<()> { + // this should not work until we implement tz for DataType::Time64 + let ctx = SessionContext::new(); + let sql = "SELECT 0::TIME WITH TIME ZONE"; + let actual = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+----------+", + "| Int64(0) |", + "+----------+", + "| 00:00:00 |", + "+----------+", + ]; + assert_batches_eq!(expected, &actual); + + Ok(()) +} + +#[tokio::test] +async fn test_cast_to_time_without_time_zone() -> Result<()> { + + let ctx = SessionContext::new(); + let sql = "SELECT 0::TIME WITHOUT TIME ZONE"; + let actual = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+----------+", + "| Int64(0) |", + "+----------+", + "| 00:00:00 |", + "+----------+", + ]; + assert_batches_eq!(expected, &actual); + + Ok(()) +} + +#[tokio::test] +async fn test_cast_to_timetz_should_not_work() -> Result<()> { + // this should not work until we implement tz for DataType::Time64 + let ctx = SessionContext::new(); + let sql = "SELECT 0::TIMETZ"; + let actual = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+----------+", + "| Int64(0) |", + "+----------+", + "| 00:00:00 |", + "+----------+", + ]; + assert_batches_eq!(expected, &actual); + + Ok(()) +} \ No newline at end of file diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rs index ef57be27f33..a619136d8cb 100644 --- a/datafusion/sql/src/planner.rs +++ b/datafusion/sql/src/planner.rs @@ -2714,7 +2714,19 @@ pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result { Ok(DataType::Timestamp(TimeUnit::Nanosecond, tz)) } SQLDataType::Date => Ok(DataType::Date32), - SQLDataType::Time(_) => Ok(DataType::Time64(TimeUnit::Nanosecond)), + SQLDataType::Time(tz_info) => { + if matches!(tz_info, TimezoneInfo::None) + || matches!(tz_info, TimezoneInfo::WithoutTimeZone) + { + Ok(DataType::Time64(TimeUnit::Nanosecond)) + } else { + // We dont support TIMETZ and TIME WITH TIME ZONE for now + Err(DataFusionError::NotImplemented(format!( + "Unsupported SQL type {:?}", + sql_type + ))) + } + } SQLDataType::Decimal(precision, scale) => make_decimal_type(*precision, *scale), SQLDataType::Bytea => Ok(DataType::Binary), // Explicitly list all other types so that if sqlparser From 94da7fc98f0a16203cfba49e9cdd844642243319 Mon Sep 17 00:00:00 2001 From: Wei-Ting Kuo Date: Wed, 5 Oct 2022 05:30:59 +0800 Subject: [PATCH 2/2] fix test cases --- datafusion/core/tests/sql/timestamp.rs | 32 +++++++++----------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/datafusion/core/tests/sql/timestamp.rs b/datafusion/core/tests/sql/timestamp.rs index fb346d23682..b6cf2bb6753 100644 --- a/datafusion/core/tests/sql/timestamp.rs +++ b/datafusion/core/tests/sql/timestamp.rs @@ -1578,23 +1578,18 @@ async fn test_cast_to_time_with_time_zone_should_not_work() -> Result<()> { // this should not work until we implement tz for DataType::Time64 let ctx = SessionContext::new(); let sql = "SELECT 0::TIME WITH TIME ZONE"; - let actual = execute_to_batches(&ctx, sql).await; + let results = plan_and_collect(&ctx, sql).await.unwrap_err(); - let expected = vec![ - "+----------+", - "| Int64(0) |", - "+----------+", - "| 00:00:00 |", - "+----------+", - ]; - assert_batches_eq!(expected, &actual); + assert_eq!( + results.to_string(), + "This feature is not implemented: Unsupported SQL type Time(WithTimeZone)" + ); Ok(()) } #[tokio::test] async fn test_cast_to_time_without_time_zone() -> Result<()> { - let ctx = SessionContext::new(); let sql = "SELECT 0::TIME WITHOUT TIME ZONE"; let actual = execute_to_batches(&ctx, sql).await; @@ -1616,16 +1611,11 @@ async fn test_cast_to_timetz_should_not_work() -> Result<()> { // this should not work until we implement tz for DataType::Time64 let ctx = SessionContext::new(); let sql = "SELECT 0::TIMETZ"; - let actual = execute_to_batches(&ctx, sql).await; - - let expected = vec![ - "+----------+", - "| Int64(0) |", - "+----------+", - "| 00:00:00 |", - "+----------+", - ]; - assert_batches_eq!(expected, &actual); + let results = plan_and_collect(&ctx, sql).await.unwrap_err(); + assert_eq!( + results.to_string(), + "This feature is not implemented: Unsupported SQL type Time(Tz)" + ); Ok(()) -} \ No newline at end of file +}