You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/06/16 15:33:15 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6654: Make 'date_trunc' returns the same type as its input

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


##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -282,54 +341,64 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> {
 
     Ok(match array {
         ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => {
-            let nano = (f)(*v)?;
-            match granularity.as_str() {
-                "minute" => {
-                    // trunc to minute
-                    let second = ScalarValue::TimestampNanosecond(
-                        Some(nano.unwrap() / 1_000_000_000 * 1_000_000_000),
-                        tz_opt.clone(),
-                    );
-                    ColumnarValue::Scalar(second)
+            _date_trunc(TimeUnit::Nanosecond, v, granularity.as_str(), f, tz_opt)?
+        }
+        ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(v, tz_opt)) => {
+            _date_trunc(TimeUnit::Microsecond, v, granularity.as_str(), f, tz_opt)?
+        }
+        ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(v, tz_opt)) => {
+            _date_trunc(TimeUnit::Millisecond, v, granularity.as_str(), f, tz_opt)?
+        }
+        ColumnarValue::Scalar(ScalarValue::TimestampSecond(v, tz_opt)) => {
+            _date_trunc(TimeUnit::Second, v, granularity.as_str(), f, tz_opt)?
+        }
+        ColumnarValue::Array(array) => {
+            let array_type = array.data_type();
+            match array_type {
+                DataType::Timestamp(TimeUnit::Second, _) => {
+                    let array = as_timestamp_second_array(array)?;
+                    let array = array
+                        .iter()
+                        .map(|x| {
+                            f(Some(x.unwrap() * 1_000_000_000))

Review Comment:
   I see this PR doesn't introduce the problem, but it appears that `date_trunc` panic's on `unwrap()` 🤔 
   
   ```
   ❯ select date_trunc('hour', '2021-02-01T12:01:02');
   +------------------------------------------------------+
   | date_trunc(Utf8("hour"),Utf8("2021-02-01T12:01:02")) |
   +------------------------------------------------------+
   | 2021-02-01T12:00:00                                  |
   +------------------------------------------------------+
   1 row in set. Query took 0.074 seconds.
   ❯ select date_trunc('hour', null);
   thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /Users/alamb/Software/arrow-datafusion/datafusion/physical-expr/src/datetime_expressions.rs:314:35
   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
   ```
   
   I will file a ticket: https://github.com/apache/arrow-datafusion/issues/6701



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org