You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "tshauck (via GitHub)" <gi...@apache.org> on 2024/07/21 15:35:50 UTC

[PR] feat: add bounds for unary math scalar functions [datafusion]

tshauck opened a new pull request, #11584:
URL: https://github.com/apache/datafusion/pull/11584

   ## Which issue does this PR close?
   
   Closes #11583 11583
   
   ## Rationale for this change
   
   Many math unary functions aren't unbounded, but because the macro doesn't include a way to modify this on a per function basis the trait default (of unbounded) is used.
   
   ## What changes are included in this PR?
   
   1. Change the `make_math_unary_udf` macro to take a bounds function
   2. Implement a function for math functions where the bounds are known a priori.
   
   ## Are these changes tested?
   
   Yes, see unittests.
   
   ## Are there any user-facing changes?
   
   No
   


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1687531392


##########
datafusion/expr/src/interval_arithmetic.rs:
##########
@@ -332,6 +332,46 @@ impl Interval {
         Ok(Self::new(unbounded_endpoint.clone(), unbounded_endpoint))
     }
 
+    /// Creates an interval between -∞ to ∞.
+    pub fn make_infinity_interval(data_type: &DataType) -> Result<Self> {

Review Comment:
   We use `ScalarValue::Float(None)` for infinite bounds by convention. Even if you give Float(Some(f::INF)) or Float(Some(f::NaN)), they are converted to Float(None)'s during interval creation. We had given such a decision to have unique representation of unboundedness. You can check the details here: https://github.com/apache/datafusion/blob/deef834e7adfd859414448ab4da461e2d4eabb9e/datafusion/expr/src/interval_arithmetic.rs#L178
   
   I recommend here to use `make_unbounded` API with floating types to not break this convention.



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686318175


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}

Review Comment:
   There is a similar method in `Interval::data_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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   I checked the new version for both unbounded creation and rounded PI usage. Everything looks good and seems ready to merge. Thanks @tshauck!


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "tshauck (via GitHub)" <gi...@apache.org>.
tshauck commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686692384


##########
datafusion/functions/src/math/monotonicity.rs:
##########
@@ -15,24 +15,17 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use arrow::datatypes::DataType;
 use datafusion_common::{exec_err, Result, ScalarValue};
 use datafusion_expr::interval_arithmetic::Interval;
 use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
 
-fn symmetric_unit_interval(data_type: &DataType) -> Result<Interval> {

Review Comment:
   [78a943f](https://github.com/apache/datafusion/pull/11584/commits/78a943f38d90a7276ca9cdfef0f1d99cb91c4883)



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   Thanks @tshauck. This is a nice step towards having comprehensive interval analysis. I have left a small suggestion. Once that is addressed, the PR will be good to go 🚀


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   IIRC nextafter is the IEEE standard name for this function, and Rust's next_up does something similar (in the upward direction). If it is in nightly, we probably should just create constants with these values explicitly given for each float 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   I took a quick look and this looks good to me. Thank you @tshauck  and @ozankabak 🚀 


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686333818


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}
+
+pub(super) fn unbounded(_input: &[&Interval]) -> crate::Result<Interval> {
+    // We cannot assume the input datatype is the same of output type.
+    Interval::make_unbounded(&DataType::Null)
+}

Review Comment:
   Is it better here returning `not_impl_err`? The current version may cause type mismatch errors and debugging would require more 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1687532640


##########
datafusion/expr/src/interval_arithmetic.rs:
##########
@@ -332,6 +332,46 @@ impl Interval {
         Ok(Self::new(unbounded_endpoint.clone(), unbounded_endpoint))
     }
 
+    /// Creates an interval between -∞ to ∞.
+    pub fn make_infinity_interval(data_type: &DataType) -> Result<Self> {
+        Self::try_new(
+            ScalarValue::new_neg_infinity(data_type)?,
+            ScalarValue::new_infinity(data_type)?,
+        )
+    }
+
+    /// Creates an interval between -1 to 1.
+    pub fn make_symmetric_unit_interval(data_type: &DataType) -> Result<Self> {
+        Self::try_new(
+            ScalarValue::new_negative_one(data_type)?,
+            ScalarValue::new_one(data_type)?,
+        )
+    }
+
+    /// Create an interval from -π to π.
+    pub fn make_symmetric_pi_interval(data_type: &DataType) -> Result<Self> {
+        Self::try_new(
+            ScalarValue::new_negative_pi(data_type)?,
+            ScalarValue::new_pi(data_type)?,
+        )
+    }
+
+    /// Create an interval from -π/2 to π/2.
+    pub fn make_symmetric_half_pi_interval(data_type: &DataType) -> Result<Self> {
+        Self::try_new(
+            ScalarValue::new_neg_frac_pi_2(data_type)?,
+            ScalarValue::new_frac_pi_2(data_type)?,
+        )
+    }
+
+    /// Create an interval from 0 to infinity.
+    pub fn make_non_negative_infinity_interval(data_type: &DataType) -> Result<Self> {

Review Comment:
   Same suggestion applies here as well



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "tshauck (via GitHub)" <gi...@apache.org>.
tshauck commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1687069516


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}
+
+pub(super) fn unbounded(_input: &[&Interval]) -> crate::Result<Interval> {
+    // We cannot assume the input datatype is the same of output type.
+    Interval::make_unbounded(&DataType::Null)
+}

Review Comment:
   Went ahead and did that here: [cf2b023](https://github.com/apache/datafusion/pull/11584/commits/cf2b023ac61196f878e0078ee54250f2a586aaad)



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   Thanks! -- it looks like `signum` was changed in the meantime, so removing that for now as it doesn't use `make_math_unary_udf` anymore. The rebase seems ok locally, but I'll check to see if things don't go through for w/e reason here.


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   Thanks to both of you for the thoughtful feedback.
   
   @berkaysynnada, I updated infinity to use unbounded in [231a717](https://github.com/apache/datafusion/pull/11584/commits/231a717cdd94ef0145038a66c18c894b754f8695).
   
   @ozankabak, for handling π, do you mean [`libm::nextafter`](https://rust-random.github.io/rand/libm/fn.nextafter.html)? There's also a [`.next_up`](https://doc.rust-lang.org/std/primitive.f64.html#method.next_up), but unfortunately, it's in nightly. Did I overlook the actual function, or if not, any opinion on bringing in `libm` as dependency vs something w/i datafusion, albeit less robust?


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   Thanks @ozankabak, would you please have a look at the latest changes and lmk what you think?


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "tshauck (via GitHub)" <gi...@apache.org>.
tshauck commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686702799


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}
+
+pub(super) fn unbounded(_input: &[&Interval]) -> crate::Result<Interval> {
+    // We cannot assume the input datatype is the same of output type.
+    Interval::make_unbounded(&DataType::Null)
+}

Review Comment:
   I think that would cause an error when trying to use these functions? The update [macro](https://github.com/apache/datafusion/pull/11584/files#diff-b2d8a351fea4f0f656b5535ab00541f6e413eba67d8a78f798f80cfb440bbb32) is going to call whatever is passed as `$EVALUATE_BOUNDS`. Right now this implementation matches the default of the `ScalarUDFImpl` trait.
   
   I think an alternative is to make something like `Interval::make_symmetric_inf`. I'm using unbounded here somewhat loosely, but probably in most of these cases it should be (-inf, inf) which could be given a 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686326772


##########
datafusion/functions/src/math/monotonicity.rs:
##########
@@ -15,24 +15,17 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use arrow::datatypes::DataType;
 use datafusion_common::{exec_err, Result, ScalarValue};
 use datafusion_expr::interval_arithmetic::Interval;
 use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
 
-fn symmetric_unit_interval(data_type: &DataType) -> Result<Interval> {

Review Comment:
   It makes sense to me



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686325658


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}
+
+pub(super) fn unbounded(_input: &[&Interval]) -> crate::Result<Interval> {
+    // We cannot assume the input datatype is the same of output type.
+    Interval::make_unbounded(&DataType::Null)
+}

Review Comment:
   We actually use the null values within their datatypes for unbounded quantities. Directly using `ScalarValue::Null` is avoided and may cause errors due to type mismatch if the user has not explicitly handled 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686318175


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}

Review Comment:
   There is a similar method `Interval::data_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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "tshauck (via GitHub)" <gi...@apache.org>.
tshauck commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686702799


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}
+
+pub(super) fn unbounded(_input: &[&Interval]) -> crate::Result<Interval> {
+    // We cannot assume the input datatype is the same of output type.
+    Interval::make_unbounded(&DataType::Null)
+}

Review Comment:
   I think that would cause an error when trying to use these functions? The update [macro](https://github.com/apache/datafusion/pull/11584/files#diff-b2d8a351fea4f0f656b5535ab00541f6e413eba67d8a78f798f80cfb440bbb32) is going to call whatever is passed as `$EVALUATE_BOUNDS`. Right now this implementation matches the default of the `ScalarUDFImpl` trait.
   
   I think an alternative is to make something like `Interval::make_symmetric_inf`. I'm using unbounded here, but probably in most of these cases it should be (-inf, inf) which could be given a 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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

   Apart from @berkaysynnada's outstanding suggestions the only thing I see is that we would need to use two different π values, depending on whether it is in the lower bound or the upper bound of the resulting interval.
   
   If π will be in the upper bound, we should use the smallest floating point number that is greater than the mathematical (precise) value of π. Let's call this `pi_upper`. Conversely, if π will be in the lower bound, we should use the largest floating point number that is less than the mathematical (precise) value of π.
   
   Since π is not exactly representable, the `pi` you get from Rust is actually either `pi_upper` or `pi_lower` (you need to check). You can obtain the other one by using the `nextafter` function (let's just be careful to do this only once and use it as a constant everywhere).
   
   Apart from this detail that is addressable by a small fix, this seems to be a great PR. Thank you.


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

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


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686325658


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}
+
+pub(super) fn unbounded(_input: &[&Interval]) -> crate::Result<Interval> {
+    // We cannot assume the input datatype is the same of output type.
+    Interval::make_unbounded(&DataType::Null)
+}

Review Comment:
   We actually use the null values within their datatypes for unbounded quantities. Directly using `ScalarValue::Null` is avoided and may cause errors due to type mismatch if the user has not explicitly handled 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "tshauck (via GitHub)" <gi...@apache.org>.
tshauck commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1686644243


##########
datafusion/functions/src/math/bounds.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::datatypes::DataType;
+use datafusion_common::ScalarValue;
+use datafusion_expr::interval_arithmetic::Interval;
+
+fn unary_interval_datatype(interval: &Interval) -> DataType {
+    interval.lower().data_type()
+}

Review Comment:
   [a1d2618](https://github.com/apache/datafusion/pull/11584/commits/a1d26181f1e1d5fce56d398ae9f6b21b561b51ba)



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "tshauck (via GitHub)" <gi...@apache.org>.
tshauck commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1685768971


##########
datafusion/functions/src/math/monotonicity.rs:
##########
@@ -15,24 +15,17 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use arrow::datatypes::DataType;
 use datafusion_common::{exec_err, Result, ScalarValue};
 use datafusion_expr::interval_arithmetic::Interval;
 use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
 
-fn symmetric_unit_interval(data_type: &DataType) -> Result<Interval> {

Review Comment:
   because this is also used in the bounds code, I added a constructor on `Interval`. I wasn't sure about adding others (e.g. `Interval::make_symmetric_pi_interval`) since right now they're only also used in the bounds code, but it would clean up that code + tests a bit.



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add bounds for unary math scalar functions [datafusion]

Posted by "tshauck (via GitHub)" <gi...@apache.org>.
tshauck commented on code in PR #11584:
URL: https://github.com/apache/datafusion/pull/11584#discussion_r1685768971


##########
datafusion/functions/src/math/monotonicity.rs:
##########
@@ -15,24 +15,17 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use arrow::datatypes::DataType;
 use datafusion_common::{exec_err, Result, ScalarValue};
 use datafusion_expr::interval_arithmetic::Interval;
 use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
 
-fn symmetric_unit_interval(data_type: &DataType) -> Result<Interval> {

Review Comment:
   because this is also used in the bounds code, I added a constructor on `Interval`. I wasn't sure about adding others (e.g. `Interval::make_symmetric_pi_interval`) since right now they're only also used in the bounds code.



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org