alamb commented on code in PR #6654:
URL: https://github.com/apache/arrow-datafusion/pull/6654#discussion_r1245581308


##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -215,13 +215,17 @@ fn quarter_month(date: &NaiveDateTime) -> u32 {
 }
 
 fn date_trunc_single(granularity: &str, value: i64) -> Result<i64> {
+    if granularity == "millisecond" || granularity == "microsecond" {
+        return Ok(value);
+    }

Review Comment:
   > You mean with_nanosecond also cleans up millisecond and microsecond in the 
timestamp? Otherwise nanosecond is smaller granularity than millisecond and 
microsecond. Why reduction at nanosecond level will affect millisecond and 
microsecond?
   
   Yes, I think that is correct. 
   
   I played around with it and we can simplify the logic like this which I 
think may make it easier to understand (the test still pass). If you like this 
better (I do) I can make it a PR (follow on to 
https://github.com/apache/arrow-datafusion/pull/6783)
   
   ```diff
   diff --git a/datafusion/physical-expr/src/datetime_expressions.rs 
b/datafusion/physical-expr/src/datetime_expressions.rs
   index 3a36e8a489..89ec508024 100644
   --- a/datafusion/physical-expr/src/datetime_expressions.rs
   +++ b/datafusion/physical-expr/src/datetime_expressions.rs
   @@ -215,42 +215,49 @@ fn quarter_month(date: &NaiveDateTime) -> u32 {
    }
    
    fn date_trunc_single(granularity: &str, value: i64) -> Result<i64> {
   -    if granularity == "millisecond" || granularity == "microsecond" {
   -        return Ok(value);
   -    }
   +    let value = timestamp_ns_to_datetime(value).ok_or_else(|| {
   +        DataFusionError::Execution(format!("Timestamp {value} out of 
range"))
   +    })?;
   +
   +    let value = Some(value);
    
   -    let value = timestamp_ns_to_datetime(value)
   -        .ok_or_else(|| {
   -            DataFusionError::Execution(format!("Timestamp {value} out of 
range"))
   -        })?
   -        .with_nanosecond(0);
        let value = match granularity {
   -        "second" => value,
   -        "minute" => value.and_then(|d| d.with_second(0)),
   +        "millisecond" => value,
   +        "microsecond" => value,
   +        "second" => value.and_then(|d| d.with_nanosecond(0)),
   +        "minute" => value
   +            .and_then(|d| d.with_nanosecond(0))
   +            .and_then(|d| d.with_second(0)),
            "hour" => value
   +            .and_then(|d| d.with_nanosecond(0))
                .and_then(|d| d.with_second(0))
                .and_then(|d| d.with_minute(0)),
            "day" => value
   +            .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" => value
   +            .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" => value
   +            .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)),
            "quarter" => value
   +            .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_month(quarter_month(&d))),
            "year" => value
   +            .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))
   
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to