-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Stream CSV file during schema inference #4661
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6bb72b7
Stream CSV file during infer schema
Jefffrey ca08907
Change error type
Jefffrey 1cd2736
Merge branch 'master' into 3658_stream_csv_infer
Jefffrey 1abec66
Refactor
Jefffrey 63bab33
Merge branch 'master' into 3658_stream_csv_infer
Jefffrey 9b33005
Add test for infer csv stream
Jefffrey 5a7a986
Make csv infer schema test more robust
Jefffrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,11 +87,20 @@ pub trait FileFormat: Send + Sync + fmt::Debug { | |
|
||
#[cfg(test)] | ||
pub(crate) mod test_util { | ||
use std::ops::Range; | ||
use std::sync::Mutex; | ||
|
||
use super::*; | ||
use crate::datasource::listing::PartitionedFile; | ||
use crate::datasource::object_store::ObjectStoreUrl; | ||
use crate::test::object_store::local_unpartitioned_file; | ||
use bytes::Bytes; | ||
use futures::stream::BoxStream; | ||
use futures::StreamExt; | ||
use object_store::local::LocalFileSystem; | ||
use object_store::path::Path; | ||
use object_store::{GetResult, ListResult, MultipartId}; | ||
use tokio::io::AsyncWrite; | ||
|
||
pub async fn scan_format( | ||
state: &SessionState, | ||
|
@@ -136,4 +145,121 @@ pub(crate) mod test_util { | |
.await?; | ||
Ok(exec) | ||
} | ||
|
||
/// Mock ObjectStore to provide an variable stream of bytes on get | ||
/// Able to keep track of how many iterations of the provided bytes were repeated | ||
#[derive(Debug)] | ||
pub struct VariableStream { | ||
bytes_to_repeat: Bytes, | ||
max_iterations: usize, | ||
iterations_detected: Arc<Mutex<usize>>, | ||
} | ||
|
||
impl std::fmt::Display for VariableStream { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "VariableStream") | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ObjectStore for VariableStream { | ||
async fn put(&self, _location: &Path, _bytes: Bytes) -> object_store::Result<()> { | ||
unimplemented!() | ||
} | ||
|
||
async fn put_multipart( | ||
&self, | ||
_location: &Path, | ||
) -> object_store::Result<(MultipartId, Box<dyn AsyncWrite + Unpin + Send>)> | ||
{ | ||
unimplemented!() | ||
} | ||
|
||
async fn abort_multipart( | ||
&self, | ||
_location: &Path, | ||
_multipart_id: &MultipartId, | ||
) -> object_store::Result<()> { | ||
unimplemented!() | ||
} | ||
|
||
async fn get(&self, _location: &Path) -> object_store::Result<GetResult> { | ||
let bytes = self.bytes_to_repeat.clone(); | ||
let arc = self.iterations_detected.clone(); | ||
Ok(GetResult::Stream( | ||
futures::stream::repeat_with(move || { | ||
let arc_inner = arc.clone(); | ||
*arc_inner.lock().unwrap() += 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
Ok(bytes.clone()) | ||
}) | ||
.take(self.max_iterations) | ||
.boxed(), | ||
)) | ||
} | ||
|
||
async fn get_range( | ||
&self, | ||
_location: &Path, | ||
_range: Range<usize>, | ||
) -> object_store::Result<Bytes> { | ||
unimplemented!() | ||
} | ||
|
||
async fn get_ranges( | ||
&self, | ||
_location: &Path, | ||
_ranges: &[Range<usize>], | ||
) -> object_store::Result<Vec<Bytes>> { | ||
unimplemented!() | ||
} | ||
|
||
async fn head(&self, _location: &Path) -> object_store::Result<ObjectMeta> { | ||
unimplemented!() | ||
} | ||
|
||
async fn delete(&self, _location: &Path) -> object_store::Result<()> { | ||
unimplemented!() | ||
} | ||
|
||
async fn list( | ||
&self, | ||
_prefix: Option<&Path>, | ||
) -> object_store::Result<BoxStream<'_, object_store::Result<ObjectMeta>>> | ||
{ | ||
unimplemented!() | ||
} | ||
|
||
async fn list_with_delimiter( | ||
&self, | ||
_prefix: Option<&Path>, | ||
) -> object_store::Result<ListResult> { | ||
unimplemented!() | ||
} | ||
|
||
async fn copy(&self, _from: &Path, _to: &Path) -> object_store::Result<()> { | ||
unimplemented!() | ||
} | ||
|
||
async fn copy_if_not_exists( | ||
&self, | ||
_from: &Path, | ||
_to: &Path, | ||
) -> object_store::Result<()> { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl VariableStream { | ||
pub fn new(bytes_to_repeat: Bytes, max_iterations: usize) -> Self { | ||
Self { | ||
bytes_to_repeat, | ||
max_iterations, | ||
iterations_detected: Arc::new(Mutex::new(0)), | ||
} | ||
} | ||
|
||
pub fn get_iterations_detected(&self) -> usize { | ||
*self.iterations_detected.lock().unwrap() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome