clickhouse_data_type/
map.rs

1use pest::iterators::{Pair, Pairs};
2
3use crate::{
4    array,
5    decimal::{self, DecimalPrecision, DecimalScale},
6    fixed_string::{self, FixedStringN},
7    type_name::TypeName,
8    type_name_parser::Rule,
9    ParseError,
10};
11
12#[derive(PartialEq, Eq, Debug, Clone)]
13pub enum MapKey {
14    UInt8,
15    UInt16,
16    UInt32,
17    UInt64,
18    UInt256,
19    Int8,
20    Int16,
21    Int32,
22    Int64,
23    Int128,
24    Int256,
25    Float32,
26    Float64,
27    Decimal(DecimalPrecision, DecimalScale),
28    String,
29    FixedString(FixedStringN),
30}
31
32impl TryFrom<Pair<'_, Rule>> for MapKey {
33    type Error = ParseError;
34
35    fn try_from(pair: Pair<'_, Rule>) -> Result<Self, Self::Error> {
36        match pair.as_rule() {
37            Rule::UInt8 => Ok(Self::UInt8),
38            Rule::UInt16 => Ok(Self::UInt16),
39            Rule::UInt32 => Ok(Self::UInt32),
40            Rule::UInt64 => Ok(Self::UInt64),
41            Rule::UInt256 => Ok(Self::UInt256),
42            Rule::Int8 => Ok(Self::Int8),
43            Rule::Int16 => Ok(Self::Int16),
44            Rule::Int32 => Ok(Self::Int32),
45            Rule::Int64 => Ok(Self::Int64),
46            Rule::Int128 => Ok(Self::Int128),
47            Rule::Int256 => Ok(Self::Int256),
48            Rule::Float32 => Ok(Self::Float32),
49            Rule::Float64 => Ok(Self::Float64),
50            Rule::Decimal => {
51                let (precision, scale) = decimal::get_precision_and_scale(pair.into_inner())?;
52
53                Ok(Self::Decimal(precision, scale))
54            }
55            Rule::String => Ok(Self::String),
56            Rule::FixedString => {
57                let n = fixed_string::get_n(pair.into_inner())?;
58
59                Ok(Self::FixedString(n))
60            }
61            _ => Err(ParseError::Unknown),
62        }
63    }
64}
65
66#[derive(PartialEq, Eq, Debug, Clone)]
67pub enum MapValue {
68    UInt8,
69    UInt16,
70    UInt32,
71    UInt64,
72    UInt256,
73    Int8,
74    Int16,
75    Int32,
76    Int64,
77    Int128,
78    Int256,
79    Float32,
80    Float64,
81    Decimal(DecimalPrecision, DecimalScale),
82    String,
83    FixedString(FixedStringN),
84    //
85    //
86    //
87    Array(Box<TypeName>),
88}
89
90impl TryFrom<Pair<'_, Rule>> for MapValue {
91    type Error = ParseError;
92
93    fn try_from(pair: Pair<'_, Rule>) -> Result<Self, Self::Error> {
94        match pair.as_rule() {
95            Rule::UInt8 => Ok(Self::UInt8),
96            Rule::UInt16 => Ok(Self::UInt16),
97            Rule::UInt32 => Ok(Self::UInt32),
98            Rule::UInt64 => Ok(Self::UInt64),
99            Rule::UInt256 => Ok(Self::UInt256),
100            Rule::Int8 => Ok(Self::Int8),
101            Rule::Int16 => Ok(Self::Int16),
102            Rule::Int32 => Ok(Self::Int32),
103            Rule::Int64 => Ok(Self::Int64),
104            Rule::Int128 => Ok(Self::Int128),
105            Rule::Int256 => Ok(Self::Int256),
106            Rule::Float32 => Ok(Self::Float32),
107            Rule::Float64 => Ok(Self::Float64),
108            Rule::Decimal => {
109                let (precision, scale) = decimal::get_precision_and_scale(pair.into_inner())?;
110
111                Ok(Self::Decimal(precision, scale))
112            }
113            Rule::String => Ok(Self::String),
114            Rule::FixedString => {
115                let n = fixed_string::get_n(pair.into_inner())?;
116
117                Ok(Self::FixedString(n))
118            }
119            //
120            //
121            //
122            Rule::Array => {
123                let data_type = self::array::get_data_type(pair.into_inner())?;
124
125                Ok(Self::Array(data_type.into()))
126            }
127            _ => Err(ParseError::Unknown),
128        }
129    }
130}
131
132pub(crate) fn get_map_key_and_map_value(
133    mut map_pairs: Pairs<'_, Rule>,
134) -> Result<(MapKey, MapValue), ParseError> {
135    let key = MapKey::try_from(
136        map_pairs
137            .next()
138            .ok_or(ParseError::Unknown)?
139            .into_inner()
140            .next()
141            .ok_or(ParseError::Unknown)?,
142    )?;
143    let value = MapValue::try_from(
144        map_pairs
145            .next()
146            .ok_or(ParseError::Unknown)?
147            .into_inner()
148            .next()
149            .ok_or(ParseError::Unknown)?,
150    )?;
151
152    Ok((key, value))
153}