@@ -110,34 +110,35 @@ impl<'a, T: Transaction> Binder<'a, T> {
110
110
} ) ,
111
111
Expr :: Subquery ( subquery) => {
112
112
let ( sub_query, column) = self . bind_subquery ( subquery) ?;
113
- self . context . sub_query ( SubQueryType :: SubQuery ( sub_query) ) ;
114
-
115
- if self . context . is_step ( & QueryBindStep :: Where ) {
116
- Ok ( self . bind_temp_column ( column) )
113
+ let ( expr, sub_query) = if !self . context . is_step ( & QueryBindStep :: Where ) {
114
+ self . bind_temp_table ( column, sub_query) ?
117
115
} else {
118
- Ok ( ScalarExpression :: ColumnRef ( column) )
119
- }
116
+ ( ScalarExpression :: ColumnRef ( column) , sub_query)
117
+ } ;
118
+ self . context . sub_query ( SubQueryType :: SubQuery ( sub_query) ) ;
119
+ Ok ( expr)
120
120
}
121
121
Expr :: InSubquery {
122
122
expr,
123
123
subquery,
124
124
negated,
125
125
} => {
126
+ let left_expr = Box :: new ( self . bind_expr ( expr) ?) ;
126
127
let ( sub_query, column) = self . bind_subquery ( subquery) ?;
127
- self . context
128
- . sub_query ( SubQueryType :: InSubQuery ( * negated, sub_query) ) ;
129
128
130
129
if !self . context . is_step ( & QueryBindStep :: Where ) {
131
130
return Err ( DatabaseError :: UnsupportedStmt (
132
131
"'IN (SUBQUERY)' can only appear in `WHERE`" . to_string ( ) ,
133
132
) ) ;
134
133
}
135
134
136
- let alias_expr = self . bind_temp_column ( column) ;
135
+ let ( alias_expr, sub_query) = self . bind_temp_table ( column, sub_query) ?;
136
+ self . context
137
+ . sub_query ( SubQueryType :: InSubQuery ( * negated, sub_query) ) ;
137
138
138
139
Ok ( ScalarExpression :: Binary {
139
140
op : expression:: BinaryOperator :: Eq ,
140
- left_expr : Box :: new ( self . bind_expr ( expr ) ? ) ,
141
+ left_expr,
141
142
right_expr : Box :: new ( alias_expr) ,
142
143
ty : LogicalType :: Boolean ,
143
144
} )
@@ -203,16 +204,22 @@ impl<'a, T: Transaction> Binder<'a, T> {
203
204
}
204
205
}
205
206
206
- fn bind_temp_column ( & mut self , column : ColumnRef ) -> ScalarExpression {
207
+ fn bind_temp_table (
208
+ & mut self ,
209
+ column : ColumnRef ,
210
+ sub_query : LogicalPlan ,
211
+ ) -> Result < ( ScalarExpression , LogicalPlan ) , DatabaseError > {
207
212
let mut alias_column = ColumnCatalog :: clone ( & column) ;
208
213
alias_column. set_table_name ( self . context . temp_table ( ) ) ;
209
214
210
- ScalarExpression :: Alias {
215
+ let alias_expr = ScalarExpression :: Alias {
211
216
expr : Box :: new ( ScalarExpression :: ColumnRef ( column) ) ,
212
217
alias : AliasType :: Expr ( Box :: new ( ScalarExpression :: ColumnRef ( Arc :: new (
213
218
alias_column,
214
219
) ) ) ) ,
215
- }
220
+ } ;
221
+ let alias_plan = self . bind_project ( sub_query, vec ! [ alias_expr. clone( ) ] ) ?;
222
+ Ok ( ( alias_expr, alias_plan) )
216
223
}
217
224
218
225
fn bind_subquery (
@@ -289,7 +296,7 @@ impl<'a, T: Transaction> Binder<'a, T> {
289
296
} else {
290
297
// handle col syntax
291
298
let mut got_column = None ;
292
- for table_catalog in self . context . bind_table . values ( ) {
299
+ for table_catalog in self . context . bind_table . values ( ) . rev ( ) {
293
300
if let Some ( column_catalog) = table_catalog. get_column_by_name ( & column_name) {
294
301
got_column = Some ( column_catalog) ;
295
302
}
0 commit comments