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


##########
core/src/main/java/org/apache/calcite/rel/metadata/RelMdForeignKeys.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.
+ */
+package org.apache.calcite.rel.metadata;
+
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelReferentialConstraint;
+import org.apache.calcite.rel.SingleRel;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Calc;
+import org.apache.calcite.rel.core.Correlate;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.SetOp;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.core.TableModify;
+import org.apache.calcite.rel.core.TableScan;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexProgram;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.IntPair;
+
+import com.google.common.collect.ImmutableListMultimap;
+import com.google.common.collect.Maps;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * RelMdReferentialConstraits supplies a default implementation of
+ * {@link RelMetadataQuery#getForeignKeys} for the standard logical algebra.
+ * The relNodes supported are the same to {@link 
RelMetadataQuery#getUniqueKeys(RelNode)}
+ */
+public class RelMdForeignKeys
+    implements MetadataHandler<BuiltInMetadata.ForeignKeys> {
+  public static final ImmutableBitSet EMPTY_BIT_SET = ImmutableBitSet.of();
+  public static final RelMetadataProvider SOURCE =
+      ReflectiveRelMetadataProvider.reflectiveSource(
+          new RelMdForeignKeys(), BuiltInMetadata.ForeignKeys.Handler.class);
+
+//~ Constructors -----------------------------------------------------------
+
+  private RelMdForeignKeys() {}
+
+//~ Methods ----------------------------------------------------------------
+
+  @Override public MetadataDef<BuiltInMetadata.ForeignKeys> getDef() {
+    return BuiltInMetadata.ForeignKeys.DEF;
+  }
+
+  public ImmutableBitSet getForeignKeys(Filter rel, RelMetadataQuery mq, 
boolean ignoreNulls) {
+    return mq.getForeignKeys(rel.getInput(), ignoreNulls);
+  }
+
+  public ImmutableBitSet getForeignKeys(Sort rel, RelMetadataQuery mq, boolean 
ignoreNulls) {
+    return mq.getForeignKeys(rel.getInput(), ignoreNulls);
+  }
+
+  public ImmutableBitSet getForeignKeys(Correlate rel, RelMetadataQuery mq,
+      boolean ignoreNulls) {
+    return mq.getForeignKeys(rel.getLeft(), ignoreNulls);
+  }
+
+  public ImmutableBitSet getForeignKeys(TableModify rel, RelMetadataQuery mq,
+      boolean ignoreNulls) {
+    return mq.getForeignKeys(rel.getInput(), ignoreNulls);
+  }
+
+  public ImmutableBitSet getForeignKeys(Join rel, RelMetadataQuery mq, boolean 
ignoreNulls) {
+    final RelNode left = rel.getLeft();
+    final RelNode right = rel.getRight();
+    if (!rel.getJoinType().projectsRight()) {
+      // only return the foreign keys from the LHS since a semijoin only
+      // returns the LHS
+      return mq.getForeignKeys(left, ignoreNulls);
+    }
+
+    int nLeftColumns = rel.getLeft().getRowType().getFieldList().size();
+    ImmutableBitSet outForeignKeys = ImmutableBitSet.of();
+
+    if (!rel.getJoinType().generatesNullsOnLeft() || ignoreNulls) {
+      ImmutableBitSet leftInputForeignKeys = mq.getForeignKeys(left, 
ignoreNulls);
+      outForeignKeys = outForeignKeys.union(leftInputForeignKeys);
+    }
+    if (!rel.getJoinType().generatesNullsOnRight() || ignoreNulls) {
+      ImmutableBitSet rightInputForeignKeys = mq.getForeignKeys(right, 
ignoreNulls);
+      ImmutableBitSet.Builder rightOutForeignKeys = ImmutableBitSet.builder();
+      for (int index : rightInputForeignKeys.asList()) {
+        rightOutForeignKeys.set(index + nLeftColumns);
+      }
+      outForeignKeys = outForeignKeys.union(rightOutForeignKeys.build());
+    }
+    return outForeignKeys;
+  }
+
+  public ImmutableBitSet getForeignKeys(Aggregate rel, RelMetadataQuery mq,

Review Comment:
   ForieignKeys info are available in a table which is often the bottom part fo 
a whole relNode.
   The foreign key may be invalid when propagate derivation from the bottom to 
up.
   I think that a foreign key propagate from the bottom to up in aggregate,
   if it is in the groupset, it can maintain the validity,
   if it is in the other position except groupset, it can't maintain the 
validity, such as count(), sum() aggregate function.



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