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): enable sub-query generation #4216

Merged
merged 8 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 18 additions & 4 deletions src/tests/sqlsmith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ struct SqlGenerator<'a, R: Rng> {
/// We might not read from all tables.
bound_relations: Vec<Table>,

/// Relations Outer corresponding to current subquery
marvenlee2486 marked this conversation as resolved.
Show resolved Hide resolved
parallel_relations: Vec<Table>,
marvenlee2486 marked this conversation as resolved.
Show resolved Hide resolved
marvenlee2486 marked this conversation as resolved.
Show resolved Hide resolved

/// Columns bound in generated query.
/// May not contain all columns from Self::bound_relations.
/// e.g. GROUP BY clause will constrain bound_columns.
Expand All @@ -110,6 +113,7 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
relation_id: 0,
is_distinct_allowed,
bound_relations: vec![],
parallel_relations: vec![],
bound_columns: vec![],
is_mview: false,
}
Expand All @@ -123,15 +127,22 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
relation_id: 0,
is_distinct_allowed: false,
bound_relations: vec![],
parallel_relations: vec![],
bound_columns: vec![],
is_mview: true,
}
}

fn add_relation_to_context(&mut self, table: Table) {
let mut bound_columns = table.get_qualified_columns();
self.bound_columns.append(&mut bound_columns);
self.bound_relations.push(table);
self.parallel_relations.push(table);
}

fn merge_parallel_to_relation(&mut self) {
for rel in &self.parallel_relations{
let mut bound_columns = rel.get_qualified_columns();
self.bound_columns.append(&mut bound_columns);
}
self.bound_relations.append(&mut self.parallel_relations);
}

fn gen_stmt(&mut self) -> Statement {
Expand Down Expand Up @@ -204,7 +215,7 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
}

/// Generates a query with local context.
/// Used by `WITH`, (and perhaps subquery should use this too)
/// Used by `WITH`, subquery
fn gen_local_query(&mut self) -> (Query, Vec<Column>) {
let old_ctxt = self.new_local_context();
let t = self.gen_query();
Expand Down Expand Up @@ -365,6 +376,9 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
from.push(self.gen_from_relation());
}
}

self.merge_parallel_to_relation();
marvenlee2486 marked this conversation as resolved.
Show resolved Hide resolved

from
}

Expand Down
18 changes: 11 additions & 7 deletions src/tests/sqlsmith/src/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ fn create_join_on_clause(left: String, right: String) -> Expr {
impl<'a, R: Rng> SqlGenerator<'a, R> {
/// A relation specified in the FROM clause.
pub(crate) fn gen_from_relation(&mut self) -> TableWithJoins {
match self.rng.gen_range(0..=9) {
0..=7 => self.gen_simple_table(),
8..=8 => self.gen_time_window_func(),
match self.rng.gen_range(0..=20) {
0..=17 => self.gen_simple_table(),
18..=18 => self.gen_time_window_func(),
// TODO: Enable after resolving: <https://github.com/singularity-data/risingwave/issues/2771>.
9..=9 => self.gen_equijoin_clause(),
19..=19 => self.gen_equijoin_clause(),
// TODO: Currently `gen_subquery` will cause panic due to some wrong assertions.
10..=10 => self.gen_subquery(),
20..=20 => self.gen_table_subquery(),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -120,8 +120,8 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
}
}

fn gen_subquery(&mut self) -> TableWithJoins {
let (subquery, columns) = self.gen_query();
fn gen_table_subquery(&mut self) -> TableWithJoins {
let (subquery, columns) = self.gen_local_query();
let alias = self.gen_table_name_with_prefix("sq");
let table = Table {
name: alias.clone(),
Expand All @@ -141,4 +141,8 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
self.add_relation_to_context(table);
relation
}

// fn gen_row_subquery(){

// }
marvenlee2486 marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 6 additions & 3 deletions src/tests/sqlsmith/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ use risingwave_sqlparser::ast::{

use crate::{Column, Expr, Ident, ObjectName, SqlGenerator, Table};

type Context = (Vec<Column>, Vec<Table>);
type Context = (Vec<Column>, Vec<Table>, Vec<Table>);

/// Context utils
impl<'a, R: Rng> SqlGenerator<'a, R> {
pub(crate) fn new_local_context(&mut self) -> Context {
let current_bound_relations = mem::take(&mut self.bound_relations);
let current_bound_columns = mem::take(&mut self.bound_columns);
(current_bound_columns, current_bound_relations)
let current_parallel_relations = mem::take(&mut self.parallel_relations);
self.parallel_relations.clear();
(current_bound_columns, current_bound_relations, current_parallel_relations)
}

pub(crate) fn restore_context(&mut self, (old_cols, old_rels): Context) {
pub(crate) fn restore_context(&mut self, (old_cols, old_rels, old_parallel): Context) {
self.bound_relations = old_rels;
self.bound_columns = old_cols;
self.parallel_relations = old_parallel;
}
}

Expand Down