@@ -2160,27 +2160,19 @@ impl DefaultPhysicalPlanner {
21602160 } )
21612161 . collect :: < Result < Vec < _ > > > ( ) ?;
21622162
2163- // Check if input plan was transformed from a PIVOT operation
2164- // The original logical plan will be a LogicalPlan::Pivot that has been transformed to an Aggregate
2163+ // When we detect a PIVOT-derived plan with a value_subquery, ensure all generated columns are preserved
21652164 match input. as_ref ( ) {
2166- // Direct PIVOT
2167- LogicalPlan :: Pivot ( _) => {
2168- // When we detect a PIVOT-derived plan, ensure all generated columns are preserved
2169- if input_exec. as_any ( ) . downcast_ref :: < AggregateExec > ( ) . is_some ( ) {
2165+ LogicalPlan :: Pivot ( pivot) => {
2166+ if pivot. value_subquery . is_some ( ) && input_exec. as_any ( ) . downcast_ref :: < AggregateExec > ( ) . is_some ( ) {
21702167 let agg_exec = input_exec. as_any ( ) . downcast_ref :: < AggregateExec > ( ) . unwrap ( ) ;
21712168 let schema = input_exec. schema ( ) ;
21722169 let group_by_len = agg_exec. group_expr ( ) . expr ( ) . len ( ) ;
21732170
2174- // If we have aggregate expressions that correspond to pivot columns
21752171 if group_by_len < schema. fields ( ) . len ( ) {
2176- // This is a pivot result - we need to include all columns from the aggregate
21772172 let mut all_exprs = physical_exprs. clone ( ) ;
21782173
2179- // Add any missing pivot columns (which are all columns after the group_by columns)
21802174 for ( i, field) in schema. fields ( ) . iter ( ) . enumerate ( ) . skip ( group_by_len) {
2181- // Check if this column is already included in the projection
21822175 if !physical_exprs. iter ( ) . any ( |( _, name) | name == field. name ( ) ) {
2183- // Add this pivot column to the projection
21842176 all_exprs. push ( (
21852177 Arc :: new ( Column :: new ( field. name ( ) , i) ) as Arc < dyn PhysicalExpr > ,
21862178 field. name ( ) . clone ( ) ,
0 commit comments