@@ -73,24 +73,32 @@ impl ModelAnalyzeRule {
73
73
& self . analyzed_wren_mdl . wren_mdl ( ) ,
74
74
table_scan. table_name . clone ( ) ,
75
75
) {
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
+ }
94
102
} else {
95
103
Ok ( Transformed :: no ( LogicalPlan :: TableScan ( table_scan) ) )
96
104
}
@@ -144,13 +152,15 @@ impl ModelAnalyzeRule {
144
152
}
145
153
146
154
fn belong_to_mdl ( mdl : & WrenMDL , table_reference : TableReference ) -> bool {
147
- let catalog_mismatch = table_reference
155
+ let catalog_match = table_reference
148
156
. catalog ( )
149
- . map_or ( true , |c| c ! = mdl. catalog ( ) ) ;
157
+ . map_or ( false , |c| c = = mdl. catalog ( ) ) ;
150
158
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 ( ) ) ;
152
162
153
- catalog_mismatch || schema_mismatch
163
+ catalog_match && schema_match
154
164
}
155
165
156
166
fn analyze_table_scan (
@@ -159,8 +169,6 @@ fn analyze_table_scan(
159
169
required_field : Vec < Expr > ,
160
170
) -> Result < LogicalPlan > {
161
171
if belong_to_mdl ( & analyzed_wren_mdl. wren_mdl ( ) , table_scan. table_name . clone ( ) ) {
162
- Ok ( LogicalPlan :: TableScan ( table_scan) )
163
- } else {
164
172
let table_name = table_scan. table_name . table ( ) ;
165
173
if let Some ( model) = analyzed_wren_mdl. wren_mdl . get_model ( table_name) {
166
174
Ok ( LogicalPlan :: Extension ( Extension {
@@ -174,6 +182,8 @@ fn analyze_table_scan(
174
182
} else {
175
183
Ok ( LogicalPlan :: TableScan ( table_scan) )
176
184
}
185
+ } else {
186
+ Ok ( LogicalPlan :: TableScan ( table_scan) )
177
187
}
178
188
}
179
189
0 commit comments