com.hazelcast.org.apache.calcite.rel.core.Correlate Maven / Gradle / Ivy
/*
* 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 com.hazelcast.com.liance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.hazelcast.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.core;
import com.hazelcast.org.apache.calcite.plan.RelOptCluster;
import com.hazelcast.org.apache.calcite.plan.RelOptCost;
import com.hazelcast.org.apache.calcite.plan.RelOptPlanner;
import com.hazelcast.org.apache.calcite.plan.RelOptUtil;
import com.hazelcast.org.apache.calcite.plan.RelTraitSet;
import com.hazelcast.org.apache.calcite.rel.BiRel;
import com.hazelcast.org.apache.calcite.rel.RelInput;
import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.rel.RelWriter;
import com.hazelcast.org.apache.calcite.rel.metadata.RelMetadataQuery;
import com.hazelcast.org.apache.calcite.rel.type.RelDataType;
import com.hazelcast.org.apache.calcite.sql.validate.SqlValidatorUtil;
import com.hazelcast.org.apache.calcite.util.ImmutableBitSet;
import com.hazelcast.org.apache.calcite.util.Litmus;
import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableList;
import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* A relational operator that performs nested-loop joins.
*
* It behaves like a kind of {@link com.hazelcast.org.apache.calcite.rel.core.Join},
* but works by setting variables in its environment and restarting its
* right-hand input.
*
*
Correlate is not a join since: typical rules should not match Correlate.
*
*
A Correlate is used to represent a correlated query. One
* implementation strategy is to de-correlate the expression.
*
*
* Mapping of physical operations to logical ones
* Physical operation Logical operation
* NestedLoops Correlate(A, B, regular)
* NestedLoopsOuter Correlate(A, B, outer)
* NestedLoopsSemi Correlate(A, B, semi)
* NestedLoopsAnti Correlate(A, B, anti)
* HashJoin EquiJoin(A, B)
* HashJoinOuter EquiJoin(A, B, outer)
* HashJoinSemi SemiJoin(A, B, semi)
* HashJoinAnti SemiJoin(A, B, anti)
*
*
* @see CorrelationId
*/
public abstract class Correlate extends BiRel {
//~ Instance fields --------------------------------------------------------
protected final CorrelationId correlationId;
protected final ImmutableBitSet requiredColumns;
protected final JoinRelType joinType;
//~ Constructors -----------------------------------------------------------
/**
* Creates a Correlate.
*
* @param cluster Cluster this relational expression belongs to
* @param left Left input relational expression
* @param right Right input relational expression
* @param correlationId Variable name for the row of left input
* @param requiredColumns Set of columns that are used by correlation
* @param joinType Join type
*/
protected Correlate(
RelOptCluster cluster,
RelTraitSet traitSet,
RelNode left,
RelNode right,
CorrelationId correlationId,
ImmutableBitSet requiredColumns,
JoinRelType joinType) {
super(cluster, traitSet, left, right);
assert !joinType.generatesNullsOnLeft() : "Correlate has invalid join type " + joinType;
this.joinType = Objects.requireNonNull(joinType);
this.correlationId = Objects.requireNonNull(correlationId);
this.requiredColumns = Objects.requireNonNull(requiredColumns);
}
/**
* Creates a Correlate by parsing serialized output.
*
* @param input Input representation
*/
public Correlate(RelInput input) {
this(
input.getCluster(), input.getTraitSet(), input.getInputs().get(0),
input.getInputs().get(1),
new CorrelationId((Integer) input.get("correlation")),
input.getBitSet("requiredColumns"),
input.getEnum("joinType", JoinRelType.class));
}
//~ Methods ----------------------------------------------------------------
@Override public boolean isValid(Litmus litmus, Context context) {
return super.isValid(litmus, context)
&& RelOptUtil.notContainsCorrelation(left, correlationId, litmus);
}
@Override public Correlate copy(RelTraitSet traitSet, List inputs) {
assert inputs.size() == 2;
return copy(traitSet,
inputs.get(0),
inputs.get(1),
correlationId,
requiredColumns,
joinType);
}
public abstract Correlate copy(RelTraitSet traitSet,
RelNode left, RelNode right, CorrelationId correlationId,
ImmutableBitSet requiredColumns, JoinRelType joinType);
public JoinRelType getJoinType() {
return joinType;
}
@Override protected RelDataType deriveRowType() {
switch (joinType) {
case LEFT:
case INNER:
return SqlValidatorUtil.deriveJoinRowType(left.getRowType(),
right.getRowType(), joinType,
getCluster().getTypeFactory(), null,
ImmutableList.of());
case ANTI:
case SEMI:
return left.getRowType();
default:
throw new IllegalStateException("Unknown join type " + joinType);
}
}
@Override public RelWriter explainTerms(RelWriter pw) {
return super.explainTerms(pw)
.item("correlation", correlationId)
.item("joinType", joinType.lowerName)
.item("requiredColumns", requiredColumns);
}
/**
* Returns the correlating expressions.
*
* @return correlating expressions
*/
public CorrelationId getCorrelationId() {
return correlationId;
}
@Override public String getCorrelVariable() {
return correlationId.getName();
}
/**
* Returns the required columns in left relation required for the correlation
* in the right.
*
* @return columns in left relation required for the correlation in the right
*/
public ImmutableBitSet getRequiredColumns() {
return requiredColumns;
}
@Override public Set getVariablesSet() {
return ImmutableSet.of(correlationId);
}
@Override public double estimateRowCount(RelMetadataQuery mq) {
double leftRowCount = mq.getRowCount(left);
switch (joinType) {
case SEMI:
case ANTI:
return leftRowCount;
default:
return leftRowCount * mq.getRowCount(right);
}
}
@Override public RelOptCost com.hazelcast.com.uteSelfCost(RelOptPlanner planner,
RelMetadataQuery mq) {
double rowCount = mq.getRowCount(this);
final double rightRowCount = right.estimateRowCount(mq);
final double leftRowCount = left.estimateRowCount(mq);
if (Double.isInfinite(leftRowCount) || Double.isInfinite(rightRowCount)) {
return planner.getCostFactory().makeInfiniteCost();
}
Double restartCount = mq.getRowCount(getLeft());
// RelMetadataQuery.getCumulativeCost(getRight()); does not work for
// RelSubset, so we ask planner to cost-estimate right relation
RelOptCost rightCost = planner.getCost(getRight(), mq);
RelOptCost rescanCost =
rightCost.multiplyBy(Math.max(1.0, restartCount - 1));
return planner.getCostFactory().makeCost(
rowCount /* generate results */ + leftRowCount /* scan left results */,
0, 0).plus(rescanCost);
}
}