From 496f7a97477bdd6f8882e2fd66c40cb4cec0ee6c Mon Sep 17 00:00:00 2001 From: Noel Kwan <47273164+kwannoel@users.noreply.github.com> Date: Fri, 3 Feb 2023 15:23:56 +0800 Subject: [PATCH] fix(sqlsmith): generate typed null (#7679) Approved-By: fuyufjh --- src/tests/sqlsmith/src/sql_gen/expr.rs | 7 +++++++ src/tests/sqlsmith/src/sql_gen/scalar.rs | 10 +++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/tests/sqlsmith/src/sql_gen/expr.rs b/src/tests/sqlsmith/src/sql_gen/expr.rs index a9a85cf68fbf7..b7482c2f3b579 100644 --- a/src/tests/sqlsmith/src/sql_gen/expr.rs +++ b/src/tests/sqlsmith/src/sql_gen/expr.rs @@ -638,6 +638,13 @@ fn make_bin_op(func: ExprType, exprs: &[Expr]) -> Option { }) } +pub(crate) fn typed_null(ty: &DataType) -> Expr { + Expr::Cast { + expr: Box::new(sql_null()), + data_type: data_type_to_ast_data_type(ty), + } +} + /// Generates a `NULL` value. pub(crate) fn sql_null() -> Expr { Expr::Value(Value::Null) diff --git a/src/tests/sqlsmith/src/sql_gen/scalar.rs b/src/tests/sqlsmith/src/sql_gen/scalar.rs index 1a0a26b5d0af8..ec01bf96bef2a 100644 --- a/src/tests/sqlsmith/src/sql_gen/scalar.rs +++ b/src/tests/sqlsmith/src/sql_gen/scalar.rs @@ -21,8 +21,7 @@ use rand::Rng; use risingwave_common::types::DataType; use risingwave_sqlparser::ast::{DataType as AstDataType, Expr, Value}; -use crate::sql_gen::expr::sql_null; -use crate::sql_gen::types::data_type_to_ast_data_type; +use crate::sql_gen::expr::typed_null; use crate::sql_gen::SqlGenerator; impl<'a, R: Rng> SqlGenerator<'a, R> { @@ -35,10 +34,7 @@ impl<'a, R: Rng> SqlGenerator<'a, R> { // NOTE(kwannoel): We generate Cast with NULL to avoid generating lots of ambiguous // expressions. For instance agg calls such as `max(NULL)` may be generated, // and coerced to VARCHAR, where we require a `NULL::int` instead. - return Expr::Cast { - expr: Box::new(sql_null()), - data_type: data_type_to_ast_data_type(typ), - }; + return typed_null(typ); } // Scalars which may generate negative numbers are wrapped in // `Nested` to ambiguity while parsing. @@ -103,7 +99,7 @@ impl<'a, R: Rng> SqlGenerator<'a, R> { // .map(|typ| self.gen_simple_scalar(typ)) // .collect(), // ), - _ => sql_null(), + _ => typed_null(typ), } }