@@ -94,7 +94,8 @@ use datafusion_expr::physical_planning_context::{
9494use datafusion_expr:: utils:: { expr_to_columns, split_conjunction} ;
9595use datafusion_expr:: {
9696 Analyze , BinaryExpr , DescribeTable , DmlStatement , Explain , ExplainFormat , Extension ,
97- FetchType , Filter , JoinType , Operator , RecursiveQuery , SkipType , StringifiedPlan ,
97+ FetchType , Filter , JoinType , MaterializedCte , MaterializedCteId , MaterializedCtePlan ,
98+ MaterializedCteScan , Operator , RecursiveQuery , SkipType , StringifiedPlan ,
9899 WindowFrame , WindowFrameBound , WriteOp ,
99100} ;
100101use datafusion_physical_expr:: aggregate:: {
@@ -260,19 +261,14 @@ impl DefaultPhysicalPlanner {
260261///
261262/// Returns `None` when `plan` has no materialized CTE.
262263///
263- /// [`MaterializedCte`]: datafusion_expr::MaterializedCte
264264/// [`ViewTable`]: crate::datasource::ViewTable
265265fn renumber_materialized_ctes ( plan : & LogicalPlan ) -> Result < Option < LogicalPlan > > {
266- fn as_cte ( plan : & LogicalPlan ) -> Option < & datafusion_expr:: MaterializedCte > {
267- match plan {
268- LogicalPlan :: Extension ( Extension { node } ) => node. as_any ( ) . downcast_ref ( ) ,
269- _ => None ,
270- }
271- }
272-
273266 let mut has_cte = false ;
274267 plan. apply_with_subqueries ( |node| {
275- if as_cte ( node) . is_some ( ) {
268+ if matches ! (
269+ node,
270+ LogicalPlan :: MaterializedCte ( MaterializedCtePlan :: Cte ( _) )
271+ ) {
276272 has_cte = true ;
277273 return Ok ( TreeNodeRecursion :: Stop ) ;
278274 }
@@ -284,53 +280,40 @@ fn renumber_materialized_ctes(plan: &LogicalPlan) -> Result<Option<LogicalPlan>>
284280
285281 plan. clone ( )
286282 . transform_down_with_subqueries ( |node| {
287- let Some ( cte) = as_cte ( & node) else {
283+ let LogicalPlan :: MaterializedCte ( MaterializedCtePlan :: Cte ( cte) ) = node else {
288284 return Ok ( Transformed :: no ( node) ) ;
289285 } ;
290286 let old_id = cte. id ;
291- let new_id = datafusion_expr:: MaterializedCteId :: next ( ) ;
292- let continuation = cte
293- . continuation
294- . clone ( )
295- . transform_down_with_subqueries ( |node| {
287+ let new_id = MaterializedCteId :: next ( ) ;
288+ let continuation = Arc :: unwrap_or_clone ( cte. continuation )
289+ . transform_down_with_subqueries ( |node| match node {
296290 // A nested occurrence of the same CTE owns the scans below
297291 // it, and is renumbered on its own.
298- if as_cte ( & node) . is_some_and ( |nested| nested. id == old_id) {
299- return Ok ( Transformed :: new (
300- node,
301- false ,
302- TreeNodeRecursion :: Jump ,
303- ) ) ;
292+ LogicalPlan :: MaterializedCte ( MaterializedCtePlan :: Cte (
293+ ref nested,
294+ ) ) if nested. id == old_id => {
295+ Ok ( Transformed :: new ( node, false , TreeNodeRecursion :: Jump ) )
304296 }
305- let LogicalPlan :: Extension ( Extension { node : scan } ) = & node else {
306- return Ok ( Transformed :: no ( node) ) ;
307- } ;
308- match scan
309- . as_any ( )
310- . downcast_ref :: < datafusion_expr:: MaterializedCteScan > ( )
297+ LogicalPlan :: MaterializedCte ( MaterializedCtePlan :: Scan ( scan) )
298+ if scan. id == old_id =>
311299 {
312- Some ( scan ) if scan . id == old_id => {
313- let scan = datafusion_expr :: MaterializedCteScan {
300+ Ok ( Transformed :: yes ( LogicalPlan :: MaterializedCte (
301+ MaterializedCtePlan :: Scan ( MaterializedCteScan {
314302 id : new_id,
315- ..scan. clone ( )
316- } ;
317- Ok ( Transformed :: yes ( LogicalPlan :: Extension ( Extension {
318- node : Arc :: new ( scan) ,
319- } ) ) )
320- }
321- _ => Ok ( Transformed :: no ( node) ) ,
303+ ..scan
304+ } ) ,
305+ ) ) )
322306 }
307+ _ => Ok ( Transformed :: no ( node) ) ,
323308 } ) ?
324309 . data ;
325- let cte = datafusion_expr:: MaterializedCte {
326- id : new_id,
327- name : cte. name . clone ( ) ,
328- cte : cte. cte . clone ( ) ,
329- continuation,
330- } ;
331- Ok ( Transformed :: yes ( LogicalPlan :: Extension ( Extension {
332- node : Arc :: new ( cte) ,
333- } ) ) )
310+ Ok ( Transformed :: yes ( LogicalPlan :: MaterializedCte (
311+ MaterializedCtePlan :: Cte ( MaterializedCte {
312+ id : new_id,
313+ continuation : Arc :: new ( continuation) ,
314+ ..cte
315+ } ) ,
316+ ) ) )
334317 } )
335318 . map ( |t| Some ( t. data ) )
336319}
@@ -838,6 +821,16 @@ impl DefaultPhysicalPlanner {
838821 self . plan_describe ( & Arc :: clone ( schema) , output_schema) ?
839822 }
840823
824+ LogicalPlan :: MaterializedCte ( MaterializedCtePlan :: Scan ( scan) ) => {
825+ // Unbound until `bind_materialized_cte_scans` runs on the whole plan.
826+ Arc :: new ( MaterializedCteScanExec :: new (
827+ scan. id . as_u64 ( ) ,
828+ & scan. name ,
829+ Arc :: clone ( scan. schema . inner ( ) ) ,
830+ session_state. config ( ) . target_partitions ( ) ,
831+ ) )
832+ }
833+
841834 // 1 Child
842835 LogicalPlan :: Copy ( CopyTo {
843836 input,
@@ -2051,31 +2044,10 @@ impl DefaultPhysicalPlanner {
20512044 ) ?)
20522045 }
20532046
2054- // N Children
2055- LogicalPlan :: Union ( _) => UnionExec :: try_new ( children. vec ( ) ) ?,
2056- LogicalPlan :: Extension ( Extension { node } )
2057- if node. as_any ( ) . is :: < datafusion_expr:: MaterializedCte > ( ) =>
2058- {
2059- let cte = node
2060- . as_any ( )
2061- . downcast_ref :: < datafusion_expr:: MaterializedCte > ( )
2062- . unwrap ( ) ;
2047+ LogicalPlan :: MaterializedCte ( MaterializedCtePlan :: Cte ( cte) ) => {
20632048 let [ body, continuation] = children. two ( ) ?;
20642049 // A CTE that nothing reads is never evaluated.
2065- let mut is_referenced = false ;
2066- cte. continuation . apply_with_subqueries ( |node| {
2067- if let LogicalPlan :: Extension ( Extension { node } ) = node
2068- && let Some ( scan) =
2069- node. as_any ( )
2070- . downcast_ref :: < datafusion_expr:: MaterializedCteScan > ( )
2071- && scan. id == cte. id
2072- {
2073- is_referenced = true ;
2074- return Ok ( TreeNodeRecursion :: Stop ) ;
2075- }
2076- Ok ( TreeNodeRecursion :: Continue )
2077- } ) ?;
2078- if !is_referenced {
2050+ if cte. scans ( ) ?. is_empty ( ) {
20792051 return Ok ( continuation) ;
20802052 }
20812053 // The body is fully buffered before a scan yields a row, so an
@@ -2090,21 +2062,9 @@ impl DefaultPhysicalPlanner {
20902062 Arc :: new ( MaterializedCteBuffer :: new ( cte. id . as_u64 ( ) , & cte. name ) ) ;
20912063 Arc :: new ( MaterializedCteExec :: new ( body, continuation, buffer) )
20922064 }
2093- LogicalPlan :: Extension ( Extension { node } )
2094- if node. as_any ( ) . is :: < datafusion_expr:: MaterializedCteScan > ( ) =>
2095- {
2096- let scan = node
2097- . as_any ( )
2098- . downcast_ref :: < datafusion_expr:: MaterializedCteScan > ( )
2099- . unwrap ( ) ;
2100- // Unbound until `bind_materialized_cte_scans` runs on the whole plan.
2101- Arc :: new ( MaterializedCteScanExec :: new (
2102- scan. id . as_u64 ( ) ,
2103- & scan. name ,
2104- Arc :: clone ( scan. schema . inner ( ) ) ,
2105- session_state. config ( ) . target_partitions ( ) ,
2106- ) )
2107- }
2065+
2066+ // N Children
2067+ LogicalPlan :: Union ( _) => UnionExec :: try_new ( children. vec ( ) ) ?,
21082068 LogicalPlan :: Extension ( Extension { node } ) => {
21092069 let mut maybe_plan = None ;
21102070 let children = children. vec ( ) ;
@@ -2710,7 +2670,8 @@ fn extract_dml_filters(
27102670 | LogicalPlan :: Ddl ( _)
27112671 | LogicalPlan :: Copy ( _)
27122672 | LogicalPlan :: Unnest ( _)
2713- | LogicalPlan :: RecursiveQuery ( _) => {
2673+ | LogicalPlan :: RecursiveQuery ( _)
2674+ | LogicalPlan :: MaterializedCte ( _) => {
27142675 // No filters to extract from leaf/meta plans
27152676 }
27162677 // Plans with inputs (may contain filters in children)
0 commit comments