comphead commented on code in PR #10430:
URL: https://github.com/apache/datafusion/pull/10430#discussion_r1596888406


##########
datafusion/optimizer/src/join_key_set.rs:
##########
@@ -0,0 +1,240 @@
+// 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.
+
+//! [JoinKeySet] for tracking the set of join keys in a plan.
+
+use datafusion_expr::Expr;
+use indexmap::{Equivalent, IndexSet};
+
+/// Tracks a set of equality Join keys
+///
+/// A join key is an expression that is used to join two tables via an equality
+/// predicate such as `a.x = b.y`
+///
+/// This struct models `a.x + 5 = b.y AND a.z = b.z` as two join keys
+/// 1. `(a.x + 5,  b.y)`
+/// 2. `(a.z,      b.z)`
+///
+/// # Important properties:
+///
+/// 1. Retains insert order
+/// 2. Can quickly look up if a pair of expressions are in the set.
+#[derive(Debug)]
+pub struct JoinKeySet {
+    inner: IndexSet<(Expr, Expr)>,
+}
+
+impl JoinKeySet {
+    /// Create a new empty set
+    pub fn new() -> Self {
+        Self {
+            inner: IndexSet::new(),
+        }
+    }
+
+    /// Return true of this set contains (left = right)
+    /// or (right = left)
+    pub fn contains(&self, left: &Expr, right: &Expr) -> bool {
+        self.inner.contains(&ExprPair::new(left, right))
+            || self.inner.contains(&ExprPair::new(right, left))
+    }
+
+    /// Insert the join key `(left = right)` into the set  it or `(right =
+    /// left)` is not already in the set
+    ///
+    /// returns true if the pair was inserted
+    pub fn insert(&mut self, left: &Expr, right: &Expr) -> bool {
+        if self.contains(left, right) {
+            false
+        } else {
+            self.inner.insert((left.clone(), right.clone()));
+            true
+        }
+    }
+
+    /// Inserts potentially many join keys into the set, copying only when 
necessary
+    ///
+    /// returns true if any of the pairs were inserted
+    pub fn insert_many<'a>(
+        &mut self,
+        iter: impl Iterator<Item = &'a (Expr, Expr)>,
+    ) -> bool {
+        let mut inserted = false;
+        for (left, right) in iter {
+            inserted |= self.insert(left, right);
+        }
+        inserted
+    }
+
+    /// Inserts any join keys that are common to both `s1` and `s2` into self
+    pub fn insert_intersection(&mut self, s1: JoinKeySet, s2: JoinKeySet) {
+        // note can't use inner.intersection as we need to consider both (l, r)
+        // and (r, l) in equality
+        for (left, right) in s1.inner.iter() {
+            if s2.contains(left, right) {
+                self.insert(left, right);
+            }
+        }
+    }
+
+    /// returns true if this set is empty
+    pub fn is_empty(&self) -> bool {
+        self.inner.is_empty()
+    }
+
+    /// Return the length of this set
+    #[cfg(test)]
+    pub fn len(&self) -> usize {
+        self.inner.len()
+    }
+
+    /// Return an iterator over the join keys in this set
+    pub fn iter(&self) -> impl Iterator<Item = (&Expr, &Expr)> {
+        self.inner.iter().map(|(l, r)| (l, r))

Review Comment:
   do we need this map here? 



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to