Skip to main content

datafusion_common/file_options/
csv_writer.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Options related to how csv files should be written
6
7use crate::config::CsvOptions;
8use crate::error::{DataFusionError, Result};
9use crate::parsers::CompressionTypeVariant;
10
11use arrow::csv::WriterBuilder;
12
13/// Options for writing CSV files
14#[derive(Clone, Debug)]
15pub struct CsvWriterOptions {
16    /// Struct from the arrow crate which contains all csv writing related settings
17    pub writer_options: WriterBuilder,
18    /// Compression to apply after ArrowWriter serializes RecordBatches.
19    /// This compression is applied by DataFusion not the ArrowWriter itself.
20    pub compression: CompressionTypeVariant,
21    /// Compression level for the output file.
22    pub compression_level: Option<u32>,
23}
24
25impl CsvWriterOptions {
26    pub fn new(
27        writer_options: WriterBuilder,
28        compression: CompressionTypeVariant,
29    ) -> Self {
30        Self {
31            writer_options,
32            compression,
33            compression_level: None,
34        }
35    }
36
37    /// Create a new `CsvWriterOptions` with the specified compression level.
38    pub fn new_with_level(
39        writer_options: WriterBuilder,
40        compression: CompressionTypeVariant,
41        compression_level: u32,
42    ) -> Self {
43        Self {
44            writer_options,
45            compression,
46            compression_level: Some(compression_level),
47        }
48    }
49}
50
51impl TryFrom<&CsvOptions> for CsvWriterOptions {
52    type Error = DataFusionError;
53
54    fn try_from(value: &CsvOptions) -> Result<Self> {
55        let mut builder = WriterBuilder::default()
56            .with_header(value.has_header.unwrap_or(true))
57            .with_quote(value.quote)
58            .with_delimiter(value.delimiter);
59
60        if let Some(v) = &value.date_format {
61            builder = builder.with_date_format(v.into())
62        }
63        if let Some(v) = &value.datetime_format {
64            builder = builder.with_datetime_format(v.into())
65        }
66        if let Some(v) = &value.timestamp_format {
67            builder = builder.with_timestamp_format(v.into())
68        }
69        if let Some(v) = &value.timestamp_tz_format {
70            builder = builder.with_timestamp_tz_format(v.into())
71        }
72        if let Some(v) = &value.time_format {
73            builder = builder.with_time_format(v.into())
74        }
75        if let Some(v) = &value.null_value {
76            builder = builder.with_null(v.into())
77        }
78        if let Some(v) = &value.escape {
79            builder = builder.with_escape(*v)
80        }
81        if let Some(v) = &value.double_quote {
82            builder = builder.with_double_quote(*v)
83        }
84        builder = builder.with_quote_style(value.quote_style.into());
85        if let Some(v) = &value.ignore_leading_whitespace {
86            builder = builder.with_ignore_leading_whitespace(*v)
87        }
88        if let Some(v) = &value.ignore_trailing_whitespace {
89            builder = builder.with_ignore_trailing_whitespace(*v)
90        }
91        Ok(CsvWriterOptions {
92            writer_options: builder,
93            compression: value.compression,
94            compression_level: value.compression_level,
95        })
96    }
97}