@@ -373,10 +373,42 @@ pub(super) fn set_cache_states(
373373 . block_at_cache ( 1 ) ;
374374 let lp = pred_pd. optimize ( start_lp, lp_arena, expr_arena) ?;
375375 lp_arena. replace ( node, lp. clone ( ) ) ;
376+
377+ // TODO: Drop filter column if it isn't used after the filter.
378+
379+ let mut updated_cache_node = node;
380+
381+ loop {
382+ match lp_arena. get ( updated_cache_node) {
383+ IR :: Cache { .. } => break ,
384+ IR :: SimpleProjection { input, .. } => updated_cache_node = * input,
385+ _ => unreachable ! ( ) ,
386+ }
387+ }
388+
376389 for & parents in & v. parents [ 1 ..] {
377- let node = get_filter_node ( parents, lp_arena)
390+ let filter_node = get_filter_node ( parents, lp_arena)
378391 . expect ( "expected filter; this is an optimizer bug" ) ;
379- lp_arena. replace ( node, lp. clone ( ) ) ;
392+
393+ let IR :: Filter { input, .. } = lp_arena. get ( filter_node) else {
394+ unreachable ! ( )
395+ } ;
396+
397+ let new_lp = match lp_arena. get ( * input) {
398+ IR :: SimpleProjection { input, columns } => {
399+ debug_assert ! ( matches!( lp_arena. get( * input) , IR :: Cache { .. } ) ) ;
400+ IR :: SimpleProjection {
401+ input : updated_cache_node,
402+ columns : columns. clone ( ) ,
403+ }
404+ } ,
405+ ir => {
406+ debug_assert ! ( matches!( ir, IR :: Cache { .. } ) ) ;
407+ lp_arena. get ( updated_cache_node) . clone ( )
408+ } ,
409+ } ;
410+
411+ lp_arena. replace ( filter_node, new_lp) ;
380412 }
381413 } else {
382414 let child = * v. children . first ( ) . unwrap ( ) ;
0 commit comments