You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "comphead (via GitHub)" <gi...@apache.org> on 2023/03/12 01:52:39 UTC

[GitHub] [arrow-datafusion] comphead commented on a diff in pull request #5555: feat: extract (epoch from col)

comphead commented on code in PR #5555:
URL: https://github.com/apache/arrow-datafusion/pull/5555#discussion_r1133172510


##########
datafusion/core/tests/sql/expr.rs:
##########
@@ -1313,6 +1313,23 @@ async fn test_extract_date_part() -> Result<()> {
     Ok(())
 }
 
+#[tokio::test]
+async fn test_extract_epoch() -> Result<()> {
+    // test_expression!(

Review Comment:
   Thanks @Weijun-H are this commented tests needed? 



##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -537,6 +539,35 @@ where
     to_ticks(array, 1_000_000_000)
 }
 
+fn epoch<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>
+where
+    T: ArrowTemporalType + ArrowNumericType,
+    i64: From<T::Native>,
+{
+    let mut b = Float64Builder::with_capacity(array.len());
+    match array.data_type() {
+        DataType::Timestamp(tu, _) => {
+            for i in 0..array.len() {
+                if array.is_null(i) {
+                    b.append_null();
+                } else {
+                    let scale = match tu {
+                        TimeUnit::Second => 1,
+                        TimeUnit::Millisecond => 1_000,
+                        TimeUnit::Microsecond => 1_000_000,
+                        TimeUnit::Nanosecond => 1_000_000_000,
+                    };
+
+                    let n: i64 = array.value(i).into();
+                    b.append_value(n as f64 / scale as f64);
+                }
+            }
+        }
+        _ => return Err(DataFusionError::Internal("Invalid data type".to_string())),

Review Comment:
   it might be useful to output the wrong datatype in error message



-- 
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