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

[PR] fix: fix json decode number [arrow-rs]

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

   # 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/arrow-rs/issues/5095
   
   # 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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399101591


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -89,6 +97,12 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {

Review Comment:
   Because the TapeElement would then need to increase in size from the current 64-bits to 128-bits (for alignment reasons), tape decoding is exceptionally hot and so concerns like this actually matter :sweat_smile: 



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

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

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


Re: [PR] fix: fix json decode number [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399034588


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -89,6 +97,12 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {

Review Comment:
   The handling of I64 is incorrect here, this will only use the low bits. There are some examples elsewhere in how to interpret this tape element



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "fansehep (via GitHub)" <gi...@apache.org>.
fansehep commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399098100


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -89,6 +97,12 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {

Review Comment:
   > The handling of I64 is incorrect here, this will only use the low bits. There are some examples elsewhere in how to interpret this tape element
   
   I'm a little confused about the design here. Why we don't use a `i64` to save the 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@arrow.apache.org

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "fansehep (via GitHub)" <gi...@apache.org>.
fansehep commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399154218


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -97,7 +97,16 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
-                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {
+                TapeElement::I64(high) if coerce_primitive => {
+                    match tape.get(p + 1) {
+                        TapeElement::I32(low) => {
+                            let val = (high as i64) << 32 | (low as u32) as i64;
+                            builder.append_value(val.to_string());
+                        }
+                        _ => unreachable!(),
+                    }
+                }
+                TapeElement::I32(n) if coerce_primitive => {
                     builder.append_value(n.to_string());
                 }
                 TapeElement::F32(n) | TapeElement::F64(n) if coerce_primitive => {

Review Comment:
   > Can you do the same for f64 please 😄
   
   done.



##########
arrow-json/src/reader/string_array.rs:
##########
@@ -97,7 +97,16 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
-                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {
+                TapeElement::I64(high) if coerce_primitive => {
+                    match tape.get(p + 1) {
+                        TapeElement::I32(low) => {
+                            let val = (high as i64) << 32 | (low as u32) as i64;
+                            builder.append_value(val.to_string());
+                        }
+                        _ => unreachable!(),
+                    }
+                }
+                TapeElement::I32(n) if coerce_primitive => {
                     builder.append_value(n.to_string());
                 }
                 TapeElement::F32(n) | TapeElement::F64(n) if coerce_primitive => {

Review Comment:
   > Can you do the same for f64 please 😄
   
   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@arrow.apache.org

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399443163


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -61,7 +61,15 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     data_capacity += tape.get_string(idx).len();
                 }
-                _ => return Err(tape.error(*p, "string")),
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {
+                    data_capacity += n.to_string().len();
+                }
+                TapeElement::F32(n) | TapeElement::F64(n) if coerce_primitive => {
+                    data_capacity += n.to_string().len();
+                }

Review Comment:
   ```suggestion
                   TapeElement::I64(n) | TapeElement::I32(n) | TapeElement::F32(n) | TapeElement::F64(n) => {
                       data_capacity += 10; // An arbitrary estimate
                   }
   ```
   
   This avoids serializing to a string multiple times, this could be improved if it becomes a bottleneck, but this is probably good enough



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399095702


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -97,7 +97,16 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
-                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {
+                TapeElement::I64(high) if coerce_primitive => {
+                    match tape.get(p + 1) {
+                        TapeElement::I32(low) => {
+                            let val = (high as i64) << 32 | (low as u32) as i64;
+                            builder.append_value(val.to_string());
+                        }
+                        _ => unreachable!(),
+                    }
+                }
+                TapeElement::I32(n) if coerce_primitive => {
                     builder.append_value(n.to_string());
                 }
                 TapeElement::F32(n) | TapeElement::F64(n) if coerce_primitive => {

Review Comment:
   Can you do the same for f64 please :smile: 



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "fansehep (via GitHub)" <gi...@apache.org>.
fansehep commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399092978


##########
arrow-json/src/reader/mod.rs:
##########
@@ -2259,4 +2261,38 @@ mod tests {
             .values();
         assert_eq!(values, &[1699148028689, 2, 3, 4]);
     }
+
+    #[test]
+    fn test_coercing_primitive_into_string_decoder() {
+        let buf = r#"[
+            {"a": 1, "b": "A", "c": "T"},
+            {"a": 2, "b": "BB", "c": "F"},
+            {"a": 3, "b": 123, "c": false}
+        ]"#;

Review Comment:
   > Perhaps we could test with some numbers that are larger than i32::MAX, i.e. require i64
   
   done, add i32::MAX and like i64::MAX 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@arrow.apache.org

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399034289


##########
arrow-json/src/reader/mod.rs:
##########
@@ -2259,4 +2261,38 @@ mod tests {
             .values();
         assert_eq!(values, &[1699148028689, 2, 3, 4]);
     }
+
+    #[test]
+    fn test_coercing_primitive_into_string_decoder() {
+        let buf = r#"[
+            {"a": 1, "b": "A", "c": "T"},
+            {"a": 2, "b": "BB", "c": "F"},
+            {"a": 3, "b": 123, "c": false}
+        ]"#;

Review Comment:
   Perhaps we could test with some numbers that are larger than i32::MAX, i.e. require i64



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "fansehep (via GitHub)" <gi...@apache.org>.
fansehep commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399981748


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -61,7 +61,15 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     data_capacity += tape.get_string(idx).len();
                 }
-                _ => return Err(tape.error(*p, "string")),
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {
+                    data_capacity += n.to_string().len();
+                }
+                TapeElement::F32(n) | TapeElement::F64(n) if coerce_primitive => {
+                    data_capacity += n.to_string().len();
+                }

Review Comment:
   This improvement looks good.
   



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

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


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

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

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


Re: [PR] fix: fix json decode number [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399034289


##########
arrow-json/src/reader/mod.rs:
##########
@@ -2259,4 +2261,38 @@ mod tests {
             .values();
         assert_eq!(values, &[1699148028689, 2, 3, 4]);
     }
+
+    #[test]
+    fn test_coercing_primitive_into_string_decoder() {
+        let buf = r#"[
+            {"a": 1, "b": "A", "c": "T"},
+            {"a": 2, "b": "BB", "c": "F"},
+            {"a": 3, "b": 123, "c": false}
+        ]"#;

Review Comment:
   Perhaps we could test with some numbers that are larger than i32::MAX, i.e. require i32



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399443163


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -61,7 +61,15 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     data_capacity += tape.get_string(idx).len();
                 }
-                _ => return Err(tape.error(*p, "string")),
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {
+                    data_capacity += n.to_string().len();
+                }
+                TapeElement::F32(n) | TapeElement::F64(n) if coerce_primitive => {
+                    data_capacity += n.to_string().len();
+                }

Review Comment:
   ```suggestion
                   TapeElement::I64(n) | TapeElement::I32(n) | TapeElement::F32(n) | TapeElement::F64(n) => {
                       data_capacity += 10; // An arbitrary estimate
                   }
   ```



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399101591


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -89,6 +97,12 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {

Review Comment:
   Because the TapeElement would then need to increase in size from the current 64-bits to 128-bits (for alignment reasons)



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

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

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


Re: [PR] fix: coerce_primitive for serde decoded data [arrow-rs]

Posted by "fansehep (via GitHub)" <gi...@apache.org>.
fansehep commented on code in PR #5101:
URL: https://github.com/apache/arrow-rs/pull/5101#discussion_r1399110901


##########
arrow-json/src/reader/string_array.rs:
##########
@@ -89,6 +97,12 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {

Review Comment:
   > Because the TapeElement would then need to increase in size from the current 64-bits to 128-bits (for alignment reasons), tape decoding is exceptionally hot and so concerns like this actually matter 😅
   
   I get it. Just for performance. 😺 



##########
arrow-json/src/reader/string_array.rs:
##########
@@ -89,6 +97,12 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
                 TapeElement::Number(idx) if coerce_primitive => {
                     builder.append_value(tape.get_string(idx));
                 }
+                TapeElement::I64(n) | TapeElement::I32(n) if coerce_primitive => {

Review Comment:
   > Because the TapeElement would then need to increase in size from the current 64-bits to 128-bits (for alignment reasons), tape decoding is exceptionally hot and so concerns like this actually matter 😅
   
   I get it. Just for performance. 😺 



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