asolimando commented on code in PR #3264:
URL: https://github.com/apache/calcite/pull/3264#discussion_r1229981772
##########
core/src/main/java/org/apache/calcite/rel/rules/ProjectJoinRemoveRule.java:
##########
@@ -78,40 +86,56 @@ public ProjectJoinRemoveRule(
final Project project = call.rel(0);
final Join join = call.rel(1);
final boolean isLeftJoin = join.getJoinType() == JoinRelType.LEFT;
- int lower = isLeftJoin
- ? join.getLeft().getRowType().getFieldCount() : 0;
- int upper = isLeftJoin
- ? join.getRowType().getFieldCount()
- : join.getLeft().getRowType().getFieldCount();
-
- // Check whether the project uses columns whose index is between
- // lower(included) and upper(excluded).
- for (RexNode expr: project.getProjects()) {
- if (RelOptUtil.InputFinder.bits(expr).asList().stream().anyMatch(
- i -> i >= lower && i < upper)) {
- return;
- }
- }
+ final boolean isRightJoin = join.getJoinType() == JoinRelType.RIGHT;
+ final boolean isInnerJoin = join.getJoinType() == JoinRelType.INNER;
- final List<Integer> leftKeys = new ArrayList<>();
- final List<Integer> rightKeys = new ArrayList<>();
- RelOptUtil.splitJoinCondition(join.getLeft(), join.getRight(),
- join.getCondition(), leftKeys, rightKeys,
- new ArrayList<>());
+ // Check project range
+ ImmutableBitSet projectBits =
RelOptUtil.InputFinder.bits(project.getProjects(), null);
+ boolean onlyUseLeft = projectBits.asList().stream()
+ .allMatch(i -> i >= 0 && i <
join.getLeft().getRowType().getFieldCount());
+ boolean onlyUseRight = projectBits.asList().stream()
+ .allMatch(i -> i >= join.getLeft().getRowType().getFieldCount()
+ && i < join.getRowType().getFieldCount());
- final List<Integer> joinKeys = isLeftJoin ? rightKeys : leftKeys;
- final ImmutableBitSet.Builder columns = ImmutableBitSet.builder();
- joinKeys.forEach(columns::set);
+ if (!onlyUseLeft && !onlyUseRight) {
+ return;
+ }
+ if (isLeftJoin && !onlyUseLeft) {
+ return;
+ }
+ if (isRightJoin && !onlyUseRight) {
+ return;
+ }
+ JoinInfo joinInfo = join.analyzeCondition();
+ final List<Integer> leftKeys = joinInfo.leftKeys;
+ final List<Integer> rightKeys = joinInfo.rightKeys;
final RelMetadataQuery mq = call.getMetadataQuery();
+
+ // For inner join, should also check foreign keys additionally
+ if (JoinRelType.INNER == join.getJoinType()) {
+ final ImmutableBitSet leftForeignKeys =
mq.getForeignKeys(join.getLeft(), false);
+ final ImmutableBitSet rightForeignKeys =
mq.getForeignKeys(join.getRight(), false);
+ if (onlyUseLeft && !areForeignKeysValid(
+ leftKeys, rightKeys, leftForeignKeys, mq, join.getLeft(),
join.getRight())) {
+ return;
+ }
+ if (onlyUseRight && !areForeignKeysValid(
+ rightKeys, leftKeys, rightForeignKeys, mq, join.getRight(),
join.getLeft())) {
+ return;
+ }
+ }
+
+ BooleanSupplier isLeftSideReserved = () -> isLeftJoin || (isInnerJoin &&
onlyUseLeft);
+ final List<Integer> joinKeys = isLeftSideReserved.getAsBoolean() ?
rightKeys : leftKeys;
if (!Boolean.TRUE.equals(
Review Comment:
```suggestion
if (Boolean.FALSE.equals(
```
--
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]