datafusion_common/file_options/
csv_writer.rs1use crate::config::CsvOptions;
21use crate::error::{DataFusionError, Result};
22use crate::parsers::CompressionTypeVariant;
23
24use arrow::csv::WriterBuilder;
25
26#[derive(Clone, Debug)]
28pub struct CsvWriterOptions {
29 pub writer_options: WriterBuilder,
31 pub compression: CompressionTypeVariant,
34}
35
36impl CsvWriterOptions {
37 pub fn new(
38 writer_options: WriterBuilder,
39 compression: CompressionTypeVariant,
40 ) -> Self {
41 Self {
42 writer_options,
43 compression,
44 }
45 }
46}
47
48impl TryFrom<&CsvOptions> for CsvWriterOptions {
49 type Error = DataFusionError;
50
51 fn try_from(value: &CsvOptions) -> Result<Self> {
52 let mut builder = WriterBuilder::default()
53 .with_header(value.has_header.unwrap_or(true))
54 .with_quote(value.quote)
55 .with_delimiter(value.delimiter);
56
57 if let Some(v) = &value.date_format {
58 builder = builder.with_date_format(v.into())
59 }
60 if let Some(v) = &value.datetime_format {
61 builder = builder.with_datetime_format(v.into())
62 }
63 if let Some(v) = &value.timestamp_format {
64 builder = builder.with_timestamp_format(v.into())
65 }
66 if let Some(v) = &value.timestamp_tz_format {
67 builder = builder.with_timestamp_tz_format(v.into())
68 }
69 if let Some(v) = &value.time_format {
70 builder = builder.with_time_format(v.into())
71 }
72 if let Some(v) = &value.null_value {
73 builder = builder.with_null(v.into())
74 }
75 if let Some(v) = &value.escape {
76 builder = builder.with_escape(*v)
77 }
78 if let Some(v) = &value.double_quote {
79 builder = builder.with_double_quote(*v)
80 }
81 Ok(CsvWriterOptions {
82 writer_options: builder,
83 compression: value.compression,
84 })
85 }
86}