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): add round trip property test for parsing #7160

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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