Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
holicc committed Dec 5, 2024
1 parent 1dfe1bd commit dbd612a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
6 changes: 6 additions & 0 deletions qurious/src/logical/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,9 @@ pub struct Like {
pub expr: Box<LogicalExpr>,
pub pattern: Box<LogicalExpr>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SubQuery {
pub subquery: Arc<LogicalPlan>,
pub outer_ref_columns: Vec<LogicalExpr>,
}
2 changes: 1 addition & 1 deletion qurious/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod count_wildcard_rule;
mod pushdown_filter_inner_join;
// mod scalar_subquery_to_join;
mod scalar_subquery_to_join;
mod type_coercion;

use crate::{error::Result, logical::plan::LogicalPlan};
Expand Down
34 changes: 26 additions & 8 deletions qurious/src/optimizer/scalar_subquery_to_join.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use arrow::ipc::Binary;

use super::OptimizerRule;
use crate::common::transformed::{TransformNode, Transformed, TransformedResult, TreeNodeRecursion};
use crate::error::Result;
use crate::logical::expr::LogicalExpr;
use crate::logical::expr::{BinaryExpr, LogicalExpr};
use crate::logical::plan::LogicalPlan;
use crate::logical::LogicalPlanBuilder;

Expand All @@ -27,14 +29,18 @@ impl OptimizerRule for ScalarSubqueryToJoin {
plan.transform(|plan| match plan {
LogicalPlan::Filter(filter) => {
if !contains_scalar_subquery(&filter.expr) {
return Ok(Transformed::no(plan));
return Ok(Transformed::no(LogicalPlan::Filter(filter)));
}

let (aliases, rewritten_subquery) = extract_scalar_subquery(&filter.expr)?;
let cur_input = filter.input;
let subqueries = extract_scalar_subquery(filter.expr);
// iterate through all subqueries in predicate, turning each into a left join
for subquery in subqueries {
let rewritten_child_subquery = self.optimize(subquery)?;

let alias_subquery = LogicalPlanBuilder::from(*filter.input)
.join_on(aliases)
.build()?;
let (join_conditions, rewritten_expr) =
self.extract_correlation_conditions(&rewritten_child_subquery)?;
}

todo!()
}
Expand All @@ -44,6 +50,10 @@ impl OptimizerRule for ScalarSubqueryToJoin {
}
}

fn extract_correlation_conditions(expr: &LogicalExpr) -> Result<(LogicalPlan, LogicalExpr)> {
todo!()
}

fn contains_scalar_subquery(expr: &LogicalExpr) -> bool {
let mut contains = false;
expr.apply(|expr| {
Expand All @@ -57,8 +67,16 @@ fn contains_scalar_subquery(expr: &LogicalExpr) -> bool {
contains
}

fn extract_scalar_subquery(expr: &LogicalExpr) -> Result<(Vec<(LogicalExpr, String)>, LogicalExpr)> {
todo!()
fn extract_scalar_subquery(expr: LogicalExpr) -> Vec<LogicalPlan> {
match expr {
LogicalExpr::SubQuery(subquery) => vec![*subquery],
LogicalExpr::BinaryExpr(BinaryExpr { left, right, .. }) => {
let mut plans = extract_scalar_subquery(*left);
plans.extend(extract_scalar_subquery(*right));
plans
}
_ => vec![],
}
}
#[cfg(test)]
mod tests {
Expand Down

0 comments on commit dbd612a

Please sign in to comment.