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

[PR] add interval arithmetic for timestamp types [arrow-datafusion]

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

   ## 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 #7756.
   
   ## Rationale for this change
   
   It will be useful for the interval calculating feature to be able to successfully determine the intervals for timestamp columns.
   
   <!--
    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?
   
   Timestamp types have custom arithmetic and need special handling when attempting to determine potential interval ranges. Change the processing of comparison operator propagation to convert timestamp intervals into int64 intervals for processing. The results are converted back the the correct datatype at the end of the process.
   
   <!--
   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 these changes tested?
   
   Yes
   
   <!--
   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)?
   -->
   
   ## Are there any user-facing changes?
   
   This changes the behaviour of the interval propagation when being used with timestamp types.
   
   <!--
   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 interval arithmetic for timestamp types [arrow-datafusion]

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

   I took the liberty of merging the comments to this PR and merging from main. 


-- 
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 interval arithmetic for timestamp types [arrow-datafusion]

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

   Thanks for the suggestion @berkaysynnada I have reverted my changes and made your suggested change, keeping the test, and it works great.
   
   


-- 
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 interval arithmetic for timestamp types [arrow-datafusion]

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


##########
datafusion/physical-expr/src/intervals/cp_solver.rs:
##########
@@ -681,13 +693,41 @@ pub fn propagate_comparison_to_time_interval_at_right(
     }
 }
 
+/// Propagate the constraints arising from comparison operators on
+/// timestamp data types. Arithmetic on timestamps is treated specially,
+/// for example subtracting two timestamps results in duration rather
+/// than another timestamp. To work around this all values are converted
+/// to int64 before applying the proagation operation. The results are
+/// converted back to their original types before being returned.
+fn propagate_comparison_timestamp(
+    op: &Operator,
+    left_child: &Interval,
+    right_child: &Interval,
+    left_type: &DataType,
+    right_type: &DataType,
+) -> Result<(Option<Interval>, Option<Interval>)> {

Review Comment:
   Should this function have some additional constraint checks on the two timestamp types? Perhaps to ensure the two timestamps are actually comparable? I couldn't think of any to add, but maybe someone else can.



-- 
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 interval arithmetic for timestamp types [arrow-datafusion]

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

   I've observed the problem is in `new_zero()` function while defining the target node for timestamps.
   
   https://github.com/apache/arrow-datafusion/blob/3d1b23a04bdc04c526e2dcb06e0cf1995707587d/datafusion/common/src/scalar.rs#L836-L845
   
   x>y is reformed as x-y>0, and (x-y) node is assigned with a new interval of [0, inf). However, `new_zero()` of `ScalarValue::Duration`'s are set as None, I don't know why these are left in that way but I believe they need to be set as `Some(0)`, similar to other types.
   
   ```
               DataType::Duration(TimeUnit::Second) => ScalarValue::DurationSecond(None),
               DataType::Duration(TimeUnit::Millisecond) => {
                   ScalarValue::DurationMillisecond(Some(0))
               }
               DataType::Duration(TimeUnit::Microsecond) => {
                   ScalarValue::DurationMicrosecond(Some(0))
               }
               DataType::Duration(TimeUnit::Nanosecond) => {
                   ScalarValue::DurationNanosecond(Some(0))
               }
   ```
   
   I think no additional implementation is needed to handle timestamp cases. When I applied these changes, the test mentioned in the issue passed successfully. 


-- 
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 interval arithmetic for timestamp types [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #7758:
URL: https://github.com/apache/arrow-datafusion/pull/7758


-- 
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 interval arithmetic for timestamp types [arrow-datafusion]

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


##########
datafusion/common/src/scalar.rs:
##########
@@ -833,15 +833,15 @@ impl ScalarValue {
             DataType::Interval(IntervalUnit::MonthDayNano) => {
                 ScalarValue::IntervalMonthDayNano(Some(0))
             }
-            DataType::Duration(TimeUnit::Second) => ScalarValue::DurationSecond(None),

Review Comment:
   👌 



##########
datafusion/physical-expr/src/intervals/cp_solver.rs:
##########
@@ -1414,4 +1415,130 @@ mod tests {
 
         Ok(())
     }
+
+    #[test]
+    fn test_propagate_comparison() {
+        let left = Interval::new(
+            IntervalBound::make_unbounded(DataType::Int64).unwrap(),
+            IntervalBound::make_unbounded(DataType::Int64).unwrap(),
+        );
+        let right = Interval::new(
+            IntervalBound::new(ScalarValue::Int64(Some(1000)), false),
+            IntervalBound::new(ScalarValue::Int64(Some(1000)), false),
+        );
+        assert_eq!(

Review Comment:
   It took me some time to work this out, so I figured it might be helpful to leave some comments explaining what is expected. 
   
   ```suggestion
           // left is unbounded, right is known to be [1000,1000]
           // so left < right results in knowing that left is now < 1000 
           assert_eq!(
   ```



##########
datafusion/physical-expr/src/intervals/cp_solver.rs:
##########
@@ -1414,4 +1415,130 @@ mod tests {
 
         Ok(())
     }
+
+    #[test]
+    fn test_propagate_comparison() {
+        let left = Interval::new(

Review Comment:
   It took me some time to work this out, so I figured it might be helpful to leave some comments explaining what is expected. 
   
   ```suggestion
           // In the examples below:
           // `left` is unbounded: [?, ?], 
           // `right` is known to be [1000,1000]
           // so `left` < `right` results in no new knowledge of `right` but knowing that `left` is now < 1000:` [?, 1000)
           let left = Interval::new(
   ```



##########
datafusion/common/src/scalar.rs:
##########
@@ -833,15 +833,15 @@ impl ScalarValue {
             DataType::Interval(IntervalUnit::MonthDayNano) => {
                 ScalarValue::IntervalMonthDayNano(Some(0))
             }
-            DataType::Duration(TimeUnit::Second) => ScalarValue::DurationSecond(None),

Review Comment:
   👌 



##########
datafusion/physical-expr/src/intervals/cp_solver.rs:
##########
@@ -1414,4 +1415,130 @@ mod tests {
 
         Ok(())
     }
+
+    #[test]
+    fn test_propagate_comparison() {
+        let left = Interval::new(
+            IntervalBound::make_unbounded(DataType::Int64).unwrap(),
+            IntervalBound::make_unbounded(DataType::Int64).unwrap(),
+        );
+        let right = Interval::new(
+            IntervalBound::new(ScalarValue::Int64(Some(1000)), false),
+            IntervalBound::new(ScalarValue::Int64(Some(1000)), false),
+        );
+        assert_eq!(

Review Comment:
   It took me some time to work this out, so I figured it might be helpful to leave some comments explaining what is expected. 
   
   ```suggestion
           // left is unbounded, right is known to be [1000,1000]
           // so left < right results in knowing that left is now < 1000 
           assert_eq!(
   ```



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