You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/10/01 05:47:16 UTC
(datafusion) branch gh-readonly-queue/main/pr-25826-14a5d1376de4b916c05cc69b64c933783b30a7b1 created (now 9ff61f9044)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-25826-14a5d1376de4b916c05cc69b64c933783b30a7b1
in repository https://gitbox.apache.org/repos/asf/datafusion.git
at 9ff61f9044 Return provider extensions directly from the statistics walk (#25826)
This branch includes the following new commits:
new 9ff61f9044 Return provider extensions directly from the statistics walk (#25826)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion) 01/01: Return provider extensions directly from the statistics walk (#25826)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-25826-14a5d1376de4b916c05cc69b64c933783b30a7b1
in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit 9ff61f9044934b8c6f5a5ec62b8cad627432184f
Author: kosiew <ko...@gmail.com>
AuthorDate: Thu Oct 1 05:46:52 2026 +0000
Return provider extensions directly from the statistics walk (#25826)
## Which issue does this PR close?
Part of #25141.
## Rationale for this change
`compute_extended` currently reads a root node’s provider extensions
back from the cache after computing its statistics. A planned change
will stop caching borrowed roots, so that read would lose their
extensions. Returning the extensions from the statistics walk preserves
the result without depending on a root cache entry.
## What changes are included in this PR?
- Return statistics and provider extensions together from the private
statistics walk. `compute_extended` uses that result; `compute` keeps
returning only the statistics.
- Continue caching provider extensions for parent nodes. On a cache hit,
read them only when the caller requests them.
- Replace `ExtendedStatistics::extensions()` with `into_parts()` to move
its statistics and extensions into the walk’s result.
The public signatures of `compute` and `compute_extended` are unchanged.
## Are these changes tested?
The patch adds `cached_node_keeps_extensions` and
`walk_returns_provider_extensions_directly`. They cover extensions
returned after a child is cached, extensions returned from a fresh
provider result, and cache reads that occur only when requested.
## Are there any user-facing changes?
No user-facing change is intended. This refactor preserves provider
extension behavior ahead of the planned cache change.
## LLM-generated code disclosure
This PR includes LLM-generated code and comments. All LLM-generated
content has been manually reviewed.
---
.../physical-plan/src/operator_statistics/mod.rs | 6 +-
datafusion/physical-plan/src/statistics.rs | 138 ++++++++++++++++-----
2 files changed, 111 insertions(+), 33 deletions(-)
diff --git a/datafusion/physical-plan/src/operator_statistics/mod.rs b/datafusion/physical-plan/src/operator_statistics/mod.rs
index 4274b3b011..e09aa6b86a 100644
--- a/datafusion/physical-plan/src/operator_statistics/mod.rs
+++ b/datafusion/physical-plan/src/operator_statistics/mod.rs
@@ -183,9 +183,9 @@ impl ExtendedStatistics {
Self { base, extensions }
}
- /// Returns the extension map.
- pub(crate) fn extensions(&self) -> &Extensions {
- &self.extensions
+ /// Splits into the base statistics and the extension map without cloning.
+ pub(crate) fn into_parts(self) -> (Arc<Statistics>, Extensions) {
+ (self.base, self.extensions)
}
}
diff --git a/datafusion/physical-plan/src/statistics.rs b/datafusion/physical-plan/src/statistics.rs
index 18d9d0f28b..881638fa6e 100644
--- a/datafusion/physical-plan/src/statistics.rs
+++ b/datafusion/physical-plan/src/statistics.rs
@@ -207,7 +207,8 @@ impl StatisticsContext {
plan: &dyn ExecutionPlan,
args: &StatisticsArgs,
) -> Result<Arc<Statistics>> {
- self.compute_base(plan, args)
+ self.compute_base(plan, args, false)
+ .map(|(statistics, _)| statistics)
}
/// Computes the [`ExtendedStatistics`] for `plan`: the core statistics plus
@@ -218,19 +219,24 @@ impl StatisticsContext {
plan: &dyn ExecutionPlan,
args: &StatisticsArgs,
) -> Result<Arc<ExtendedStatistics>> {
- let statistics = self.compute_base(plan, args)?;
- let extensions = self
- .cached_extensions(plan, args.partition())
- .unwrap_or_default();
+ let (statistics, extensions) = self.compute_base(plan, args, true)?;
Ok(Arc::new(ExtendedStatistics::new_with_extensions(
- statistics, extensions,
+ statistics,
+ extensions.unwrap_or_default(),
)))
}
/// Bottom-up walk producing the node's core statistics, resolving children
/// first and consulting the provider chain before the operator's built-in
- /// [`ExecutionPlan::statistics_from_inputs`]. Any extensions a provider
- /// attaches are recorded in the extension cache for [`Self::compute_extended`].
+ /// [`ExecutionPlan::statistics_from_inputs`].
+ ///
+ /// Also returns the extensions a provider attached to this node, so callers
+ /// never need to read the node's own extensions back out of the cache.
+ /// Extensions are still recorded in the cache so parents can consume them.
+ /// A freshly computed provider result always returns its extensions (they
+ /// are moved, not cloned); on a cache hit they are cloned out of the cache
+ /// only when `read_cached_extensions` is set, so child lookups that discard
+ /// them pay nothing.
///
/// When `args.partition()` is `Some(idx)`, `idx` is validated against the
/// plan's partition count.
@@ -238,7 +244,8 @@ impl StatisticsContext {
&self,
plan: &dyn ExecutionPlan,
args: &StatisticsArgs,
- ) -> Result<Arc<Statistics>> {
+ read_cached_extensions: bool,
+ ) -> Result<(Arc<Statistics>, Option<Extensions>)> {
let partition = args.partition();
if let Some(idx) = partition {
@@ -252,24 +259,33 @@ impl StatisticsContext {
}
if let Some(cached) = self.cached_statistics(plan, partition) {
- return Ok(cached);
+ // Only providers store extensions, so with an empty registry the
+ // extension cache is never touched.
+ let extensions =
+ if read_cached_extensions && !self.registry.providers().is_empty() {
+ self.cached_extensions(plan, partition)
+ } else {
+ None
+ };
+ return Ok((cached, extensions));
}
let children = plan.children();
// Try providers before resolving the operator's own children, so a
// provider that overrides this node is not blocked by the fallback walk.
- let statistics = match self.try_provider_stats(plan, &children, args)? {
- Some(statistics) => statistics,
- None => {
- let requests = plan.child_stats_requests(partition);
- self.validate_child_requests(plan, &children, &requests)?;
- let child_statistics =
- self.resolve_children(plan, &children, &requests)?;
- plan.statistics_from_inputs(&child_statistics, args)?
- }
- };
+ let (statistics, extensions) =
+ match self.try_provider_stats(plan, &children, args)? {
+ Some(computed) => computed,
+ None => {
+ let requests = plan.child_stats_requests(partition);
+ self.validate_child_requests(plan, &children, &requests)?;
+ let child_statistics =
+ self.resolve_children(plan, &children, &requests)?;
+ (plan.statistics_from_inputs(&child_statistics, args)?, None)
+ }
+ };
self.store_statistics(plan, partition, Arc::clone(&statistics));
- Ok(statistics)
+ Ok((statistics, extensions))
}
/// Validates child stat `requests` against `plan`'s children: the count must
@@ -318,7 +334,12 @@ impl StatisticsContext {
.enumerate()
.map(|(i, (child, directive))| match directive {
ChildStats::At(p) => self
- .compute_base(child.as_ref(), &StatisticsArgs::new().with_partition(*p))
+ .compute_base(
+ child.as_ref(),
+ &StatisticsArgs::new().with_partition(*p),
+ false,
+ )
+ .map(|(statistics, _)| statistics)
.map_err(|e| {
e.context(format!(
"computing statistics for child {i} ({}) of {} at partition {p:?}",
@@ -334,9 +355,10 @@ impl StatisticsContext {
}
/// Runs the provider chain, returning the first `Computed` result's core
- /// statistics (and recording its extensions), or `None` if the chain is empty
- /// or all delegate. A partition-blind provider applies only to overall stats
- /// (its default `compute_statistics_with_args` delegates per partition).
+ /// statistics and non-empty extensions (also recording the extensions in
+ /// the cache), or `None` if the chain is empty or all delegate. A
+ /// partition-blind provider applies only to overall stats (its default
+ /// `compute_statistics_with_args` delegates per partition).
///
/// Each provider's child statistics come from its own
/// [`child_stats_requests`](crate::operator_statistics::StatisticsProvider::child_stats_requests)
@@ -346,7 +368,7 @@ impl StatisticsContext {
plan: &dyn ExecutionPlan,
children: &[&Arc<dyn ExecutionPlan>],
args: &StatisticsArgs,
- ) -> Result<Option<Arc<Statistics>>> {
+ ) -> Result<Option<(Arc<Statistics>, Option<Extensions>)>> {
let providers = self.registry.providers();
if providers.is_empty() {
return Ok(None);
@@ -379,10 +401,14 @@ impl StatisticsContext {
if let StatisticsResult::Computed(computed) =
provider.compute_statistics_with_args(plan, &child_extended, args)?
{
- if !computed.extensions().is_empty() {
- self.store_extensions(plan, partition, computed.extensions().clone());
- }
- return Ok(Some(Arc::clone(computed.base_arc())));
+ let (statistics, extensions) = computed.into_parts();
+ let extensions = if extensions.is_empty() {
+ None
+ } else {
+ self.store_extensions(plan, partition, extensions.clone());
+ Some(extensions)
+ };
+ return Ok(Some((statistics, extensions)));
}
}
Ok(None)
@@ -652,6 +678,58 @@ mod tests {
assert_eq!(extended.get_extension::<Tag>(), Some(&Tag(14)));
}
+ #[test]
+ fn cached_node_keeps_extensions() {
+ let leaf = make_stats_leaf(100);
+ let parent: Arc<dyn ExecutionPlan> =
+ Arc::new(CoalescePartitionsExec::new(Arc::clone(&leaf)));
+ let ctx = ctx_with(Arc::new(TagLeafProvider { rows: 100, tag: 7 }));
+
+ // Walking the parent caches the leaf as a child ...
+ let _ = ctx
+ .compute(parent.as_ref(), &StatisticsArgs::new())
+ .unwrap();
+ // ... so computing the leaf directly is a cache hit that must still
+ // return the extensions its provider attached.
+ let first = ctx
+ .compute_extended(leaf.as_ref(), &StatisticsArgs::new())
+ .unwrap();
+ let second = ctx
+ .compute_extended(leaf.as_ref(), &StatisticsArgs::new())
+ .unwrap();
+ assert_eq!(first.get_extension::<Tag>(), Some(&Tag(7)));
+ assert_eq!(second.get_extension::<Tag>(), Some(&Tag(7)));
+ assert!(Arc::ptr_eq(first.base_arc(), second.base_arc()));
+ }
+
+ /// The walk returns a freshly computed node's extensions from the provider
+ /// result itself, not from the cache, so `compute_extended` does not depend
+ /// on the root having a cache entry. On a cache hit they are read only on
+ /// request.
+ #[test]
+ fn walk_returns_provider_extensions_directly() {
+ let leaf = make_stats_leaf(100);
+ let ctx = ctx_with(Arc::new(TagLeafProvider { rows: 100, tag: 7 }));
+ let args = StatisticsArgs::new();
+
+ // Fresh computation: returned even though no cached read was requested,
+ // and still recorded for parents.
+ let (_, extensions) = ctx.compute_base(leaf.as_ref(), &args, false).unwrap();
+ assert_eq!(extensions.unwrap().get::<Tag>(), Some(&Tag(7)));
+ assert!(
+ ctx.cache
+ .borrow()
+ .extensions
+ .contains_key(&cache_key(leaf.as_ref(), None))
+ );
+
+ // Cache hit: extensions are cloned out of the cache only on request.
+ let (_, extensions) = ctx.compute_base(leaf.as_ref(), &args, false).unwrap();
+ assert!(extensions.is_none());
+ let (_, extensions) = ctx.compute_base(leaf.as_ref(), &args, true).unwrap();
+ assert_eq!(extensions.unwrap().get::<Tag>(), Some(&Tag(7)));
+ }
+
#[test]
fn builtin_fallback_drops_extensions() {
let leaf = make_stats_leaf(100);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org