You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "vaibhawvipul (via GitHub)" <gi...@apache.org> on 2024/05/06 07:34:58 UTC
[PR] feat: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
vaibhawvipul opened a new pull request, #384:
URL: https://github.com/apache/datafusion-comet/pull/384
## 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 #371
## 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.
-->
Improve compatibility with spark
## 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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1592732303
##########
spark/src/test/scala/org/apache/comet/CometCastSuite.scala:
##########
@@ -402,7 +402,7 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
castTest(generateDoubles(), DataTypes.FloatType)
}
- ignore("cast DoubleType to DecimalType(10,2)") {
+ test("cast DoubleType to DecimalType(10,2)") {
// Comet should have failed with [NUMERIC_VALUE_OUT_OF_RANGE]
Review Comment:
This comment can be removed now.
```suggestion
// Comet should have failed with [NUMERIC_VALUE_OUT_OF_RANGE]
```
--
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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#issuecomment-2099517341
## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/384?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 34.00%. Comparing base [(`9ab6c75`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/9ab6c75f41456234f2fb93fcec15ff3cd435f49e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`a8dcdea`)](https://app.codecov.io/gh/apache/datafusion-comet/pull/384?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
> Report is 60 commits behind head on main.
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## main #384 +/- ##
============================================
+ Coverage 33.47% 34.00% +0.53%
- Complexity 795 857 +62
============================================
Files 110 116 +6
Lines 37533 38552 +1019
Branches 8215 8513 +298
============================================
+ Hits 12563 13110 +547
- Misses 22322 22690 +368
- Partials 2648 2752 +104
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/384?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).
--
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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "vaibhawvipul (via GitHub)" <gi...@apache.org>.
vaibhawvipul commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1592854344
##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -395,6 +399,58 @@ impl Cast {
Ok(cast_array)
}
+ fn cast_float64_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ let input = array.as_any().downcast_ref::<Float64Array>().unwrap();
+ let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());
+
+ let mul = (precision as f64).powi(scale as i32);
Review Comment:
nice catch.. it was a bug.
```let mul = 10_f64.powi(scale as i32);``` taken from decimal.rs - arrow_cast.
--
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: Implement Spark-compatible CAST from floating-point/double to decimal [datafusion-comet]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1594491463
##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -395,6 +404,83 @@ impl Cast {
Ok(cast_array)
}
+ fn cast_float64_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float64Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_float32_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float32Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_floating_point_to_decimal128<T: ArrowPrimitiveType>(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef>
+ where
+ <T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
+ {
+ let input = array.as_any().downcast_ref::<PrimitiveArray<T>>().unwrap();
+ let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());
+
+ let mul = 10_f64.powi(scale as i32);
+
+ for i in 0..input.len() {
+ if input.is_null(i) {
+ cast_array.append_null();
+ } else {
+ let input_value = input.value(i).as_();
+ let value = (input_value * mul).round().to_i128();
+
+ match value {
+ Some(v) => {
+ if Decimal128Type::validate_decimal_precision(v, precision).is_err() {
+ if eval_mode == EvalMode::Ansi {
+ return Err(CometError::NumericValueOutOfRange {
+ value: input_value.to_string(),
+ precision,
+ scale,
+ });
+ } else {
+ cast_array.append_null();
+ }
+ }
+ cast_array.append_value(v);
Review Comment:
Thank you for the fix. As it is not detected by the test, maybe we can add a test for follow up.
--
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: Implement Spark-compatible CAST from floating-point/double to decimal [datafusion-comet]
Posted by "vaibhawvipul (via GitHub)" <gi...@apache.org>.
vaibhawvipul commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1594485238
##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -395,6 +404,79 @@ impl Cast {
Ok(cast_array)
}
+ fn cast_float64_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float64Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_float32_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float32Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_floating_point_to_decimal128<T: ArrowPrimitiveType>(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef>
+ where
+ <T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
+ {
+ let input = array.as_any().downcast_ref::<PrimitiveArray<T>>().unwrap();
+ let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());
+
+ let mul = 10_f64.powi(scale as i32);
+
+ for i in 0..input.len() {
+ if input.is_null(i) {
+ cast_array.append_null();
+ } else {
+ let input_value = input.value(i).as_();
+ let value = (input_value * mul).round().to_i128();
+
+ match value {
+ Some(v) => {
+ if Decimal128Type::validate_decimal_precision(v, precision).is_err() {
+ return Err(CometError::NumericValueOutOfRange {
+ value: input_value.to_string(),
+ precision,
+ scale,
+ });
+ }
+ cast_array.append_value(v);
Review Comment:
fixed 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: Implement Spark-compatible CAST from floating-point/double to decimal [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384
--
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: Implement Spark-compatible CAST from floating-point/double to decimal [datafusion-comet]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1594450936
##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -395,6 +404,79 @@ impl Cast {
Ok(cast_array)
}
+ fn cast_float64_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float64Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_float32_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float32Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_floating_point_to_decimal128<T: ArrowPrimitiveType>(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef>
+ where
+ <T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
+ {
+ let input = array.as_any().downcast_ref::<PrimitiveArray<T>>().unwrap();
+ let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());
+
+ let mul = 10_f64.powi(scale as i32);
+
+ for i in 0..input.len() {
+ if input.is_null(i) {
+ cast_array.append_null();
+ } else {
+ let input_value = input.value(i).as_();
+ let value = (input_value * mul).round().to_i128();
+
+ match value {
+ Some(v) => {
+ if Decimal128Type::validate_decimal_precision(v, precision).is_err() {
+ return Err(CometError::NumericValueOutOfRange {
+ value: input_value.to_string(),
+ precision,
+ scale,
+ });
+ }
+ cast_array.append_value(v);
Review Comment:
Hmm, don't we need to handle `eval_mode` in this block?
--
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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1592861816
##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -395,6 +399,58 @@ impl Cast {
Ok(cast_array)
}
+ fn cast_float64_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ let input = array.as_any().downcast_ref::<Float64Array>().unwrap();
+ let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());
+
+ let mul = (precision as f64).powi(scale as i32);
Review Comment:
Makes _much_ more sense now ( I thought this might be some new Math :) )
The unit test didn't catch this because the precision in the test was 10! Perhaps we can add another unit test with some odd numbers.
--
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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#issuecomment-2098808791
@vaibhawvipul Do you plan on supporting `FloatType -> DecimalType` as well as `DecimalType -> DecimalType` in this PR?
--
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: Implement Spark-compatible CAST from floating-point/double to decimal [datafusion-comet]
Posted by "vaibhawvipul (via GitHub)" <gi...@apache.org>.
vaibhawvipul commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1594409792
##########
spark/src/test/scala/org/apache/comet/CometCastSuite.scala:
##########
@@ -960,11 +958,19 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
val cometMessageModified = cometMessage
.replace("[CAST_INVALID_INPUT] ", "")
.replace("[CAST_OVERFLOW] ", "")
- assert(cometMessageModified == sparkMessage)
+ .replace("[NUMERIC_VALUE_OUT_OF_RANGE] ", "")
+
+ if (sparkMessage.contains("cannot be represented as")) {
+ assert(cometMessage.contains("cannot be represented as"))
+ } else {
+ assert(cometMessageModified == sparkMessage)
+ }
} else {
// for Spark 3.2 we just make sure we are seeing a similar type of error
if (sparkMessage.contains("causes overflow")) {
assert(cometMessage.contains("due to an overflow"))
+ } else if (sparkMessage.contains("cannot be represented as")) {
+ assert(cometMessage.contains("cannot be represented as"))
Review Comment:
okay, that would be 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@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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "vaibhawvipul (via GitHub)" <gi...@apache.org>.
vaibhawvipul commented on PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#issuecomment-2099074637
> @vaibhawvipul Do you plan on supporting `FloatType -> DecimalType` as well as `DoubleType -> DecimalType` in this PR?
@andygrove - This PR now supports both FloatType/DoubleType -> DecimalType.
--
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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "vaibhawvipul (via GitHub)" <gi...@apache.org>.
vaibhawvipul commented on PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#issuecomment-2098823494
> @vaibhawvipul Do you plan on supporting `FloatType -> DecimalType` as well as `DecimalType -> DecimalType` in this PR?
no, but do you want me to? I thought that the issue is specific to float to decimal. Please let me know if you want me to support DecimalType->DecimalType.
--
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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#issuecomment-2096401875
This is looking good @vaibhawvipul.
--
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: Implement Spark-compatible CAST from floating-point to decimal [datafusion-comet]
Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1592834377
##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -395,6 +399,58 @@ impl Cast {
Ok(cast_array)
}
+ fn cast_float64_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ let input = array.as_any().downcast_ref::<Float64Array>().unwrap();
+ let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());
+
+ let mul = (precision as f64).powi(scale as i32);
Review Comment:
Not sure I understand how this works. Can you add a comment?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@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: Implement Spark-compatible CAST from floating-point/double to decimal [datafusion-comet]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1594450936
##########
core/src/execution/datafusion/expressions/cast.rs:
##########
@@ -395,6 +404,79 @@ impl Cast {
Ok(cast_array)
}
+ fn cast_float64_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float64Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_float32_to_decimal128(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef> {
+ Self::cast_floating_point_to_decimal128::<Float32Type>(array, precision, scale, eval_mode)
+ }
+
+ fn cast_floating_point_to_decimal128<T: ArrowPrimitiveType>(
+ array: &dyn Array,
+ precision: u8,
+ scale: i8,
+ eval_mode: EvalMode,
+ ) -> CometResult<ArrayRef>
+ where
+ <T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
+ {
+ let input = array.as_any().downcast_ref::<PrimitiveArray<T>>().unwrap();
+ let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());
+
+ let mul = 10_f64.powi(scale as i32);
+
+ for i in 0..input.len() {
+ if input.is_null(i) {
+ cast_array.append_null();
+ } else {
+ let input_value = input.value(i).as_();
+ let value = (input_value * mul).round().to_i128();
+
+ match value {
+ Some(v) => {
+ if Decimal128Type::validate_decimal_precision(v, precision).is_err() {
+ return Err(CometError::NumericValueOutOfRange {
+ value: input_value.to_string(),
+ precision,
+ scale,
+ });
+ }
+ cast_array.append_value(v);
Review Comment:
Hmm, don't we need to handle `eval_mode` in this block?
If the i128 is out of range, I think we need to return null if it is not ansi mode.
--
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: Implement Spark-compatible CAST from floating-point/double to decimal [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #384:
URL: https://github.com/apache/datafusion-comet/pull/384#discussion_r1594407351
##########
spark/src/test/scala/org/apache/comet/CometCastSuite.scala:
##########
@@ -960,11 +958,19 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
val cometMessageModified = cometMessage
.replace("[CAST_INVALID_INPUT] ", "")
.replace("[CAST_OVERFLOW] ", "")
- assert(cometMessageModified == sparkMessage)
+ .replace("[NUMERIC_VALUE_OUT_OF_RANGE] ", "")
+
+ if (sparkMessage.contains("cannot be represented as")) {
+ assert(cometMessage.contains("cannot be represented as"))
+ } else {
+ assert(cometMessageModified == sparkMessage)
+ }
} else {
// for Spark 3.2 we just make sure we are seeing a similar type of error
if (sparkMessage.contains("causes overflow")) {
assert(cometMessage.contains("due to an overflow"))
+ } else if (sparkMessage.contains("cannot be represented as")) {
+ assert(cometMessage.contains("cannot be represented as"))
Review Comment:
I can see that the approach we have for handling error message comparison for Spark 3.2 and 3.3 needs some rethinking. I am going to make a proposal to improve this.
https://github.com/apache/datafusion-comet/issues/402
--
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