risingwave_sqlparser/ast/
data_type.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use std::fmt;
14
15use crate::ast::{Ident, ObjectName, display_comma_separated};
16
17/// SQL data types
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub enum DataType {
20    /// Fixed-length character type e.g. CHAR(10)
21    Char(Option<u64>),
22    /// Variable-length character type.
23    /// We diverge from postgres by disallowing Varchar(n).
24    Varchar,
25    /// Uuid type
26    Uuid,
27    /// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
28    Decimal(Option<u64>, Option<u64>),
29    /// Floating point with optional precision e.g. FLOAT(8)
30    Float(Option<u64>),
31    /// SMALLINT (int2)
32    SmallInt,
33    /// INTEGER (int4)
34    Int,
35    /// BIGINT (int8)
36    BigInt,
37    /// Floating point e.g. REAL
38    Real,
39    /// Double e.g. DOUBLE PRECISION
40    Double,
41    /// Boolean
42    Boolean,
43    /// Date
44    Date,
45    /// Time with optional time zone
46    Time(bool),
47    /// Timestamp with optional time zone
48    Timestamp(bool),
49    /// Interval
50    Interval,
51    /// Regclass used in postgresql serial
52    Regclass,
53    /// Regproc used in postgresql function
54    Regproc,
55    /// Text
56    Text,
57    /// Bytea
58    Bytea,
59    /// JSONB
60    Jsonb,
61    /// Custom type such as enums
62    Custom(ObjectName),
63    /// Arrays
64    Array(Box<DataType>),
65    /// Structs
66    Struct(Vec<StructField>),
67    /// Map(key_type, value_type)
68    Map(Box<(DataType, DataType)>),
69    /// Vector of f32, fixed-length
70    Vector(u64),
71}
72
73impl fmt::Display for DataType {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            DataType::Char(size) => format_type_with_optional_length(f, "CHAR", size),
77            DataType::Varchar => write!(f, "CHARACTER VARYING"),
78            DataType::Uuid => write!(f, "UUID"),
79            DataType::Decimal(precision, scale) => {
80                if let Some(scale) = scale {
81                    write!(f, "NUMERIC({},{})", precision.unwrap(), scale)
82                } else {
83                    format_type_with_optional_length(f, "NUMERIC", precision)
84                }
85            }
86            DataType::Float(size) => format_type_with_optional_length(f, "FLOAT", size),
87            DataType::SmallInt => {
88                write!(f, "SMALLINT")
89            }
90            DataType::Int => write!(f, "INT"),
91            DataType::BigInt => write!(f, "BIGINT"),
92            DataType::Real => write!(f, "REAL"),
93            DataType::Double => write!(f, "DOUBLE"),
94            DataType::Boolean => write!(f, "BOOLEAN"),
95            DataType::Date => write!(f, "DATE"),
96            DataType::Time(tz) => write!(f, "TIME{}", if *tz { " WITH TIME ZONE" } else { "" }),
97            DataType::Timestamp(tz) => {
98                write!(f, "TIMESTAMP{}", if *tz { " WITH TIME ZONE" } else { "" })
99            }
100            DataType::Interval => write!(f, "INTERVAL"),
101            DataType::Regclass => write!(f, "REGCLASS"),
102            DataType::Regproc => write!(f, "REGPROC"),
103            DataType::Text => write!(f, "TEXT"),
104            DataType::Bytea => write!(f, "BYTEA"),
105            DataType::Jsonb => write!(f, "JSONB"),
106            DataType::Array(ty) => write!(f, "{}[]", ty),
107            DataType::Custom(ty) => write!(f, "{}", ty),
108            DataType::Struct(defs) => {
109                write!(f, "STRUCT<")?;
110                if defs.is_empty() {
111                    // We require a whitespace for empty(zero-field) struct to prevent `<>` from
112                    // being tokenized as a single token `Neq`.
113                    write!(f, " ")?;
114                } else {
115                    write!(f, "{}", display_comma_separated(defs))?;
116                }
117                write!(f, ">")
118            }
119            DataType::Map(kv) => {
120                write!(f, "MAP({},{})", kv.0, kv.1)
121            }
122            DataType::Vector(size) => {
123                write!(f, "VECTOR({})", size)
124            }
125        }
126    }
127}
128
129fn format_type_with_optional_length(
130    f: &mut fmt::Formatter<'_>,
131    sql_type: &'static str,
132    len: &Option<u64>,
133) -> fmt::Result {
134    write!(f, "{}", sql_type)?;
135    if let Some(len) = len {
136        write!(f, "({})", len)?;
137    }
138    Ok(())
139}
140
141#[derive(Debug, Clone, PartialEq, Eq, Hash)]
142pub struct StructField {
143    pub name: Ident,
144    pub data_type: DataType,
145}
146
147impl fmt::Display for StructField {
148    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149        write!(f, "{} {}", self.name, self.data_type)
150    }
151}