@@ -180,13 +180,27 @@ fn resolve_against(schema: &DFSchema, col: &Column) -> Option<Column> {
180180///
181181/// `inlined` are the expressions that the merge resolves, and
182182/// `direct_columns` the columns that the nodes above reference directly.
183+ ///
184+ /// The inliner and the merge must agree on which output column a reference
185+ /// names, so the references are checked in the canonical spelling of
186+ /// `schema`, the output schema of the projection (see [`column_index`]).
183187fn pinned_for_merge < ' a > (
184188 inliner : & ProjectionInliner < ' _ > ,
189+ schema : & DFSchema ,
185190 inlined : impl IntoIterator < Item = & ' a Expr > ,
186191 direct_columns : & IndexSet < Column > ,
187- ) -> PinnedDefinitions {
188- let direct: Vec < Expr > = direct_columns. iter ( ) . cloned ( ) . map ( Expr :: Column ) . collect ( ) ;
189- inliner. pinned_with_unknown_consumers ( inlined, & direct, DuplicationCost :: Free )
192+ ) -> Result < PinnedDefinitions > {
193+ let inlined = inlined
194+ . into_iter ( )
195+ . map ( |expr| respell_cols ( expr. clone ( ) , schema, schema) )
196+ . collect :: < Result < Vec < _ > > > ( ) ?;
197+ let direct: Vec < Expr > = direct_columns
198+ . iter ( )
199+ . map ( |col| {
200+ Expr :: Column ( resolve_against ( schema, col) . unwrap_or_else ( || col. clone ( ) ) )
201+ } )
202+ . collect ( ) ;
203+ Ok ( inliner. pinned_with_unknown_consumers ( & inlined, & direct, DuplicationCost :: Free ) )
190204}
191205
192206/// Extracts `MoveTowardsLeafNodes` sub-expressions from non-projection nodes
@@ -359,7 +373,7 @@ fn extract_from_plan(
359373 LeafExpressionExtractor :: new ( schema. as_ref ( ) , alias_generator)
360374 . with_input_projection ( projection. as_ref ( ) , & node_exprs, & IndexSet :: new ( ) )
361375 } )
362- . collect ( ) ;
376+ . collect :: < Result < _ > > ( ) ? ;
363377
364378 // Build per-input column sets for routing expressions to the correct input
365379 let input_column_sets: Vec < HashSet < ColumnReference > > = input_schemas
@@ -691,13 +705,18 @@ impl<'a> LeafExpressionExtractor<'a> {
691705 projection : Option < & ' a Projection > ,
692706 consumers : impl IntoIterator < Item = & ' e Expr > ,
693707 direct_columns : & IndexSet < Column > ,
694- ) -> Self {
708+ ) -> Result < Self > {
695709 if let Some ( projection) = projection {
696710 let inliner = ProjectionInliner :: new ( projection) ;
697- self . pinned = pinned_for_merge ( & inliner, consumers, direct_columns) ;
711+ self . pinned = pinned_for_merge (
712+ & inliner,
713+ & projection. schema ,
714+ consumers,
715+ direct_columns,
716+ ) ?;
698717 self . input_inliner = Some ( inliner) ;
699718 }
700- self
719+ Ok ( self )
701720 }
702721
703722 /// Returns `true` if `expr` can be extracted into the input. See
@@ -788,22 +807,11 @@ fn build_extraction_projection_impl(
788807 target_schema : & DFSchema ,
789808) -> Result < Option < Projection > > {
790809 if let LogicalPlan :: Projection ( existing) = target. as_ref ( ) {
791- // Resolves column references through the existing projection
792- // The inliner and the merge below must agree on which output column a
793- // reference names, so check the references in the canonical spelling
794- // of the output schema (see [`column_index`]).
795810 let inliner = ProjectionInliner :: new ( existing) ;
796- let extracted = extracted_exprs
797- . iter ( )
798- . map ( |( e, _) | respell_cols ( e. clone ( ) , & existing. schema , & existing. schema ) )
799- . collect :: < Result < Vec < _ > > > ( ) ?;
800- let direct_columns: IndexSet < Column > = direct_columns
801- . iter ( )
802- . map ( |col| {
803- resolve_against ( & existing. schema , col) . unwrap_or_else ( || col. clone ( ) )
804- } )
805- . collect ( ) ;
806- if !pinned_for_merge ( & inliner, & extracted, & direct_columns) . is_empty ( ) {
811+ let extracted = extracted_exprs. iter ( ) . map ( |( e, _) | e) ;
812+ if !pinned_for_merge ( & inliner, & existing. schema , extracted, direct_columns) ?
813+ . is_empty ( )
814+ {
807815 return Ok ( None ) ;
808816 }
809817
@@ -1062,7 +1070,7 @@ fn split_and_push_projection(
10621070 input_projection,
10631071 consumers,
10641072 known_direct_columns. unwrap_or ( & no_direct_columns) ,
1065- ) ;
1073+ ) ? ;
10661074 let mut extractors = vec ! [ extractor] ;
10671075 let input_column_sets = vec ! [ schema_columns( input_schema. as_ref( ) ) ] ;
10681076
@@ -1155,9 +1163,10 @@ fn split_and_push_projection(
11551163 if let Some ( inliner) = & extractor. input_inliner
11561164 && !pinned_for_merge (
11571165 inliner,
1166+ input_schema. as_ref ( ) ,
11581167 extraction_pairs. iter ( ) . map ( |( expr, _) | expr) ,
11591168 & direct_columns,
1160- )
1169+ ) ?
11611170 . is_empty ( )
11621171 {
11631172 return Ok ( None ) ;
@@ -1407,11 +1416,16 @@ fn route_to_inputs(
14071416 None => return Ok ( None ) , // Ambiguous column — bail out
14081417 }
14091418 }
1410- // A direct reference counts for every input that can own it.
1419+ // A direct reference counts for every input that can own it: by
1420+ // name, or resolved like the merge resolves it (see [`column_index`]).
14111421 for col in & target. direct_columns {
14121422 let col_ref = ColumnReference :: new ( col. relation . as_ref ( ) , col. name ( ) ) ;
1413- for ( idx, cols) in input_column_sets. iter ( ) . enumerate ( ) {
1414- if cols. contains ( & col_ref) {
1423+ for ( idx, ( cols, schema) ) in
1424+ input_column_sets. iter ( ) . zip ( input_schemas) . enumerate ( )
1425+ {
1426+ if let Some ( resolved) = resolve_against ( schema, col) {
1427+ per_input[ idx] . direct_columns . insert ( resolved) ;
1428+ } else if cols. contains ( & col_ref) {
14151429 per_input[ idx] . direct_columns . insert ( col. clone ( ) ) ;
14161430 }
14171431 }
@@ -4119,8 +4133,14 @@ mod tests {
41194133 ) ?;
41204134 let inliner = ProjectionInliner :: new ( & child) ;
41214135 let blocks = |pairs : & [ ( Expr , String ) ] , columns : & IndexSet < Column > | {
4122- !pinned_for_merge ( & inliner, pairs. iter ( ) . map ( |( expr, _) | expr) , columns)
4123- . is_empty ( )
4136+ !pinned_for_merge (
4137+ & inliner,
4138+ & child. schema ,
4139+ pairs. iter ( ) . map ( |( expr, _) | expr) ,
4140+ columns,
4141+ )
4142+ . unwrap ( )
4143+ . is_empty ( )
41244144 } ;
41254145 let no_columns = IndexSet :: new ( ) ;
41264146
0 commit comments