risingwave_sqlsmith/sql_gen/
cast.rs1use rand::Rng;
16use rand::seq::IndexedRandom;
17use risingwave_common::types::DataType;
18use risingwave_frontend::expr::CastContext;
19use risingwave_sqlparser::ast::Expr;
20
21use crate::sql_gen::types::{EXPLICIT_CAST_TABLE, data_type_to_ast_data_type};
22use crate::sql_gen::{SqlGenerator, SqlGeneratorContext};
23
24impl<R: Rng> SqlGenerator<'_, R> {
25 pub(crate) fn gen_explicit_cast(
26 &mut self,
27 ret: &DataType,
28 context: SqlGeneratorContext,
29 ) -> Expr {
30 self.gen_explicit_cast_inner(ret, context)
31 .unwrap_or_else(|| self.gen_simple_scalar(ret))
32 }
33
34 fn gen_explicit_cast_inner(
37 &mut self,
38 ret: &DataType,
39 context: SqlGeneratorContext,
40 ) -> Option<Expr> {
41 let casts = EXPLICIT_CAST_TABLE.get(ret)?;
42 let cast_sig = casts.choose(&mut self.rng).unwrap();
43
44 match cast_sig.context {
45 CastContext::Explicit => {
46 let expr = self.gen_expr(&cast_sig.from_type, context).into();
47 let data_type = data_type_to_ast_data_type(&cast_sig.to_type);
48 Some(Expr::Cast { expr, data_type })
49 }
50
51 _ => unreachable!(),
54 }
55 }
56
57 pub(crate) fn gen_implicit_cast(
60 &mut self,
61 ret: &DataType,
62 context: SqlGeneratorContext,
63 ) -> Expr {
64 self.gen_expr(ret, context)
65 }
66}