@@ -214,7 +214,7 @@ impl DefaultPhysicalPlanner {
214214 {
215215 return Ok ( plan) ;
216216 }
217- let renumbered = renumber_duplicate_materialized_ctes ( logical_plan) ?;
217+ let renumbered = renumber_materialized_ctes ( logical_plan) ?;
218218 let logical_plan = renumbered. as_ref ( ) . unwrap_or ( logical_plan) ;
219219 let plan = self
220220 . create_initial_plan ( logical_plan, session_state)
@@ -225,56 +225,64 @@ impl DefaultPhysicalPlanner {
225225 }
226226}
227227
228- /// Give each occurrence of a [`MaterializedCte`] in `plan` its own id.
228+ /// Give each occurrence of a [`MaterializedCte`] in `plan` a new id, and
229+ /// update the scans in its continuation, including the scans in subqueries.
229230///
230231/// The id is copied when a logical plan is cloned, so one CTE can occur more
231- /// than once in a plan, for example when a view that declares it is referenced
232- /// two times, or when a DataFrame is joined to itself. Each occurrence is a
233- /// separate CTE, as in PostgreSQL. Thus every occurrence after the first gets a
234- /// new id, and so do the scans in its continuation, including the scans in
235- /// subqueries.
232+ /// than once, for example when a view that declares it is referenced two
233+ /// times, or when a DataFrame is joined to itself. Each occurrence is a
234+ /// separate CTE, as in PostgreSQL.
236235///
237- /// Returns `None` when all ids are already unique.
236+ /// The ids must also be new for each planning run, not only unique within
237+ /// `plan`. A table provider can plan a saved logical plan in its `scan`, as a
238+ /// [`ViewTable`] that is not inlined does. Each such scan is a separate
239+ /// planning run of the same saved plan, and its CTEs end up in the same
240+ /// physical plan as the CTEs of the outer run.
241+ ///
242+ /// Returns `None` when `plan` has no materialized CTE.
238243///
239244/// [`MaterializedCte`]: datafusion_expr::MaterializedCte
240- fn renumber_duplicate_materialized_ctes (
241- plan : & LogicalPlan ,
242- ) -> Result < Option < LogicalPlan > > {
245+ /// [`ViewTable`]: crate::datasource::ViewTable
246+ fn renumber_materialized_ctes ( plan : & LogicalPlan ) -> Result < Option < LogicalPlan > > {
243247 fn as_cte ( plan : & LogicalPlan ) -> Option < & datafusion_expr:: MaterializedCte > {
244248 match plan {
245249 LogicalPlan :: Extension ( Extension { node } ) => node. as_any ( ) . downcast_ref ( ) ,
246250 _ => None ,
247251 }
248252 }
249253
250- let mut ids = HashSet :: new ( ) ;
251- let mut has_duplicate = false ;
254+ let mut has_cte = false ;
252255 plan. apply_with_subqueries ( |node| {
253- if let Some ( cte) = as_cte ( node) {
254- has_duplicate |= !ids. insert ( cte. id ) ;
256+ if as_cte ( node) . is_some ( ) {
257+ has_cte = true ;
258+ return Ok ( TreeNodeRecursion :: Stop ) ;
255259 }
256260 Ok ( TreeNodeRecursion :: Continue )
257261 } ) ?;
258- if !has_duplicate {
262+ if !has_cte {
259263 return Ok ( None ) ;
260264 }
261265
262- let mut ids = HashSet :: new ( ) ;
263266 plan. clone ( )
264267 . transform_down_with_subqueries ( |node| {
265268 let Some ( cte) = as_cte ( & node) else {
266269 return Ok ( Transformed :: no ( node) ) ;
267270 } ;
268- if ids. insert ( cte. id ) {
269- return Ok ( Transformed :: no ( node) ) ;
270- }
271271 let old_id = cte. id ;
272272 let new_id = datafusion_expr:: MaterializedCteId :: next ( ) ;
273- ids. insert ( new_id) ;
274273 let continuation = cte
275274 . continuation
276275 . clone ( )
277276 . transform_down_with_subqueries ( |node| {
277+ // A nested occurrence of the same CTE owns the scans below
278+ // it, and is renumbered on its own.
279+ if as_cte ( & node) . is_some_and ( |nested| nested. id == old_id) {
280+ return Ok ( Transformed :: new (
281+ node,
282+ false ,
283+ TreeNodeRecursion :: Jump ,
284+ ) ) ;
285+ }
278286 let LogicalPlan :: Extension ( Extension { node : scan } ) = & node else {
279287 return Ok ( Transformed :: no ( node) ) ;
280288 } ;
@@ -311,12 +319,18 @@ fn renumber_duplicate_materialized_ctes(
311319/// Point every [`MaterializedCteScanExec`] at the buffer of the
312320/// [`MaterializedCteExec`] with the same id.
313321///
314- /// [`renumber_duplicate_materialized_ctes`] makes the ids unique before the
315- /// physical plan is created.
322+ /// [`renumber_materialized_ctes`] makes the ids unique before the physical
323+ /// plan is created.
324+ ///
325+ /// A scan that is already bound was planned and bound by a separate planning
326+ /// run, such as the `scan` of a [`ViewTable`], and is left as is. Binding it
327+ /// again would count its partitions twice in the buffer.
316328///
317329/// Scans are planned before the node that owns their CTE, and a scan inside a
318330/// scalar subquery is planned in a separate subtree, so the binding is done
319331/// once on the whole initial plan.
332+ ///
333+ /// [`ViewTable`]: crate::datasource::ViewTable
320334fn bind_materialized_cte_scans (
321335 plan : Arc < dyn ExecutionPlan > ,
322336) -> Result < Arc < dyn ExecutionPlan > > {
@@ -341,6 +355,9 @@ fn bind_materialized_cte_scans(
341355 let Some ( scan) = node. downcast_ref :: < MaterializedCteScanExec > ( ) else {
342356 return Ok ( Transformed :: no ( node) ) ;
343357 } ;
358+ if scan. is_bound ( ) {
359+ return Ok ( Transformed :: no ( node) ) ;
360+ }
344361 let Some ( buffer) = buffers. get ( & scan. id ( ) ) else {
345362 return internal_err ! ( "MaterializedCteScanExec {} has no CTE" , scan. id( ) ) ;
346363 } ;
0 commit comments