JingDas commented on code in PR #3264:
URL: https://github.com/apache/calcite/pull/3264#discussion_r1239142348


##########
core/src/main/java/org/apache/calcite/rel/metadata/RelMdForeignKeys.java:
##########
@@ -174,72 +176,88 @@ private static ImmutableBitSet 
getProjectForeignKeys(SingleRel rel, RelMetadataQ
         allOutColumns = allOutColumns.union(outColumns);
       }
     }
-    return allOutColumns.isEmpty() ? EMPTY_BIT_SET : allOutColumns;
+    return allOutColumns;
   }
 
   public ImmutableBitSet getForeignKeys(TableScan rel, RelMetadataQuery mq,
-      boolean ignoreNulls) {
+      boolean containNulls) {
     final RelOptTable table = rel.getTable();
     final BuiltInMetadata.ForeignKeys.Handler handler =
         table.unwrap(BuiltInMetadata.ForeignKeys.Handler.class);
     if (handler != null) {
-      return handler.getForeignKeys(rel, mq, ignoreNulls);
+      return handler.getForeignKeys(rel, mq, containNulls);
     }
 
     final List<RelReferentialConstraint> referentialConstraints =
         table.getReferentialConstraints();
-    if (referentialConstraints == null || referentialConstraints.size() == 0) {
+    if (referentialConstraints == null || referentialConstraints.isEmpty()) {
       return EMPTY_BIT_SET;
     }
-    final List<IntPair> foreignUniquePair = referentialConstraints.stream()
+    Set<Integer> foreignKeys = referentialConstraints.stream()
         .map(RelReferentialConstraint::getColumnPairs)
         .flatMap(Collection::stream)
-        .collect(Collectors.toList());
+        .map(pair -> {
+          return pair.source;
+        })
+        .collect(Collectors.toSet());
 
-    List<Integer> foreignKeys = IntPair.left(foreignUniquePair);
-    if (!ignoreNulls) {
+    if (!containNulls) {
       final List<RelDataTypeField> fieldList = rel.getRowType().getFieldList();
       foreignKeys = foreignKeys.stream()
           .filter(index -> !fieldList.get(index).getType().isNullable())
-          .collect(Collectors.toList());
+          .collect(Collectors.toSet());
     }
-    return foreignKeys.isEmpty() ? EMPTY_BIT_SET : 
ImmutableBitSet.of(foreignKeys);
+    return ImmutableBitSet.of(foreignKeys);
   }
 
-  public ImmutableBitSet getForeignKeys(SetOp rel, RelMetadataQuery mq,
-      boolean ignoreNulls) {
+  /**
+   * The foreign keys of Union are precisely the intersection of its every
+   * input foreign keys.
+   */
+  public ImmutableBitSet getForeignKeys(Union rel, RelMetadataQuery mq,
+      boolean containNulls) {
 
     ImmutableBitSet foreignKeys = ImmutableBitSet.of();
     for (RelNode input : rel.getInputs()) {
-      ImmutableBitSet inputForeignKeys = mq.getForeignKeys(input, ignoreNulls);
+      ImmutableBitSet inputForeignKeys = mq.getForeignKeys(input, 
containNulls);
       if (inputForeignKeys.isEmpty()) {
         return EMPTY_BIT_SET;
       }
       foreignKeys = foreignKeys.isEmpty()
           ? inputForeignKeys : foreignKeys.intersect(inputForeignKeys);
     }
-    if (foreignKeys.isEmpty()) {
-      return EMPTY_BIT_SET;
+    return foreignKeys;
+  }
+
+  /**
+   * The foreign keys of Intersect are precisely the union set of its every

Review Comment:
   OK, I get it.



-- 
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]

Reply via email to