Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error from JSON writer rather than panic #1205

Merged
merged 2 commits into from Jan 19, 2022

Conversation

Ted-Jiang
Copy link
Member

Which issue does this PR close?

Closes #1157.

Rationale for this change

Not panic in JSON write, just throw up error

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Jan 19, 2022
@codecov-commenter
Copy link

Codecov Report

Merging #1205 (77d7977) into master (4218c74) will increase coverage by 0.09%.
The diff coverage is 59.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1205      +/-   ##
==========================================
+ Coverage   82.57%   82.67%   +0.09%     
==========================================
  Files         173      175       +2     
  Lines       50713    51568     +855     
==========================================
+ Hits        41875    42632     +757     
- Misses       8838     8936      +98     
Impacted Files Coverage Δ
arrow/src/json/writer.rs 92.14% <59.61%> (-0.49%) ⬇️
parquet/src/util/test_common/file_util.rs 75.00% <0.00%> (-18.11%) ⬇️
parquet/src/arrow/arrow_array_reader.rs 73.66% <0.00%> (-5.59%) ⬇️
parquet/src/arrow/converter.rs 64.34% <0.00%> (-5.22%) ⬇️
...rquet/src/arrow/record_reader/definition_levels.rs 88.50% <0.00%> (-1.83%) ⬇️
parquet/src/column/reader.rs 68.80% <0.00%> (-1.09%) ⬇️
parquet/src/file/serialized_reader.rs 94.37% <0.00%> (-0.60%) ⬇️
arrow/src/datatypes/datatype.rs 66.38% <0.00%> (-0.43%) ⬇️
arrow/src/array/array_binary.rs 93.13% <0.00%> (-0.42%) ⬇️
parquet/src/arrow/arrow_writer.rs 97.97% <0.00%> (-0.10%) ⬇️
... and 24 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4218c74...77d7977. Read the comment docs.

.iter()
.map(|maybe_value| match maybe_value {
Some(v) => Value::Array(array_to_json_array(&v)),
Some(v) => Value::Array(array_to_json_array(&v).unwrap()),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when i change unwarp() with ?
the trait bound Value: FromResidual<std::result::Result<Infallible, ArrowError>>is not satisfied
Can someone give some advice?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is inside a closure, that closure needs to have a return type of Result in order to use the ? operator. You can achieve that by wrapping the Value in Ok like this:

        DataType::List(_) => as_list_array(array)
            .iter()
            .map(|maybe_value| {
             match maybe_value {
                Some(v) => Ok(Value::Array(array_to_json_array(&v)?)),
                None => Ok(Value::Null),
            }})
            .collect(),

The collect at the end is able to create a Result<Vec<X>> from that Iterator<Result<X>>. The type is inferred based on the method return type. Often you would have to specify that explicitly like .collect::<Result<Vec<_>>().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! @jhorstmann this really helps me!
I have a little doubt about Ok(Value::Array(array_to_json_array(&v)?))
if ? return a Err then it will be Ok(Err), is there some info i lost (i am a Rust newbie 😂)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ? return a Err then it will be Ok(Err), is there some info i lost (i am a Rust newbie 😂)

The difference is that ? actually returns an Err from the function (as opposed to evaluating to Err)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good @Ted-Jiang -- thank you very much. ❤️

I'll plan to merge it when CI is green

Thanks a lot !

@alamb alamb added the api-change Changes to the arrow API label Jan 19, 2022
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Ted-Jiang -- this is looking quite nice and is quite close to merging. 👍

Comment on lines 139 to 147
match set_column_for_json_rows(
&mut inner_objs,
row_count,
struct_col,
inner_col_names[j],
) {
Ok(_) => {}
Err(e) => return Err(e),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is exactly what the ? operator does. There is some nice coverage of this operator in "The Book": https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator

Suggested change
match set_column_for_json_rows(
&mut inner_objs,
row_count,
struct_col,
inner_col_names[j],
) {
Ok(_) => {}
Err(e) => return Err(e),
}
set_column_for_json_rows(
&mut inner_objs,
row_count,
struct_col,
inner_col_names[j],
)?;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alamb Thanks! After this PR, I have learn more philosophy of the rust error handling .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed -- we are all learning together! Thank you for your contributions

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good @Ted-Jiang -- thank you very much. ❤️

I'll plan to merge it when CI is green

@alamb alamb merged commit fcd37ee into apache:master Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change Changes to the arrow API arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return Err from JSON writer rather than panic! for unsupported types
4 participants