-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-11055: [Rust] [DataFusion] Support date_trunc function #9040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
777234e
Date trunc implementation
paveltiunov 3d83d92
Date trunc implementation: code format
paveltiunov fccb181
Date trunc implementation: SQL test
paveltiunov 0442902
Date trunc implementation: SQL test
paveltiunov 03caae4
Date trunc implementation: review fixes
paveltiunov 68d0cbc
Date trunc implementation: code format
paveltiunov 7a1a618
Date trunc implementation: clippy fix
paveltiunov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ use arrow::{ | |
| buffer::Buffer, | ||
| datatypes::{DataType, TimeUnit, ToByteSlice}, | ||
| }; | ||
| use chrono::Duration; | ||
| use chrono::{prelude::*, LocalResult}; | ||
|
|
||
| #[inline] | ||
|
|
@@ -205,6 +206,108 @@ pub fn to_timestamp(args: &[ArrayRef]) -> Result<TimestampNanosecondArray> { | |
| Ok(TimestampNanosecondArray::from(Arc::new(data))) | ||
| } | ||
|
|
||
| /// date_trunc SQL function | ||
| pub fn date_trunc(args: &[ArrayRef]) -> Result<TimestampNanosecondArray> { | ||
| let granularity_array = | ||
| &args[0] | ||
| .as_any() | ||
| .downcast_ref::<StringArray>() | ||
| .ok_or_else(|| { | ||
| DataFusionError::Execution( | ||
| "Could not cast date_trunc granularity input to StringArray" | ||
| .to_string(), | ||
| ) | ||
| })?; | ||
|
|
||
| let array = &args[1] | ||
| .as_any() | ||
| .downcast_ref::<TimestampNanosecondArray>() | ||
| .ok_or_else(|| { | ||
| DataFusionError::Execution( | ||
| "Could not cast date_trunc array input to TimestampNanosecondArray" | ||
| .to_string(), | ||
| ) | ||
| })?; | ||
|
|
||
| let range = 0..array.len(); | ||
| let result = range | ||
| .map(|i| { | ||
| if array.is_null(i) { | ||
| Ok(0_i64) | ||
| } else { | ||
| let date_time = match granularity_array.value(i) { | ||
| "second" => array | ||
| .value_as_datetime(i) | ||
| .and_then(|d| d.with_nanosecond(0)), | ||
| "minute" => array | ||
| .value_as_datetime(i) | ||
| .and_then(|d| d.with_nanosecond(0)) | ||
| .and_then(|d| d.with_second(0)), | ||
| "hour" => array | ||
| .value_as_datetime(i) | ||
| .and_then(|d| d.with_nanosecond(0)) | ||
| .and_then(|d| d.with_second(0)) | ||
| .and_then(|d| d.with_minute(0)), | ||
| "day" => array | ||
| .value_as_datetime(i) | ||
| .and_then(|d| d.with_nanosecond(0)) | ||
| .and_then(|d| d.with_second(0)) | ||
| .and_then(|d| d.with_minute(0)) | ||
| .and_then(|d| d.with_hour(0)), | ||
| "week" => array | ||
| .value_as_datetime(i) | ||
| .and_then(|d| d.with_nanosecond(0)) | ||
| .and_then(|d| d.with_second(0)) | ||
| .and_then(|d| d.with_minute(0)) | ||
| .and_then(|d| d.with_hour(0)) | ||
| .map(|d| { | ||
| d - Duration::seconds(60 * 60 * 24 * d.weekday() as i64) | ||
| }), | ||
| "month" => array | ||
| .value_as_datetime(i) | ||
| .and_then(|d| d.with_nanosecond(0)) | ||
| .and_then(|d| d.with_second(0)) | ||
| .and_then(|d| d.with_minute(0)) | ||
| .and_then(|d| d.with_hour(0)) | ||
| .and_then(|d| d.with_day0(0)), | ||
| "year" => array | ||
| .value_as_datetime(i) | ||
| .and_then(|d| d.with_nanosecond(0)) | ||
| .and_then(|d| d.with_second(0)) | ||
| .and_then(|d| d.with_minute(0)) | ||
| .and_then(|d| d.with_hour(0)) | ||
| .and_then(|d| d.with_day0(0)) | ||
| .and_then(|d| d.with_month0(0)), | ||
| unsupported => { | ||
| return Err(DataFusionError::Execution(format!( | ||
| "Unsupported date_trunc granularity: {}", | ||
| unsupported | ||
| ))) | ||
| } | ||
| }; | ||
| date_time.map(|d| d.timestamp_nanos()).ok_or_else(|| { | ||
| DataFusionError::Execution(format!( | ||
| "Can't truncate date time: {:?}", | ||
| array.value_as_datetime(i) | ||
| )) | ||
| }) | ||
| } | ||
| }) | ||
| .collect::<Result<Vec<_>>>()?; | ||
|
|
||
| let data = ArrayData::new( | ||
| DataType::Timestamp(TimeUnit::Nanosecond, None), | ||
| array.len(), | ||
| Some(array.null_count()), | ||
| array.data().null_buffer().cloned(), | ||
| 0, | ||
| vec![Buffer::from(result.to_byte_slice())], | ||
| vec![], | ||
| ); | ||
|
|
||
| Ok(TimestampNanosecondArray::from(Arc::new(data))) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
@@ -378,6 +481,64 @@ mod tests { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn date_trunc_test() -> Result<()> { | ||
| let mut ts_builder = StringBuilder::new(2); | ||
| let mut truncated_builder = StringBuilder::new(2); | ||
| let mut string_builder = StringBuilder::new(2); | ||
|
|
||
| ts_builder.append_null()?; | ||
| truncated_builder.append_null()?; | ||
| string_builder.append_value("second")?; | ||
|
|
||
| ts_builder.append_value("2020-09-08T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-09-08T13:42:29.000000Z")?; | ||
| string_builder.append_value("second")?; | ||
|
|
||
| ts_builder.append_value("2020-09-08T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-09-08T13:42:00.000000Z")?; | ||
| string_builder.append_value("minute")?; | ||
|
|
||
| ts_builder.append_value("2020-09-08T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-09-08T13:00:00.000000Z")?; | ||
| string_builder.append_value("hour")?; | ||
|
|
||
| ts_builder.append_value("2020-09-08T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-09-08T00:00:00.000000Z")?; | ||
| string_builder.append_value("day")?; | ||
|
|
||
| ts_builder.append_value("2020-09-08T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-09-07T00:00:00.000000Z")?; | ||
| string_builder.append_value("week")?; | ||
|
|
||
| ts_builder.append_value("2020-09-08T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-09-01T00:00:00.000000Z")?; | ||
| string_builder.append_value("month")?; | ||
|
|
||
| ts_builder.append_value("2020-09-08T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-01-01T00:00:00.000000Z")?; | ||
| string_builder.append_value("year")?; | ||
|
|
||
| ts_builder.append_value("2021-01-01T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2020-12-28T00:00:00.000000Z")?; | ||
| string_builder.append_value("week")?; | ||
|
|
||
| ts_builder.append_value("2020-01-01T13:42:29.190855Z")?; | ||
| truncated_builder.append_value("2019-12-30T00:00:00.000000Z")?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| string_builder.append_value("week")?; | ||
|
|
||
| let string_array = Arc::new(string_builder.finish()); | ||
| let ts_array = Arc::new(to_timestamp(&[Arc::new(ts_builder.finish())]).unwrap()); | ||
| let date_trunc_array = date_trunc(&[string_array, ts_array]) | ||
| .expect("that to_timestamp parsed values without error"); | ||
|
|
||
| let expected_timestamps = | ||
| to_timestamp(&[Arc::new(truncated_builder.finish())]).unwrap(); | ||
|
|
||
| assert_eq!(date_trunc_array, expected_timestamps); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn to_timestamp_invalid_input_type() -> Result<()> { | ||
| // pass the wrong type of input array to to_timestamp and test | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,8 @@ pub(crate) enum GroupByScalar { | |
| Int32(i32), | ||
| Int64(i64), | ||
| Utf8(Box<String>), | ||
| TimeMicrosecond(i64), | ||
| TimeNanosecond(i64), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
|
|
||
| impl TryFrom<&ScalarValue> for GroupByScalar { | ||
|
|
@@ -87,6 +89,8 @@ impl From<&GroupByScalar> for ScalarValue { | |
| GroupByScalar::UInt32(v) => ScalarValue::UInt32(Some(*v)), | ||
| GroupByScalar::UInt64(v) => ScalarValue::UInt64(Some(*v)), | ||
| GroupByScalar::Utf8(v) => ScalarValue::Utf8(Some(v.to_string())), | ||
| GroupByScalar::TimeMicrosecond(v) => ScalarValue::TimeMicrosecond(Some(*v)), | ||
| GroupByScalar::TimeNanosecond(v) => ScalarValue::TimeNanosecond(Some(*v)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This list seems consistent with https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC 👍 I think it is fine that we don't yeet support some of the more esoteric stuff (like milleniumm')