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

[GitHub] [arrow-datafusion] waitingkuo opened a new pull request, #5906: add with_timezone

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

   # 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 #5905 
   
   # Rationale for this change
   
   <!--
    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?
   
   <!--
   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.
   -->
   
   a new function `with_timezone` that replace the timezone of timestamp without changing underline value
   
   ```bash
   ❯ select WITH_TIMEZONE(timestamp '2000-01-01T00:00:00.123456789', '+08:00');
   +--------------------------------------------------------------------+
   | withtimezone(Utf8("2000-01-01T00:00:00.123456789"),Utf8("+08:00")) |
   +--------------------------------------------------------------------+
   | 2000-01-01T08:00:00.123456789+08:00                                |
   +--------------------------------------------------------------------+
   1 row in set. Query took 0.004 seconds.
   ```
   
   ```bash
   ❯ select WITH_TIMEZONE(WITH_TIMEZONE(timestamp '2000-01-01T00:00:00.123456789', '+08:00'), '+07:00');
   +-------------------------------------------------------------------------------------------------+
   | withtimezone(withtimezone(Utf8("2000-01-01T00:00:00.123456789"),Utf8("+08:00")),Utf8("+07:00")) |
   +-------------------------------------------------------------------------------------------------+
   | 2000-01-01T07:00:00.123456789+07:00                                                             |
   +-------------------------------------------------------------------------------------------------+
   1 row in set. Query took 0.003 seconds.
   ```
   
   while the timezone is incorrect, it raises error
   ```bash
   ❯ select WITH_TIMEZONE(timestamp '2000-01-01T00:00:00.123456789', '+25:00');
   Arrow error: Parser error: Invalid timezone "+25:00": '+25:00' is not a valid timezone
   ```
   
   # Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   yes, test cases are added to `timestamps.slt`
   
   # 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


Re: [PR] add with_timezone [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb closed pull request #5906: add with_timezone
URL: https://github.com/apache/arrow-datafusion/pull/5906


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


Re: [PR] add with_timezone [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#issuecomment-2043645008

   Since this has been open for more than a year, closing it down. Feel free to reopen if/when you keep working on it. 


-- 
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] tustvold commented on pull request #5906: add with_timezone

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#issuecomment-1500210703

   > In fact i'm working on adding timezone support for arrow_cast now, will send a pr soon
   
   I think we may want to fix https://github.com/apache/arrow-rs/issues/1936 upstream, before exposing the currently incorrect timezone casting behaviour in 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] waitingkuo commented on pull request #5906: add with_timezone

Posted by "waitingkuo (via GitHub)" <gi...@apache.org>.
waitingkuo commented on PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#issuecomment-1500382511

   @alamb thanks, i'll think about how to fix this


-- 
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 a diff in pull request #5906: add with_timezone

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#discussion_r1160626335


##########
datafusion/expr/src/function.rs:
##########
@@ -262,6 +262,25 @@ pub fn return_type(
 
         BuiltinScalarFunction::ArrowTypeof => Ok(DataType::Utf8),
 
+        BuiltinScalarFunction::WithTimezone => match input_expr_types[0] {
+            DataType::Timestamp(TimeUnit::Nanosecond, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Nanosecond, None))
+            }
+            DataType::Timestamp(TimeUnit::Microsecond, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Microsecond, None))
+            }
+            DataType::Timestamp(TimeUnit::Millisecond, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Millisecond, None))
+            }
+            DataType::Timestamp(TimeUnit::Second, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Second, None))
+            }
+            _ => return Err(DataFusionError::Internal(
+                "The with_timezone function can only accept timestamp as the first arg."
+                    .to_string(),
+            )),
+        },

Review Comment:
   I think you may need to special case it, in the same way as `arrow_cast`: https://github.com/apache/arrow-datafusion/blob/f00ef9d5484039dc7013af5b8fa04f7b29ffaba7/datafusion/sql/src/expr/function.rs#L139-L142
   
   



-- 
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 #5906: add with_timezone

Posted by "waitingkuo (via GitHub)" <gi...@apache.org>.
waitingkuo commented on PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#issuecomment-1500209232

   > Thanks @waitingkuo --
   > 
   > Even though `arrow_cast` could (and probably should) be updated to support timezones having a specific WITH_TIMEZONE seems like a good idea to me as it will be a better experience
   > 
   > I think this PR needs
   > 
   > 1. Get the type correct (I left a comment about how to do this)
   > 2. Add an entry to `WITH_TIMEZONE` the user documentation (e.g. https://github.com/apache/arrow-datafusion/blob/f00ef9d5484039dc7013af5b8fa04f7b29ffaba7/docs/source/user-guide/sql/scalar_functions.md?plain=1#L1208
   >    )
   
   Hi @alamb thanks, in fact i'm working on adding timezone support for `arrow_cast` now, will send a pr soon


-- 
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 #5906: add with_timezone

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#issuecomment-1500332019

   > does it mean that i need to move WITH_TIMEZONE out of BuiltinScalarFunction ?
   
   I am not sure to be honest -- there might not be a good existing pattern to follow for such functions. :tin
   


-- 
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 #5906: add with_timezone

Posted by "waitingkuo (via GitHub)" <gi...@apache.org>.
waitingkuo commented on code in PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#discussion_r1160494954


##########
datafusion/physical-expr/src/functions.rs:
##########
@@ -169,6 +169,22 @@ pub fn create_physical_expr(
                 )))))
             })
         }
+        BuiltinScalarFunction::WithTimezone => {
+            // function::return_type doesn't consider the arg value which contains the timezone info
+            // i.e. it could only output Timestamp<TimeUnit, None> or Timestamo<TimeUnit, Some(SomeFixedTimestamp))
+            // here we parse the second arg as timestamp and modify the return data_type
+            let timezone = format!("{}", input_phy_exprs[1]);
+            data_type = match data_type {
+                DataType::Timestamp(TimeUnit::Nanosecond, _) => DataType::Timestamp(TimeUnit::Nanosecond, Some(timezone)),
+                DataType::Timestamp(TimeUnit::Microsecond, _) => DataType::Timestamp(TimeUnit::Microsecond, Some(timezone)),
+                DataType::Timestamp(TimeUnit::Millisecond, _) => DataType::Timestamp(TimeUnit::Millisecond, Some(timezone)),
+                DataType::Timestamp(TimeUnit::Second, _) => DataType::Timestamp(TimeUnit::Second, Some(timezone)),
+                other => return Err(DataFusionError::Internal(format!(
+                    "Unsupported data type {other:?} as the first arg for function with_timezone",
+                )))
+            };

Review Comment:
   correct the timezone in return data_type according to the args value
   
   @alamb is this implementation ok for now? or it's recommended to modify the `return_type` function to accept some new args to achieve this?
   
   I could submit another ticket to modify it as well



##########
datafusion/expr/src/function.rs:
##########
@@ -262,6 +262,25 @@ pub fn return_type(
 
         BuiltinScalarFunction::ArrowTypeof => Ok(DataType::Utf8),
 
+        BuiltinScalarFunction::WithTimezone => match input_expr_types[0] {
+            DataType::Timestamp(TimeUnit::Nanosecond, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Nanosecond, None))
+            }
+            DataType::Timestamp(TimeUnit::Microsecond, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Microsecond, None))
+            }
+            DataType::Timestamp(TimeUnit::Millisecond, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Millisecond, None))
+            }
+            DataType::Timestamp(TimeUnit::Second, _) => {
+                Ok(DataType::Timestamp(TimeUnit::Second, None))
+            }
+            _ => return Err(DataFusionError::Internal(
+                "The with_timezone function can only accept timestamp as the first arg."
+                    .to_string(),
+            )),
+        },

Review Comment:
   ```rust
   pub fn return_type(
       fun: &BuiltinScalarFunction,
       input_expr_types: &[DataType],
   ) -> Result<DataType> {
   ```
   this function only has 2 inputs: function pointer and args types. there's no way to inference timezone in the final return type



-- 
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 #5906: add with_timezone

Posted by "waitingkuo (via GitHub)" <gi...@apache.org>.
waitingkuo commented on PR #5906:
URL: https://github.com/apache/arrow-datafusion/pull/5906#issuecomment-1500322148

   > Thanks @waitingkuo --
   > 
   > Even though `arrow_cast` could (and probably should) be updated to support timezones having a specific WITH_TIMEZONE seems like a good idea to me as it will be a better experience
   > 
   > I think this PR needs
   > 
   > 1. Get the type correct (I left a comment about how to do this)
   > 2. Add an entry to `WITH_TIMEZONE` the user documentation (e.g. https://github.com/apache/arrow-datafusion/blob/f00ef9d5484039dc7013af5b8fa04f7b29ffaba7/docs/source/user-guide/sql/scalar_functions.md?plain=1#L1208
   >    )
   
   Hi @alamb  thank you for the suggestion.
   
   does it mean that i need to move `WITH_TIMEZONE` out of `BuiltinScalarFunction` ?


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