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 Custom(ObjectName),
63 Array(Box<DataType>),
65 Struct(Vec<StructField>),
67 Map(Box<(DataType, DataType)>),
69 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 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}