xiedeyantu commented on code in PR #4540: URL: https://github.com/apache/calcite/pull/4540#discussion_r2380608760
########## core/src/main/java/org/apache/calcite/rel/metadata/FunctionalDependencySet.java: ########## @@ -0,0 +1,403 @@ +/* + * 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 com.google.common.collect.ImmutableSet; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Set; + +/** + * A set of functional dependencies with closure and minimal cover operations. + * This class implements standard algorithms for functional dependency reasoning. + */ +public class FunctionalDependencySet { + // Maximum number of transitive closure iterations to prevent infinite loops + public static final int MAX_TRANSITIVE_CLOSURE_LOOPS = 10; + // Maximum number of attributes supported in closure computation + private static final int MAX_CLOSURE_ATTRS = 10000; + + private final Set<FunctionalDependency> fdSet = new HashSet<>(); + + public FunctionalDependencySet() {} + + public FunctionalDependencySet(Set<FunctionalDependency> fds) { + this.fdSet.addAll(fds); + } + + public void addFD(FunctionalDependency fd) { + if (!fd.isTrivial()) { + fdSet.add(fd); + } + } + + public void addFD(ImmutableBitSet determinants, ImmutableBitSet dependents) { + addFD(FunctionalDependency.of(determinants, dependents)); + } + + public void addBidirectionalFD(ImmutableBitSet determinants, ImmutableBitSet dependents) { + addFD(determinants, dependents); + addFD(dependents, determinants); + } + + public void addFD(int determinant, int dependent) { + addFD(ImmutableBitSet.of(determinant), ImmutableBitSet.of(dependent)); + } + + public void addBidirectionalFD(int determinant, int dependent) { + addFD(ImmutableBitSet.of(determinant), ImmutableBitSet.of(dependent)); + addFD(ImmutableBitSet.of(dependent), ImmutableBitSet.of(determinant)); + } + + public void removeFD(FunctionalDependency fd) { + fdSet.remove(fd); + } + + public Set<FunctionalDependency> getFDs() { + return Collections.unmodifiableSet(fdSet); + } + + /** + * Returns an ImmutableBitSet containing all attribute indexes that appear in any FD in the set. + */ + public static ImmutableBitSet allAttributesFromFDs(FunctionalDependencySet fds) { + ImmutableBitSet.Builder builder = ImmutableBitSet.builder(); + Set<FunctionalDependency> fdSet = fds.getFDs(); + for (FunctionalDependency fd : fdSet) { + builder.addAll(fd.getDeterminants()); + builder.addAll(fd.getDependents()); + } + return builder.build(); + } + + /** + * Computes the closure of a set of attributes under this functional dependency set. + * The closure of X, denoted X+, is the set of all attributes that can be functionally + * determined by X using the functional dependencies in this set and + * <a href="https://en.wikipedia.org/wiki/Armstrong%27s_axioms">Armstrong's axioms</a> + * + * @param attributes the input attribute set + * @return the closure of the input attributes + */ + public ImmutableBitSet closure(ImmutableBitSet attributes) { + if (attributes.isEmpty()) { + return ImmutableBitSet.of(); + } + + if (attributes.cardinality() > MAX_CLOSURE_ATTRS) { + throw new IllegalArgumentException( Review Comment: @silundong I have completed the modifications you mentioned. Please review it again. Thank you very much! -- 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]
