Skip to content

Commit 4449f5a

Browse files
committed
[CALCITE-7440] Avoid ambiguous rel2sql correlation fallback
1 parent 5447f29 commit 4449f5a

File tree

1 file changed

+1
-26
lines changed

1 file changed

+1
-26
lines changed

core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,10 +1589,7 @@ public static SqlNode toSql(RexLiteral literal) {
15891589
protected Context getAliasContext(RexCorrelVariable variable) {
15901590
Context context = correlTableMap.get(variable.id);
15911591
if (context == null) {
1592-
// Some rewrites can introduce a new correlation id that still points to
1593-
// the same outer scope. Reuse a compatible known context before failing.
1594-
context = findCompatibleCorrelContext(variable.id, variable.getType().getFieldCount());
1595-
if (context == null && correlTableMap.isEmpty()) {
1592+
if (correlTableMap.isEmpty()) {
15961593
context =
15971594
aliasContext(ImmutableMap.of(variable.id.getName(), variable.getType()), true);
15981595
}
@@ -1603,28 +1600,6 @@ protected Context getAliasContext(RexCorrelVariable variable) {
16031600
return requireNonNull(context, () -> "variable " + variable.id + " is not found");
16041601
}
16051602

1606-
private @Nullable Context findCompatibleCorrelContext(CorrelationId missingId,
1607-
int expectedFieldCount) {
1608-
Context fallback = null;
1609-
String fallbackName = null;
1610-
for (Map.Entry<CorrelationId, Context> entry : correlTableMap.entrySet()) {
1611-
final CorrelationId id = entry.getKey();
1612-
final Context context = entry.getValue();
1613-
if (id.equals(missingId)) {
1614-
continue;
1615-
}
1616-
if (context.fieldCount == expectedFieldCount) {
1617-
return context;
1618-
}
1619-
final String name = id.getName();
1620-
if (fallbackName == null || name.compareTo(fallbackName) < 0) {
1621-
fallbackName = name;
1622-
fallback = context;
1623-
}
1624-
}
1625-
return fallback;
1626-
}
1627-
16281603
/** Simple implementation of {@link Context} that cannot handle sub-queries
16291604
* or correlations. Because it is so simple, you do not need to create a
16301605
* {@link SqlImplementor} or {@link org.apache.calcite.tools.RelBuilder}

0 commit comments

Comments
 (0)