All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.org.apache.calcite.rel.metadata.RelMdTableReferences Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * 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 com.hazelcast.org.apache.calcite.rel.metadata;

import com.hazelcast.org.apache.calcite.plan.volcano.RelSubset;
import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.rel.core.Aggregate;
import com.hazelcast.org.apache.calcite.rel.core.Calc;
import com.hazelcast.org.apache.calcite.rel.core.Exchange;
import com.hazelcast.org.apache.calcite.rel.core.Filter;
import com.hazelcast.org.apache.calcite.rel.core.Join;
import com.hazelcast.org.apache.calcite.rel.core.Project;
import com.hazelcast.org.apache.calcite.rel.core.Sample;
import com.hazelcast.org.apache.calcite.rel.core.SetOp;
import com.hazelcast.org.apache.calcite.rel.core.Sort;
import com.hazelcast.org.apache.calcite.rel.core.TableModify;
import com.hazelcast.org.apache.calcite.rel.core.TableScan;
import com.hazelcast.org.apache.calcite.rel.core.Window;
import com.hazelcast.org.apache.calcite.rex.RexTableInputRef.RelTableRef;
import com.hazelcast.org.apache.calcite.util.Util;

import com.hazelcast.com.google.common.collect.HashMultimap;
import com.hazelcast.com.google.common.collect.ImmutableSet;
import com.hazelcast.com.google.common.collect.Multimap;

import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Default implementation of {@link RelMetadataQuery#getTableReferences} for the
 * standard logical algebra.
 *
 * 

The goal of this provider is to return all tables used by a given * expression identified uniquely by a {@link RelTableRef}. * *

Each unique identifier {@link RelTableRef} of a table will equal to the * identifier obtained running {@link RelMdExpressionLineage} over the same plan * node for an expression that refers to the same table. * *

If tables cannot be obtained, we return null. */ public class RelMdTableReferences implements MetadataHandler { public static final RelMetadataProvider SOURCE = ReflectiveRelMetadataProvider.reflectiveSource( new RelMdTableReferences(), BuiltInMetadata.TableReferences.Handler.class); //~ Constructors ----------------------------------------------------------- protected RelMdTableReferences() {} //~ Methods ---------------------------------------------------------------- @Override public MetadataDef getDef() { return BuiltInMetadata.TableReferences.DEF; } // Catch-all rule when none of the others apply. public @Nullable Set getTableReferences(RelNode rel, RelMetadataQuery mq) { return null; } public @Nullable Set getTableReferences(RelSubset rel, RelMetadataQuery mq) { RelNode bestOrOriginal = Util.first(rel.getBest(), rel.getOriginal()); if (bestOrOriginal == null) { return null; } return mq.getTableReferences(bestOrOriginal); } /** * TableScan table reference. */ public Set getTableReferences(TableScan rel, RelMetadataQuery mq) { return ImmutableSet.of(RelTableRef.of(rel.getTable(), 0)); } /** * Table references from Aggregate. */ public @Nullable Set getTableReferences(Aggregate rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from Join. */ public @Nullable Set getTableReferences(Join rel, RelMetadataQuery mq) { final RelNode leftInput = rel.getLeft(); final RelNode rightInput = rel.getRight(); final Set result = new HashSet<>(); // Gather table references, left input references remain unchanged final Multimap, RelTableRef> leftQualifiedNamesToRefs = HashMultimap.create(); final Set leftTableRefs = mq.getTableReferences(leftInput); if (leftTableRefs == null) { // We could not infer the table refs from left input return null; } for (RelTableRef leftRef : leftTableRefs) { assert !result.contains(leftRef); result.add(leftRef); leftQualifiedNamesToRefs.put(leftRef.getQualifiedName(), leftRef); } // Gather table references, right input references might need to be // updated if there are table names clashes with left input final Set rightTableRefs = mq.getTableReferences(rightInput); if (rightTableRefs == null) { // We could not infer the table refs from right input return null; } for (RelTableRef rightRef : rightTableRefs) { int shift = 0; Collection lRefs = leftQualifiedNamesToRefs.get(rightRef.getQualifiedName()); if (lRefs != null) { shift = lRefs.size(); } RelTableRef shiftTableRef = RelTableRef.of( rightRef.getTable(), shift + rightRef.getEntityNumber()); assert !result.contains(shiftTableRef); result.add(shiftTableRef); } // Return result return result; } /** * Table references from Union, Intersect, Minus. * *

For Union operator, we might be able to extract multiple table * references. */ public @Nullable Set getTableReferences(SetOp rel, RelMetadataQuery mq) { final Set result = new HashSet<>(); // Infer column origin expressions for given references final Multimap, RelTableRef> qualifiedNamesToRefs = HashMultimap.create(); for (RelNode input : rel.getInputs()) { final Map currentTablesMapping = new HashMap<>(); final Set inputTableRefs = mq.getTableReferences(input); if (inputTableRefs == null) { // We could not infer the table refs from input return null; } for (RelTableRef tableRef : inputTableRefs) { int shift = 0; Collection lRefs = qualifiedNamesToRefs.get( tableRef.getQualifiedName()); if (lRefs != null) { shift = lRefs.size(); } RelTableRef shiftTableRef = RelTableRef.of( tableRef.getTable(), shift + tableRef.getEntityNumber()); assert !result.contains(shiftTableRef); result.add(shiftTableRef); currentTablesMapping.put(tableRef, shiftTableRef); } // Add to existing qualified names for (RelTableRef newRef : currentTablesMapping.values()) { qualifiedNamesToRefs.put(newRef.getQualifiedName(), newRef); } } // Return result return result; } /** * Table references from Project. */ public @Nullable Set getTableReferences(Project rel, final RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from Filter. */ public @Nullable Set getTableReferences(Filter rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from Calc. */ public @Nullable Set getTableReferences(Calc rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from Sort. */ public @Nullable Set getTableReferences(Sort rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from TableModify. */ public @Nullable Set getTableReferences(TableModify rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from Exchange. */ public @Nullable Set getTableReferences(Exchange rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from Window. */ public @Nullable Set getTableReferences(Window rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } /** * Table references from Sample. */ public @Nullable Set getTableReferences(Sample rel, RelMetadataQuery mq) { return mq.getTableReferences(rel.getInput()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy