Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sqlsmith): Generate more join expressions #8395

Merged
merged 17 commits into from
Apr 6, 2023
7 changes: 7 additions & 0 deletions src/tests/sqlsmith/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ This test will be run as a unit test:
./risedev test -E "package(risingwave_sqlsmith)" --features enable_sqlsmith_unit_test
```

## Generate snapshots

Take a look at [`gen_queries.sh`](scripts/gen_queries.sh).

Caveat: Even with a given snapshot, certain parts of the system are non-determninistic.
For instance with scheduler errors, the same query may not trigger errors when executed.

## E2E

In the second mode, it will test the entire query handling end-to-end. We provide a CLI tool that represents a Postgres client. You can run this tool via:
Expand Down
1 change: 0 additions & 1 deletion src/tests/sqlsmith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub fn sql_gen(rng: &mut impl Rng, tables: Vec<Table>) -> String {
}

/// Generate `INSERT`
#[allow(dead_code)]
pub fn insert_sql_gen(rng: &mut impl Rng, tables: Vec<Table>, count: usize) -> Vec<String> {
let mut gen = SqlGenerator::new(rng, vec![]);
tables
Expand Down
11 changes: 5 additions & 6 deletions src/tests/sqlsmith/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ pub async fn run(client: &Client, testdata: &str, count: usize, seed: Option<u64
tracing::info!("Set session variables");

let base_tables = create_base_tables(testdata, client).await.unwrap();
let (tables, mviews) = create_mviews(&mut rng, base_tables.clone(), client)
.await
.unwrap();
tracing::info!("Created tables");

let rows_per_table = 10;
populate_tables(client, &mut rng, base_tables.clone(), rows_per_table).await;
tracing::info!("Populated base tables");

let (tables, mviews) = create_mviews(&mut rng, base_tables.clone(), client)
.await
.unwrap();
tracing::info!("Created tables");

let max_rows_inserted = rows_per_table * base_tables.len();
test_sqlsmith(
client,
Expand Down Expand Up @@ -201,7 +202,6 @@ fn generate_rng(seed: Option<u64>) -> impl Rng {
}
}

#[allow(dead_code)]
async fn populate_tables<R: Rng>(
client: &Client,
rng: &mut R,
Expand Down Expand Up @@ -274,7 +274,6 @@ async fn test_session_variable<R: Rng>(client: &Client, rng: &mut R) -> String {
}

/// Expects at least 50% of inserted rows included.
#[allow(dead_code)]
async fn test_population_count(client: &Client, base_tables: Vec<Table>, expected_count: usize) {
let mut actual_count = 0;
for t in base_tables {
Expand Down
10 changes: 5 additions & 5 deletions src/tests/sqlsmith/src/sql_gen/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,9 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
let casts = EXPLICIT_CAST_TABLE.get(ret)?;
let cast_sig = casts.choose(&mut self.rng).unwrap();

use CastContext as T;
match cast_sig.context {
T::Explicit => {
let expr = self
.gen_expr(&cast_sig.from_type, context.set_inside_explicit_cast())
.into();
CastContext::Explicit => {
let expr = self.gen_expr(&cast_sig.from_type, context).into();
let data_type = data_type_to_ast_data_type(&cast_sig.to_type);
Some(Expr::Cast { expr, data_type })
}
Expand Down Expand Up @@ -693,6 +690,9 @@ pub(crate) fn sql_null() -> Expr {
Expr::Value(Value::Null)
}

// TODO(kwannoel):
// Add variadic function signatures. Can add these functions
// to a FUNC_TABLE too.
pub fn print_function_table() -> String {
let func_str = func_sigs()
.map(|sign| {
Expand Down
1 change: 0 additions & 1 deletion src/tests/sqlsmith/src/sql_gen/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::sql_gen::SqlGenerator;
use crate::Table;

impl<'a, R: Rng> SqlGenerator<'a, R> {
#[allow(dead_code)]
pub(crate) fn gen_insert_stmt(&mut self, table: Table, row_count: usize) -> Statement {
let table_name = ObjectName(vec![table.name.as_str().into()]);
let data_types = table
Expand Down
15 changes: 0 additions & 15 deletions src/tests/sqlsmith/src/sql_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,20 @@ pub(crate) struct SqlGeneratorContext {
// Used in top level, where we want to test queries
// without aggregates.
inside_agg: bool,
inside_explicit_cast: bool,
}

#[allow(dead_code)]
impl SqlGeneratorContext {
pub fn new() -> Self {
SqlGeneratorContext {
can_agg: true,
inside_agg: false,
inside_explicit_cast: false,
}
}

pub fn new_with_can_agg(can_agg: bool) -> Self {
Self {
can_agg,
inside_agg: false,
inside_explicit_cast: false,
}
}

Expand All @@ -106,17 +102,6 @@ impl SqlGeneratorContext {
}
}

pub fn set_inside_explicit_cast(self) -> Self {
Self {
inside_explicit_cast: true,
..self
}
}

pub fn can_implicit_cast(self) -> bool {
!self.inside_explicit_cast
}

pub fn can_gen_agg(self) -> bool {
self.can_agg && !self.inside_agg
}
Expand Down
Loading