-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Minor: Simplify date_trunc
code and add comments
#6783
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -214,7 +214,11 @@ fn quarter_month(date: &NaiveDateTime) -> u32 { | |
1 + 3 * ((date.month() - 1) / 3) | ||
} | ||
|
||
fn date_trunc_single(granularity: &str, value: i64) -> Result<i64> { | ||
/// Tuncates the single `value`, expressed in nanoseconds since the | ||
/// epoch, for granularities greater than 1 second, in taking into | ||
/// account that some granularities are not uniform durations of time | ||
/// (e.g. months are not always the same lengths, leap seconds, etc) | ||
fn date_trunc_coarse(granularity: &str, value: i64) -> Result<i64> { | ||
if granularity == "millisecond" || granularity == "microsecond" { | ||
return Ok(value); | ||
} | ||
|
@@ -266,11 +270,11 @@ fn date_trunc_single(granularity: &str, value: i64) -> Result<i64> { | |
Ok(value.unwrap().timestamp_nanos()) | ||
} | ||
|
||
// truncates a single value with the given timeunit to the specified granularity | ||
fn _date_trunc( | ||
tu: TimeUnit, | ||
value: &Option<i64>, | ||
granularity: &str, | ||
f: impl Fn(Option<i64>) -> Result<Option<i64>>, | ||
) -> Result<Option<i64>, DataFusionError> { | ||
let scale = match tu { | ||
TimeUnit::Second => 1_000_000_000, | ||
|
@@ -284,9 +288,7 @@ fn _date_trunc( | |
}; | ||
|
||
// convert to nanoseconds | ||
let Some(nano) = (f)(Some(value * scale))? else { | ||
return Ok(None); | ||
}; | ||
let nano = date_trunc_coarse(granularity, scale * value)?; | ||
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. There is no need for a closure, and the function can be called directly which I think makes things easier to understand |
||
|
||
let result = match tu { | ||
TimeUnit::Second => match granularity { | ||
|
@@ -328,29 +330,24 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> { | |
)); | ||
}; | ||
|
||
let f = |x: Option<i64>| { | ||
x.map(|x| date_trunc_single(granularity.as_str(), x)) | ||
.transpose() | ||
}; | ||
|
||
Ok(match array { | ||
ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => { | ||
let value = _date_trunc(TimeUnit::Nanosecond, v, granularity.as_str(), f)?; | ||
let value = _date_trunc(TimeUnit::Nanosecond, v, granularity.as_str())?; | ||
let value = ScalarValue::TimestampNanosecond(value, tz_opt.clone()); | ||
ColumnarValue::Scalar(value) | ||
} | ||
ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(v, tz_opt)) => { | ||
let value = _date_trunc(TimeUnit::Microsecond, v, granularity.as_str(), f)?; | ||
let value = _date_trunc(TimeUnit::Microsecond, v, granularity.as_str())?; | ||
let value = ScalarValue::TimestampMicrosecond(value, tz_opt.clone()); | ||
ColumnarValue::Scalar(value) | ||
} | ||
ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(v, tz_opt)) => { | ||
let value = _date_trunc(TimeUnit::Millisecond, v, granularity.as_str(), f)?; | ||
let value = _date_trunc(TimeUnit::Millisecond, v, granularity.as_str())?; | ||
let value = ScalarValue::TimestampMillisecond(value, tz_opt.clone()); | ||
ColumnarValue::Scalar(value) | ||
} | ||
ColumnarValue::Scalar(ScalarValue::TimestampSecond(v, tz_opt)) => { | ||
let value = _date_trunc(TimeUnit::Second, v, granularity.as_str(), f)?; | ||
let value = _date_trunc(TimeUnit::Second, v, granularity.as_str())?; | ||
let value = ScalarValue::TimestampSecond(value, tz_opt.clone()); | ||
ColumnarValue::Scalar(value) | ||
} | ||
|
@@ -361,9 +358,7 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> { | |
let array = as_timestamp_second_array(array)?; | ||
let array = array | ||
.iter() | ||
.map(|x| { | ||
_date_trunc(TimeUnit::Second, &x, granularity.as_str(), f) | ||
}) | ||
.map(|x| _date_trunc(TimeUnit::Second, &x, granularity.as_str())) | ||
.collect::<Result<TimestampSecondArray>>()?; | ||
ColumnarValue::Array(Arc::new(array)) | ||
} | ||
|
@@ -372,12 +367,7 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> { | |
let array = array | ||
.iter() | ||
.map(|x| { | ||
_date_trunc( | ||
TimeUnit::Millisecond, | ||
&x, | ||
granularity.as_str(), | ||
f, | ||
) | ||
_date_trunc(TimeUnit::Millisecond, &x, granularity.as_str()) | ||
}) | ||
.collect::<Result<TimestampMillisecondArray>>()?; | ||
ColumnarValue::Array(Arc::new(array)) | ||
|
@@ -387,12 +377,7 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> { | |
let array = array | ||
.iter() | ||
.map(|x| { | ||
_date_trunc( | ||
TimeUnit::Microsecond, | ||
&x, | ||
granularity.as_str(), | ||
f, | ||
) | ||
_date_trunc(TimeUnit::Microsecond, &x, granularity.as_str()) | ||
}) | ||
.collect::<Result<TimestampMicrosecondArray>>()?; | ||
ColumnarValue::Array(Arc::new(array)) | ||
|
@@ -402,7 +387,7 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> { | |
let array = array | ||
.iter() | ||
.map(|x| { | ||
_date_trunc(TimeUnit::Nanosecond, &x, granularity.as_str(), f) | ||
_date_trunc(TimeUnit::Nanosecond, &x, granularity.as_str()) | ||
}) | ||
.collect::<Result<TimestampNanosecondArray>>()?; | ||
|
||
|
@@ -996,7 +981,7 @@ mod tests { | |
cases.iter().for_each(|(original, granularity, expected)| { | ||
let left = string_to_timestamp_nanos(original).unwrap(); | ||
let right = string_to_timestamp_nanos(expected).unwrap(); | ||
let result = date_trunc_single(granularity, left).unwrap(); | ||
let result = date_trunc_coarse(granularity, left).unwrap(); | ||
assert_eq!(result, right, "{original} = {expected}"); | ||
}); | ||
} | ||
|
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.
I think this is correct -- @Weijun-H does it match your understanding?
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.
Nice simplification 👍 !