LakeShen commented on code in PR #3345:
URL: https://github.com/apache/calcite/pull/3345#discussion_r1285776926


##########
core/src/main/java/org/apache/calcite/plan/RelOptForeignKey.java:
##########
@@ -0,0 +1,411 @@
+/*
+ * 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.plan;
+
+import org.apache.calcite.linq4j.Linq4j;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.InferredConstraintKey;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Pair;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * ForeignKey represents the foreign and unique key constraint relationship
+ * on the current {@link org.apache.calcite.rel.RelNode}.
+ *
+ * <p><b>constraints</b> field {@link #constraints} are
+ * constraints that foreign key and unique key relationships in bottom-up 
derivation.
+ *
+ * <p><b>foreignColumns</b> field {@link #foreignColumns} indicates the
+ * position of the foreign key on the current {@link 
org.apache.calcite.rel.RelNode}
+ * if not or not be confirmed, it is an empty set.
+ *
+ * <p><b>uniqueColumns</b> field {@link #uniqueColumns} indicates the position 
of
+ * the unique key on the current {@link org.apache.calcite.rel.RelNode},
+ * if not or not be confirmed, it is an empty set, the position in 
uniqueColumns
+ * corresponds to foreignColumns.
+ *
+ * <p>The element positions in {@code uniqueColumns} and {@code foreignColumns}
+ * correspond to each other. The order of elements in {@code uniqueColumns}
+ * and {@code foreignColumns} is consistent with the order of constraints in
+ * the constraints list.
+ *
+ * <p>For instance,
+ * <blockquote>
+ * <pre>select e.deptno, e.ename, d.deptno
+ * from emp as e
+ * inner join dept as d
+ * on e.deptno = d.deptno</pre></blockquote>
+ *
+ * <p>the foreign key is the DEPTNO column of CATALOG.SALES.EMP table,
+ * reference the DEPTNO unique column of CATALOG.SALES.DEPT table.
+ *
+ * <p>Invoke the {@link RelMetadataQuery#getConfirmedForeignKeys} method which
+ * input param is the top {@link org.apache.calcite.rel.core.Project},
+ * the following results can be obtained.
+ *
+ * <p>{@code constraints} is
+ * [{left: [CATALOG, SALES, EMP].$7.#true, right: [CATALOG, SALES, 
DEPT].$0.#true}]
+ * {@code foreignColumns} is {0}
+ * {@code uniqueColumns} is {2}
+ *
+ * <p>For instance,
+ * <blockquote>
+ * <pre>select name, deptno
+ * from dept</pre></blockquote>
+ *
+ * <p>{@code constraints} is
+ * [{left: null.null.#false, right: [CATALOG, SALES, DEPT].$0.#false}]
+ * {@code foreignColumns} is {}
+ * {@code uniqueColumns} is {1}
+ *
+ * <p>For instance,
+ * <blockquote>
+ * <pre>select ename, sal, deptno
+ * from emp</pre></blockquote>
+ *
+ * <p>{@code constraints} is
+ * [{left: [CATALOG, SALES, EMP].$7.#false, right: [CATALOG, SALES, 
DEPT].$0.#false}]
+ * {@code foreignColumns} is {2}
+ * {@code uniqueColumns} is {}
+ *
+ * @see InferredConstraintKey
+ * @see org.apache.calcite.plan.RelOptForeignKey
+ */
+public class RelOptForeignKey {
+
+  /** Foreign key and unique key relationships in bottom-up derivation. */
+  private final List<Pair<InferredConstraintKey, InferredConstraintKey>> 
constraints;
+  /** Position of the foreign key on the current {@link 
org.apache.calcite.rel.RelNode}. */
+  private final ImmutableBitSet foreignColumns;
+  /** Position of the unique key on the current {@link 
org.apache.calcite.rel.RelNode}. */
+  private final ImmutableBitSet uniqueColumns;
+
+  private RelOptForeignKey(
+      List<Pair<InferredConstraintKey, InferredConstraintKey>> constraints,
+      ImmutableBitSet foreignColumns,
+      ImmutableBitSet uniqueColumns) {
+    this.constraints = constraints;
+    this.foreignColumns = foreignColumns;
+    this.uniqueColumns = uniqueColumns;
+  }
+
+  public static RelOptForeignKey of(List<Pair<InferredConstraintKey,
+      InferredConstraintKey>> constraints,
+      ImmutableBitSet foreignColumns,
+      ImmutableBitSet uniqueColumns) {
+    return new RelOptForeignKey(constraints, foreignColumns, uniqueColumns);
+  }
+
+  public ImmutableBitSet getForeignColumns() {
+    return foreignColumns;
+  }
+
+  public ImmutableBitSet getUniqueColumns() {
+    return uniqueColumns;
+  }
+
+  public List<Pair<InferredConstraintKey, InferredConstraintKey>> 
getConstraints() {
+    return constraints;
+  }
+
+  /** Returns the left side of a list of constraints. */

Review Comment:
   What is meaning of left side,I think It should be described with clearer 
comments



-- 
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-unsubscr...@calcite.apache.org

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

Reply via email to