-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-11707: [Rust] support CSV schema inference without file IO #9534
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -98,8 +98,32 @@ fn infer_field_schema(string: &str) -> DataType { | |
| /// | ||
| /// If `max_read_records` is not set, the whole file is read to infer its schema. | ||
| /// | ||
| /// Return infered schema and number of records used for inference. This function does not change | ||
| /// reader cursor offset. | ||
| pub fn infer_file_schema<R: Read + Seek>( | ||
| reader: &mut R, | ||
| delimiter: u8, | ||
| max_read_records: Option<usize>, | ||
| has_header: bool, | ||
| ) -> Result<(Schema, usize)> { | ||
| let saved_offset = reader.seek(SeekFrom::Current(0))?; | ||
|
|
||
| let (schema, records_count) = | ||
| infer_reader_schema(reader, delimiter, max_read_records, has_header)?; | ||
|
|
||
| // return the reader seek back to the start | ||
| reader.seek(SeekFrom::Start(saved_offset))?; | ||
|
|
||
| Ok((schema, records_count)) | ||
| } | ||
|
|
||
| /// Infer schema of CSV records provided by struct that implements `Read` trait. | ||
| /// | ||
| /// `max_read_records` controlling the maximum number of records to read. If `max_read_records` is | ||
| /// not set, all records are read to infer the schema. | ||
| /// | ||
| /// Return infered schema and number of records used for inference. | ||
| fn infer_file_schema<R: Read + Seek>( | ||
| pub fn infer_reader_schema<R: Read>( | ||
| reader: &mut R, | ||
| delimiter: u8, | ||
| max_read_records: Option<usize>, | ||
|
|
@@ -121,18 +145,12 @@ fn infer_file_schema<R: Read + Seek>( | |
| .collect() | ||
| }; | ||
|
|
||
| // save the csv reader position after reading headers | ||
| let position = csv_reader.position().clone(); | ||
|
|
||
| let header_length = headers.len(); | ||
| // keep track of inferred field types | ||
| let mut column_types: Vec<HashSet<DataType>> = vec![HashSet::new(); header_length]; | ||
| // keep track of columns with nulls | ||
| let mut nulls: Vec<bool> = vec![false; header_length]; | ||
|
|
||
| // return csv reader position to after headers | ||
| csv_reader.seek(position)?; | ||
|
||
|
|
||
| let mut records_count = 0; | ||
| let mut fields = vec![]; | ||
|
|
||
|
|
@@ -184,9 +202,6 @@ fn infer_file_schema<R: Read + Seek>( | |
| } | ||
| } | ||
|
|
||
| // return the reader seek back to the start | ||
| csv_reader.into_inner().seek(SeekFrom::Start(0))?; | ||
|
|
||
| Ok((Schema::new(fields), records_count)) | ||
| } | ||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.