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: support subquery in FROM clause #756

Merged
merged 26 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d762e35
subuqery must hve an alias
MingjiHan99 Dec 30, 2022
f88620a
enhance binding
MingjiHan99 Dec 30, 2022
54d6e36
binder
MingjiHan99 Dec 30, 2022
c41c0c3
binder
MingjiHan99 Dec 30, 2022
5dcc43c
clippy
MingjiHan99 Dec 30, 2022
3229ce9
add id table
MingjiHan99 Dec 31, 2022
2943be6
add id table
MingjiHan99 Dec 31, 2022
597135e
add id table
MingjiHan99 Dec 31, 2022
4076721
resolve column type in subquery
MingjiHan99 Dec 31, 2022
5e8818e
fail
MingjiHan99 Dec 31, 2022
0e787fc
ongoing lol
MingjiHan99 Dec 31, 2022
50f85e1
introduce `as` node. refactor binder context
wangrunji0408 Dec 31, 2022
4e7b6d2
fix bugs to make subquery work
wangrunji0408 Dec 31, 2022
6794dba
keep `as` in schema
wangrunji0408 Jan 1, 2023
3ea58c3
update subquery test
wangrunji0408 Jan 1, 2023
51979f9
rename back to `ColumnRefId`
wangrunji0408 Jan 1, 2023
bc48ed0
revert test
wangrunji0408 Jan 1, 2023
9e5058b
remove the `as` node, rename `nested` node to `ref`
wangrunji0408 Jan 1, 2023
4451179
remove Option for schema
wangrunji0408 Jan 1, 2023
fa4ef54
fix clippy and ignore failed test
wangrunji0408 Jan 1, 2023
5a9e017
remove type analysis field
wangrunji0408 Jan 1, 2023
378be90
update planner test
wangrunji0408 Jan 1, 2023
9529062
remove `ColumnPrune` and `ColumnMerge` node
wangrunji0408 Jan 1, 2023
d2b604e
fix projection pushdown for join subqueries
wangrunji0408 Jan 1, 2023
1a54e4b
Merge branch 'main' into mingji-subquery-binder
wangrunji0408 Jan 7, 2023
e1142f1
fix planner test on CI
wangrunji0408 Jan 7, 2023
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
Prev Previous commit
Next Next commit
fix bugs to make subquery work
Signed-off-by: Runji Wang <wangrunji0408@163.com>
  • Loading branch information
wangrunji0408 committed Dec 31, 2022
commit 4e7b6d284fb72d99f2562000dcc9a73528c8e15e
1 change: 1 addition & 0 deletions src/binder_v2/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl Binder {
fn bind_join_constraint(&mut self, constraint: JoinConstraint) -> Result {
match constraint {
JoinConstraint::On(expr) => self.bind_expr(expr),
JoinConstraint::None => Ok(self.egraph.add(Node::true_())),
_ => todo!("Support more join constraints"),
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/executor_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,13 @@ impl<S: Storage> Builder<S> {
/// Resolve the column index of `expr` in `plan`.
fn resolve_column_index(&self, expr: Id, plan: Id) -> RecExpr {
let schema = self.egraph[plan].data.schema.as_ref().expect("no schema");
self.node(expr).build_recexpr(|id| {
if let Some(idx) = schema.iter().position(|x| {
if let Expr::As([alias, _]) = self.node(*x) {
*alias == id
} else {
*x == id
}
}) {
self.node(expr).build_recexpr(|mut id| {
// skip AS
if let Expr::As([_, expr]) = self.node(id) {
id = *expr;
}
// resolve column index
if let Some(idx) = schema.iter().position(|x| *x == id) {
return Expr::ColumnIndex(ColumnIndex(idx as _));
}
match self.node(id) {
Expand Down
7 changes: 7 additions & 0 deletions src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ impl Expr {
v.clone()
}

pub fn as_as(&self) -> Option<Id> {
match self {
Self::As([alias, _]) => Some(*alias),
_ => None,
}
}

pub const fn binary_op(&self) -> Option<(BinaryOperator, Id, Id)> {
use BinaryOperator as Op;
#[allow(clippy::match_ref_pats)]
Expand Down
12 changes: 10 additions & 2 deletions src/planner/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ impl Analysis<Expr> for ExprAnalysis {
Data {
constant: expr::eval_constant(egraph, enode),
columns: plan::analyze_columns(egraph, enode),
schema: schema::analyze_schema(enode, |i| egraph[*i].data.schema.clone()),
schema: schema::analyze_schema(
enode,
|i| egraph[*i].data.schema.clone(),
|i| egraph[*i].nodes[0].as_as(),
),
rows: rows::analyze_rows(egraph, enode),
}
}
Expand Down Expand Up @@ -150,7 +154,11 @@ impl Analysis<Expr> for TypeSchemaAnalysis {
fn make(egraph: &egg::EGraph<Expr, Self>, enode: &Expr) -> Self::Data {
TypeSchema {
type_: type_::analyze_type(enode, |i| egraph[*i].data.type_.clone(), &egraph.analysis),
schema: schema::analyze_schema(enode, |i| egraph[*i].data.schema.clone()),
schema: schema::analyze_schema(
enode,
|i| egraph[*i].data.schema.clone(),
|i| egraph[*i].nodes[0].as_as(),
),
aggs: agg::analyze_aggs(enode, |i| egraph[*i].data.aggs.clone()),
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/planner/rules/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ fn merge_rules() -> Vec<Rewrite> { vec![
"(filter ?cond1 (filter ?cond2 ?child))" =>
"(filter (and ?cond1 ?cond2) ?child)"
),
rw!("proj-merge";
"(proj ?exprs1 (proj ?exprs2 ?child))" =>
"(proj ?exprs1 ?child)"
),
]}

#[rustfmt::skip]
Expand Down
14 changes: 12 additions & 2 deletions src/planner/rules/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use super::*;
pub type Schema = Option<Vec<Id>>;

/// Returns the output expressions for plan node.
pub fn analyze_schema(enode: &Expr, x: impl Fn(&Id) -> Schema) -> Schema {
pub fn analyze_schema(
enode: &Expr,
x: impl Fn(&Id) -> Schema,
as_as: impl Fn(&Id) -> Option<Id>,
) -> Schema {
use Expr::*;
let concat = |v1: Vec<Id>, v2: Vec<Id>| v1.into_iter().chain(v2.into_iter()).collect();
Some(match enode {
Expand All @@ -17,7 +21,13 @@ pub fn analyze_schema(enode: &Expr, x: impl Fn(&Id) -> Schema) -> Schema {
Join([_, _, l, r]) | HashJoin([_, _, _, l, r]) => concat(x(l)?, x(r)?),

// list is the source for the following nodes
List(ids) => ids.to_vec(),
List(ids) => ids
.iter()
.map(|id| {
// only keep alias for `(as alias _)`
as_as(id).unwrap_or(*id)
})
.collect(),

// plans that change schema
Scan([_, columns]) => x(columns)?,
Expand Down