Skip to content

Commit beeb631

Browse files
authored
Raise Unsupported SQL type for Time(WithTimeZone) and Time(Tz) (#3718)
* raise error for timetz * fix test cases
1 parent 27f3e90 commit beeb631

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

datafusion/core/tests/sql/timestamp.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,3 +1554,68 @@ async fn cast_timestamp_to_timestamptz() -> Result<()> {
15541554

15551555
Ok(())
15561556
}
1557+
1558+
#[tokio::test]
1559+
async fn test_cast_to_time() -> Result<()> {
1560+
let ctx = SessionContext::new();
1561+
let sql = "SELECT 0::TIME";
1562+
let actual = execute_to_batches(&ctx, sql).await;
1563+
1564+
let expected = vec![
1565+
"+----------+",
1566+
"| Int64(0) |",
1567+
"+----------+",
1568+
"| 00:00:00 |",
1569+
"+----------+",
1570+
];
1571+
assert_batches_eq!(expected, &actual);
1572+
1573+
Ok(())
1574+
}
1575+
1576+
#[tokio::test]
1577+
async fn test_cast_to_time_with_time_zone_should_not_work() -> Result<()> {
1578+
// this should not work until we implement tz for DataType::Time64
1579+
let ctx = SessionContext::new();
1580+
let sql = "SELECT 0::TIME WITH TIME ZONE";
1581+
let results = plan_and_collect(&ctx, sql).await.unwrap_err();
1582+
1583+
assert_eq!(
1584+
results.to_string(),
1585+
"This feature is not implemented: Unsupported SQL type Time(WithTimeZone)"
1586+
);
1587+
1588+
Ok(())
1589+
}
1590+
1591+
#[tokio::test]
1592+
async fn test_cast_to_time_without_time_zone() -> Result<()> {
1593+
let ctx = SessionContext::new();
1594+
let sql = "SELECT 0::TIME WITHOUT TIME ZONE";
1595+
let actual = execute_to_batches(&ctx, sql).await;
1596+
1597+
let expected = vec![
1598+
"+----------+",
1599+
"| Int64(0) |",
1600+
"+----------+",
1601+
"| 00:00:00 |",
1602+
"+----------+",
1603+
];
1604+
assert_batches_eq!(expected, &actual);
1605+
1606+
Ok(())
1607+
}
1608+
1609+
#[tokio::test]
1610+
async fn test_cast_to_timetz_should_not_work() -> Result<()> {
1611+
// this should not work until we implement tz for DataType::Time64
1612+
let ctx = SessionContext::new();
1613+
let sql = "SELECT 0::TIMETZ";
1614+
let results = plan_and_collect(&ctx, sql).await.unwrap_err();
1615+
1616+
assert_eq!(
1617+
results.to_string(),
1618+
"This feature is not implemented: Unsupported SQL type Time(Tz)"
1619+
);
1620+
Ok(())
1621+
}

datafusion/sql/src/planner.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,19 @@ pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
27202720
Ok(DataType::Timestamp(TimeUnit::Nanosecond, tz))
27212721
}
27222722
SQLDataType::Date => Ok(DataType::Date32),
2723-
SQLDataType::Time(_) => Ok(DataType::Time64(TimeUnit::Nanosecond)),
2723+
SQLDataType::Time(tz_info) => {
2724+
if matches!(tz_info, TimezoneInfo::None)
2725+
|| matches!(tz_info, TimezoneInfo::WithoutTimeZone)
2726+
{
2727+
Ok(DataType::Time64(TimeUnit::Nanosecond))
2728+
} else {
2729+
// We dont support TIMETZ and TIME WITH TIME ZONE for now
2730+
Err(DataFusionError::NotImplemented(format!(
2731+
"Unsupported SQL type {:?}",
2732+
sql_type
2733+
)))
2734+
}
2735+
}
27242736
SQLDataType::Decimal(precision, scale) => make_decimal_type(*precision, *scale),
27252737
SQLDataType::Bytea => Ok(DataType::Binary),
27262738
// Explicitly list all other types so that if sqlparser

0 commit comments

Comments
 (0)