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