@@ -11,7 +11,7 @@ use std::collections::HashMap;
1111use std:: slice;
1212use std:: sync:: Arc ;
1313
14- use super :: { lower_ident, Binder , BinderContext , QueryBindStep , SubQueryType } ;
14+ use super :: { lower_ident, Binder , BinderContext , QueryBindStep , Source , SubQueryType } ;
1515use crate :: expression:: function:: scala:: { ArcScalarFunctionImpl , ScalarFunction } ;
1616use crate :: expression:: function:: table:: { ArcTableFunctionImpl , TableFunction } ;
1717use crate :: expression:: function:: FunctionSummary ;
@@ -259,9 +259,10 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
259259
260260 let alias_expr = ScalarExpression :: Alias {
261261 expr : Box :: new ( expr) ,
262- alias : AliasType :: Expr ( Box :: new ( ScalarExpression :: ColumnRef ( ColumnRef :: from (
263- alias_column,
264- ) ) ) ) ,
262+ alias : AliasType :: Expr ( Box :: new ( ScalarExpression :: ColumnRef (
263+ ColumnRef :: from ( alias_column) ,
264+ false ,
265+ ) ) ) ,
265266 } ;
266267 let alias_plan = self . bind_project ( sub_query, vec ! [ alias_expr. clone( ) ] ) ?;
267268 Ok ( ( alias_expr, alias_plan) )
@@ -279,7 +280,6 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
279280 scala_functions,
280281 table_functions,
281282 temp_table_id,
282- parent_name,
283283 ..
284284 } = & self . context ;
285285 let mut binder = Binder :: new (
@@ -290,17 +290,11 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
290290 scala_functions,
291291 table_functions,
292292 temp_table_id. clone ( ) ,
293- parent_name. clone ( ) ,
294293 ) ,
295294 self . args ,
296295 Some ( self ) ,
297296 ) ;
298297
299- self . context . bind_table . iter ( ) . find ( |( ( t, _, _) , _) | {
300- binder. context . parent_name . push ( t. as_str ( ) . to_string ( ) ) ;
301- true
302- } ) ;
303-
304298 let mut sub_query = binder. bind_query ( subquery) ?;
305299 let sub_query_schema = sub_query. output_schema ( ) ;
306300
@@ -319,13 +313,13 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
319313
320314 let columns = sub_query_schema
321315 . iter ( )
322- . map ( |column| ScalarExpression :: ColumnRef ( column. clone ( ) ) )
316+ . map ( |column| ScalarExpression :: ColumnRef ( column. clone ( ) , false ) )
323317 . collect :: < Vec < _ > > ( ) ;
324318 ScalarExpression :: Tuple ( columns)
325319 } else {
326320 fn_check ( 1 ) ?;
327321
328- ScalarExpression :: ColumnRef ( sub_query_schema[ 0 ] . clone ( ) )
322+ ScalarExpression :: ColumnRef ( sub_query_schema[ 0 ] . clone ( ) , false )
329323 } ;
330324 Ok ( ( sub_query, expr) )
331325 }
@@ -376,13 +370,39 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
376370 try_default ! ( & full_name. 0 , full_name. 1 ) ;
377371 }
378372 if let Some ( table) = full_name. 0 . or ( bind_table_name) {
379- let source = self . context . bind_source ( & table) ?;
373+ let mut source: & Source ;
374+ let mut from_parent: bool ;
375+ let ( mut parent, mut parent_context) = if let Some ( parent) = self . parent {
376+ ( Some ( parent) , Some ( & parent. context ) )
377+ } else {
378+ ( None , None )
379+ } ;
380+
381+ loop {
382+ ( source, from_parent) = match self . context . bind_source ( parent_context, & table) {
383+ ( Ok ( source) , from_parent) => ( source, from_parent) ,
384+ ( Err ( e) , _) => {
385+ if let Some ( p) = parent {
386+ ( parent, parent_context) = match p. parent {
387+ Some ( parent) => ( Some ( parent) , Some ( & parent. context ) ) ,
388+ None => return Err ( e) ,
389+ }
390+ } else {
391+ return Err ( e) ;
392+ }
393+ continue ;
394+ }
395+ } ;
396+ break ;
397+ }
398+
380399 let schema_buf = self . table_schema_buf . entry ( Arc :: new ( table) ) . or_default ( ) ;
381400
382401 Ok ( ScalarExpression :: ColumnRef (
383402 source
384403 . column ( & full_name. 1 , schema_buf)
385404 . ok_or_else ( || DatabaseError :: ColumnNotFound ( full_name. 1 . to_string ( ) ) ) ?,
405+ from_parent,
386406 ) )
387407 } else {
388408 let op =
@@ -411,7 +431,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
411431 table_schema_buf. entry ( table_name. clone ( ) ) . or_default ( ) ;
412432 source. column ( & full_name. 1 , schema_buf)
413433 } {
414- * got_column = Some ( ScalarExpression :: ColumnRef ( column) ) ;
434+ * got_column = Some ( ScalarExpression :: ColumnRef ( column, false ) ) ;
415435 }
416436 }
417437 } ;
0 commit comments