risingwave_sqlparser/ast/
data_type.rs1use std::fmt;
14
15use crate::ast::{Ident, ObjectName, display_comma_separated};
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub enum DataType {
20 Char(Option<u64>),
22 Varchar,
25 Uuid,
27 Decimal(Option<u64>, Option<u64>),
29 Float(Option<u64>),
31 SmallInt,
33 Int,
35 BigInt,
37 Real,
39 Double,
41 Boolean,
43 Date,
45 Time(bool),
47 Timestamp(bool),
49 Interval,
51 Regclass,
53 Regproc,
55 Text,
57 Bytea,
59 Jsonb,
61 Variant,
63 Custom(ObjectName),
65 Array(Box<DataType>),
67 Struct(Vec<StructField>),
69 Map(Box<(DataType, DataType)>),
71 Vector(u64),
73}
74
75impl fmt::Display for DataType {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 match self {
78 DataType::Char(size) => format_type_with_optional_length(f, "CHAR", size),
79 DataType::Varchar => write!(f, "CHARACTER VARYING"),
80 DataType::Uuid => write!(f, "UUID"),
81 DataType::Decimal(precision, scale) => {
82 if let Some(scale) = scale {
83 write!(f, "NUMERIC({},{})", precision.unwrap(), scale)
84 } else {
85 format_type_with_optional_length(f, "NUMERIC", precision)
86 }
87 }
88 DataType::Float(size) => format_type_with_optional_length(f, "FLOAT", size),
89 DataType::SmallInt => {
90 write!(f, "SMALLINT")
91 }
92 DataType::Int => write!(f, "INT"),
93 DataType::BigInt => write!(f, "BIGINT"),
94 DataType::Real => write!(f, "REAL"),
95 DataType::Double => write!(f, "DOUBLE"),
96 DataType::Boolean => write!(f, "BOOLEAN"),
97 DataType::Date => write!(f, "DATE"),
98 DataType::Time(tz) => write!(f, "TIME{}", if *tz { " WITH TIME ZONE" } else { "" }),
99 DataType::Timestamp(tz) => {
100 write!(f, "TIMESTAMP{}", if *tz { " WITH TIME ZONE" } else { "" })
101 }
102 DataType::Interval => write!(f, "INTERVAL"),
103 DataType::Regclass => write!(f, "REGCLASS"),
104 DataType::Regproc => write!(f, "REGPROC"),
105 DataType::Text => write!(f, "TEXT"),
106 DataType::Bytea => write!(f, "BYTEA"),
107 DataType::Jsonb => write!(f, "JSONB"),
108 DataType::Variant => write!(f, "VARIANT"),
109 DataType::Array(ty) => write!(f, "{}[]", ty),
110 DataType::Custom(ty) => write!(f, "{}", ty),
111 DataType::Struct(defs) => {
112 write!(f, "STRUCT<")?;
113 if defs.is_empty() {
114 write!(f, " ")?;
117 } else {
118 write!(f, "{}", display_comma_separated(defs))?;
119 }
120 write!(f, ">")
121 }
122 DataType::Map(kv) => {
123 write!(f, "MAP({},{})", kv.0, kv.1)
124 }
125 DataType::Vector(size) => {
126 write!(f, "VECTOR({})", size)
127 }
128 }
129 }
130}
131
132fn format_type_with_optional_length(
133 f: &mut fmt::Formatter<'_>,
134 sql_type: &'static str,
135 len: &Option<u64>,
136) -> fmt::Result {
137 write!(f, "{}", sql_type)?;
138 if let Some(len) = len {
139 write!(f, "({})", len)?;
140 }
141 Ok(())
142}
143
144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
145pub struct StructField {
146 pub name: Ident,
147 pub data_type: DataType,
148}
149
150impl fmt::Display for StructField {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 write!(f, "{} {}", self.name, self.data_type)
153 }
154}