Skip to content

Commit 2591b15

Browse files
authored
[BugFix] Fix the join condition resolving bug introduced by IN subquery impl (#3377)
1 parent 6668aa8 commit 2591b15

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

core/src/main/java/org/opensearch/sql/calcite/CalcitePlanContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.sql.Connection;
1111
import java.util.function.BiFunction;
1212
import lombok.Getter;
13+
import lombok.Setter;
1314
import org.apache.calcite.rex.RexNode;
1415
import org.apache.calcite.tools.FrameworkConfig;
1516
import org.apache.calcite.tools.RelBuilder;
@@ -23,7 +24,7 @@ public class CalcitePlanContext {
2324
public final RelBuilder relBuilder;
2425
public final ExtendedRexBuilder rexBuilder;
2526

26-
@Getter private boolean isResolvingJoinCondition = false;
27+
@Getter @Setter private boolean isResolvingJoinCondition = false;
2728

2829
private CalcitePlanContext(FrameworkConfig config) {
2930
this.config = config;

core/src/main/java/org/opensearch/sql/calcite/CalciteRexNodeVisitor.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,12 @@ public RexNode visitQualifiedName(QualifiedName node, CalcitePlanContext context
161161
if (parts.size() == 1) {
162162
// Handle the case of `id = cid`
163163
try {
164-
// TODO what if there is join clause in InSubquery in join condition
165-
// for subquery in join condition
166-
return context.relBuilder.field(parts.get(0));
167-
} catch (IllegalArgumentException e) {
168-
try {
169-
return context.relBuilder.field(2, 0, parts.get(0));
170-
} catch (IllegalArgumentException ee) {
171-
return context.relBuilder.field(2, 1, parts.get(0));
172-
}
164+
return context.relBuilder.field(2, 0, parts.getFirst());
165+
} catch (IllegalArgumentException ee) {
166+
return context.relBuilder.field(2, 1, parts.getFirst());
173167
}
174-
} else if (parts.size()
175-
== 2) { // Handle the case of `t1.id = t2.id` or `alias1.id = alias2.id`
168+
} else if (parts.size() == 2) {
169+
// Handle the case of `t1.id = t2.id` or `alias1.id = alias2.id`
176170
return context.relBuilder.field(2, parts.get(0), parts.get(1));
177171
} else if (parts.size() == 3) {
178172
throw new UnsupportedOperationException("Unsupported qualified name: " + node);
@@ -184,7 +178,7 @@ public RexNode visitQualifiedName(QualifiedName node, CalcitePlanContext context
184178
return context.relBuilder.field(qualifiedName);
185179
} else if (node.getParts().size() == 2) {
186180
List<String> parts = node.getParts();
187-
return context.relBuilder.field(1, parts.get(0), parts.get(1));
181+
return context.relBuilder.field(parts.get(0), parts.get(1));
188182
} else if (currentFields.stream().noneMatch(f -> f.startsWith(qualifiedName))) {
189183
return context.relBuilder.field(qualifiedName);
190184
}
@@ -262,9 +256,20 @@ public RexNode visitFunction(Function node, CalcitePlanContext context) {
262256
@Override
263257
public RexNode visitInSubquery(InSubquery node, CalcitePlanContext context) {
264258
List<RexNode> nodes = node.getChild().stream().map(child -> analyze(child, context)).toList();
259+
// clear and store the outer state
260+
boolean isResolvingJoinConditionOuter = context.isResolvingJoinCondition();
261+
if (isResolvingJoinConditionOuter) {
262+
context.setResolvingJoinCondition(false);
263+
}
265264
UnresolvedPlan subquery = node.getQuery();
265+
266266
RelNode subqueryRel = subquery.accept(planVisitor, context);
267+
// pop the inner plan
267268
context.relBuilder.build();
269+
// restore to the previous state
270+
if (isResolvingJoinConditionOuter) {
271+
context.setResolvingJoinCondition(true);
272+
}
268273
try {
269274
return context.relBuilder.in(subqueryRel, nodes);
270275
// TODO

0 commit comments

Comments
 (0)