xiedeyantu commented on code in PR #4758:
URL: https://github.com/apache/calcite/pull/4758#discussion_r2708427938
##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -121,41 +121,36 @@ protected SingleValuesRelTransformer(
if (!transformable.test(join)) {
return null;
}
- int end = valuesAsLeftChild
- ? join.getLeft().getRowType().getFieldCount()
- : join.getRowType().getFieldCount();
-
- int start = valuesAsLeftChild
- ? 0
- : join.getLeft().getRowType().getFieldCount();
- ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
- RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
- final RexNode filterCondition =
- new RexNodeReplacer(bitSet,
- literals,
- (valuesAsLeftChild ? 0 : -1) *
join.getLeft().getRowType().getFieldCount())
+ final int leftCount = join.getLeft().getRowType().getFieldCount();
+ final int rightCount = join.getRight().getRowType().getFieldCount();
+ final int start = valuesAsLeftChild ? 0 : leftCount;
+ final int end = start + (valuesAsLeftChild ? leftCount : rightCount);
+ final int offset = valuesAsLeftChild ? 0 : -leftCount;
+
+ final ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+ RexNode condition =
+ new RexNodeReplacer(bitSet, literals, offset)
.go(join.getCondition());
- RexNode fixedCondition =
- valuesAsLeftChild
- ? RexUtil.shift(filterCondition,
- -1 * join.getLeft().getRowType().getFieldCount())
- : filterCondition;
-
- List<RexNode> rexLiterals = litTransformer.apply(fixedCondition,
literals);
- relBuilder.push(relNode)
- .filter(join.getJoinType().isOuterJoin() ? trueNode :
fixedCondition);
-
- List<RexNode> rexNodes = relNode
- .getRowType()
- .getFieldList()
- .stream()
- .map(fld -> relBuilder.field(fld.getIndex()))
- .collect(Collectors.toList());
-
- List<RexNode> projects = new ArrayList<>();
- projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
- projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
Review Comment:
This code looked like it had room for optimization, so I made some
adjustments. But the fundamental problem is that the join type wasn't
considered when constructing the project. When it's a semi/anti-join, we only
need to project the left child. The current code adds both children to the
project.
--
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]