Skip to content

Commit e8e0dd5

Browse files
committed
[CALCITE-7501] Assertion error in alias expansion for LEFT JOIN USING
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
1 parent 97fd524 commit e8e0dd5

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
import org.apache.calcite.util.trace.CalciteTrace;
124124

125125
import com.google.common.collect.ImmutableList;
126-
import com.google.common.collect.ImmutableMap;
127126
import com.google.common.collect.ImmutableSet;
128127
import com.google.common.collect.Sets;
129128

@@ -568,8 +567,8 @@ private boolean isNonAggregatedNonGroupedColumn(SqlNode node, SqlSelect select)
568567
return false;
569568
}
570569

571-
private static Map<String, String> getFieldAliases(final SelectScope scope) {
572-
final ImmutableMap.Builder<String, String> fieldAliases = new ImmutableMap.Builder<>();
570+
private static ImmutableSet<String> getFieldsAliased(final SelectScope scope) {
571+
final ImmutableSet.Builder<String> result = new ImmutableSet.Builder<>();
573572

574573
for (SqlNode selectItem : scope.getNode().getSelectList()) {
575574
if (selectItem instanceof SqlCall) {
@@ -580,12 +579,11 @@ private static Map<String, String> getFieldAliases(final SelectScope scope) {
580579
}
581580

582581
final SqlIdentifier fieldIdentifier = call.operand(0);
583-
fieldAliases.put(fieldIdentifier.getSimple(),
584-
((SqlIdentifier) call.operand(1)).getSimple());
582+
result.add(fieldIdentifier.names.get(fieldIdentifier.names.size() - 1));
585583
}
586584
}
587585

588-
return fieldAliases.build();
586+
return result.build();
589587
}
590588

591589
/** Returns the set of field names in the join condition specified by USING
@@ -7520,7 +7518,7 @@ private SqlNode expandExprFromJoin(SqlJoin join, SqlIdentifier identifier, Selec
75207518
}
75217519

75227520
final SqlNameMatcher matcher = validator.getCatalogReader().nameMatcher();
7523-
final Map<String, String> fieldAliases = getFieldAliases(scope);
7521+
final Set<String> fieldAliases = getFieldsAliased(scope);
75247522

75257523
for (String name : commonColumnNames) {
75267524
if (matcher.matches(identifier.getSimple(), name)) {
@@ -7537,13 +7535,12 @@ private SqlNode expandExprFromJoin(SqlJoin join, SqlIdentifier identifier, Selec
75377535

75387536
assert qualifiedNode.size() == 2;
75397537

7540-
// If there is an alias for the column, no need to wrap the coalesce with an AS operator
7541-
boolean haveAlias = fieldAliases.containsKey(name);
7542-
75437538
final SqlCall coalesceCall =
75447539
SqlStdOperatorTable.COALESCE.createCall(SqlParserPos.ZERO, qualifiedNode.get(0),
75457540
qualifiedNode.get(1));
75467541

7542+
// If there is an alias for the column, no need to wrap the coalesce with an AS operator
7543+
boolean haveAlias = fieldAliases.contains(name);
75477544
if (haveAlias) {
75487545
return coalesceCall;
75497546
} else {

core/src/test/resources/sql/planner.iq

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,40 @@ EnumerableCalc(expr#0..2=[{inputs}], $f0=[$t1], $f1=[$t2])
146146
EnumerableValues(tuples=[[{ 10 }, { 10 }, { 20 }, { 30 }, { 30 }, { 50 }, { 50 }, { 60 }, { null }]])
147147
!plan
148148
!set planner-rules original
149+
!use blank
150+
151+
# Test case for [CALCITE-7501] Assertion error in alias expansion for LEFT JOIN USING
152+
CREATE TABLE D(sk_cid INT, dt DATE, dm_sym VARCHAR, fhd DATE);
153+
(0 rows modified)
154+
155+
!update
156+
157+
CREATE TABLE F(sk_cid INT);
158+
(0 rows modified)
159+
160+
!update
161+
162+
CREATE TABLE S(sk_sid INT, sym VARCHAR);
163+
(0 rows modified)
164+
165+
!update
166+
167+
SELECT
168+
d.dt as dtn,
169+
fhd as sk_fhd
170+
FROM D
171+
JOIN S
172+
ON S.sym = D.dm_sym
173+
LEFT JOIN F USING (sk_cid);
174+
+-----+--------+
175+
| DTN | SK_FHD |
176+
+-----+--------+
177+
+-----+--------+
178+
(0 rows)
179+
180+
!ok
149181

150182
# Add tests for [CALCITE-6985] to verify AggregateMinMaxToLimitRule handles empty tables correctly
151-
!use blank
152183
create table t_empty (id int);
153184
(0 rows modified)
154185

0 commit comments

Comments
 (0)