Skip to content

Commit

Permalink
fix(core): implement the scope analysis to support the correlated sub…
Browse files Browse the repository at this point in the history
…query syntax (#787)

* introduce scope analysis

* refactor the analysis rule

* expand view before analyze model

* implement the scope analysis and refactor model analysis

* enhance the doc

* cache the catalog_schema prefix

* add comment and missing rule

* rollback unused change

* cargo fmt
  • Loading branch information
goldmedal committed Sep 12, 2024
1 parent 96048f5 commit 2ff1086
Show file tree
Hide file tree
Showing 9 changed files with 758 additions and 204 deletions.
62 changes: 62 additions & 0 deletions wren-modeling-rs/core/src/logical_plan/analyze/expand_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::logical_plan::utils::belong_to_mdl;
use crate::mdl::utils::quoted;
use crate::mdl::{AnalyzedWrenMDL, SessionStateRef};
use datafusion::common::tree_node::Transformed;
use datafusion::common::Result;
use datafusion::config::ConfigOptions;
use datafusion::logical_expr::{LogicalPlan, LogicalPlanBuilder};
use datafusion::optimizer::AnalyzerRule;
use std::sync::Arc;

pub struct ExpandWrenViewRule {
analyzed_wren_mdl: Arc<AnalyzedWrenMDL>,
session_state: SessionStateRef,
}

impl ExpandWrenViewRule {
pub fn new(
analyzed_wren_mdl: Arc<AnalyzedWrenMDL>,
session_state: SessionStateRef,
) -> Self {
Self {
analyzed_wren_mdl,
session_state,
}
}
}

impl AnalyzerRule for ExpandWrenViewRule {
fn analyze(&self, plan: LogicalPlan, _: &ConfigOptions) -> Result<LogicalPlan> {
let plan = plan
.transform_up_with_subqueries(|plan| match &plan {
LogicalPlan::TableScan(table_scan) => {
if belong_to_mdl(
&self.analyzed_wren_mdl.wren_mdl(),
table_scan.table_name.clone(),
Arc::clone(&self.session_state),
) && self
.analyzed_wren_mdl
.wren_mdl()
.get_view(table_scan.table_name.table())
.is_some()
{
if let Some(logical_plan) = table_scan.source.get_logical_plan() {
let subquery = LogicalPlanBuilder::from(logical_plan.clone())
.alias(quoted(table_scan.table_name.table()))?
.build()?;
return Ok(Transformed::yes(subquery));
}
}
Ok(Transformed::no(plan))
}
_ => Ok(Transformed::no(plan)),
})?
.map_data(|plan| plan.recompute_schema())?
.data;
Ok(plan)
}

fn name(&self) -> &str {
"ExpandWrenViewRule"
}
}
1 change: 1 addition & 0 deletions wren-modeling-rs/core/src/logical_plan/analyze/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod expand_view;
pub mod model_anlayze;
pub mod model_generation;
pub mod plan;
Expand Down
Loading

0 comments on commit 2ff1086

Please sign in to comment.