xiedeyantu commented on code in PR #4480: URL: https://github.com/apache/calcite/pull/4480#discussion_r2238669259
########## core/src/main/java/org/apache/calcite/rel/metadata/RelMdFunctionalDependency.java: ########## @@ -0,0 +1,263 @@ +/* + * 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.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.core.Calc; +import org.apache.calcite.rel.core.Correlate; +import org.apache.calcite.rel.core.Join; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.util.ImmutableBitSet; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; + +/** + * 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 determinesImpl2(rel, mq, key, column); + } + + public @Nullable Boolean determines(Join rel, RelMetadataQuery mq, + int key, int column) { + return determinesImpl2(rel, mq, key, column); + } + + public @Nullable Boolean determines(Correlate rel, RelMetadataQuery mq, + int key, int column) { + return determinesImpl2(rel, mq, key, column); + } + + public @Nullable Boolean determines(Aggregate rel, RelMetadataQuery mq, + int key, int column) { + return determinesImpl(rel, mq, key, column); + } + + public @Nullable Boolean determines(Calc rel, RelMetadataQuery mq, + int key, int column) { + return determinesImpl(rel, mq, key, column); + } + + public @Nullable Boolean determines(Project rel, RelMetadataQuery mq, + int key, int column) { + return determinesImpl(rel, mq, key, column); + } + + /** + * Checks if a column is functionally determined by a key column through expression analysis. + * + * @param rel The input relation + * @param mq Metadata query instance + * @param key Index of the determinant expression + * @param column Index of the dependent expression + * @return TRUE if column is determined by key, + * FALSE if not determined, + * NULL if undetermined + */ + private Boolean determinesImpl(RelNode rel, RelMetadataQuery mq, + int key, int column) { + if (preCheck(rel, mq, key, column)) { + return true; + } + + ImmutableBitSet keyInputIndices = null; + ImmutableBitSet columnInputIndices = null; + if (rel instanceof Project || rel instanceof Calc) { + List<RexNode> exprs = null; + if (rel instanceof Project) { + Project project = (Project) rel; + exprs = project.getProjects(); + } else { + Calc calc = (Calc) rel; + final RexProgram program = calc.getProgram(); + exprs = program.expandList(program.getProjectList()); + } + + // TODO: Supports dependency analysis for all types of expressions + if (!(exprs.get(column) instanceof RexInputRef)) { + return false; + } + + RexNode keyExpr = exprs.get(key); + RexNode columnExpr = exprs.get(column); + + // Identical expressions imply functional dependency + if (keyExpr.equals(columnExpr)) { + return true; + } + + keyInputIndices = extractDeterministicRefs(keyExpr); + columnInputIndices = extractDeterministicRefs(columnExpr); + } else if (rel instanceof Aggregate) { + Aggregate aggregate = (Aggregate) rel; + + int groupByCnt = aggregate.getGroupCount(); + if (key < groupByCnt && column >= groupByCnt) { + return false; + } + + keyInputIndices = extractDeterministicRefs(aggregate, key); + columnInputIndices = extractDeterministicRefs(aggregate, column); + } else { + throw new UnsupportedOperationException("Unsupported RelNode type: " + + rel.getClass().getSimpleName()); + } + + // Early return if invalid cases + if (keyInputIndices == null + || columnInputIndices == null + || keyInputIndices.isEmpty() + || columnInputIndices.isEmpty()) { + return false; + } + + // Verify all input dependencies + for (Integer columnRef : columnInputIndices) { + for (Integer keyRef : keyInputIndices) { + if (Boolean.FALSE.equals(mq.determines(rel.getInput(0), keyRef, columnRef))) { + return false; + } + } + } + + return true; + } + + /** + * determinesImpl2is similar to determinesImpl, but it doesn't need to handle the + * mapping between output and input columns. + */ + private @Nullable Boolean determinesImpl2(RelNode rel, RelMetadataQuery mq, + int key, int column) { + if (preCheck(rel, mq, key, column)) { + return true; + } + + if (rel instanceof TableScan) { + TableScan tableScan = (TableScan) rel; + RelOptTable table = tableScan.getTable(); + if (table.getKeys().size() == 1 + && table.getKeys().get(0).equals(ImmutableBitSet.of(column))) { + return true; + } + return false; + } else if (rel instanceof Join) { + return Boolean.TRUE.equals(mq.determines(rel.getInput(0), key, column)) + && Boolean.TRUE.equals(mq.determines(rel.getInput(1), key, column)); + } else if (rel instanceof Correlate) { + // TODO Support Correlate. + return false; + } + + return mq.determines(rel.getInput(0), key, column); Review Comment: I marked TODO and only checked the case where the column references in SETOP were equal. Other complex cases will not be handled for the time being. -- 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]
