risingwave_sqlparser/ast/
data_type.rs#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::ast::{display_comma_separated, Ident, ObjectName};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DataType {
Char(Option<u64>),
Varchar,
Uuid,
Decimal(Option<u64>, Option<u64>),
Float(Option<u64>),
SmallInt,
Int,
BigInt,
Real,
Double,
Boolean,
Date,
Time(bool),
Timestamp(bool),
Interval,
Regclass,
Regproc,
Text,
Bytea,
Jsonb,
Custom(ObjectName),
Array(Box<DataType>),
Struct(Vec<StructField>),
Map(Box<(DataType, DataType)>),
}
impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DataType::Char(size) => format_type_with_optional_length(f, "CHAR", size),
DataType::Varchar => write!(f, "CHARACTER VARYING"),
DataType::Uuid => write!(f, "UUID"),
DataType::Decimal(precision, scale) => {
if let Some(scale) = scale {
write!(f, "NUMERIC({},{})", precision.unwrap(), scale)
} else {
format_type_with_optional_length(f, "NUMERIC", precision)
}
}
DataType::Float(size) => format_type_with_optional_length(f, "FLOAT", size),
DataType::SmallInt => {
write!(f, "SMALLINT")
}
DataType::Int => write!(f, "INT"),
DataType::BigInt => write!(f, "BIGINT"),
DataType::Real => write!(f, "REAL"),
DataType::Double => write!(f, "DOUBLE"),
DataType::Boolean => write!(f, "BOOLEAN"),
DataType::Date => write!(f, "DATE"),
DataType::Time(tz) => write!(f, "TIME{}", if *tz { " WITH TIME ZONE" } else { "" }),
DataType::Timestamp(tz) => {
write!(f, "TIMESTAMP{}", if *tz { " WITH TIME ZONE" } else { "" })
}
DataType::Interval => write!(f, "INTERVAL"),
DataType::Regclass => write!(f, "REGCLASS"),
DataType::Regproc => write!(f, "REGPROC"),
DataType::Text => write!(f, "TEXT"),
DataType::Bytea => write!(f, "BYTEA"),
DataType::Jsonb => write!(f, "JSONB"),
DataType::Array(ty) => write!(f, "{}[]", ty),
DataType::Custom(ty) => write!(f, "{}", ty),
DataType::Struct(defs) => {
write!(f, "STRUCT<{}>", display_comma_separated(defs))
}
DataType::Map(kv) => {
write!(f, "MAP({},{})", kv.0, kv.1)
}
}
}
}
fn format_type_with_optional_length(
f: &mut fmt::Formatter<'_>,
sql_type: &'static str,
len: &Option<u64>,
) -> fmt::Result {
write!(f, "{}", sql_type)?;
if let Some(len) = len {
write!(f, "({})", len)?;
}
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StructField {
pub name: Ident,
pub data_type: DataType,
}
impl fmt::Display for StructField {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.name, self.data_type)
}
}