asolimando commented on code in PR #21815:
URL: https://github.com/apache/datafusion/pull/21815#discussion_r3268940471


##########
datafusion/physical-plan/src/statistics_context.rs:
##########
@@ -0,0 +1,235 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Context for computing statistics in physical plans.
+//!
+//! [`StatisticsContext`] provides external context to
+//! [`ExecutionPlan::partition_statistics_with_context`], enabling operators
+//! to receive pre-computed child statistics and additional context for
+//! statistics computation.
+
+use crate::ExecutionPlan;
+use datafusion_common::Result;
+use datafusion_common::Statistics;
+use std::cell::RefCell;
+use std::collections::HashMap;
+use std::rc::Rc;
+use std::sync::Arc;
+
+/// Per-call memoization cache for [`compute_statistics`].
+///
+/// Keyed by `(plan node pointer address, partition)`. Created once per
+/// top-level [`compute_statistics`] call and shared across all recursive
+/// and operator-internal calls via [`StatisticsContext`].
+///
+/// The pointer-based key is safe within a single synchronous
+/// `compute_statistics` call: all `Arc<dyn ExecutionPlan>` nodes are held
+/// by the plan tree for the duration of the walk, so addresses cannot be
+/// reused.
+#[derive(Debug, Default)]
+struct StatsCache(HashMap<(usize, Option<usize>), Arc<Statistics>>);
+
+impl StatsCache {
+    fn get(
+        &self,
+        plan: &dyn ExecutionPlan,
+        partition: Option<usize>,
+    ) -> Option<&Arc<Statistics>> {
+        let key = (
+            plan as *const dyn ExecutionPlan as *const () as usize,
+            partition,
+        );
+        self.0.get(&key)
+    }
+
+    fn insert(
+        &mut self,
+        plan: &dyn ExecutionPlan,
+        partition: Option<usize>,
+        stats: Arc<Statistics>,
+    ) {
+        let key = (
+            plan as *const dyn ExecutionPlan as *const () as usize,
+            partition,
+        );
+        self.0.insert(key, stats);
+    }
+}
+
+/// Context passed to [`ExecutionPlan::partition_statistics_with_context`]
+/// carrying external information that operators can use when computing
+/// their statistics.
+#[derive(Debug)]
+pub struct StatisticsContext {
+    /// Pre-computed statistics for each child of the current node,
+    /// in the same order as [`ExecutionPlan::children`].
+    child_stats: Vec<Arc<Statistics>>,

Review Comment:
   I have implemented the suggestion in 53bbf5e, I think it was the right call 
but I have left it as a separate commit so it's simpler to check



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to