Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datafusion/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ path = "src/lib.rs"
[features]
avro = ["apache-avro"]
compression = ["xz2", "bzip2", "flate2", "zstd", "async-compression"]
default = ["compression"]
default = ["compression", "parquet"]
pyarrow = ["pyo3", "arrow/pyarrow"]

[dependencies]
Expand Down
7 changes: 7 additions & 0 deletions datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub enum DataFusionError {
/// This error happens whenever a plan is not valid. Examples include
/// impossible casts.
Plan(String),
/// This error happens when an invalid or unsupported option is passed
/// in a SQL statement
Configuration(String),
/// This error happens with schema-related errors, such as schema inference not possible
/// and non-unique column names.
SchemaError(SchemaError),
Expand Down Expand Up @@ -288,6 +291,9 @@ impl Display for DataFusionError {
DataFusionError::SQL(ref desc) => {
write!(f, "SQL error: {desc:?}")
}
DataFusionError::Configuration(ref desc) => {
write!(f, "Invalid or Unsupported Configuration: {desc}")
}
DataFusionError::NotImplemented(ref desc) => {
write!(f, "This feature is not implemented: {desc}")
}
Expand Down Expand Up @@ -338,6 +344,7 @@ impl Error for DataFusionError {
DataFusionError::SQL(e) => Some(e),
DataFusionError::NotImplemented(_) => None,
DataFusionError::Internal(_) => None,
DataFusionError::Configuration(_) => None,
DataFusionError::Plan(_) => None,
DataFusionError::SchemaError(e) => Some(e),
DataFusionError::Execution(_) => None,
Expand Down
36 changes: 36 additions & 0 deletions datafusion/common/src/file_options/arrow_writer.rs
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 {})
}
}
36 changes: 36 additions & 0 deletions datafusion/common/src/file_options/avro_writer.rs
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 {})
}
}
107 changes: 107 additions & 0 deletions datafusion/common/src/file_options/csv_writer.rs
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> {
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
})
}
}
Loading