|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Options related to how csv files should be written |
| 19 | +
|
| 20 | +use std::str::FromStr; |
| 21 | + |
| 22 | +use arrow::csv::WriterBuilder; |
| 23 | + |
| 24 | +use crate::{ |
| 25 | + config::ConfigOptions, |
| 26 | + error::{DataFusionError, Result}, |
| 27 | + parsers::CompressionTypeVariant, |
| 28 | +}; |
| 29 | + |
| 30 | +use super::StatementOptions; |
| 31 | + |
| 32 | +/// Options for writing CSV files |
| 33 | +#[derive(Clone, Debug)] |
| 34 | +pub struct CsvWriterOptions { |
| 35 | + /// Struct from the arrow crate which contains all csv writing related settings |
| 36 | + pub writer_options: WriterBuilder, |
| 37 | + /// Compression to apply after ArrowWriter serializes RecordBatches. |
| 38 | + /// This compression is applied by DataFusion not the ArrowWriter itself. |
| 39 | + pub compression: CompressionTypeVariant, |
| 40 | + /// Indicates whether WriterBuilder.has_header() is set to true. |
| 41 | + /// This is duplicative as WriterBuilder also stores this information. |
| 42 | + /// However, WriterBuilder does not allow public read access to the |
| 43 | + /// has_header parameter. |
| 44 | + pub has_header: bool, |
| 45 | + // TODO: expose a way to read has_header in arrow create |
| 46 | + // https://github.com/apache/arrow-rs/issues/4735 |
| 47 | +} |
| 48 | + |
| 49 | +impl TryFrom<(&ConfigOptions, &StatementOptions)> for CsvWriterOptions { |
| 50 | + type Error = DataFusionError; |
| 51 | + |
| 52 | + fn try_from(value: (&ConfigOptions, &StatementOptions)) -> Result<Self> { |
| 53 | + let _configs = value.0; |
| 54 | + let statement_options = value.1; |
| 55 | + let mut has_header = true; |
| 56 | + let mut builder = WriterBuilder::default(); |
| 57 | + let mut compression = CompressionTypeVariant::UNCOMPRESSED; |
| 58 | + for (option, value) in &statement_options.options { |
| 59 | + builder = match option.to_lowercase().as_str(){ |
| 60 | + "header" => { |
| 61 | + has_header = value.parse() |
| 62 | + .map_err(|_| DataFusionError::Configuration(format!("Unable to parse {value} as bool as required for {option}!")))?; |
| 63 | + builder.has_headers(has_header) |
| 64 | + }, |
| 65 | + "date_format" => builder.with_date_format(value.to_owned()), |
| 66 | + "datetime_format" => builder.with_datetime_format(value.to_owned()), |
| 67 | + "timestamp_format" => builder.with_timestamp_format(value.to_owned()), |
| 68 | + "time_format" => builder.with_time_format(value.to_owned()), |
| 69 | + "rfc3339" => { |
| 70 | + let value_bool = value.parse() |
| 71 | + .map_err(|_| DataFusionError::Configuration(format!("Unable to parse {value} as bool as required for {option}!")))?; |
| 72 | + if value_bool{ |
| 73 | + builder.with_rfc3339() |
| 74 | + } else{ |
| 75 | + builder |
| 76 | + } |
| 77 | + }, |
| 78 | + "null_value" => builder.with_null(value.to_owned()), |
| 79 | + "compression" => { |
| 80 | + compression = CompressionTypeVariant::from_str(value.replace('\'', "").as_str())?; |
| 81 | + builder |
| 82 | + }, |
| 83 | + "delimeter" => { |
| 84 | + // Ignore string literal single quotes passed from sql parsing |
| 85 | + let value = value.replace('\'', ""); |
| 86 | + let chars: Vec<char> = value.chars().collect(); |
| 87 | + if chars.len()>1{ |
| 88 | + return Err(DataFusionError::Configuration(format!( |
| 89 | + "CSV Delimeter Option must be a single char, got: {}", value |
| 90 | + ))) |
| 91 | + } |
| 92 | + builder.with_delimiter(chars[0].try_into().map_err(|_| { |
| 93 | + DataFusionError::Internal( |
| 94 | + "Unable to convert CSV delimiter into u8".into(), |
| 95 | + ) |
| 96 | + })?) |
| 97 | + }, |
| 98 | + _ => return Err(DataFusionError::Configuration(format!("Found unsupported option {option} with value {value} for CSV format!"))) |
| 99 | + } |
| 100 | + } |
| 101 | + Ok(CsvWriterOptions { |
| 102 | + has_header, |
| 103 | + writer_options: builder, |
| 104 | + compression, |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments