Skip to content

Commit

Permalink
feat(sqlsmith): add round trip property test for parsing (#7160)
Browse files Browse the repository at this point in the history
- Does not seem to catch any errors yet, perhaps when:
- multi statement is supported: #6849
- #7132 is implemented
- we will start to see more errors caught.

Approved-By: lmatz
  • Loading branch information
kwannoel authored Jan 3, 2023
1 parent 1406718 commit 53a6e5d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/tests/sqlsmith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn mview_sql_gen<R: Rng>(rng: &mut R, tables: Vec<Table>, name: &str) -> (St
}

/// 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
31 changes: 27 additions & 4 deletions src/tests/sqlsmith/tests/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ async fn create_tables(
Ok((tables, setup_sql))
}

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

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

/// 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 +156,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 +203,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 53a6e5d

Please sign in to comment.