risingwave_sqlsmith/sql_gen/
types.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! This module contains datatypes and functions which can be generated by sqlsmith.
16
17use 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::Vector(n) => AstDataType::Vector(*n as _),
57        DataType::Map(_) => todo!(),
58    }
59}
60
61fn data_type_name_to_ast_data_type(data_type_name: &DataTypeName) -> Option<DataType> {
62    use DataTypeName as T;
63    match data_type_name {
64        T::Boolean => Some(DataType::Boolean),
65        T::Int16 => Some(DataType::Int16),
66        T::Int32 => Some(DataType::Int32),
67        T::Int64 => Some(DataType::Int64),
68        T::Decimal => Some(DataType::Decimal),
69        T::Float32 => Some(DataType::Float32),
70        T::Float64 => Some(DataType::Float64),
71        T::Varchar => Some(DataType::Varchar),
72        T::Date => Some(DataType::Date),
73        T::Timestamp => Some(DataType::Timestamp),
74        T::Timestamptz => Some(DataType::Timestamptz),
75        T::Time => Some(DataType::Time),
76        T::Interval => Some(DataType::Interval),
77        _ => None,
78    }
79}
80
81/// Provide internal `CastSig` which can be used for `struct` and `list`.
82#[derive(Clone)]
83pub struct CastSig {
84    pub from_type: DataType,
85    pub to_type: DataType,
86    pub context: CastContext,
87}
88
89impl TryFrom<RwCastSig> for CastSig {
90    type Error = String;
91
92    fn try_from(value: RwCastSig) -> Result<Self, Self::Error> {
93        if let Some(from_type) = data_type_name_to_ast_data_type(&value.from_type)
94            && let Some(to_type) = data_type_name_to_ast_data_type(&value.to_type)
95        {
96            Ok(CastSig {
97                from_type,
98                to_type,
99                context: value.context,
100            })
101        } else {
102            Err(format!("unsupported cast sig: {:?}", value))
103        }
104    }
105}
106
107/// Function ban list.
108/// These functions should be generated eventually, by adding expression constraints.
109/// If we naively generate arguments for these functions, it will affect sqlsmith
110/// effectiveness, e.g. cause it to crash.
111static FUNC_BAN_LIST: LazyLock<HashSet<ExprType>> = LazyLock::new(|| {
112    [
113        // FIXME: https://github.com/risingwavelabs/risingwave/issues/8003
114        ExprType::Repeat,
115        // The format argument needs to be handled specially. It is still generated in `gen_special_func`.
116        ExprType::Decode,
117        // ENABLE: https://github.com/risingwavelabs/risingwave/issues/16293
118        ExprType::Sqrt,
119        // ENABLE: https://github.com/risingwavelabs/risingwave/issues/16293
120        ExprType::Pow,
121    ]
122    .into_iter()
123    .collect()
124});
125
126/// Table which maps functions' return types to possible function signatures.
127// ENABLE: https://github.com/risingwavelabs/risingwave/issues/5826
128// TODO: Create a `SPECIAL_FUNC` table.
129// Otherwise when we dump the function table, we won't include those functions in
130// gen_special_func.
131pub(crate) static FUNC_TABLE: LazyLock<HashMap<DataType, Vec<&'static FuncSign>>> =
132    LazyLock::new(|| {
133        let mut funcs = HashMap::<DataType, Vec<&'static FuncSign>>::new();
134        FUNCTION_REGISTRY
135            .iter_scalars()
136            .filter(|func| {
137                func.inputs_type.iter().all(|t| {
138                    t.is_exact()
139                        && t.as_exact() != &DataType::Timestamptz
140                        && t.as_exact() != &DataType::Serial
141                }) && func.ret_type.is_exact()
142                    && !FUNC_BAN_LIST.contains(&func.name.as_scalar())
143                    && !func.deprecated // deprecated functions are not accepted by frontend
144            })
145            .for_each(|func| {
146                funcs
147                    .entry(func.ret_type.as_exact().clone())
148                    .or_default()
149                    .push(func)
150            });
151        funcs
152    });
153
154/// Set of invariant functions
155// ENABLE: https://github.com/risingwavelabs/risingwave/issues/5826
156pub(crate) static INVARIANT_FUNC_SET: LazyLock<HashSet<ExprType>> = LazyLock::new(|| {
157    FUNCTION_REGISTRY
158        .iter_scalars()
159        .map(|sig| sig.name.as_scalar())
160        .counts()
161        .into_iter()
162        .filter(|(_key, count)| *count == 1)
163        .map(|(key, _)| key)
164        .collect()
165});
166
167/// Table which maps aggregate functions' return types to possible function signatures.
168// ENABLE: https://github.com/risingwavelabs/risingwave/issues/5826
169pub(crate) static AGG_FUNC_TABLE: LazyLock<HashMap<DataType, Vec<&'static FuncSign>>> =
170    LazyLock::new(|| {
171        let mut funcs = HashMap::<DataType, Vec<&'static FuncSign>>::new();
172        FUNCTION_REGISTRY
173            .iter_aggregates()
174            .filter(|func| {
175                func.inputs_type
176                    .iter()
177                    .all(|t| t.is_exact() && t.as_exact() != &DataType::Timestamptz && t.as_exact() != &DataType::Serial)
178                    && func.ret_type.is_exact()
179                    // Ignored functions
180                    && ![
181                        PbAggKind::InternalLastSeenValue, // Use internally
182                        PbAggKind::Sum0, // Used internally
183                        PbAggKind::ApproxCountDistinct,
184                        PbAggKind::BitAnd,
185                        PbAggKind::BitOr,
186                        PbAggKind::BoolAnd,
187                        PbAggKind::BoolOr,
188                        PbAggKind::PercentileCont,
189                        PbAggKind::PercentileDisc,
190                        PbAggKind::Mode,
191                        PbAggKind::ApproxPercentile, // ENABLE: https://github.com/risingwavelabs/risingwave/issues/16293
192                        PbAggKind::JsonbObjectAgg, // ENABLE: https://github.com/risingwavelabs/risingwave/issues/16293
193                        PbAggKind::StddevSamp, // ENABLE: https://github.com/risingwavelabs/risingwave/issues/16293
194                        PbAggKind::VarSamp, // ENABLE: https://github.com/risingwavelabs/risingwave/issues/16293
195                    ]
196                    .contains(&func.name.as_aggregate())
197                    // Exclude 2 phase agg global sum.
198                    // Sum(Int64) -> Int64.
199                    // Otherwise it conflicts with normal aggregation:
200                    // Sum(Int64) -> Decimal.
201                    // And sqlsmith will generate expressions with wrong types.
202                    && if func.name.as_aggregate() == PbAggKind::Sum {
203                       !(func.inputs_type[0].as_exact() == &DataType::Int64 && func.ret_type.as_exact() == &DataType::Int64)
204                    } else {
205                       true
206                    }
207            })
208            .for_each(|func| {
209                funcs.entry(func.ret_type.as_exact().clone()).or_default().push(func)
210            });
211        funcs
212    });
213
214/// Build a cast map from return types to viable cast-signatures.
215/// NOTE: We avoid cast from varchar to other datatypes apart from itself.
216/// This is because arbitrary strings may not be able to cast,
217/// creating large number of invalid queries.
218pub(crate) static EXPLICIT_CAST_TABLE: LazyLock<HashMap<DataType, Vec<CastSig>>> =
219    LazyLock::new(|| {
220        let mut casts = HashMap::<DataType, Vec<CastSig>>::new();
221        cast_sigs()
222            .filter_map(|cast| cast.try_into().ok())
223            .filter(|cast: &CastSig| cast.context == CastContext::Explicit)
224            .filter(|cast| cast.from_type != DataType::Varchar || cast.to_type == DataType::Varchar)
225            .for_each(|cast| casts.entry(cast.to_type.clone()).or_default().push(cast));
226        casts
227    });
228
229/// Build a cast map from return types to viable cast-signatures.
230/// NOTE: We avoid cast from varchar to other datatypes apart from itself.
231/// This is because arbitrary strings may not be able to cast,
232/// creating large number of invalid queries.
233pub(crate) static IMPLICIT_CAST_TABLE: LazyLock<HashMap<DataType, Vec<CastSig>>> =
234    LazyLock::new(|| {
235        let mut casts = HashMap::<DataType, Vec<CastSig>>::new();
236        cast_sigs()
237            .filter_map(|cast| cast.try_into().ok())
238            .filter(|cast: &CastSig| cast.context == CastContext::Implicit)
239            .filter(|cast| cast.from_type != DataType::Varchar || cast.to_type == DataType::Varchar)
240            .for_each(|cast| casts.entry(cast.to_type.clone()).or_default().push(cast));
241        casts
242    });
243
244fn expr_type_to_inequality_op(typ: ExprType) -> Option<BinaryOperator> {
245    match typ {
246        ExprType::GreaterThan => Some(BinaryOperator::Gt),
247        ExprType::GreaterThanOrEqual => Some(BinaryOperator::GtEq),
248        ExprType::LessThan => Some(BinaryOperator::Lt),
249        ExprType::LessThanOrEqual => Some(BinaryOperator::LtEq),
250        ExprType::NotEqual => Some(BinaryOperator::NotEq),
251        _ => None,
252    }
253}
254
255/// Build set of binary inequality functions like `>`, `<`, etc...
256/// Maps from LHS, RHS argument to Inequality Operation
257/// For instance:
258/// GreaterThanOrEqual(Int16, Int64) -> Boolean
259/// Will store an entry of:
260/// Key: Int16, Int64
261/// Value: `BinaryOp::GreaterThanOrEqual`
262/// in the table.
263pub(crate) static BINARY_INEQUALITY_OP_TABLE: LazyLock<
264    HashMap<(DataType, DataType), Vec<BinaryOperator>>,
265> = LazyLock::new(|| {
266    let mut funcs = HashMap::<(DataType, DataType), Vec<BinaryOperator>>::new();
267    FUNCTION_REGISTRY
268        .iter_scalars()
269        .filter(|func| {
270            !FUNC_BAN_LIST.contains(&func.name.as_scalar())
271                && func.ret_type == DataType::Boolean.into()
272                && func.inputs_type.len() == 2
273                && func
274                    .inputs_type
275                    .iter()
276                    .all(|t| t.is_exact() && t.as_exact() != &DataType::Timestamptz)
277        })
278        .filter_map(|func| {
279            let lhs = func.inputs_type[0].as_exact().clone();
280            let rhs = func.inputs_type[1].as_exact().clone();
281            let op = expr_type_to_inequality_op(func.name.as_scalar())?;
282            Some(((lhs, rhs), op))
283        })
284        .for_each(|(args, op)| funcs.entry(args).or_default().push(op));
285    funcs
286});