Skip to content

Commit 15a9a4b

Browse files
authored
feat: Add support for TIME literal values (#3010)
* feat: Initial work to add TIME literal value support * chore: Improve unit test coverage * chore: Improve test names and simplify test
1 parent 49a3b00 commit 15a9a4b

3 files changed

Lines changed: 84 additions & 4 deletions

File tree

‎datafusion/common/src/scalar.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub enum ScalarValue {
8383
Date32(Option<i32>),
8484
/// Date stored as a signed 64bit int milliseconds since UNIX epoch 1970-01-01
8585
Date64(Option<i64>),
86+
/// Time stored as a signed 64bit int as nanoseconds since midnight
87+
Time64(Option<i64>),
8688
/// Timestamp Second
8789
TimestampSecond(Option<i64>, Option<String>),
8890
/// Timestamp Milliseconds
@@ -163,6 +165,8 @@ impl PartialEq for ScalarValue {
163165
(Date32(_), _) => false,
164166
(Date64(v1), Date64(v2)) => v1.eq(v2),
165167
(Date64(_), _) => false,
168+
(Time64(v1), Time64(v2)) => v1.eq(v2),
169+
(Time64(_), _) => false,
166170
(TimestampSecond(v1, _), TimestampSecond(v2, _)) => v1.eq(v2),
167171
(TimestampSecond(_, _), _) => false,
168172
(TimestampMillisecond(v1, _), TimestampMillisecond(v2, _)) => v1.eq(v2),
@@ -255,6 +259,8 @@ impl PartialOrd for ScalarValue {
255259
(Date32(_), _) => None,
256260
(Date64(v1), Date64(v2)) => v1.partial_cmp(v2),
257261
(Date64(_), _) => None,
262+
(Time64(v1), Time64(v2)) => v1.partial_cmp(v2),
263+
(Time64(_), _) => None,
258264
(TimestampSecond(v1, _), TimestampSecond(v2, _)) => v1.partial_cmp(v2),
259265
(TimestampSecond(_, _), _) => None,
260266
(TimestampMillisecond(v1, _), TimestampMillisecond(v2, _)) => {
@@ -338,6 +344,7 @@ impl std::hash::Hash for ScalarValue {
338344
}
339345
Date32(v) => v.hash(state),
340346
Date64(v) => v.hash(state),
347+
Time64(v) => v.hash(state),
341348
TimestampSecond(v, _) => v.hash(state),
342349
TimestampMillisecond(v, _) => v.hash(state),
343350
TimestampMicrosecond(v, _) => v.hash(state),
@@ -681,6 +688,7 @@ impl ScalarValue {
681688
))),
682689
ScalarValue::Date32(_) => DataType::Date32,
683690
ScalarValue::Date64(_) => DataType::Date64,
691+
ScalarValue::Time64(_) => DataType::Time64(TimeUnit::Nanosecond),
684692
ScalarValue::IntervalYearMonth(_) => {
685693
DataType::Interval(IntervalUnit::YearMonth)
686694
}
@@ -741,6 +749,7 @@ impl ScalarValue {
741749
ScalarValue::List(v, _) => v.is_none(),
742750
ScalarValue::Date32(v) => v.is_none(),
743751
ScalarValue::Date64(v) => v.is_none(),
752+
ScalarValue::Time64(v) => v.is_none(),
744753
ScalarValue::TimestampSecond(v, _) => v.is_none(),
745754
ScalarValue::TimestampMillisecond(v, _) => v.is_none(),
746755
ScalarValue::TimestampMicrosecond(v, _) => v.is_none(),
@@ -963,6 +972,9 @@ impl ScalarValue {
963972
DataType::LargeBinary => build_array_string!(LargeBinaryArray, LargeBinary),
964973
DataType::Date32 => build_array_primitive!(Date32Array, Date32),
965974
DataType::Date64 => build_array_primitive!(Date64Array, Date64),
975+
DataType::Time64(TimeUnit::Nanosecond) => {
976+
build_array_primitive!(Time64NanosecondArray, Time64)
977+
}
966978
DataType::Timestamp(TimeUnit::Second, _) => {
967979
build_array_primitive_tz!(TimestampSecondArray, TimestampSecond)
968980
}
@@ -1357,6 +1369,15 @@ impl ScalarValue {
13571369
ScalarValue::Date64(e) => {
13581370
build_array_from_option!(Date64, Date64Array, e, size)
13591371
}
1372+
ScalarValue::Time64(e) => {
1373+
build_array_from_option!(
1374+
Time64,
1375+
TimeUnit::Nanosecond,
1376+
Time64NanosecondArray,
1377+
e,
1378+
size
1379+
)
1380+
}
13601381
ScalarValue::IntervalDayTime(e) => build_array_from_option!(
13611382
Interval,
13621383
IntervalUnit::DayTime,
@@ -1496,6 +1517,9 @@ impl ScalarValue {
14961517
DataType::Date64 => {
14971518
typed_cast!(array, index, Date64Array, Date64)
14981519
}
1520+
DataType::Time64(TimeUnit::Nanosecond) => {
1521+
typed_cast!(array, index, Time64NanosecondArray, Time64)
1522+
}
14991523
DataType::Timestamp(TimeUnit::Second, tz_opt) => {
15001524
typed_cast_tz!(
15011525
array,
@@ -1691,6 +1715,9 @@ impl ScalarValue {
16911715
ScalarValue::Date64(val) => {
16921716
eq_array_primitive!(array, index, Date64Array, val)
16931717
}
1718+
ScalarValue::Time64(val) => {
1719+
eq_array_primitive!(array, index, Time64NanosecondArray, val)
1720+
}
16941721
ScalarValue::TimestampSecond(val, _) => {
16951722
eq_array_primitive!(array, index, TimestampSecondArray, val)
16961723
}
@@ -1845,6 +1872,7 @@ impl TryFrom<ScalarValue> for i64 {
18451872
match value {
18461873
ScalarValue::Int64(Some(inner_value))
18471874
| ScalarValue::Date64(Some(inner_value))
1875+
| ScalarValue::Time64(Some(inner_value))
18481876
| ScalarValue::TimestampNanosecond(Some(inner_value), _)
18491877
| ScalarValue::TimestampMicrosecond(Some(inner_value), _)
18501878
| ScalarValue::TimestampMillisecond(Some(inner_value), _)
@@ -1906,6 +1934,7 @@ impl TryFrom<&DataType> for ScalarValue {
19061934
DataType::LargeUtf8 => ScalarValue::LargeUtf8(None),
19071935
DataType::Date32 => ScalarValue::Date32(None),
19081936
DataType::Date64 => ScalarValue::Date64(None),
1937+
DataType::Time64(TimeUnit::Nanosecond) => ScalarValue::Time64(None),
19091938
DataType::Timestamp(TimeUnit::Second, tz_opt) => {
19101939
ScalarValue::TimestampSecond(None, tz_opt.clone())
19111940
}
@@ -2007,6 +2036,7 @@ impl fmt::Display for ScalarValue {
20072036
},
20082037
ScalarValue::Date32(e) => format_option!(f, e)?,
20092038
ScalarValue::Date64(e) => format_option!(f, e)?,
2039+
ScalarValue::Time64(e) => format_option!(f, e)?,
20102040
ScalarValue::IntervalDayTime(e) => format_option!(f, e)?,
20112041
ScalarValue::IntervalYearMonth(e) => format_option!(f, e)?,
20122042
ScalarValue::IntervalMonthDayNano(e) => format_option!(f, e)?,
@@ -2067,6 +2097,7 @@ impl fmt::Debug for ScalarValue {
20672097
ScalarValue::List(_, _) => write!(f, "List([{}])", self),
20682098
ScalarValue::Date32(_) => write!(f, "Date32(\"{}\")", self),
20692099
ScalarValue::Date64(_) => write!(f, "Date64(\"{}\")", self),
2100+
ScalarValue::Time64(_) => write!(f, "Time64(\"{}\")", self),
20702101
ScalarValue::IntervalDayTime(_) => {
20712102
write!(f, "IntervalDayTime(\"{}\")", self)
20722103
}
@@ -2665,6 +2696,7 @@ mod tests {
26652696
make_binary_test_case!(str_vals, LargeBinaryArray, LargeBinary),
26662697
make_test_case!(i32_vals, Date32Array, Date32),
26672698
make_test_case!(i64_vals, Date64Array, Date64),
2699+
make_test_case!(i64_vals, Time64NanosecondArray, Time64),
26682700
make_test_case!(i64_vals, TimestampSecondArray, TimestampSecond, None),
26692701
make_test_case!(
26702702
i64_vals,

‎datafusion/core/tests/sql/timestamp.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,44 @@ async fn sub_interval_day() -> Result<()> {
986986
Ok(())
987987
}
988988

989+
#[tokio::test]
990+
async fn cast_string_to_time() {
991+
let ctx = SessionContext::new();
992+
993+
let sql = "select \
994+
time '08:09:10.123456789' as time_nano, \
995+
time '13:14:15.123456' as time_micro,\
996+
time '13:14:15.123' as time_milli,\
997+
time '13:14:15' as time;";
998+
let results = execute_to_batches(&ctx, sql).await;
999+
1000+
let expected = vec![
1001+
"+--------------------+-----------------+--------------+----------+",
1002+
"| time_nano | time_micro | time_milli | time |",
1003+
"+--------------------+-----------------+--------------+----------+",
1004+
"| 08:09:10.123456789 | 13:14:15.123456 | 13:14:15.123 | 13:14:15 |",
1005+
"+--------------------+-----------------+--------------+----------+",
1006+
];
1007+
assert_batches_eq!(expected, &results);
1008+
1009+
// Fallible cases
1010+
1011+
let sql = "SELECT TIME 'not a time' as time;";
1012+
let result = try_execute_to_batches(&ctx, sql).await;
1013+
assert_eq!(
1014+
result.err().unwrap().to_string(),
1015+
"Arrow error: Cast error: Cannot cast string 'not a time' to value of Time64(Nanosecond) type"
1016+
);
1017+
1018+
// An invalid time
1019+
let sql = "SELECT TIME '24:01:02' as time;";
1020+
let result = try_execute_to_batches(&ctx, sql).await;
1021+
assert_eq!(
1022+
result.err().unwrap().to_string(),
1023+
"Arrow error: Cast error: Cannot cast string '24:01:02' to value of Time64(Nanosecond) type"
1024+
);
1025+
}
1026+
9891027
#[tokio::test]
9901028
async fn cast_to_timestamp_twice() -> Result<()> {
9911029
let ctx = SessionContext::new();

‎datafusion/sql/src/planner.rs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
496496
SQLDataType::Double => Ok(DataType::Float64),
497497
SQLDataType::Boolean => Ok(DataType::Boolean),
498498
SQLDataType::Date => Ok(DataType::Date32),
499-
SQLDataType::Time => Ok(DataType::Time64(TimeUnit::Millisecond)),
499+
SQLDataType::Time => Ok(DataType::Time64(TimeUnit::Nanosecond)),
500500
SQLDataType::Timestamp => Ok(DataType::Timestamp(TimeUnit::Nanosecond, None)),
501501
_ => Err(DataFusionError::NotImplemented(format!(
502502
"The SQL data type {:?} is not implemented",
@@ -2563,6 +2563,7 @@ pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
25632563
| SQLDataType::String => Ok(DataType::Utf8),
25642564
SQLDataType::Timestamp => Ok(DataType::Timestamp(TimeUnit::Nanosecond, None)),
25652565
SQLDataType::Date => Ok(DataType::Date32),
2566+
SQLDataType::Time => Ok(DataType::Time64(TimeUnit::Nanosecond)),
25662567
SQLDataType::Decimal(precision, scale) => make_decimal_type(*precision, *scale),
25672568
SQLDataType::Binary(_) => Ok(DataType::Binary),
25682569
SQLDataType::Bytea => Ok(DataType::Binary),
@@ -4360,10 +4361,19 @@ mod tests {
43604361
}
43614362

43624363
#[test]
4363-
fn select_typedstring() {
4364-
let sql = "SELECT date '2020-12-10' AS date FROM person";
4364+
fn select_typed_date_string() {
4365+
let sql = "SELECT date '2020-12-10' AS date";
43654366
let expected = "Projection: CAST(Utf8(\"2020-12-10\") AS Date32) AS date\
4366-
\n TableScan: person";
4367+
\n EmptyRelation";
4368+
quick_test(sql, expected);
4369+
}
4370+
4371+
#[test]
4372+
fn select_typed_time_string() {
4373+
let sql = "SELECT TIME '08:09:10.123' AS time";
4374+
let expected =
4375+
"Projection: CAST(Utf8(\"08:09:10.123\") AS Time64(Nanosecond)) AS time\
4376+
\n EmptyRelation";
43674377
quick_test(sql, expected);
43684378
}
43694379

0 commit comments

Comments
 (0)