forked from apache/calcite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-6236] EnumerableBatchNestedLoopJoin::estimateRowCount return…
…s wrong value
- Loading branch information
Showing
4 changed files
with
53 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import org.apache.calcite.rel.core.Join; | ||
import org.apache.calcite.rel.core.JoinRelType; | ||
import org.apache.calcite.rel.metadata.RelMdCollation; | ||
import org.apache.calcite.rel.metadata.RelMdUtil; | ||
import org.apache.calcite.rel.metadata.RelMetadataQuery; | ||
import org.apache.calcite.rex.RexNode; | ||
import org.apache.calcite.util.BuiltInMethod; | ||
|
@@ -55,6 +56,7 @@ | |
public class EnumerableBatchNestedLoopJoin extends Join implements EnumerableRel { | ||
|
||
private final ImmutableBitSet requiredColumns; | ||
private final double rightSideFilterSelectivity; | ||
protected EnumerableBatchNestedLoopJoin( | ||
RelOptCluster cluster, | ||
RelTraitSet traits, | ||
|
@@ -63,9 +65,11 @@ protected EnumerableBatchNestedLoopJoin( | |
RexNode condition, | ||
Set<CorrelationId> variablesSet, | ||
ImmutableBitSet requiredColumns, | ||
JoinRelType joinType) { | ||
JoinRelType joinType, | ||
double rightSideFilterSelectivity) { | ||
super(cluster, traits, ImmutableList.of(), left, right, condition, variablesSet, joinType); | ||
this.requiredColumns = requiredColumns; | ||
this.rightSideFilterSelectivity = rightSideFilterSelectivity; | ||
} | ||
|
||
public static EnumerableBatchNestedLoopJoin create( | ||
|
@@ -74,7 +78,8 @@ public static EnumerableBatchNestedLoopJoin create( | |
RexNode condition, | ||
ImmutableBitSet requiredColumns, | ||
Set<CorrelationId> variablesSet, | ||
JoinRelType joinType) { | ||
JoinRelType joinType, | ||
double rightSideFilterSelectivity) { | ||
final RelOptCluster cluster = left.getCluster(); | ||
final RelMetadataQuery mq = cluster.getMetadataQuery(); | ||
final RelTraitSet traitSet = | ||
|
@@ -89,7 +94,19 @@ public static EnumerableBatchNestedLoopJoin create( | |
condition, | ||
variablesSet, | ||
requiredColumns, | ||
joinType); | ||
joinType, | ||
rightSideFilterSelectivity); | ||
} | ||
|
||
@Deprecated | ||
public static EnumerableBatchNestedLoopJoin create( | ||
RelNode left, | ||
RelNode right, | ||
RexNode condition, | ||
ImmutableBitSet requiredColumns, | ||
Set<CorrelationId> variablesSet, | ||
JoinRelType joinType) { | ||
return create(left, right, condition, requiredColumns, variablesSet, joinType, 1.0); | ||
} | ||
|
||
@Override public @Nullable Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits( | ||
|
@@ -115,14 +132,14 @@ public static EnumerableBatchNestedLoopJoin create( | |
@Override public EnumerableBatchNestedLoopJoin copy(RelTraitSet traitSet, | ||
RexNode condition, RelNode left, RelNode right, JoinRelType joinType, | ||
boolean semiJoinDone) { | ||
return new EnumerableBatchNestedLoopJoin(getCluster(), traitSet, | ||
left, right, condition, variablesSet, requiredColumns, joinType); | ||
return new EnumerableBatchNestedLoopJoin(getCluster(), traitSet, left, right, condition, | ||
variablesSet, requiredColumns, joinType, rightSideFilterSelectivity); | ||
} | ||
|
||
@Override public @Nullable RelOptCost computeSelfCost( | ||
final RelOptPlanner planner, | ||
final RelMetadataQuery mq) { | ||
double rowCount = mq.getRowCount(this); | ||
double rowCount = estimateRowCount(mq); | ||
|
||
final double rightRowCount = right.estimateRowCount(mq); | ||
final double leftRowCount = left.estimateRowCount(mq); | ||
|
@@ -144,6 +161,18 @@ public static EnumerableBatchNestedLoopJoin create( | |
rowCount + leftRowCount, 0, 0).plus(rescanCost); | ||
} | ||
|
||
@Override public double estimateRowCount(RelMetadataQuery mq) { | ||
return unwrapDouble(RelMdUtil.getJoinRowCount(mq, this, condition, | ||
Check failure on line 165 in core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoin.java GitHub Actions / CheckerFramework (JDK 11), oldest Guava
Check failure on line 165 in core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoin.java GitHub Actions / CheckerFramework (JDK 11)
|
||
unwrapDouble(mq.getRowCount(getRight())) / rightSideFilterSelectivity)); | ||
} | ||
|
||
static double unwrapDouble(Double value) { | ||
if (value == null) { | ||
return Double.POSITIVE_INFINITY; | ||
} | ||
return value.doubleValue(); | ||
} | ||
|
||
@Override public RelWriter explainTerms(RelWriter pw) { | ||
super.explainTerms(pw); | ||
return pw.item("batchSize", variablesSet.size()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,8 @@ | |
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.apache.calcite.adapter.enumerable.EnumerableBatchNestedLoopJoin.unwrapDouble; | ||
|
||
/** Rule to convert a {@link LogicalJoin} to an {@link EnumerableBatchNestedLoopJoin}. | ||
* You may provide a custom config to convert other nodes that extend {@link Join}. | ||
* | ||
|
@@ -134,9 +136,14 @@ public EnumerableBatchNestedLoopJoinRule(RelBuilderFactory relBuilderFactory, | |
conditionList.add(condition2); | ||
} | ||
|
||
RexNode filterCondition = relBuilder.or(conditionList); | ||
|
||
// Push a filter with batchSize disjunctions | ||
relBuilder.push(join.getRight()).filter(relBuilder.or(conditionList)); | ||
relBuilder.push(join.getRight()).filter(filterCondition); | ||
final RelNode right = relBuilder.build(); | ||
final double filterSelectivity = right.getInputs().size() == 1 | ||
? unwrapDouble(call.getMetadataQuery().getSelectivity(right.getInput(0), filterCondition)) | ||
Check failure on line 145 in core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoinRule.java GitHub Actions / CheckerFramework (JDK 11), oldest Guava
Check failure on line 145 in core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBatchNestedLoopJoinRule.java GitHub Actions / CheckerFramework (JDK 11)
|
||
: 1.0; | ||
|
||
call.transformTo( | ||
EnumerableBatchNestedLoopJoin.create( | ||
|
@@ -147,7 +154,8 @@ public EnumerableBatchNestedLoopJoinRule(RelBuilderFactory relBuilderFactory, | |
join.getCondition(), | ||
requiredColumns.build(), | ||
correlationIds, | ||
join.getJoinType())); | ||
join.getJoinType(), | ||
filterSelectivity)); | ||
} | ||
|
||
/** Rule configuration. */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters