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


##########
core/src/main/java/org/apache/calcite/rel/metadata/RelMdFunctionalDependency.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.rel.RelNode;
+import org.apache.calcite.rel.SingleRel;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.SetOp;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Default implementation of
+ * {@link RelMetadataQuery#determines(RelNode, int, int)}
+ * for the standard logical algebra.
+ *
+ * <p>The goal of this provider is to determine whether
+ * key is functionally dependent on column.
+ *
+ * <p>If the functional dependency cannot be determined, we return false.
+ */
+public class RelMdFunctionalDependency
+    implements MetadataHandler<BuiltInMetadata.FunctionalDependency> {
+  public static final RelMetadataProvider SOURCE =
+      ReflectiveRelMetadataProvider.reflectiveSource(
+          new RelMdFunctionalDependency(), 
BuiltInMetadata.FunctionalDependency.Handler.class);
+
+  //~ Constructors -----------------------------------------------------------
+
+  protected RelMdFunctionalDependency() {}
+
+  //~ Methods ----------------------------------------------------------------
+
+  @Override public MetadataDef<BuiltInMetadata.FunctionalDependency> getDef() {
+    return BuiltInMetadata.FunctionalDependency.DEF;
+  }
+
+  public @Nullable Boolean determines(RelNode rel, RelMetadataQuery mq,
+      int key, int column) {
+    return preCheck(rel, mq, key, column);
+  }
+
+  public @Nullable Boolean determines(SingleRel rel, RelMetadataQuery mq,
+      int key, int column) {
+    if (preCheck(rel, mq, key, column)) {
+      return true;
+    }
+    return mq.determines(rel.getInput(), key, column);
+  }
+
+  public @Nullable Boolean determines(SetOp rel, RelMetadataQuery mq,
+      int key, int column) {
+    if (preCheck(rel, mq, key, column)) {
+      return true;
+    }
+    return mq.determines(rel.getInput(0), key, column);
+  }
+
+  public @Nullable Boolean determines(Project rel, RelMetadataQuery mq,
+      int key, int column) {
+    if (preCheck(rel, mq, key, column)) {
+      return true;
+    }
+
+    List<RexNode> projects = rel.getProjects();
+    RexNode keyExpr = projects.get(key);
+    RexNode columnExpr = projects.get(column);
+    // Identical expressions imply deterministic dependency
+    if (keyExpr.equals(columnExpr)) {
+      return true;
+    }
+
+    // Maps expression indices to their corresponding inputs
+    Set<Integer> keyInputIndices = extractDeterministicRefs(keyExpr);
+    Set<Integer> columnInputIndices = extractDeterministicRefs(columnExpr);

Review Comment:
   Thanks, that's the problem, I will fix 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