xiedeyantu commented on code in PR #4540:
URL: https://github.com/apache/calcite/pull/4540#discussion_r2354401460


##########
core/src/main/java/org/apache/calcite/rel/metadata/FunctionalDependency.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Represents a functional dependency X determines Y in relational database 
theory.
+ *
+ * <p>A functional dependency X determines Y holds in a relation R if and only 
if:
+ * for any two tuples t1 and t2 in R, if t1[X] = t2[X], then t1[Y] = t2[Y].
+ *
+ * <p>In other words, the values of attributes X uniquely determine the values
+ * of attributes Y. X is called the determinant and Y is called the dependent.
+ *
+ * <p>This class is immutable and thread-safe.
+ */
+public class FunctionalDependency {
+  private final ImmutableBitSet determinants;
+  private final ImmutableBitSet dependents;
+
+  public FunctionalDependency(ImmutableBitSet determinants, ImmutableBitSet 
dependents) {
+    this.determinants = requireNonNull(determinants, "determinants");
+    this.dependents = requireNonNull(dependents, "dependents");
+  }
+
+  /**
+   * Create FD from column indices.
+   */
+  public static FunctionalDependency of(int[] determinantColumns, int[] 
dependentColumns) {
+    return new FunctionalDependency(
+        ImmutableBitSet.of(determinantColumns),
+        ImmutableBitSet.of(dependentColumns));
+  }
+
+  /**
+   * Create FD from single determinant to single dependent.
+   */
+  public static FunctionalDependency of(int determinant, int dependent) {
+    return new FunctionalDependency(
+        ImmutableBitSet.of(determinant),
+        ImmutableBitSet.of(dependent));
+  }
+
+  /**
+   * Create FD from determinant set to dependent set.
+   */
+  public static FunctionalDependency of(ImmutableBitSet determinants, 
ImmutableBitSet dependents) {
+    return new FunctionalDependency(determinants, dependents);
+  }
+
+  public ImmutableBitSet getDeterminants() {
+    return determinants;
+  }
+
+  public ImmutableBitSet getDependents() {
+    return dependents;
+  }
+
+  /**
+   * Returns true if this FD is trivial (dependents ⊆ determinants).
+   */
+  public boolean isTrivial() {
+    return determinants.contains(dependents);
+  }
+
+  /**
+   * Split this FD into multiple FDs, each with a single dependent column.
+   */
+  public Set<FunctionalDependency> split() {

Review Comment:
   Here it is just split into a one-to-one dependency form, and `findKeys` will 
return a minimum dependency set.



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