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

when inferring the schema of compressed CSV, decompress before newline-delimited chunking #5860

Merged
merged 1 commit into from
Apr 11, 2023

Conversation

jiangzhx
Copy link
Contributor

@jiangzhx jiangzhx commented Apr 4, 2023

Which issue does this PR close?

Closes #5657.

Rationale for this change

#5041
#1736

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the core Core DataFusion crate label Apr 4, 2023
@jiangzhx jiangzhx marked this pull request as draft April 4, 2023 07:32
@jiangzhx jiangzhx force-pushed the issues/5657 branch 3 times, most recently from 2e62a39 to 3b8f4e9 Compare April 4, 2023 12:04
@jiangzhx jiangzhx marked this pull request as ready for review April 4, 2023 12:09
@jiangzhx
Copy link
Contributor Author

jiangzhx commented Apr 6, 2023

@jackwener @Jefffrey I wonder if you have time to review this PR.

Copy link
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

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

Thanks for this, left some comments

@@ -124,8 +154,13 @@ impl FileFormat for CsvFormat {
let mut records_to_read = self.schema_infer_max_rec.unwrap_or(usize::MAX);

for object in objects {
// stream to only read as many rows as needed into memory
let stream = read_to_delimited_chunks(store, object).await;
let stream = store.get(&object.location).await.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

safe unwrap?

Copy link
Contributor Author

@jiangzhx jiangzhx Apr 7, 2023

Choose a reason for hiding this comment

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

thanks. fixed

Comment on lines 132 to 134
.unwrap()
.downcast::<DataFusionError>()
.unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

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

safe unwraps?

Copy link
Contributor Author

@jiangzhx jiangzhx Apr 7, 2023

Choose a reason for hiding this comment

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

I think it's fine.
Because err_converter only accepts std::io::Error type as input, and DataFusionError has also implemented it.

https://github.com/apache/arrow-datafusion/blob/fe46a1ed9833f2f9ea4c4ccd4d77718e5c371ab1/datafusion-common/src/error.rs#L78-L82

@@ -48,5 +49,22 @@ async fn main() -> Result<()> {
// print the results
df.show().await?;

// query with
Copy link
Contributor

Choose a reason for hiding this comment

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

expand on this comment a bit? seems it was cutoff, so like query compressed CSV with specific options etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

@jiangzhx
Copy link
Contributor Author

jiangzhx commented Apr 7, 2023

Thanks for this, left some comments

@Jefffrey Thanks for the review.

Comment on lines 175 to 187
// #[cfg(feature = "compression")]
let err_converter = |e: std::io::Error| match e
.get_ref()
.and_then(|e| e.downcast_ref::<DataFusionError>())
{
Some(_) => {
*(e.into_inner()
.unwrap()
.downcast::<DataFusionError>()
.unwrap())
}
None => Into::<DataFusionError>::into(e),
};
Copy link
Contributor

@Jefffrey Jefffrey Apr 7, 2023

Choose a reason for hiding this comment

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

I find this syntax to be a confusing and not ideal, however didn't find any alternative when I experimented myself (without introducing a clone)

This unstable feature seems to solve the usecase, if ever get around to refactoring this in future if/when it becomes available: rust-lang/rust#99262

Some other points:

  • Is that first commented line about cfg supposed to be there or not?
  • None => Into::<DataFusionError>::into(e) -> None => DataFusionError::from(e) looks cleaner I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • Is that first commented line about cfg supposed to be there or not?
  • None => Into::<DataFusionError>::into(e) -> None => DataFusionError::from(e) looks cleaner I think

fixed.

@jiangzhx
Copy link
Contributor Author

jiangzhx commented Apr 7, 2023

Thank you again for your review @Jefffrey

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 code cold be simplified, and I left some suggestions. However, I think it works as written so we could also merge it in as is and simpify afterwards. Thank you @jiangzhx @izveigor and @Jefffrey

testing Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

I verified this is a commit on master of testing: 👍 apache/arrow-testing@47f7b56

Comment on lines 141 to 143
GZIP => Box::new(
ReaderStream::new(AsyncGzEncoder::new(StreamReader::new(s)))
.map_err(err_converter),
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure why we need the err_converer -- this worked for me locally:

Suggested change
GZIP => Box::new(
ReaderStream::new(AsyncGzEncoder::new(StreamReader::new(s)))
.map_err(err_converter),
GZIP => Box::new(
ReaderStream::new(AsyncGzEncoder::new(StreamReader::new(s)))
.map_err(DataFusionError::from),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed. thanks

/// Return a newline delimited stream from the specified file on
/// Stream, decompressing if necessary
/// Each returned `Bytes` has a whole number of newline delimited rows
async fn read_to_delimited_chunks(
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like there must be a simpler way to express this code, but this does appear to work.

I wonder if we could use BoxStream rather than impl Stream.... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed,thanks a lot.

),
#[cfg(feature = "compression")]
ZSTD => Box::new(
ReaderStream::new(AsyncZstdEncoer::new(StreamReader::new(s)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should be ´AsyncZstdEncoder´

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, fixed.

@jiangzhx jiangzhx force-pushed the issues/5657 branch 2 times, most recently from 3101231 to 9667c21 Compare April 10, 2023 07:37
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.

Looks good -- thank you @jiangzhx

@alamb alamb merged commit 155d0a9 into apache:main Apr 11, 2023
korowa pushed a commit to korowa/arrow-datafusion that referenced this pull request Apr 13, 2023
@jiangzhx jiangzhx deleted the issues/5657 branch April 24, 2023 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request for documentation for compressed CSV/JSON support
4 participants