@@ -428,6 +428,7 @@ fn check_async_udf(body: &Arc<dyn PhysicalExpr>) -> Result<()> {
428428
429429#[ cfg( test) ]
430430mod tests {
431+ use crate :: PhysicalExpr ;
431432 use crate :: expressions:: { Column , LambdaVariable , NoOp , lambda:: lambda} ;
432433 use arrow:: {
433434 array:: RecordBatch ,
@@ -533,7 +534,7 @@ mod tests {
533534 // indices the inner LambdaExpr::new would produce: sorted referenced
534535 // indices, so the names alone matter here — what matters for
535536 // shadow tracking is the names, not the indices.
536- let inner_body: Arc < dyn crate :: PhysicalExpr > =
537+ let inner_body: Arc < dyn PhysicalExpr > =
537538 Arc :: new ( crate :: expressions:: BinaryExpr :: new (
538539 Arc :: new ( crate :: expressions:: BinaryExpr :: new (
539540 Arc :: new ( LambdaVariable :: new ( 1 , Arc :: clone ( & outer_k_field) ) ) ,
@@ -552,7 +553,7 @@ mod tests {
552553 // regular column reference so the walk has something non-trivial
553554 // to descend through. The outer body references the inner lambda
554555 // via `inner_lambda`.
555- let outer_body: Arc < dyn crate :: PhysicalExpr > =
556+ let outer_body: Arc < dyn PhysicalExpr > =
556557 Arc :: new ( crate :: expressions:: BinaryExpr :: new (
557558 Arc :: new ( Column :: new ( "col" , 0 ) ) ,
558559 datafusion_expr:: Operator :: Plus ,
@@ -571,6 +572,94 @@ mod tests {
571572 ) ;
572573 }
573574
575+ /// Reconstructs nested lambdas through [`ProjectLambdaBody`] and asserts
576+ /// the `captures ++ used_params` index layout at both levels.
577+ ///
578+ /// `(k, v) -> col + (k, v2) -> k + v2 + v` covers:
579+ /// 1. an outer captured column (`col` at a non-zero planner index),
580+ /// 2. an outer parameter captured by the inner lambda (`v`),
581+ /// 3. an inner parameter that shadows an outer name (`k`).
582+ ///
583+ /// The inner body is built with stale combined-schema indexes so each
584+ /// `LambdaExpr::new` has to rebase, matching a projection rewrite that
585+ /// changes the input schema width.
586+ #[ test]
587+ fn test_nested_lambda_project_lambda_body_rebasing ( ) {
588+ let k_field = Arc :: new ( Field :: new ( "k" , DataType :: Int32 , true ) ) ;
589+ let v_field = Arc :: new ( Field :: new ( "v" , DataType :: Int32 , true ) ) ;
590+ let v2_field = Arc :: new ( Field :: new ( "v2" , DataType :: Int32 , true ) ) ;
591+
592+ // Planner combined schema: a@0, b@1, col@2, outer k@3, outer v@4,
593+ // inner k@5, inner v2@6. Inner body: k + v2 + v.
594+ let inner_body: Arc < dyn PhysicalExpr > =
595+ Arc :: new ( crate :: expressions:: BinaryExpr :: new (
596+ Arc :: new ( crate :: expressions:: BinaryExpr :: new (
597+ Arc :: new ( LambdaVariable :: new ( 5 , Arc :: clone ( & k_field) ) ) ,
598+ datafusion_expr:: Operator :: Plus ,
599+ Arc :: new ( LambdaVariable :: new ( 6 , Arc :: clone ( & v2_field) ) ) ,
600+ ) ) ,
601+ datafusion_expr:: Operator :: Plus ,
602+ Arc :: new ( LambdaVariable :: new ( 4 , Arc :: clone ( & v_field) ) ) ,
603+ ) ) ;
604+ let inner_lambda =
605+ LambdaExpr :: try_new ( vec ! [ "k" . to_string( ) , "v2" . to_string( ) ] , inner_body)
606+ . unwrap ( ) ;
607+
608+ // Inner reconstruction: capture outer `v`@4, then own params `k`, `v2`.
609+ assert_eq ! ( inner_lambda. projection( ) , & [ 4 ] ) ;
610+ assert_eq ! ( inner_lambda. used_param_indices( ) , & [ 0 , 1 ] ) ;
611+ assert_eq ! (
612+ inner_lambda. projected_body( ) . as_ref( ) . to_string( ) ,
613+ "k@1 + v2@2 + v@0" ,
614+ "inner projected body should be captures(v) ++ params(k, v2)"
615+ ) ;
616+
617+ let outer_body: Arc < dyn PhysicalExpr > =
618+ Arc :: new ( crate :: expressions:: BinaryExpr :: new (
619+ Arc :: new ( Column :: new ( "col" , 2 ) ) ,
620+ datafusion_expr:: Operator :: Plus ,
621+ Arc :: new ( inner_lambda) ,
622+ ) ) ;
623+ let outer_lambda =
624+ LambdaExpr :: try_new ( vec ! [ "k" . to_string( ) , "v" . to_string( ) ] , outer_body)
625+ . unwrap ( ) ;
626+
627+ // Outer reconstruction: capture `col`@2, then used param `v` (`k` is
628+ // shadowed by the inner lambda, so it is not a used outer param).
629+ assert_eq ! ( outer_lambda. projection( ) , & [ 2 ] ) ;
630+ assert_eq ! ( outer_lambda. used_param_indices( ) , & [ 1 ] ) ;
631+ assert_eq ! (
632+ outer_lambda. projected_body( ) . as_ref( ) . to_string( ) ,
633+ "col@0 + (k, v2) -> k@5 + v2@6 + v@1" ,
634+ "outer projected body should rebase col@2 -> col@0 and rewrite the \
635+ nested capture of `v` onto the outer captures++params slot"
636+ ) ;
637+
638+ let reconstructed_inner = outer_lambda
639+ . projected_body ( )
640+ . children ( )
641+ . into_iter ( )
642+ . find_map ( |c| c. downcast_ref :: < LambdaExpr > ( ) )
643+ . expect ( "outer projected body should still wrap the nested lambda" ) ;
644+
645+ // Inner reconstruction after the outer rewrite: capture the remapped
646+ // outer `v`@1, then own params. Shadowed inner `k` stays an inner param.
647+ assert_eq ! ( reconstructed_inner. projection( ) , & [ 1 ] ) ;
648+ assert_eq ! ( reconstructed_inner. used_param_indices( ) , & [ 0 , 1 ] ) ;
649+ assert_eq ! (
650+ reconstructed_inner. projected_body( ) . as_ref( ) . to_string( ) ,
651+ "k@1 + v2@2 + v@0" ,
652+ "inner projected body should rebase the outer-remapped `v`@1 back \
653+ onto capture slot 0, with shadowed `k` and `v2` as own params"
654+ ) ;
655+ assert_eq ! (
656+ reconstructed_inner. body( ) . as_ref( ) . to_string( ) ,
657+ "k@5 + v2@6 + v@1" ,
658+ "inner original body should keep shadowed `k`/`v2` planner indexes \
659+ and the outer-rebased capture of `v`"
660+ ) ;
661+ }
662+
574663 /// A planner-assigned combined-schema index that would collide with a
575664 /// real input column after a later projection rewrite must not appear
576665 /// in the capture projection. See #24372.
0 commit comments