asolimando commented on code in PR #3264: URL: https://github.com/apache/calcite/pull/3264#discussion_r1229961703
########## core/src/main/java/org/apache/calcite/rel/metadata/RelMdForeignKeys.java: ########## @@ -0,0 +1,246 @@ +/* + * 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.rel.RelNode; +import org.apache.calcite.rel.RelReferentialConstraint; +import org.apache.calcite.rel.SingleRel; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.Calc; +import org.apache.calcite.rel.core.Correlate; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Join; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Sort; +import org.apache.calcite.rel.core.TableModify; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.util.ImmutableBitSet; +import org.apache.calcite.util.Util; +import org.apache.calcite.util.mapping.IntPair; + +import com.google.common.collect.ImmutableListMultimap; +import com.google.common.collect.Maps; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * RelMdReferentialConstraits supplies a default implementation of + * {@link RelMetadataQuery#getForeignKeys} for the standard logical algebra. + * The relNodes supported are the same to {@link RelMetadataQuery#getUniqueKeys(RelNode)} + */ +public class RelMdForeignKeys + implements MetadataHandler<BuiltInMetadata.ForeignKeys> { + public static final ImmutableBitSet EMPTY_BIT_SET = ImmutableBitSet.of(); + public static final RelMetadataProvider SOURCE = + ReflectiveRelMetadataProvider.reflectiveSource( + new RelMdForeignKeys(), BuiltInMetadata.ForeignKeys.Handler.class); + +//~ Constructors ----------------------------------------------------------- + + private RelMdForeignKeys() {} + +//~ Methods ---------------------------------------------------------------- + + @Override public MetadataDef<BuiltInMetadata.ForeignKeys> getDef() { + return BuiltInMetadata.ForeignKeys.DEF; + } + + public ImmutableBitSet getForeignKeys(Filter rel, RelMetadataQuery mq, boolean ignoreNulls) { + return mq.getForeignKeys(rel.getInput(), ignoreNulls); + } + + public ImmutableBitSet getForeignKeys(Sort rel, RelMetadataQuery mq, boolean ignoreNulls) { + return mq.getForeignKeys(rel.getInput(), ignoreNulls); + } + + public ImmutableBitSet getForeignKeys(Correlate rel, RelMetadataQuery mq, + boolean ignoreNulls) { + return mq.getForeignKeys(rel.getLeft(), ignoreNulls); + } + + public ImmutableBitSet getForeignKeys(TableModify rel, RelMetadataQuery mq, + boolean ignoreNulls) { + return mq.getForeignKeys(rel.getInput(), ignoreNulls); + } + + public ImmutableBitSet getForeignKeys(Join rel, RelMetadataQuery mq, boolean ignoreNulls) { + final RelNode left = rel.getLeft(); + final RelNode right = rel.getRight(); + if (!rel.getJoinType().projectsRight()) { Review Comment: `projectRight()` is true for (left?) anti joins too, would this be ok for them as well? If so, maybe we can make the comment more generic. -- 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]
