Skip to content

Commit

Permalink
feat(SqlSmith): gen Table Subquery EXISTS (risingwavelabs#4431)
Browse files Browse the repository at this point in the history
* Implement exists

* Fix formatting

* Add correlated query

* Restore unwanted changes

* Edit Syntax error

* Allow subquery without agg

* Workaround subquery and inside_agg but there is a few problem yet

* Test changed to no subquery first

* Done implementation

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
marvenlee2486 and mergify[bot] authored Nov 26, 2022
1 parent 4b55926 commit cbdab8c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion src/tests/sqlsmith/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
let range = if can_agg & !inside_agg { 99 } else { 90 };

match self.rng.gen_range(0..=range) {
0..=80 => self.gen_func(typ, can_agg, inside_agg),
0..=70 => self.gen_func(typ, can_agg, inside_agg),
71..=80 => self.gen_exists(typ, inside_agg),
81..=90 => self.gen_cast(typ, can_agg, inside_agg),
91..=99 => self.gen_agg(typ),
// TODO: There are more that are not in the functions table, e.g. CAST.
Expand Down Expand Up @@ -242,6 +243,22 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
.unwrap_or_else(|| self.gen_simple_scalar(ret))
}

fn gen_exists(&mut self, ret: DataTypeName, inside_agg: bool) -> Expr {
// TODO: Streaming nested loop join is not implemented yet.
// Tracked by: <https://github.com/singularity-data/risingwave/issues/2655>.

// Generation of subquery inside aggregation is now workaround.
// Tracked by: <https://github.com/risingwavelabs/risingwave/issues/3896>.
if self.is_mview || ret != DataTypeName::Boolean || inside_agg {
return self.gen_simple_scalar(ret);
};
// TODO: Feature is not yet implemented: correlated subquery in HAVING or SELECT with agg
// let (subquery, _) = self.gen_correlated_query();
// Tracked by: <https://github.com/risingwavelabs/risingwave/issues/2275>
let (subquery, _) = self.gen_local_query();
Expr::Exists(Box::new(subquery))
}

fn gen_agg(&mut self, ret: DataTypeName) -> Expr {
// TODO: workaround for <https://github.com/risingwavelabs/risingwave/issues/4508>
if ret == DataTypeName::Interval {
Expand Down
12 changes: 11 additions & 1 deletion src/tests/sqlsmith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,24 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
}

/// Generates a query with local context.
/// Used by `WITH`, subquery
/// Used by `WITH`, `Table Subquery` in Relation
fn gen_local_query(&mut self) -> (Query, Vec<Column>) {
let old_ctxt = self.new_local_context();
let t = self.gen_query();
self.restore_context(old_ctxt);
t
}

/// Generates a query with correlated context to ensure proper recursion.
/// Used by Exists `Subquery`
/// TODO: <https://github.com/risingwavelabs/risingwave/pull/4431#issuecomment-1327417328>
fn _gen_correlated_query(&mut self) -> (Query, Vec<Column>) {
let old_ctxt = self._clone_local_context();
let t = self.gen_query();
self.restore_context(old_ctxt);
t
}

fn gen_with(&mut self) -> (Option<With>, Vec<Table>) {
match self.flip_coin() {
true => (None, vec![]),
Expand Down
7 changes: 7 additions & 0 deletions src/tests/sqlsmith/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
self.bound_relations = old_rels;
self.bound_columns = old_cols;
}

// TODO: <https://github.com/risingwavelabs/risingwave/pull/4431#issuecomment-1327417328>
pub(crate) fn _clone_local_context(&mut self) -> Context {
let current_bound_relations = self.bound_relations.clone();
let current_bound_columns = self.bound_columns.clone();
(current_bound_columns, current_bound_relations)
}
}

/// Gen utils
Expand Down

0 comments on commit cbdab8c

Please sign in to comment.