risingwave_sqlsmith/sql_gen/
types.rs1use std::collections::{HashMap, HashSet};
18use std::sync::LazyLock;
19
20use itertools::Itertools;
21use risingwave_common::types::{DataType, DataTypeName};
22use risingwave_expr::aggregate::PbAggKind;
23use risingwave_expr::sig::{FUNCTION_REGISTRY, FuncSign};
24use risingwave_frontend::expr::{CastContext, CastSig as RwCastSig, ExprType, cast_sigs};
25use risingwave_sqlparser::ast::{BinaryOperator, DataType as AstDataType, StructField};
26
27pub(super) fn data_type_to_ast_data_type(data_type: &DataType) -> AstDataType {
28 match data_type {
29 DataType::Boolean => AstDataType::Boolean,
30 DataType::Int16 => AstDataType::SmallInt,
31 DataType::Int32 => AstDataType::Int,
32 DataType::Int64 => AstDataType::BigInt,
33 DataType::Int256 => AstDataType::Custom(vec!["rw_int256".into()].into()),
34 DataType::Serial => unreachable!("serial should not be generated"),
35 DataType::Decimal => AstDataType::Decimal(None, None),
36 DataType::Float32 => AstDataType::Real,
37 DataType::Float64 => AstDataType::Double,
38 DataType::Varchar => AstDataType::Varchar,
39 DataType::Bytea => AstDataType::Bytea,
40 DataType::Date => AstDataType::Date,
41 DataType::Timestamp => AstDataType::Timestamp(false),
42 DataType::Timestamptz => AstDataType::Timestamp(true),
43 DataType::Time => AstDataType::Time(false),
44 DataType::Interval => AstDataType::Interval,
45 DataType::Jsonb => AstDataType::Custom(vec!["JSONB".into()].into()),
46 DataType::Struct(inner) => AstDataType::Struct(
47 inner
48 .iter()
49 .map(|(name, typ)| StructField {
50 name: name.into(),
51 data_type: data_type_to_ast_data_type(typ),
52 })
53 .collect(),
54 ),
55 DataType::List(typ) => AstDataType::Array(Box::new(data_type_to_ast_data_type(typ))),
56 DataType::Map(_) => todo!(),
57 }
58}
59
60fn data_type_name_to_ast_data_type(data_type_name: &DataTypeName) -> Option<DataType> {
61 use DataTypeName as T;
62 match data_type_name {
63 T::Boolean => Some(DataType::Boolean),
64 T::Int16 => Some(DataType::Int16),
65 T::Int32 => Some(DataType::Int32),
66 T::Int64 => Some(DataType::Int64),
67 T::Decimal => Some(DataType::Decimal),
68 T::Float32 => Some(DataType::Float32),
69 T::Float64 => Some(DataType::Float64),
70 T::Varchar => Some(DataType::Varchar),
71 T::Date => Some(DataType::Date),
72 T::Timestamp => Some(DataType::Timestamp),
73 T::Timestamptz => Some(DataType::Timestamptz),
74 T::Time => Some(DataType::Time),
75 T::Interval => Some(DataType::Interval),
76 _ => None,
77 }
78}
79
80#[derive(Clone)]
82pub struct CastSig {
83 pub from_type: DataType,
84 pub to_type: DataType,
85 pub context: CastContext,
86}
87
88impl TryFrom<RwCastSig> for CastSig {
89 type Error = String;
90
91 fn try_from(value: RwCastSig) -> Result<Self, Self::Error> {
92 if let Some(from_type) = data_type_name_to_ast_data_type(&value.from_type)
93 && let Some(to_type) = data_type_name_to_ast_data_type(&value.to_type)
94 {
95 Ok(CastSig {
96 from_type,
97 to_type,
98 context: value.context,
99 })
100 } else {
101 Err(format!("unsupported cast sig: {:?}", value))
102 }
103 }
104}
105
106static FUNC_BAN_LIST: LazyLock<HashSet<ExprType>> = LazyLock::new(|| {
111 [
112 ExprType::Repeat,
114 ExprType::Decode,
116 ExprType::Sqrt,
118 ExprType::Pow,
120 ]
121 .into_iter()
122 .collect()
123});
124
125pub(crate) static FUNC_TABLE: LazyLock<HashMap<DataType, Vec<&'static FuncSign>>> =
131 LazyLock::new(|| {
132 let mut funcs = HashMap::<DataType, Vec<&'static FuncSign>>::new();
133 FUNCTION_REGISTRY
134 .iter_scalars()
135 .filter(|func| {
136 func.inputs_type.iter().all(|t| {
137 t.is_exact()
138 && t.as_exact() != &DataType::Timestamptz
139 && t.as_exact() != &DataType::Serial
140 }) && func.ret_type.is_exact()
141 && !FUNC_BAN_LIST.contains(&func.name.as_scalar())
142 && !func.deprecated })
144 .for_each(|func| {
145 funcs
146 .entry(func.ret_type.as_exact().clone())
147 .or_default()
148 .push(func)
149 });
150 funcs
151 });
152
153pub(crate) static INVARIANT_FUNC_SET: LazyLock<HashSet<ExprType>> = LazyLock::new(|| {
156 FUNCTION_REGISTRY
157 .iter_scalars()
158 .map(|sig| sig.name.as_scalar())
159 .counts()
160 .into_iter()
161 .filter(|(_key, count)| *count == 1)
162 .map(|(key, _)| key)
163 .collect()
164});
165
166pub(crate) static AGG_FUNC_TABLE: LazyLock<HashMap<DataType, Vec<&'static FuncSign>>> =
169 LazyLock::new(|| {
170 let mut funcs = HashMap::<DataType, Vec<&'static FuncSign>>::new();
171 FUNCTION_REGISTRY
172 .iter_aggregates()
173 .filter(|func| {
174 func.inputs_type
175 .iter()
176 .all(|t| t.is_exact() && t.as_exact() != &DataType::Timestamptz && t.as_exact() != &DataType::Serial)
177 && func.ret_type.is_exact()
178 && ![
180 PbAggKind::InternalLastSeenValue, PbAggKind::Sum0, PbAggKind::BitAnd,
183 PbAggKind::BitOr,
184 PbAggKind::BoolAnd,
185 PbAggKind::BoolOr,
186 PbAggKind::PercentileCont,
187 PbAggKind::PercentileDisc,
188 PbAggKind::Mode,
189 PbAggKind::ApproxPercentile, PbAggKind::JsonbObjectAgg, PbAggKind::StddevSamp, PbAggKind::VarSamp, ]
194 .contains(&func.name.as_aggregate())
195 && if func.name.as_aggregate() == PbAggKind::Sum {
201 !(func.inputs_type[0].as_exact() == &DataType::Int64 && func.ret_type.as_exact() == &DataType::Int64)
202 } else {
203 true
204 }
205 })
206 .for_each(|func| {
207 funcs.entry(func.ret_type.as_exact().clone()).or_default().push(func)
208 });
209 funcs
210 });
211
212pub(crate) static EXPLICIT_CAST_TABLE: LazyLock<HashMap<DataType, Vec<CastSig>>> =
217 LazyLock::new(|| {
218 let mut casts = HashMap::<DataType, Vec<CastSig>>::new();
219 cast_sigs()
220 .filter_map(|cast| cast.try_into().ok())
221 .filter(|cast: &CastSig| cast.context == CastContext::Explicit)
222 .filter(|cast| cast.from_type != DataType::Varchar || cast.to_type == DataType::Varchar)
223 .for_each(|cast| casts.entry(cast.to_type.clone()).or_default().push(cast));
224 casts
225 });
226
227pub(crate) static IMPLICIT_CAST_TABLE: LazyLock<HashMap<DataType, Vec<CastSig>>> =
232 LazyLock::new(|| {
233 let mut casts = HashMap::<DataType, Vec<CastSig>>::new();
234 cast_sigs()
235 .filter_map(|cast| cast.try_into().ok())
236 .filter(|cast: &CastSig| cast.context == CastContext::Implicit)
237 .filter(|cast| cast.from_type != DataType::Varchar || cast.to_type == DataType::Varchar)
238 .for_each(|cast| casts.entry(cast.to_type.clone()).or_default().push(cast));
239 casts
240 });
241
242fn expr_type_to_inequality_op(typ: ExprType) -> Option<BinaryOperator> {
243 match typ {
244 ExprType::GreaterThan => Some(BinaryOperator::Gt),
245 ExprType::GreaterThanOrEqual => Some(BinaryOperator::GtEq),
246 ExprType::LessThan => Some(BinaryOperator::Lt),
247 ExprType::LessThanOrEqual => Some(BinaryOperator::LtEq),
248 ExprType::NotEqual => Some(BinaryOperator::NotEq),
249 _ => None,
250 }
251}
252
253pub(crate) static BINARY_INEQUALITY_OP_TABLE: LazyLock<
262 HashMap<(DataType, DataType), Vec<BinaryOperator>>,
263> = LazyLock::new(|| {
264 let mut funcs = HashMap::<(DataType, DataType), Vec<BinaryOperator>>::new();
265 FUNCTION_REGISTRY
266 .iter_scalars()
267 .filter(|func| {
268 !FUNC_BAN_LIST.contains(&func.name.as_scalar())
269 && func.ret_type == DataType::Boolean.into()
270 && func.inputs_type.len() == 2
271 && func
272 .inputs_type
273 .iter()
274 .all(|t| t.is_exact() && t.as_exact() != &DataType::Timestamptz)
275 })
276 .filter_map(|func| {
277 let lhs = func.inputs_type[0].as_exact().clone();
278 let rhs = func.inputs_type[1].as_exact().clone();
279 let op = expr_type_to_inequality_op(func.name.as_scalar())?;
280 Some(((lhs, rhs), op))
281 })
282 .for_each(|(args, op)| funcs.entry(args).or_default().push(op));
283 funcs
284});