You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/01 05:04:10 UTC

[GitHub] [arrow-datafusion] naosense opened a new pull request, #4054: Implement `current_time` scalar function

naosense opened a new pull request, #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #3982 .
   
   # Rationale for this change
   DataFusion users need to be able to use the current time to calculate things like "all data in the last 5 hours"
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   Scalar `current_time` function
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


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


[GitHub] [arrow-datafusion] naosense commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
naosense commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298092286

   I followed @comphead 's `current_date` test case
   ```
   let sql = "select case when current_time() = cast(now() as time) then 'OK' else 'FAIL' end result";
       let results = execute_to_batches(&ctx, sql).await;
   
       let expected = vec![
           "+--------+",
           "| result |",
           "+--------+",
           "| OK     |",
           "+--------+",
       ];
   
       assert_batches_eq!(expected, &results);
   ```
   but it reported an error
   ```
   thread 'sql::timestamp::test_current_time' panicked at 'called `Result::unwrap()` on an `Err` value: "NotImplemented(\"Unsupported CAST from Timestamp(Nanosecond, Some(\\\"UTC\\\")) to Time64(Nanosecond)\") at Creating physical plan for 'select case when current_time() = cast(now() as time) then 'OK' else 'FAIL' end result': Projection: CASE WHEN currenttime() = CAST(now() AS Time64(Nanosecond)) THEN Utf8(\"OK\") ELSE Utf8(\"FAIL\") END AS result\n  EmptyRelation"', datafusion/core/tests/sql/mod.rs:806:10
   ```
   Is anything i missed?


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


[GitHub] [arrow-datafusion] waitingkuo commented on a diff in pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#discussion_r1010313227


##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -200,6 +200,20 @@ pub fn make_current_date(
     move |_arg| Ok(ColumnarValue::Scalar(ScalarValue::Date32(days)))
 }
 
+/// Create an implementation of `current_time()` that always returns the
+/// specified current time.
+///
+/// The semantics of `current_time()` require it to return the same value
+/// wherever it appears within a single statement. This value is
+/// chosen during planning time.
+pub fn make_current_time(
+    now_ts: DateTime<Utc>,
+) -> impl Fn(&[ColumnarValue]) -> Result<ColumnarValue> {
+    let nano =
+        Duration::seconds(now_ts.num_seconds_from_midnight() as i64).num_nanoseconds();

Review Comment:
   i think you might not need Duration::seconds here.
   
   ```rust
   let nano = (now_ts.num_seconds_from_midnight() as i64) * 1000000000 + (now_ts.nanosecond() as i64);
   ```
   
    [1] [nanosecond](https://docs.rs/chrono/latest/chrono/trait.Timelike.html#tymethod.nanosecond)



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


[GitHub] [arrow-datafusion] waitingkuo commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298087636

   LGTM, thank you @naosense 


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


[GitHub] [arrow-datafusion] alamb commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298813043

   Hi @naosense  -- it seems this PR needs to have `cargo fmt` to run on it in order to pass CI. I took the liberty of doing so (and merging up from master) to prevent another 24 hour cycle


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


[GitHub] [arrow-datafusion] naosense commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
naosense commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298438498

   > @waitingkuo 
   > 
   > i'll be great if you could fire a issue (or even a pr) to arrow-rs to support  cast Timestamp to Time
   
   it's a pleasure, I'll investigate how to do that
   
   


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


[GitHub] [arrow-datafusion] mingmwang commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
mingmwang commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298114262

   If there are multiple current_time() in a SQL statement, will it be invoked multiple times or just once ?


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


[GitHub] [arrow-datafusion] waitingkuo commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298151410

   @naosense 
   you can do the following for now if you want to add that test case
   ```bash
   ❯ select (now()::bigint % 86400000000000)::time;
   +-------------------------------+
   | now() % Int64(86400000000000) |
   +-------------------------------+
   | 07:41:27.421667               |
   +-------------------------------+
   ```
   
   i'll be great if you could fire a issue (or even a pr) to arrow-rs


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


[GitHub] [arrow-datafusion] naosense commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
naosense commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298438483

   > @waitingkuo 
   > 
   > i'll be great if you could fire a issue (or even a pr) to arrow-rs to support  cast Timestamp to Time
   
   it's a pleasure, I'll investigate how to do that
   
   


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


[GitHub] [arrow-datafusion] waitingkuo commented on a diff in pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#discussion_r1010323470


##########
datafusion/core/tests/sql/timestamp.rs:
##########
@@ -1651,3 +1651,34 @@ async fn test_current_date() -> Result<()> {
 
     Ok(())
 }
+
+#[tokio::test]
+async fn test_current_time() -> Result<()> {
+    let ctx = SessionContext::new();
+
+    let sql = "select current_time() dt";
+    let results = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        results[0]
+            .schema()
+            .field_with_name("dt")
+            .unwrap()
+            .data_type()
+            .to_owned(),
+        DataType::Time64(TimeUnit::Nanosecond)
+    );
+
+    let sql = "select case when current_time() = (now()::bigint % 86400000000000)::time then 'OK' else 'FAIL' end result";
+    let results = execute_to_batches(&ctx, sql).await;
+
+    let expected = vec![
+        "+--------+",
+        "| result |",
+        "+--------+",
+        "| OK     |",
+        "+--------+",
+    ];

Review Comment:
   👍 



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


[GitHub] [arrow-datafusion] alamb merged pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
alamb merged PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054


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


[GitHub] [arrow-datafusion] naosense commented on a diff in pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
naosense commented on code in PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#discussion_r1010241100


##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -200,6 +200,20 @@ pub fn make_current_date(
     move |_arg| Ok(ColumnarValue::Scalar(ScalarValue::Date32(days)))
 }
 
+/// Create an implementation of `current_time()` that always returns the
+/// specified current time.
+///
+/// The semantics of `current_time()` require it to return the same value
+/// wherever it appears within a single statement. This value is
+/// chosen during planning time.
+pub fn make_current_time(
+    now_ts: DateTime<Utc>,
+) -> impl Fn(&[ColumnarValue]) -> Result<ColumnarValue> {
+    let nano =
+        Duration::seconds(now_ts.num_seconds_from_midnight() as i64).num_nanoseconds();

Review Comment:
   I found a bug, nanoseconds less than a second will be loss if `num_seconds_from_midnight()` is used,     I will do  this in another way



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


[GitHub] [arrow-datafusion] waitingkuo commented on a diff in pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#discussion_r1010176627


##########
datafusion/physical-expr/src/functions.rs:
##########
@@ -433,6 +433,12 @@ pub fn create_physical_fun(
                 execution_props.query_execution_start_time,
             ))
         }
+        BuiltinScalarFunction::CurrentTime => {
+            // bind value for current_time at plan time
+            Arc::new(datetime_expressions::make_current_time(
+                execution_props.query_execution_start_time,
+            ))

Review Comment:
   👍 
   
   @mingmwang all the current_time within a query uses the same query_execution_start_time



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


[GitHub] [arrow-datafusion] waitingkuo commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1298126404

   casting `Timestamp` to `Time` isn't supported in `arrow-rs` 
   https://github.com/apache/arrow-rs/blob/master/arrow/src/compute/kernels/cast.rs#L236-L265


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


[GitHub] [arrow-datafusion] alamb commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1299075419

   Thanks again @naosense  and @waitingkuo !


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


[GitHub] [arrow-datafusion] waitingkuo commented on a diff in pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
waitingkuo commented on code in PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#discussion_r1010319328


##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -200,6 +200,20 @@ pub fn make_current_date(
     move |_arg| Ok(ColumnarValue::Scalar(ScalarValue::Date32(days)))
 }
 
+/// Create an implementation of `current_time()` that always returns the
+/// specified current time.
+///
+/// The semantics of `current_time()` require it to return the same value
+/// wherever it appears within a single statement. This value is
+/// chosen during planning time.
+pub fn make_current_time(
+    now_ts: DateTime<Utc>,
+) -> impl Fn(&[ColumnarValue]) -> Result<ColumnarValue> {
+    let nano =
+        Some(now_ts.timestamp_nanos() % 86400000000000);
+    move |_arg| Ok(ColumnarValue::Scalar(ScalarValue::Time64(nano)))

Review Comment:
   👍 



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


[GitHub] [arrow-datafusion] ursabot commented on pull request #4054: Implement `current_time` scalar function

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #4054:
URL: https://github.com/apache/arrow-datafusion/pull/4054#issuecomment-1299081795

   Benchmark runs are scheduled for baseline = 8c2653057d96a73570a608282266e856d5869da3 and contender = 97f2e4fd5517c762b0862d22b81f957db511e22e. 97f2e4fd5517c762b0862d22b81f957db511e22e is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/040e22214a154b22ab77de8370f2bfb9...bd54a91a456f41db9c71cd1422e04346/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/484564b2b1fc4b0991c0e2a34e877121...f061284d5a2f419091a49883c2e23fa0/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/6aa0915a4b8c4381b3fdd140800ae007...63eb4beef5e840f5a808c9b770b472c5/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/3235e53aef1f4ac7a81587d72e1e9e28...f7a064117a9945178fecee4f0250766e/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


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