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

Remove unwrap on datetime cast for CSV writer #3570

Merged
merged 4 commits into from
Jan 22, 2023

Conversation

comphead
Copy link
Contributor

Which issue does this PR close?

Closes #3547 .

Rationale for this change

CSV writer panics if cast cannot be done, we should handle this gracefully

What changes are included in this PR?

Remove unwrap on datetime cast for CSV writer

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Jan 20, 2023
let mut writer = Writer::new(&mut file);
let batches = vec![&batch, &batch];
for batch in batches {
writer.write(batch).map_err(|e| { dbg!(e.to_string()); assert!(e.to_string().ends_with(invalid_cast_error("arrow_array::array::primitive_array::PrimitiveArray<arrow_array::types::Date64Type>".to_owned(), 1, 1).to_string().as_str()))}).unwrap_err();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please let me know if error message like

Cannot cast to arrow_array::array::primitive_array::PrimitiveArray<arrow_array::types::Date64Type> at row index: 1, col index: 1

is enough user friendly. The long type name concerns me a bit

Copy link
Contributor

Choose a reason for hiding this comment

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

How about printing the DataType instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@tustvold tustvold left a comment

Choose a reason for hiding this comment

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

Left some minor nits

@@ -88,6 +88,35 @@ where
lexical_to_string(c.value(i))
}

fn invalid_cast_error(dt: String, col_index: usize, row_index: usize) -> ArrowError {
let mut s = String::new();
Copy link
Contributor

Choose a reason for hiding this comment

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

format! might be a nicer way to write this, should also perform better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @tustvold
Reg to https://github.com/hoodie/concatenation_benchmarks-rs the format macro is 5x slower than string push. But you right, the exception is evauluated in ok_or_else whic is lazy and won't affect performance on millions of rows, Will fix it

ArrowError::CastError(s)
}

macro_rules! write_temporal_value {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be a macro or can it be a generic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Generic was too complex, as we need to use different conversion functions depending on the type, I have tried generics first and then falled back to macros

Copy link
Contributor

Choose a reason for hiding this comment

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

Aah I missed the $f

let mut writer = Writer::new(&mut file);
let batches = vec![&batch, &batch];
for batch in batches {
writer.write(batch).map_err(|e| { dbg!(e.to_string()); assert!(e.to_string().ends_with(invalid_cast_error("arrow_array::array::primitive_array::PrimitiveArray<arrow_array::types::Date64Type>".to_owned(), 1, 1).to_string().as_str()))}).unwrap_err();
Copy link
Contributor

Choose a reason for hiding this comment

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

How about printing the DataType instead?

@@ -88,6 +88,25 @@ where
lexical_to_string(c.value(i))
}

fn invalid_cast_error(dt: String, col_index: usize, row_index: usize) -> ArrowError {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn invalid_cast_error(dt: String, col_index: usize, row_index: usize) -> ArrowError {
fn invalid_cast_error(dt: &str, col_index: usize, row_index: usize) -> ArrowError {

Might simplify some things

Comment on lines 108 to 112
}};
}
/// A CSV writer
#[derive(Debug)]
pub struct Writer<W: Write> {
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
}};
}
/// A CSV writer
#[derive(Debug)]
pub struct Writer<W: Write> {
}};
}
/// A CSV writer
#[derive(Debug)]
pub struct Writer<W: Write> {

&self.date_format,
col_index,
row_index,
value_as_datetime,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
value_as_datetime,
value_as_date,

@comphead comphead requested review from viirya and tustvold and removed request for viirya and tustvold January 21, 2023 19:22
@comphead
Copy link
Contributor Author

Done folks, thanks for the review

@viirya viirya merged commit 24e5dae into apache:master Jan 22, 2023
@ursabot
Copy link

ursabot commented Jan 22, 2023

Benchmark runs are scheduled for baseline = acaba0a and contender = 24e5dae. 24e5dae is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Arrow CSV writer should not fail when cannot cast the value
4 participants