Skip to content

Commit d626d97

Browse files
committed
fix: give materialized CTEs new ids on every planning run
A table provider can plan a saved logical plan in `scan`, as a `ViewTable` that is not inlined does. Each read of such a table is a separate planning run of the same saved plan, so the CTE ids only had to be unique within that plan. Reading the table twice put two `MaterializedCteExec` nodes with the same id into the final plan, and the binding pass failed with "Two MaterializedCteExec nodes have the id ...". Every occurrence of a materialized CTE now gets a new id in each planning run, not only the duplicates. The binding pass also skips scans that an inner planning run has already bound, so their partitions are not counted twice in the buffer.
1 parent 5c88405 commit d626d97

3 files changed

Lines changed: 116 additions & 23 deletions

File tree

‎datafusion/core/src/datasource/view_test.rs‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,75 @@ mod tests {
456456

457457
Ok(())
458458
}
459+
460+
/// A [`ViewTable`] that is not inlined: each read plans the saved query in
461+
/// `scan`.
462+
///
463+
/// [`ViewTable`]: crate::datasource::ViewTable
464+
#[derive(Debug)]
465+
struct NotInlinedView(crate::datasource::ViewTable);
466+
467+
#[async_trait::async_trait]
468+
impl crate::datasource::TableProvider for NotInlinedView {
469+
fn schema(&self) -> arrow::datatypes::SchemaRef {
470+
self.0.schema()
471+
}
472+
473+
fn table_type(&self) -> crate::datasource::TableType {
474+
self.0.table_type()
475+
}
476+
477+
async fn scan(
478+
&self,
479+
state: &dyn crate::catalog::Session,
480+
projection: Option<&[usize]>,
481+
filters: &[datafusion_expr::Expr],
482+
limit: Option<usize>,
483+
) -> Result<std::sync::Arc<dyn crate::physical_plan::ExecutionPlan>> {
484+
self.0.scan(state, projection, filters, limit).await
485+
}
486+
}
487+
488+
#[tokio::test]
489+
async fn query_not_inlined_view_with_materialized_cte_twice() -> Result<()> {
490+
// sqlparser only parses `[NOT] MATERIALIZED` for the PostgreSQL dialect.
491+
let session_ctx = SessionContext::new_with_config(
492+
SessionConfig::new().set_str("datafusion.sql_parser.dialect", "PostgreSQL"),
493+
);
494+
session_ctx
495+
.sql("CREATE TABLE t AS VALUES (1), (2)")
496+
.await?
497+
.collect()
498+
.await?;
499+
let plan = session_ctx
500+
.sql(
501+
"WITH r AS MATERIALIZED (SELECT column1 AS a FROM t) \
502+
SELECT a FROM r",
503+
)
504+
.await?
505+
.into_optimized_plan()?;
506+
let view = crate::datasource::ViewTable::new(plan, None);
507+
session_ctx.register_table("w", std::sync::Arc::new(NotInlinedView(view)))?;
508+
509+
// Each read of `w` plans the saved query, which has the same CTE id
510+
// each time.
511+
let results = session_ctx
512+
.sql("SELECT x.a AS x, y.a AS y FROM w x, w y ORDER BY x, y")
513+
.await?
514+
.collect()
515+
.await?;
516+
517+
insta::assert_snapshot!(batches_to_string(&results),@r"
518+
+---+---+
519+
| x | y |
520+
+---+---+
521+
| 1 | 1 |
522+
| 1 | 2 |
523+
| 2 | 1 |
524+
| 2 | 2 |
525+
+---+---+
526+
");
527+
528+
Ok(())
529+
}
459530
}

‎datafusion/core/src/physical_planner.rs‎

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
320334
fn 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
};

‎datafusion/physical-plan/src/materialized_cte.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,11 @@ impl MaterializedCteScanExec {
620620
pub fn id(&self) -> u64 {
621621
self.id
622622
}
623+
624+
/// Whether [`Self::bind`] has been called on this scan.
625+
pub fn is_bound(&self) -> bool {
626+
self.buffer.is_some()
627+
}
623628
}
624629

625630
impl DisplayAs for MaterializedCteScanExec {

0 commit comments

Comments
 (0)