datafusion_common/
parquet_config.rs

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
18use std::fmt::{self, Display};
19use std::str::FromStr;
20
21use crate::config::{ConfigField, Visit};
22use crate::error::{DataFusionError, Result};
23
24/// Parquet writer version options for controlling the Parquet file format version
25///
26/// This enum validates parquet writer version values at configuration time,
27/// ensuring only valid versions ("1.0" or "2.0") can be set via `SET` commands
28/// or proto deserialization.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
30pub enum DFParquetWriterVersion {
31    /// Parquet format version 1.0
32    #[default]
33    V1_0,
34    /// Parquet format version 2.0
35    V2_0,
36}
37
38/// Implement parsing strings to `DFParquetWriterVersion`
39impl FromStr for DFParquetWriterVersion {
40    type Err = DataFusionError;
41
42    fn from_str(s: &str) -> Result<Self, Self::Err> {
43        match s.to_lowercase().as_str() {
44            "1.0" => Ok(DFParquetWriterVersion::V1_0),
45            "2.0" => Ok(DFParquetWriterVersion::V2_0),
46            other => Err(DataFusionError::Configuration(format!(
47                "Invalid parquet writer version: {other}. Expected one of: 1.0, 2.0"
48            ))),
49        }
50    }
51}
52
53impl Display for DFParquetWriterVersion {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        let s = match self {
56            DFParquetWriterVersion::V1_0 => "1.0",
57            DFParquetWriterVersion::V2_0 => "2.0",
58        };
59        write!(f, "{s}")
60    }
61}
62
63impl ConfigField for DFParquetWriterVersion {
64    fn visit<V: Visit>(&self, v: &mut V, key: &str, description: &'static str) {
65        v.some(key, self, description)
66    }
67
68    fn set(&mut self, _: &str, value: &str) -> Result<()> {
69        *self = DFParquetWriterVersion::from_str(value)?;
70        Ok(())
71    }
72}
73
74/// Convert `DFParquetWriterVersion` to parquet crate's `WriterVersion`
75///
76/// This conversion is infallible since `DFParquetWriterVersion` only contains
77/// valid values that have been validated at configuration time.
78#[cfg(feature = "parquet")]
79impl From<DFParquetWriterVersion> for parquet::file::properties::WriterVersion {
80    fn from(value: DFParquetWriterVersion) -> Self {
81        match value {
82            DFParquetWriterVersion::V1_0 => {
83                parquet::file::properties::WriterVersion::PARQUET_1_0
84            }
85            DFParquetWriterVersion::V2_0 => {
86                parquet::file::properties::WriterVersion::PARQUET_2_0
87            }
88        }
89    }
90}
91
92/// Convert parquet crate's `WriterVersion` to `DFParquetWriterVersion`
93///
94/// This is used when converting from existing parquet writer properties,
95/// such as when reading from proto or test code.
96#[cfg(feature = "parquet")]
97impl From<parquet::file::properties::WriterVersion> for DFParquetWriterVersion {
98    fn from(version: parquet::file::properties::WriterVersion) -> Self {
99        match version {
100            parquet::file::properties::WriterVersion::PARQUET_1_0 => {
101                DFParquetWriterVersion::V1_0
102            }
103            parquet::file::properties::WriterVersion::PARQUET_2_0 => {
104                DFParquetWriterVersion::V2_0
105            }
106        }
107    }
108}