Skip to content

Commit

Permalink
implement round trip property test in sqlsmith
Browse files Browse the repository at this point in the history
  • Loading branch information
kwannoel committed Jan 3, 2023
1 parent 40048c9 commit e8d52ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/tests/sqlsmith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ pub fn mview_sql_gen<R: Rng>(rng: &mut R, tables: Vec<Table>, name: &str) -> (St
(mview.to_string(), table)
}

/// Parse first SQL statement
pub fn parse_first_sql_stmt(sql: &str) -> Statement {
parse_sql(sql)[0].clone()
}

/// Parse SQL
/// FIXME(Noel): Introduce error type for sqlsmith for this.
pub fn parse_sql(sql: &str) -> Vec<Statement> {
Parser::parse_sql(sql).unwrap_or_else(|_| panic!("Failed to parse SQL: {}", sql))
}
Expand Down
28 changes: 23 additions & 5 deletions src/tests/sqlsmith/tests/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use risingwave_frontend::{
};
use risingwave_sqlparser::ast::{ExplainOptions, Statement};
use risingwave_sqlsmith::{
create_table_statement_to_table, is_permissible_error, mview_sql_gen, parse_sql, sql_gen, Table,
create_table_statement_to_table, is_permissible_error, mview_sql_gen, parse_first_sql_stmt,
parse_sql, sql_gen, Table,
};
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -115,6 +116,25 @@ async fn create_tables(
Ok((tables, setup_sql))
}

fn unparse(sql: Statement) -> String {
format!("{}", sql)
}

/// Tests property `parse(unparse(parse(sql))) == parse(sql)`
fn round_trip_parse_test(sql: &str) -> Result<Statement> {
let start = parse_first_sql_stmt(sql);
let round_trip = parse_first_sql_stmt(&unparse(parse_first_sql_stmt(sql)));
if start != round_trip {
Err(format!(
"Roundtrip test failed\nStart: {}\nRoundtrip: {}",
start, round_trip
)
.into())
} else {
Ok(start)
}
}

async fn test_stream_query(
session: Arc<SessionImpl>,
tables: Vec<Table>,
Expand All @@ -131,8 +151,7 @@ async fn test_stream_query(
let (sql, table) = mview_sql_gen(&mut rng, tables.clone(), "stream_query");
reproduce_failing_queries(setup_sql, &sql);
// The generated SQL must be parsable.
let statements = parse_sql(&sql);
let stmt = statements[0].clone();
let stmt = round_trip_parse_test(&sql)?;
let skipped = handle(session.clone(), stmt, &sql).await?;
if !skipped {
let drop_sql = format!("DROP MATERIALIZED VIEW {}", table.name);
Expand Down Expand Up @@ -179,8 +198,7 @@ fn test_batch_query(
reproduce_failing_queries(setup_sql, &sql);

// The generated SQL must be parsable.
let statements = parse_sql(&sql);
let stmt = statements[0].clone();
let stmt = round_trip_parse_test(&sql)?;
let context: OptimizerContextRef = OptimizerContext::new(
session.clone(),
Arc::from(sql),
Expand Down

0 comments on commit e8d52ec

Please sign in to comment.