diff --git a/datafusion/core/tests/sql/timestamp.rs b/datafusion/core/tests/sql/timestamp.rs index 29119792531..b6cf2bb6753 100644 --- a/datafusion/core/tests/sql/timestamp.rs +++ b/datafusion/core/tests/sql/timestamp.rs @@ -1554,3 +1554,68 @@ 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 results = plan_and_collect(&ctx, sql).await.unwrap_err(); + + 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; + + 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 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(()) +} diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rs index a5374cb4a1a..f3a0c4850fd 100644 --- a/datafusion/sql/src/planner.rs +++ b/datafusion/sql/src/planner.rs @@ -2720,7 +2720,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