You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/07 08:03:36 UTC

[GitHub] [arrow-rs] viirya opened a new pull request, #3040: Cast decimal256 to signed integer

viirya opened a new pull request, #3040:
URL: https://github.com/apache/arrow-rs/pull/3040

   # 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 #3039.
   
   # 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.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] viirya commented on a diff in pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#discussion_r1016030872


##########
arrow-buffer/src/bigint.rs:
##########
@@ -478,6 +480,44 @@ define_as_primitive!(i16);
 define_as_primitive!(i32);
 define_as_primitive!(i64);
 
+impl ToPrimitive for i256 {
+    fn to_i64(&self) -> Option<i64> {
+        let as_i128 = self.low as i128;
+
+        let high_negative = self.high < 0;
+        let low_negative = as_i128 < 0;
+        let high_valid = self.high == -1 || self.high == 0;
+
+        if high_negative == low_negative && high_valid {
+            let (low_bytes, high_bytes) = split_array(u128::to_le_bytes(self.low));
+            let high = i64::from_le_bytes(high_bytes);
+            let low = i64::from_le_bytes(low_bytes);
+
+            let high_negative = high < 0;
+            let low_negative = low < 0;
+            let high_valid = self.high == -1 || self.high == 0;
+
+            (high_negative == low_negative && high_valid).then_some(low)

Review Comment:
   I tried simply calling `this.low.to_i64()`, but it cannot pass
   
   ```
   let a = i256::from_i128(i64::MIN as i128);
   assert_eq!(a.to_i64().unwrap(), i64::MIN);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] ursabot commented on pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#issuecomment-1306862261

   Benchmark runs are scheduled for baseline = 879b461af8c1259d48fbb1bc67d50fa2a38bea68 and contender = a950b52ec83e5ac14e147f9605f871ba6bd06ee0. a950b52ec83e5ac14e147f9605f871ba6bd06ee0 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/06777e24daec44759b014f8c79db98e5...7dd747a84c0243a88e1a1a2aa05a3579/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/ffdcfe01cf9c4ff4b28d63f8657c5895...02cd6b6912274d48aedf2c65024a402c/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/7fb62b70bb5045c28c27742ca7751c20...c6a75fa239a84f8b95bec98f98e7fe49/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/8d6aa5bcd9bf47728637cf9c5b90e4b2...643ef7c4249946f28dc11f76bc917ac3/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] viirya commented on a diff in pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#discussion_r1016027312


##########
arrow-cast/src/cast.rs:
##########
@@ -411,34 +412,51 @@ fn cast_reinterpret_arrays<
     ))
 }
 
-// cast the decimal array to integer array
-macro_rules! cast_decimal_to_integer {
-    ($ARRAY:expr, $SCALE : ident, $VALUE_BUILDER: ident, $NATIVE_TYPE : ident, $DATA_TYPE : expr) => {{
-        let array = $ARRAY.as_any().downcast_ref::<Decimal128Array>().unwrap();
-        let mut value_builder = $VALUE_BUILDER::with_capacity(array.len());
-        let div: i128 = 10_i128.pow(*$SCALE as u32);
-        let min_bound = ($NATIVE_TYPE::MIN) as i128;
-        let max_bound = ($NATIVE_TYPE::MAX) as i128;
-        for i in 0..array.len() {
-            if array.is_null(i) {
-                value_builder.append_null();
+fn cast_decimal_to_integer<D, T>(

Review Comment:
   castoptions is not supported originally for casting from decimal128 to signed integer. We should support it actually. Can be in this PR or a later 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@arrow.apache.org

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


[GitHub] [arrow-rs] viirya merged pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
viirya merged PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] viirya commented on a diff in pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#discussion_r1016030044


##########
arrow-cast/src/cast.rs:
##########
@@ -411,34 +412,51 @@ fn cast_reinterpret_arrays<
     ))
 }
 
-// cast the decimal array to integer array
-macro_rules! cast_decimal_to_integer {
-    ($ARRAY:expr, $SCALE : ident, $VALUE_BUILDER: ident, $NATIVE_TYPE : ident, $DATA_TYPE : expr) => {{
-        let array = $ARRAY.as_any().downcast_ref::<Decimal128Array>().unwrap();
-        let mut value_builder = $VALUE_BUILDER::with_capacity(array.len());
-        let div: i128 = 10_i128.pow(*$SCALE as u32);
-        let min_bound = ($NATIVE_TYPE::MIN) as i128;
-        let max_bound = ($NATIVE_TYPE::MAX) as i128;
-        for i in 0..array.len() {
-            if array.is_null(i) {
-                value_builder.append_null();
+fn cast_decimal_to_integer<D, T>(
+    array: &ArrayRef,
+    base: D::Native,
+    scale: u8,
+    max_bound: D::Native,

Review Comment:
   Implemented `ToPrimitive` for i256 now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#discussion_r1016055474


##########
arrow-buffer/src/bigint.rs:
##########
@@ -478,6 +480,44 @@ define_as_primitive!(i16);
 define_as_primitive!(i32);
 define_as_primitive!(i64);
 
+impl ToPrimitive for i256 {

Review Comment:
   I think it might be possible to simplify this, I'll have a quick play this afternoon



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] viirya commented on a diff in pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#discussion_r1016081759


##########
arrow-cast/src/cast.rs:
##########
@@ -433,34 +434,52 @@ fn cast_reinterpret_arrays<
     ))
 }
 
-// cast the decimal array to integer array
-macro_rules! cast_decimal_to_integer {
-    ($ARRAY:expr, $SCALE : ident, $VALUE_BUILDER: ident, $NATIVE_TYPE : ident, $DATA_TYPE : expr) => {{
-        let array = $ARRAY.as_any().downcast_ref::<Decimal128Array>().unwrap();
-        let mut value_builder = $VALUE_BUILDER::with_capacity(array.len());
-        let div: i128 = 10_i128.pow(*$SCALE as u32);
-        let min_bound = ($NATIVE_TYPE::MIN) as i128;
-        let max_bound = ($NATIVE_TYPE::MAX) as i128;
-        for i in 0..array.len() {
-            if array.is_null(i) {
-                value_builder.append_null();
+fn cast_decimal_to_integer<D, T>(
+    array: &ArrayRef,
+    base: D::Native,
+    scale: u8,
+    max_bound: D::Native,
+    min_bound: D::Native,
+) -> Result<ArrayRef, ArrowError>
+where
+    T: ArrowPrimitiveType,
+    <T as ArrowPrimitiveType>::Native: NumCast,
+    D: DecimalType + ArrowPrimitiveType,
+    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
+{
+    let array = array.as_any().downcast_ref::<PrimitiveArray<D>>().unwrap();
+
+    let div: D::Native = base.pow_checked(scale as u32).map_err(|_| {
+        ArrowError::CastError(format!(
+            "Cannot cast to {:?}. The scale {} causes overflow.",
+            D::PREFIX,
+            scale,
+        ))
+    })?;
+
+    let mut value_builder = PrimitiveBuilder::<T>::with_capacity(array.len());
+    for i in 0..array.len() {
+        if array.is_null(i) {
+            value_builder.append_null();
+        } else {
+            let v = array.value(i).div_checked(div)?;
+
+            // check the overflow
+            // For example: Decimal(128,10,0) as i8

Review Comment:
   Yea, but it requires `CastOptions` so it won't break existing tests. I added `CastOptions` together.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#discussion_r1015750891


##########
arrow-cast/src/cast.rs:
##########
@@ -411,34 +412,51 @@ fn cast_reinterpret_arrays<
     ))
 }
 
-// cast the decimal array to integer array
-macro_rules! cast_decimal_to_integer {
-    ($ARRAY:expr, $SCALE : ident, $VALUE_BUILDER: ident, $NATIVE_TYPE : ident, $DATA_TYPE : expr) => {{
-        let array = $ARRAY.as_any().downcast_ref::<Decimal128Array>().unwrap();
-        let mut value_builder = $VALUE_BUILDER::with_capacity(array.len());
-        let div: i128 = 10_i128.pow(*$SCALE as u32);
-        let min_bound = ($NATIVE_TYPE::MIN) as i128;
-        let max_bound = ($NATIVE_TYPE::MAX) as i128;
-        for i in 0..array.len() {
-            if array.is_null(i) {
-                value_builder.append_null();
+fn cast_decimal_to_integer<D, T>(

Review Comment:
   Do we need to support castoptions here?



##########
arrow-cast/src/cast.rs:
##########
@@ -411,34 +412,51 @@ fn cast_reinterpret_arrays<
     ))
 }
 
-// cast the decimal array to integer array
-macro_rules! cast_decimal_to_integer {
-    ($ARRAY:expr, $SCALE : ident, $VALUE_BUILDER: ident, $NATIVE_TYPE : ident, $DATA_TYPE : expr) => {{
-        let array = $ARRAY.as_any().downcast_ref::<Decimal128Array>().unwrap();
-        let mut value_builder = $VALUE_BUILDER::with_capacity(array.len());
-        let div: i128 = 10_i128.pow(*$SCALE as u32);
-        let min_bound = ($NATIVE_TYPE::MIN) as i128;
-        let max_bound = ($NATIVE_TYPE::MAX) as i128;
-        for i in 0..array.len() {
-            if array.is_null(i) {
-                value_builder.append_null();
+fn cast_decimal_to_integer<D, T>(
+    array: &ArrayRef,
+    base: D::Native,
+    scale: u8,
+    max_bound: D::Native,

Review Comment:
   We could implement ToPrimitive for i256 and then use NumCast here?



##########
arrow-buffer/src/bigint.rs:
##########
@@ -457,6 +457,21 @@ macro_rules! define_as_primitive {
                 i256::from_i128(self as i128)
             }
         }
+
+        impl AsPrimitive<$native_ty> for i256 {
+            fn as_(self) -> $native_ty {
+                let integer = self.to_i128();

Review Comment:
   I think this should just be `self.low as _`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3040: Cast decimal256 to signed integer

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #3040:
URL: https://github.com/apache/arrow-rs/pull/3040#discussion_r1016054603


##########
arrow-cast/src/cast.rs:
##########
@@ -433,34 +434,52 @@ fn cast_reinterpret_arrays<
     ))
 }
 
-// cast the decimal array to integer array
-macro_rules! cast_decimal_to_integer {
-    ($ARRAY:expr, $SCALE : ident, $VALUE_BUILDER: ident, $NATIVE_TYPE : ident, $DATA_TYPE : expr) => {{
-        let array = $ARRAY.as_any().downcast_ref::<Decimal128Array>().unwrap();
-        let mut value_builder = $VALUE_BUILDER::with_capacity(array.len());
-        let div: i128 = 10_i128.pow(*$SCALE as u32);
-        let min_bound = ($NATIVE_TYPE::MIN) as i128;
-        let max_bound = ($NATIVE_TYPE::MAX) as i128;
-        for i in 0..array.len() {
-            if array.is_null(i) {
-                value_builder.append_null();
+fn cast_decimal_to_integer<D, T>(
+    array: &ArrayRef,
+    base: D::Native,
+    scale: u8,
+    max_bound: D::Native,
+    min_bound: D::Native,
+) -> Result<ArrayRef, ArrowError>
+where
+    T: ArrowPrimitiveType,
+    <T as ArrowPrimitiveType>::Native: NumCast,
+    D: DecimalType + ArrowPrimitiveType,
+    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
+{
+    let array = array.as_any().downcast_ref::<PrimitiveArray<D>>().unwrap();
+
+    let div: D::Native = base.pow_checked(scale as u32).map_err(|_| {
+        ArrowError::CastError(format!(
+            "Cannot cast to {:?}. The scale {} causes overflow.",
+            D::PREFIX,
+            scale,
+        ))
+    })?;
+
+    let mut value_builder = PrimitiveBuilder::<T>::with_capacity(array.len());
+    for i in 0..array.len() {
+        if array.is_null(i) {
+            value_builder.append_null();
+        } else {
+            let v = array.value(i).div_checked(div)?;
+
+            // check the overflow
+            // For example: Decimal(128,10,0) as i8

Review Comment:
   Can we just use `append_option(NumCast::from(v))` and eliminate the bounds checks from this 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@arrow.apache.org

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