You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "coderfender (via GitHub)" <gi...@apache.org> on 2025/10/10 03:56:55 UTC

[PR] [WIP]: Support ansi mode rounding function [datafusion-comet]

coderfender opened a new pull request, #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542

   ## 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 https://github.com/apache/datafusion-comet/issues/466
   
   ## 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.
   -->
   
   ## How 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)?
   -->
   


-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2424722661


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,37 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    val data = Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue))
+    Seq("true", "false").foreach { p =>
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> p) {
+        withParquetTable(data, "tbl") {
+          val res = spark.sql(s"""
+                                |SELECT
+                                |  round(_1, -1) ,
+                                |  round(_1, -10) ,
+                                |  round(${Int.MaxValue}, -10)

Review Comment:
   It would also be better to test the input values individually. I'd suggest something like this:
   
   ```scala
     test("ANSI support for round function") {
       for (value <- Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue))) {
         val data = Seq(value)
         withParquetTable(data, "tbl") {
           for (scale <- Seq(-10, -1, 0, 1, 10)) {
             for (ansi <- Seq(true, false)) {
               withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi.toString) {
                 val res = spark.sql(s"SELECT round(_1, $scale) from tbl")
                 checkSparkMaybeThrows(res) match {
                   case (Some(sparkException), Some(cometException)) =>
                     assert(sparkException.getMessage.contains("ARITHMETIC_OVERFLOW"))
                     assert(cometException.getMessage.contains("ARITHMETIC_OVERFLOW"))
                   case (None, None) => checkSparkAnswerAndOperator(res)
                   case (None, Some(ex)) =>
                     fail(
                       "Comet threw an exception but Spark did not. Comet exception: " + ex.getMessage)
                   case (Some(sparkException), None) =>
                     fail(
                       "Spark threw an exception but Comet did not. Spark exception: " +
                         sparkException.getMessage)
                 }
               }
             }
           }
         }
       }
     }
   ```
   
   



-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2424657319


##########
native/spark-expr/src/math_funcs/round.rs:
##########
@@ -15,49 +15,90 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::arithmetic_overflow_error;
 use crate::math_funcs::utils::{get_precision_scale, make_decimal_array, make_decimal_scalar};
 use arrow::array::{Array, ArrowNativeTypeOp};
 use arrow::array::{Int16Array, Int32Array, Int64Array, Int8Array};
 use arrow::datatypes::DataType;
+use arrow::error::ArrowError;
 use datafusion::common::{exec_err, internal_err, DataFusionError, ScalarValue};
 use datafusion::{functions::math::round::round, physical_plan::ColumnarValue};
 use std::{cmp::min, sync::Arc};
 
 macro_rules! integer_round {
-    ($X:expr, $DIV:expr, $HALF:expr) => {{
+    ($X:expr, $DIV:expr, $HALF:expr, $FAIL_ON_ERROR:expr) => {{
         let rem = $X % $DIV;
         if rem <= -$HALF {
-            ($X - rem).sub_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).sub_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }
+            } else {
+                Ok(($X - rem).sub_wrapping($DIV))
+            }
         } else if rem >= $HALF {
-            ($X - rem).add_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).add_checked($DIV) {

Review Comment:
   `map_err` should be used here as well



##########
native/spark-expr/src/math_funcs/round.rs:
##########
@@ -15,49 +15,90 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::arithmetic_overflow_error;
 use crate::math_funcs::utils::{get_precision_scale, make_decimal_array, make_decimal_scalar};
 use arrow::array::{Array, ArrowNativeTypeOp};
 use arrow::array::{Int16Array, Int32Array, Int64Array, Int8Array};
 use arrow::datatypes::DataType;
+use arrow::error::ArrowError;
 use datafusion::common::{exec_err, internal_err, DataFusionError, ScalarValue};
 use datafusion::{functions::math::round::round, physical_plan::ColumnarValue};
 use std::{cmp::min, sync::Arc};
 
 macro_rules! integer_round {
-    ($X:expr, $DIV:expr, $HALF:expr) => {{
+    ($X:expr, $DIV:expr, $HALF:expr, $FAIL_ON_ERROR:expr) => {{
         let rem = $X % $DIV;
         if rem <= -$HALF {
-            ($X - rem).sub_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).sub_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }
+            } else {
+                Ok(($X - rem).sub_wrapping($DIV))
+            }
         } else if rem >= $HALF {
-            ($X - rem).add_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).add_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }
+            } else {
+                Ok(($X - rem).add_wrapping($DIV))
+            }
         } else {
-            $X - rem
+            if $FAIL_ON_ERROR {
+                match $X.sub_checked(rem) {

Review Comment:
   `map_err` should be used 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:support ansi mode rounding function [datafusion-comet]

Posted by "coderfender (via GitHub)" <gi...@apache.org>.
coderfender commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2424780661


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,37 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    val data = Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue))
+    Seq("true", "false").foreach { p =>
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> p) {
+        withParquetTable(data, "tbl") {
+          val res = spark.sql(s"""
+                                |SELECT
+                                |  round(_1, -1) ,
+                                |  round(_1, -10) ,
+                                |  round(${Int.MaxValue}, -10)

Review Comment:
   Done 



-- 
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:support ansi mode rounding function [datafusion-comet]

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

   @andygrove , I made the changes per review . Please take a look whenever you get a chance 


-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2424655991


##########
native/spark-expr/src/math_funcs/round.rs:
##########
@@ -15,49 +15,90 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::arithmetic_overflow_error;
 use crate::math_funcs::utils::{get_precision_scale, make_decimal_array, make_decimal_scalar};
 use arrow::array::{Array, ArrowNativeTypeOp};
 use arrow::array::{Int16Array, Int32Array, Int64Array, Int8Array};
 use arrow::datatypes::DataType;
+use arrow::error::ArrowError;
 use datafusion::common::{exec_err, internal_err, DataFusionError, ScalarValue};
 use datafusion::{functions::math::round::round, physical_plan::ColumnarValue};
 use std::{cmp::min, sync::Arc};
 
 macro_rules! integer_round {
-    ($X:expr, $DIV:expr, $HALF:expr) => {{
+    ($X:expr, $DIV:expr, $HALF:expr, $FAIL_ON_ERROR:expr) => {{
         let rem = $X % $DIV;
         if rem <= -$HALF {
-            ($X - rem).sub_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).sub_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }

Review Comment:
   Since the `Ok` value is unchanged and the `Err` is changed, it would be better to use `map_err` rather than a match here.
   
   ```suggestion
                   ($X - rem).sub_checked($DIV).map_err(|_| {
                       ArrowError::ComputeError(arithmetic_overflow_error("integer").to_string())
                   })
   ```



-- 
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:support ansi mode rounding function [datafusion-comet]

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

   Thank you for the approval @andygrove 
   


-- 
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:support ansi mode rounding function [datafusion-comet]

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

   @coderfender Could you also updatre the documentation (list of supported expressions)


-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2426546243


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,32 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue)).foreach { value =>

Review Comment:
   Could you also add Byte and Short min and max



-- 
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:support ansi mode rounding function [datafusion-comet]

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


-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "coderfender (via GitHub)" <gi...@apache.org>.
coderfender commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2426574213


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,32 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue)).foreach { value =>

Review Comment:
   Sure @andygrove 



-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "coderfender (via GitHub)" <gi...@apache.org>.
coderfender commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2426548923


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,32 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue)).foreach { value =>

Review Comment:
   Sure @andygrove 



-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#issuecomment-3393891317

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `0%` with `38 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 10.41%. Comparing base ([`f09f8af`](https://app.codecov.io/gh/apache/datafusion-comet/commit/f09f8af64c6599255e116a376f4f008f2fd63b43?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`e2f5d90`](https://app.codecov.io/gh/apache/datafusion-comet/commit/e2f5d903e38f0f66a143d43dee078f93c1937642?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 586 commits behind head on main.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...k/src/main/scala/org/apache/comet/serde/math.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Fmath.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9tYXRoLnNjYWxh) | 0.00% | [9 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...k/src/main/scala/org/apache/comet/serde/hash.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Fhash.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9oYXNoLnNjYWxh) | 0.00% | [7 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...main/scala/org/apache/comet/serde/arithmetic.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Farithmetic.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9hcml0aG1ldGljLnNjYWxh) | 0.00% | [6 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...c/main/scala/org/apache/comet/serde/datetime.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Fdatetime.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9kYXRldGltZS5zY2FsYQ==) | 0.00% | [6 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [.../scala/org/apache/comet/serde/QueryPlanSerde.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2FQueryPlanSerde.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9RdWVyeVBsYW5TZXJkZS5zY2FsYQ==) | 0.00% | [4 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...src/main/scala/org/apache/comet/serde/arrays.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Farrays.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9hcnJheXMuc2NhbGE=) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...rc/main/scala/org/apache/comet/serde/bitwise.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Fbitwise.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9iaXR3aXNlLnNjYWxh) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...k/src/main/scala/org/apache/comet/serde/maps.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Fmaps.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9tYXBzLnNjYWxh) | 0.00% | [1 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...main/scala/org/apache/comet/serde/predicates.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2Fpredicates.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9wcmVkaWNhdGVzLnNjYWxh) | 0.00% | [1 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@              Coverage Diff              @@
   ##               main    #2542       +/-   ##
   =============================================
   - Coverage     56.12%   10.41%   -45.72%     
   + Complexity      976      232      -744     
   =============================================
     Files           119      147       +28     
     Lines         11743    13652     +1909     
     Branches       2251     2349       +98     
   =============================================
   - Hits           6591     1422     -5169     
   - Misses         4012    12053     +8041     
   + Partials       1140      177      -963     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/2542?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   </details>


-- 
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:support ansi mode rounding function [datafusion-comet]

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

   @andygrove  , This is the PR to support ANSI mode with rounding functions. Please take a look whenever you get a chance. 


-- 
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:support ansi mode rounding function [datafusion-comet]

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

   Sure let me update the documentation and the test cases @andygrove 


-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2426549081


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,32 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue)).foreach { value =>

Review Comment:
   For consistency, it would be good to stick to Scala's constants for min and max, rather than mixing Java and Scala versions.
   
   ```suggestion
       Seq((Int.MaxValue Int.MinValue Long.MinValue, Long.MaxValue)).foreach { value =>
   ```



-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "coderfender (via GitHub)" <gi...@apache.org>.
coderfender commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2424669749


##########
native/spark-expr/src/math_funcs/round.rs:
##########
@@ -15,49 +15,90 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::arithmetic_overflow_error;
 use crate::math_funcs::utils::{get_precision_scale, make_decimal_array, make_decimal_scalar};
 use arrow::array::{Array, ArrowNativeTypeOp};
 use arrow::array::{Int16Array, Int32Array, Int64Array, Int8Array};
 use arrow::datatypes::DataType;
+use arrow::error::ArrowError;
 use datafusion::common::{exec_err, internal_err, DataFusionError, ScalarValue};
 use datafusion::{functions::math::round::round, physical_plan::ColumnarValue};
 use std::{cmp::min, sync::Arc};
 
 macro_rules! integer_round {
-    ($X:expr, $DIV:expr, $HALF:expr) => {{
+    ($X:expr, $DIV:expr, $HALF:expr, $FAIL_ON_ERROR:expr) => {{
         let rem = $X % $DIV;
         if rem <= -$HALF {
-            ($X - rem).sub_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).sub_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }

Review Comment:
   Thank you for the suggestion @andygrove  . Let me update it to use `map_err` instead of `match` statement



##########
native/spark-expr/src/math_funcs/round.rs:
##########
@@ -15,49 +15,90 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::arithmetic_overflow_error;
 use crate::math_funcs::utils::{get_precision_scale, make_decimal_array, make_decimal_scalar};
 use arrow::array::{Array, ArrowNativeTypeOp};
 use arrow::array::{Int16Array, Int32Array, Int64Array, Int8Array};
 use arrow::datatypes::DataType;
+use arrow::error::ArrowError;
 use datafusion::common::{exec_err, internal_err, DataFusionError, ScalarValue};
 use datafusion::{functions::math::round::round, physical_plan::ColumnarValue};
 use std::{cmp::min, sync::Arc};
 
 macro_rules! integer_round {
-    ($X:expr, $DIV:expr, $HALF:expr) => {{
+    ($X:expr, $DIV:expr, $HALF:expr, $FAIL_ON_ERROR:expr) => {{
         let rem = $X % $DIV;
         if rem <= -$HALF {
-            ($X - rem).sub_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).sub_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }
+            } else {
+                Ok(($X - rem).sub_wrapping($DIV))
+            }
         } else if rem >= $HALF {
-            ($X - rem).add_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).add_checked($DIV) {

Review Comment:
   Sure 



##########
native/spark-expr/src/math_funcs/round.rs:
##########
@@ -15,49 +15,90 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::arithmetic_overflow_error;
 use crate::math_funcs::utils::{get_precision_scale, make_decimal_array, make_decimal_scalar};
 use arrow::array::{Array, ArrowNativeTypeOp};
 use arrow::array::{Int16Array, Int32Array, Int64Array, Int8Array};
 use arrow::datatypes::DataType;
+use arrow::error::ArrowError;
 use datafusion::common::{exec_err, internal_err, DataFusionError, ScalarValue};
 use datafusion::{functions::math::round::round, physical_plan::ColumnarValue};
 use std::{cmp::min, sync::Arc};
 
 macro_rules! integer_round {
-    ($X:expr, $DIV:expr, $HALF:expr) => {{
+    ($X:expr, $DIV:expr, $HALF:expr, $FAIL_ON_ERROR:expr) => {{
         let rem = $X % $DIV;
         if rem <= -$HALF {
-            ($X - rem).sub_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).sub_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }
+            } else {
+                Ok(($X - rem).sub_wrapping($DIV))
+            }
         } else if rem >= $HALF {
-            ($X - rem).add_wrapping($DIV)
+            if $FAIL_ON_ERROR {
+                match ($X - rem).add_checked($DIV) {
+                    Ok(v) => Ok(v),
+                    Err(_e) => Err(ArrowError::ComputeError(
+                        arithmetic_overflow_error("integer").to_string(),
+                    )),
+                }
+            } else {
+                Ok(($X - rem).add_wrapping($DIV))
+            }
         } else {
-            $X - rem
+            if $FAIL_ON_ERROR {
+                match $X.sub_checked(rem) {

Review Comment:
   Sure 



-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2424701100


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,37 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    val data = Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue))
+    Seq("true", "false").foreach { p =>
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> p) {
+        withParquetTable(data, "tbl") {
+          val res = spark.sql(s"""
+                                |SELECT
+                                |  round(_1, -1) ,
+                                |  round(_1, -10) ,
+                                |  round(${Int.MaxValue}, -10)

Review Comment:
   The test should really just include one instance of `round` per query, otherwise we don't really know which one is throwing an exception.



-- 
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:support ansi mode rounding function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2542:
URL: https://github.com/apache/datafusion-comet/pull/2542#discussion_r2424676096


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3017,6 +3017,37 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for round function") {
+    val data = Seq((Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MinValue, Long.MaxValue))
+    Seq("true", "false").foreach { p =>
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> p) {
+        withParquetTable(data, "tbl") {
+          val res = spark.sql(s"""
+                                |SELECT
+                                |  round(_1, -1) ,
+                                |  round(_1, -10) ,
+                                |  round(${Int.MaxValue}, -10)

Review Comment:
   could you also add tests where scale is 0 and >0.



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