adriangb commented on code in PR #18192:
URL: https://github.com/apache/datafusion/pull/18192#discussion_r2446640086
##########
datafusion/proto/src/physical_plan/mod.rs:
##########
@@ -101,6 +102,42 @@ use datafusion_physical_plan::{ExecutionPlan,
InputOrderMode, PhysicalExpr, Wind
use prost::bytes::BufMut;
use prost::Message;
+/// Context for decoding physical expressions with caching support.
+///
+/// This struct wraps a `TaskContext` and maintains a cache of previously
deserialized
+/// physical expressions. The cache is keyed by the expression's ID (derived
from the
+/// Arc pointer during serialization), allowing duplicate expressions in a
plan to be
+/// deserialized only once.
+pub struct DecodeContext<'a> {
+ task_context: &'a TaskContext,
+ cache: Mutex<HashMap<u64, Arc<dyn PhysicalExpr>>>,
+}
+
+impl<'a> DecodeContext<'a> {
+ /// Create a new DecodeContext wrapping the given TaskContext.
+ pub fn new(task_context: &'a TaskContext) -> Self {
+ Self {
+ task_context,
+ cache: Mutex::new(HashMap::new()),
+ }
+ }
+
+ /// Get the underlying TaskContext reference.
+ pub fn task_context(&self) -> &'a TaskContext {
+ self.task_context
+ }
+
+ /// Attempt to retrieve a cached physical expression by its ID.
+ pub fn get_cached_expr(&self, id: u64) -> Option<Arc<dyn PhysicalExpr>> {
+ self.cache.lock().unwrap().get(&id).cloned()
+ }
+
+ /// Insert a physical expression into the cache with the given ID.
+ pub fn insert_cached_expr(&self, id: u64, expr: Arc<dyn PhysicalExpr>) {
+ self.cache.lock().unwrap().insert(id, expr);
+ }
Review Comment:
This does assume there's no other reader/writer so it's safe to check
`get()` then call `insert()` later and it's not possible that someone else
inserted before us.
--
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]