Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 9 additions & 78 deletions datafusion/core/src/physical_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ use datafusion_expr::physical_planning_context::{
use datafusion_expr::utils::{expr_to_columns, split_conjunction};
use datafusion_expr::{
Analyze, BinaryExpr, DmlStatement, Explain, ExplainFormat, Extension, FetchType,
Filter, JoinType, MaterializedCte, MaterializedCteId, MaterializedCtePlan, Operator,
RecursiveQuery, Show, ShowKind, SkipType, StringifiedPlan, WindowFrame,
WindowFrameBound, WriteOp,
Filter, JoinType, MaterializedCtePlan, Operator, RecursiveQuery, Show, ShowKind,
SkipType, StringifiedPlan, WindowFrame, WindowFrameBound, WriteOp,
renumber_materialized_ctes,
};
use datafusion_physical_expr::aggregate::{
AggregateFunctionExpr, LoweredAggregate, LoweredAggregateBuilder,
Expand Down Expand Up @@ -227,6 +227,8 @@ impl DefaultPhysicalPlanner {
{
return Ok(plan);
}
// The ids must be new for each planning run. See
// `renumber_materialized_ctes`.
let renumbered = renumber_materialized_ctes(logical_plan)?;
let logical_plan = renumbered.as_ref().unwrap_or(logical_plan);
let plan = self
Expand All @@ -236,77 +238,6 @@ impl DefaultPhysicalPlanner {
}
}

/// Give each occurrence of a [`MaterializedCte`] in `plan` a new id, and
/// update the scans in its continuation, including the scans in subqueries.
///
/// The id is copied when a logical plan is cloned, so one CTE can occur more
/// than once, for example when a view that declares it is referenced two
/// times, or when a DataFrame is joined to itself. Each occurrence is a
/// separate CTE, as in PostgreSQL.
///
/// The ids must also be new for each planning run, not only unique within
/// `plan`. A table provider can plan a saved logical plan in its `scan`, as a
/// [`ViewTable`] that is not inlined does. Each such scan is a separate
/// planning run of the same saved plan, and its CTEs end up in the same
/// physical plan as the CTEs of the outer run.
///
/// Returns `None` when `plan` has no materialized CTE.
///
/// [`ViewTable`]: crate::datasource::ViewTable
fn renumber_materialized_ctes(plan: &LogicalPlan) -> Result<Option<LogicalPlan>> {
let mut has_cte = false;
plan.apply_with_subqueries(|node| {
if matches!(
node,
LogicalPlan::MaterializedCte(MaterializedCtePlan::Cte(_))
) {
has_cte = true;
return Ok(TreeNodeRecursion::Stop);
}
Ok(TreeNodeRecursion::Continue)
})?;
if !has_cte {
return Ok(None);
}

plan.clone()
.transform_down_with_subqueries(|node| {
let LogicalPlan::MaterializedCte(MaterializedCtePlan::Cte(cte)) = node else {
return Ok(Transformed::no(node));
};
let old_id = cte.id;
let new_id = MaterializedCteId::next();
let continuation = Arc::unwrap_or_clone(cte.continuation)
.transform_down_with_subqueries(|node| match node {
// A nested occurrence of the same CTE owns the scans below
// it, and is renumbered on its own.
LogicalPlan::MaterializedCte(MaterializedCtePlan::Cte(
ref nested,
)) if nested.id == old_id => {
Ok(Transformed::new(node, false, TreeNodeRecursion::Jump))
}
LogicalPlan::MaterializedCte(MaterializedCtePlan::Scan(mut scan))
if scan.id == old_id =>
{
scan.id = new_id;
Ok(Transformed::yes(LogicalPlan::MaterializedCte(
MaterializedCtePlan::Scan(scan),
)))
}
_ => Ok(Transformed::no(node)),
})?
.data;
Ok(Transformed::yes(LogicalPlan::MaterializedCte(
MaterializedCtePlan::Cte(MaterializedCte {
id: new_id,
continuation: Arc::new(continuation),
..cte
}),
)))
})
.map(|t| Some(t.data))
}

/// Points every [`MaterializedCteScanExec`] at the buffer of the
/// [`MaterializedCteExec`] with the same id, and gives it the statistics of
/// the CTE body.
Expand Down Expand Up @@ -947,8 +878,8 @@ impl DefaultPhysicalPlanner {
LogicalPlan::MaterializedCte(MaterializedCtePlan::Scan(scan)) => {
// Unbound until `bind_materialized_cte_scans` runs on the whole plan.
let exec = MaterializedCteScanExec::new(
scan.id.as_u64(),
&scan.name,
scan.id().as_u64(),
scan.name(),
Arc::clone(scan.schema().inner()),
session.config().target_partitions(),
);
Expand Down Expand Up @@ -2044,11 +1975,11 @@ impl DefaultPhysicalPlanner {
if body.boundedness().is_unbounded() {
return plan_err!(
"MATERIALIZED CTE {} reads an unbounded source, which cannot be materialized",
cte.name
cte.name()
);
}
let buffer =
Arc::new(MaterializedCteBuffer::new(cte.id.as_u64(), &cte.name));
Arc::new(MaterializedCteBuffer::new(cte.id().as_u64(), cte.name()));
Arc::new(MaterializedCteExec::new(body, continuation, buffer))
}

Expand Down
9 changes: 1 addition & 8 deletions datafusion/expr/src/logical_plan/invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,7 @@ fn assert_valid_semantic_plan(plan: &LogicalPlan) -> Result<()> {
fn assert_materialized_cte_scans_are_valid(plan: &LogicalPlan) -> Result<()> {
plan.apply_with_subqueries(|node| {
if let LogicalPlan::MaterializedCte(MaterializedCtePlan::Cte(cte)) = node {
for scan in cte.scans()? {
scan.check_body_schema(cte.body().schema()).map_err(|e| {
e.context(format!(
"A scan of the materialized CTE {} does not match the CTE",
cte.name()
))
})?;
}
cte.check_scans()?;
}
Ok(TreeNodeRecursion::Continue)
})
Expand Down
Loading