suibianwanwank commented on code in PR #4382:
URL: https://github.com/apache/calcite/pull/4382#discussion_r2108556953
##########
core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java:
##########
@@ -960,13 +1001,45 @@ private static void matchJoin(SubQueryRemoveRule rule,
RelOptRuleCall call) {
.union(ImmutableBitSet.range(nFields - nFieldsRight, nFields)));
builder.project(fields);
} else {
- builder.push(join.getLeft());
builder.push(join.getRight());
+ final RelOptUtil.Logic logic =
+ LogicVisitor.find(join.getJoinType().generatesNullsOnLeft()
+ ? RelOptUtil.Logic.TRUE_FALSE_UNKNOWN :
RelOptUtil.Logic.TRUE,
+ ImmutableList.of(join.getCondition()), e);
+
+ RexSubQuery subQuery = e;
+
+ if (!variablesSet.isEmpty()) {
+ // Original correlates reference joint row type, but we are about to
create
+ // new join of original right side and correlated sub-query. Therefore
we have
+ // to adjust correlated variables int following way:
+ // 1) new correlation variable must reference row type of right
side only
+ // 2) field index must be shifted on the size of the left side
+ CorrelationId id = Iterables.getOnlyElement(variablesSet);
+ RexBuilder rexBuilder = builder.getRexBuilder();
+
+ RelNode newSubQueryRel = e.rel.accept(new RelHomogeneousShuttle() {
+ @Override public RelNode visit(RelNode other) {
Review Comment:
Same as the comment in `replaceCorrelationId`.
##########
core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java:
##########
@@ -840,6 +857,29 @@ private static List<RexNode> fields(RelBuilder builder,
int fieldCount) {
return projects;
}
+ /**
+ * Replace every {@link RexFieldAccess} in the given {@link RelNode}
+ * that reference the specified {@link CorrelationId} with another {@link
CorrelationId}.
+ */
+ public static RelNode replaceCorrelationId(RexBuilder rexBuilder, RelNode
node,
+ final CorrelationId from, final CorrelationId to, RelDataType type) {
+ return node.accept(new RelHomogeneousShuttle() {
Review Comment:
rexsubquery extends rexcall, operands will be visited sequentially. I
believe it supports recursion. Would you like me to add a test?
##########
core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java:
##########
@@ -725,10 +731,21 @@ private static RexNode rewriteIn(RexSubQuery e,
Set<CorrelationId> variablesSet,
builder.aggregate(builder.groupKey(),
builder.count(false, "c"),
builder.count(builder.fields()).as("ck"));
- builder.as(ctAlias);
+
if (!variablesSet.isEmpty()) {
- builder.join(JoinRelType.LEFT, trueLiteral, variablesSet);
+ // The same variablesSet will generate a Correlate with the
corresponding CorrelationId,
Review Comment:
Thanks! I learned something new!
##########
core/src/main/java/org/apache/calcite/rex/RexUtil.java:
##########
@@ -1867,6 +1869,29 @@ public static RexNode shift(RexNode node, final int
start, final int offset) {
});
}
+ /**
+ * Shifts every {@link RexFieldAccess} with {@link CorrelationId}
+ * in an {@link RelNode} by {@code offset}.
+ */
+ public static RelNode shiftFieldAccess(RexBuilder rexBuilder, RelNode node,
+ final CorrelationId id, RelNode outer, final int offset) {
Review Comment:
The construction of CorrelationId is public, and the Id type is int. We
cannot assume users won't use negative numbers. Therefore, I don't recommend
implementing validation here.
--
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]