Skip to content

Commit a7de461

Browse files
authored
Introduce the View of MDL (#645)
* use session context handle table provider * use positive match * introduce the view in mdl * modify the wren-modeling-py * remove unused tests * cargo clippy * cargo fmt * revert python change and provide sync fn for sql transform * cargo fmt
1 parent a137bb5 commit a7de461

File tree

13 files changed

+354
-374
lines changed

13 files changed

+354
-374
lines changed

wren-modeling-py/Cargo.lock

+43-43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wren-modeling-rs/core/src/logical_plan/analyze/rule.rs

+34-24
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,32 @@ impl ModelAnalyzeRule {
7373
&self.analyzed_wren_mdl.wren_mdl(),
7474
table_scan.table_name.clone(),
7575
) {
76-
return Ok(Transformed::no(LogicalPlan::TableScan(table_scan)));
77-
}
78-
let table_name = table_scan.table_name.table();
79-
if let Some(model) =
80-
self.analyzed_wren_mdl.wren_mdl().get_model(table_name)
81-
{
82-
let field: Vec<Expr> =
83-
used_columns.borrow().iter().cloned().collect();
84-
let model = LogicalPlan::Extension(Extension {
85-
node: Arc::new(ModelPlanNode::new(
86-
model,
87-
field,
88-
Some(LogicalPlan::TableScan(table_scan.clone())),
89-
Arc::clone(&self.analyzed_wren_mdl),
90-
)?),
91-
});
92-
used_columns.borrow_mut().clear();
93-
Ok(Transformed::yes(model))
76+
let table_name = table_scan.table_name.table();
77+
// transform ViewTable to a subquery plan
78+
if let Some(logical_plan) = table_scan.source.get_logical_plan() {
79+
let subquery = LogicalPlanBuilder::from(logical_plan.clone())
80+
.alias(table_name)?
81+
.build()?;
82+
return Ok(Transformed::yes(subquery));
83+
}
84+
if let Some(model) =
85+
self.analyzed_wren_mdl.wren_mdl().get_model(table_name)
86+
{
87+
let field: Vec<Expr> =
88+
used_columns.borrow().iter().cloned().collect();
89+
let model = LogicalPlan::Extension(Extension {
90+
node: Arc::new(ModelPlanNode::new(
91+
model,
92+
field,
93+
Some(LogicalPlan::TableScan(table_scan.clone())),
94+
Arc::clone(&self.analyzed_wren_mdl),
95+
)?),
96+
});
97+
used_columns.borrow_mut().clear();
98+
Ok(Transformed::yes(model))
99+
} else {
100+
Ok(Transformed::no(LogicalPlan::TableScan(table_scan)))
101+
}
94102
} else {
95103
Ok(Transformed::no(LogicalPlan::TableScan(table_scan)))
96104
}
@@ -144,13 +152,15 @@ impl ModelAnalyzeRule {
144152
}
145153

146154
fn belong_to_mdl(mdl: &WrenMDL, table_reference: TableReference) -> bool {
147-
let catalog_mismatch = table_reference
155+
let catalog_match = table_reference
148156
.catalog()
149-
.map_or(true, |c| c != mdl.catalog());
157+
.map_or(false, |c| c == mdl.catalog());
150158

151-
let schema_mismatch = table_reference.schema().map_or(true, |s| s != mdl.schema());
159+
let schema_match = table_reference
160+
.schema()
161+
.map_or(false, |s| s == mdl.schema());
152162

153-
catalog_mismatch || schema_mismatch
163+
catalog_match && schema_match
154164
}
155165

156166
fn analyze_table_scan(
@@ -159,8 +169,6 @@ fn analyze_table_scan(
159169
required_field: Vec<Expr>,
160170
) -> Result<LogicalPlan> {
161171
if belong_to_mdl(&analyzed_wren_mdl.wren_mdl(), table_scan.table_name.clone()) {
162-
Ok(LogicalPlan::TableScan(table_scan))
163-
} else {
164172
let table_name = table_scan.table_name.table();
165173
if let Some(model) = analyzed_wren_mdl.wren_mdl.get_model(table_name) {
166174
Ok(LogicalPlan::Extension(Extension {
@@ -174,6 +182,8 @@ fn analyze_table_scan(
174182
} else {
175183
Ok(LogicalPlan::TableScan(table_scan))
176184
}
185+
} else {
186+
Ok(LogicalPlan::TableScan(table_scan))
177187
}
178188
}
179189

0 commit comments

Comments
 (0)