You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "rubenada (via GitHub)" <gi...@apache.org> on 2025/10/01 11:11:11 UTC

Re: [PR] [CALCITE-7199] Improve column uniqueness computation for Join [calcite]

rubenada commented on code in PR #4556:
URL: https://github.com/apache/calcite/pull/4556#discussion_r2394211999


##########
core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnUniqueness.java:
##########
@@ -334,45 +334,56 @@ public Boolean areColumnsUnique(Intersect rel, RelMetadataQuery mq,
     }
 
     // If the original column mask contains columns from both the left and
-    // right hand side, then the columns are unique if and only if they're
+    // right hand side, then the columns are unique if (but not only if) they're
     // unique for their respective join inputs
     Boolean leftUnique = mq.areColumnsUnique(left, leftColumns, ignoreNulls);
     Boolean rightUnique = mq.areColumnsUnique(right, rightColumns, ignoreNulls);
     if ((leftColumns.cardinality() > 0)
         && (rightColumns.cardinality() > 0)) {
       if ((leftUnique == null) || (rightUnique == null)) {
         return null;
-      } else {
-        return leftUnique && rightUnique;
+      } else if (leftUnique && rightUnique) {
+        return true;
       }
     }
 
-    // If we're only trying to determine uniqueness for columns that
-    // originate from one join input, then determine if the equijoin
-    // columns from the other join input are unique.  If they are, then
-    // the columns are unique for the entire join if they're unique for
-    // the corresponding join input, provided that input is not null
-    // generating.
+    // Determine if the equijoin columns from the other join input are unique.
+    // If they are, then the columns are unique for the entire join if they're unique for
+    // the corresponding join input, provided that input is not null generating.
     if (leftColumns.cardinality() > 0) {
       if (rel.getJoinType().generatesNullsOnLeft()) {
         return false;
       }
-      Boolean rightJoinColsUnique =
-          mq.areColumnsUnique(right, joinInfo.rightSet(), ignoreNulls);
-      if ((rightJoinColsUnique == null) || (leftUnique == null)) {
+      if (leftUnique == null) {
         return null;
       }
-      return rightJoinColsUnique && leftUnique;
-    } else if (rightColumns.cardinality() > 0) {
+      if (leftUnique) {
+        Boolean joinRightKeyUnique = mq.areColumnsUnique(right, joinInfo.rightSet(), ignoreNulls);
+        if (joinRightKeyUnique == null) {
+          return null;
+        }
+        if (joinRightKeyUnique) {
+          return true;
+        }
+      }
+    }
+    // Similarly, if the columns originate from the right input
+    if (rightColumns.cardinality() > 0) {
       if (rel.getJoinType().generatesNullsOnRight()) {
         return false;
       }
-      Boolean leftJoinColsUnique =
-          mq.areColumnsUnique(left, joinInfo.leftSet(), ignoreNulls);
-      if ((leftJoinColsUnique == null) || (rightUnique == null)) {
+      if (rightUnique == null) {
         return null;
       }
-      return leftJoinColsUnique && rightUnique;
+      if (rightUnique) {
+        Boolean joinRightKeyUnique = mq.areColumnsUnique(left, joinInfo.leftSet(), ignoreNulls);

Review Comment:
   done



-- 
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: commits-unsubscribe@calcite.apache.org

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