-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Support Configuring Arrow RecordBatch Writers via SQL Statement Options #7390
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
29c96b4
squash, merge main
devinjdangelo 403c387
merge main again
devinjdangelo 53dbf1d
cargo fmt
devinjdangelo 309182f
try to fix pyarrow compile failure
devinjdangelo e78628a
Merge remote-tracking branch 'apache/main' into writeoptions
alamb 9ecd846
Merge remote-tracking branch 'apache/main' into writeoptions
alamb c0b358e
Apply suggestions from code review
devinjdangelo 7be4814
review comments + add test cases
devinjdangelo fcdb3d4
update Display for FileTypeWriterOptions
devinjdangelo e2581aa
add unit tests
devinjdangelo 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
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
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Options related to how Arrow files should be written | ||
|
|
||
| use crate::{ | ||
| config::ConfigOptions, | ||
| error::{DataFusionError, Result}, | ||
| }; | ||
|
|
||
| use super::StatementOptions; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct ArrowWriterOptions {} | ||
|
|
||
| impl TryFrom<(&ConfigOptions, &StatementOptions)> for ArrowWriterOptions { | ||
| type Error = DataFusionError; | ||
|
|
||
| fn try_from(_value: (&ConfigOptions, &StatementOptions)) -> Result<Self> { | ||
| Ok(ArrowWriterOptions {}) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Options related to how avro files should be written | ||
|
|
||
| use crate::{ | ||
| config::ConfigOptions, | ||
| error::{DataFusionError, Result}, | ||
| }; | ||
|
|
||
| use super::StatementOptions; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct AvroWriterOptions {} | ||
|
|
||
| impl TryFrom<(&ConfigOptions, &StatementOptions)> for AvroWriterOptions { | ||
| type Error = DataFusionError; | ||
|
|
||
| fn try_from(_value: (&ConfigOptions, &StatementOptions)) -> Result<Self> { | ||
| Ok(AvroWriterOptions {}) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Options related to how csv files should be written | ||
|
|
||
| use std::str::FromStr; | ||
|
|
||
| use arrow::csv::WriterBuilder; | ||
|
|
||
| use crate::{ | ||
| config::ConfigOptions, | ||
| error::{DataFusionError, Result}, | ||
| parsers::CompressionTypeVariant, | ||
| }; | ||
|
|
||
| use super::StatementOptions; | ||
|
|
||
| /// Options for writing CSV files | ||
| #[derive(Clone, Debug)] | ||
| pub struct CsvWriterOptions { | ||
| /// Struct from the arrow crate which contains all csv writing related settings | ||
| pub writer_options: WriterBuilder, | ||
| /// Compression to apply after ArrowWriter serializes RecordBatches. | ||
| /// This compression is applied by DataFusion not the ArrowWriter itself. | ||
| pub compression: CompressionTypeVariant, | ||
| /// Indicates whether WriterBuilder.has_header() is set to true. | ||
| /// This is duplicative as WriterBuilder also stores this information. | ||
| /// However, WriterBuilder does not allow public read access to the | ||
| /// has_header parameter. | ||
| pub has_header: bool, | ||
| // TODO: expose a way to read has_header in arrow create | ||
| // https://github.com/apache/arrow-rs/issues/4735 | ||
| } | ||
|
|
||
| impl TryFrom<(&ConfigOptions, &StatementOptions)> for CsvWriterOptions { | ||
| type Error = DataFusionError; | ||
|
|
||
| fn try_from(value: (&ConfigOptions, &StatementOptions)) -> Result<Self> { | ||
|
Contributor
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. this is cool |
||
| let _configs = value.0; | ||
| let statement_options = value.1; | ||
| let mut has_header = true; | ||
| let mut builder = WriterBuilder::default(); | ||
| let mut compression = CompressionTypeVariant::UNCOMPRESSED; | ||
| for (option, value) in &statement_options.options { | ||
| builder = match option.to_lowercase().as_str(){ | ||
| "header" => { | ||
| has_header = value.parse() | ||
| .map_err(|_| DataFusionError::Configuration(format!("Unable to parse {value} as bool as required for {option}!")))?; | ||
| builder.has_headers(has_header) | ||
| }, | ||
| "date_format" => builder.with_date_format(value.to_owned()), | ||
| "datetime_format" => builder.with_datetime_format(value.to_owned()), | ||
| "timestamp_format" => builder.with_timestamp_format(value.to_owned()), | ||
| "time_format" => builder.with_time_format(value.to_owned()), | ||
| "rfc3339" => { | ||
| let value_bool = value.parse() | ||
| .map_err(|_| DataFusionError::Configuration(format!("Unable to parse {value} as bool as required for {option}!")))?; | ||
| if value_bool{ | ||
| builder.with_rfc3339() | ||
| } else{ | ||
| builder | ||
| } | ||
| }, | ||
| "null_value" => builder.with_null(value.to_owned()), | ||
| "compression" => { | ||
| compression = CompressionTypeVariant::from_str(value.replace('\'', "").as_str())?; | ||
| builder | ||
| }, | ||
| "delimeter" => { | ||
| // Ignore string literal single quotes passed from sql parsing | ||
| let value = value.replace('\'', ""); | ||
| let chars: Vec<char> = value.chars().collect(); | ||
| if chars.len()>1{ | ||
| return Err(DataFusionError::Configuration(format!( | ||
| "CSV Delimeter Option must be a single char, got: {}", value | ||
| ))) | ||
| } | ||
| builder.with_delimiter(chars[0].try_into().map_err(|_| { | ||
| DataFusionError::Internal( | ||
| "Unable to convert CSV delimiter into u8".into(), | ||
| ) | ||
| })?) | ||
| }, | ||
| _ => return Err(DataFusionError::Configuration(format!("Found unsupported option {option} with value {value} for CSV format!"))) | ||
| } | ||
| } | ||
| Ok(CsvWriterOptions { | ||
| has_header, | ||
| writer_options: builder, | ||
| compression, | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.