1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use parquet_format_safe::Type;

use crate::error::Error;
#[cfg(feature = "serde_types")]
use serde_derive::{Deserialize, Serialize};

/// The set of all physical types representable in Parquet
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Deserialize, Serialize))]
pub enum PhysicalType {
    Boolean,
    Int32,
    Int64,
    Int96,
    Float,
    Double,
    ByteArray,
    FixedLenByteArray(usize),
}

impl TryFrom<(Type, Option<i32>)> for PhysicalType {
    type Error = Error;

    fn try_from((type_, length): (Type, Option<i32>)) -> Result<Self, Self::Error> {
        Ok(match type_ {
            Type::BOOLEAN => PhysicalType::Boolean,
            Type::INT32 => PhysicalType::Int32,
            Type::INT64 => PhysicalType::Int64,
            Type::INT96 => PhysicalType::Int96,
            Type::FLOAT => PhysicalType::Float,
            Type::DOUBLE => PhysicalType::Double,
            Type::BYTE_ARRAY => PhysicalType::ByteArray,
            Type::FIXED_LEN_BYTE_ARRAY => {
                let length = length
                    .ok_or_else(|| Error::oos("Length must be defined for FixedLenByteArray"))?;
                PhysicalType::FixedLenByteArray(length.try_into()?)
            }
            _ => return Err(Error::oos("Unknown type")),
        })
    }
}

impl From<PhysicalType> for (Type, Option<i32>) {
    fn from(physical_type: PhysicalType) -> Self {
        match physical_type {
            PhysicalType::Boolean => (Type::BOOLEAN, None),
            PhysicalType::Int32 => (Type::INT32, None),
            PhysicalType::Int64 => (Type::INT64, None),
            PhysicalType::Int96 => (Type::INT96, None),
            PhysicalType::Float => (Type::FLOAT, None),
            PhysicalType::Double => (Type::DOUBLE, None),
            PhysicalType::ByteArray => (Type::BYTE_ARRAY, None),
            PhysicalType::FixedLenByteArray(length) => {
                (Type::FIXED_LEN_BYTE_ARRAY, Some(length as i32))
            }
        }
    }
}