Skip to content

Commit

Permalink
Raise Unsupported SQL type for Time(WithTimeZone) and Time(Tz) (#…
Browse files Browse the repository at this point in the history
…3718)

* raise error for timetz

* fix test cases
  • Loading branch information
waitingkuo committed Oct 5, 2022
1 parent 27f3e90 commit beeb631
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
65 changes: 65 additions & 0 deletions datafusion/core/tests/sql/timestamp.rs
Expand Up @@ -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(())
}
14 changes: 13 additions & 1 deletion datafusion/sql/src/planner.rs
Expand Up @@ -2720,7 +2720,19 @@ pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
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
Expand Down

0 comments on commit beeb631

Please sign in to comment.