This is an automated email from the ASF dual-hosted git repository.

mihaibudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new e8e0dd5414 [CALCITE-7501] Assertion error in alias expansion for LEFT 
JOIN USING
e8e0dd5414 is described below

commit e8e0dd54145c44f61b73acad1ffb96c14bddff78
Author: Mihai Budiu <[email protected]>
AuthorDate: Mon May 4 12:53:07 2026 -0700

    [CALCITE-7501] Assertion error in alias expansion for LEFT JOIN USING
    
    Signed-off-by: Mihai Budiu <[email protected]>
---
 .../calcite/sql/validate/SqlValidatorImpl.java     | 17 +++++------
 core/src/test/resources/sql/planner.iq             | 33 +++++++++++++++++++++-
 2 files changed, 39 insertions(+), 11 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java 
b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
index 320ba72153..6881bae035 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
@@ -123,7 +123,6 @@
 import org.apache.calcite.util.trace.CalciteTrace;
 
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 
@@ -568,8 +567,8 @@ private boolean isNonAggregatedNonGroupedColumn(SqlNode 
node, SqlSelect select)
     return false;
   }
 
-  private static Map<String, String> getFieldAliases(final SelectScope scope) {
-    final ImmutableMap.Builder<String, String> fieldAliases = new 
ImmutableMap.Builder<>();
+  private static ImmutableSet<String> getFieldsAliased(final SelectScope 
scope) {
+    final ImmutableSet.Builder<String> result = new ImmutableSet.Builder<>();
 
     for (SqlNode selectItem : scope.getNode().getSelectList()) {
       if (selectItem instanceof SqlCall) {
@@ -580,12 +579,11 @@ private static Map<String, String> getFieldAliases(final 
SelectScope scope) {
         }
 
         final SqlIdentifier fieldIdentifier = call.operand(0);
-        fieldAliases.put(fieldIdentifier.getSimple(),
-            ((SqlIdentifier) call.operand(1)).getSimple());
+        result.add(fieldIdentifier.names.get(fieldIdentifier.names.size() - 
1));
       }
     }
 
-    return fieldAliases.build();
+    return result.build();
   }
 
   /** 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
       }
 
       final SqlNameMatcher matcher = 
validator.getCatalogReader().nameMatcher();
-      final Map<String, String> fieldAliases = getFieldAliases(scope);
+      final Set<String> fieldAliases = getFieldsAliased(scope);
 
       for (String name : commonColumnNames) {
         if (matcher.matches(identifier.getSimple(), name)) {
@@ -7537,13 +7535,12 @@ private SqlNode expandExprFromJoin(SqlJoin join, 
SqlIdentifier identifier, Selec
 
           assert qualifiedNode.size() == 2;
 
-          // If there is an alias for the column, no need to wrap the coalesce 
with an AS operator
-          boolean haveAlias = fieldAliases.containsKey(name);
-
           final SqlCall coalesceCall =
               SqlStdOperatorTable.COALESCE.createCall(SqlParserPos.ZERO, 
qualifiedNode.get(0),
                   qualifiedNode.get(1));
 
+          // If there is an alias for the column, no need to wrap the coalesce 
with an AS operator
+          boolean haveAlias = fieldAliases.contains(name);
           if (haveAlias) {
             return coalesceCall;
           } else {
diff --git a/core/src/test/resources/sql/planner.iq 
b/core/src/test/resources/sql/planner.iq
index 96aaaf5eed..16168b820a 100644
--- a/core/src/test/resources/sql/planner.iq
+++ b/core/src/test/resources/sql/planner.iq
@@ -146,9 +146,40 @@ EnumerableCalc(expr#0..2=[{inputs}], $f0=[$t1], $f1=[$t2])
           EnumerableValues(tuples=[[{ 10 }, { 10 }, { 20 }, { 30 }, { 30 }, { 
50 }, { 50 }, { 60 }, { null }]])
 !plan
 !set planner-rules original
+!use blank
+
+# Test case for [CALCITE-7501] Assertion error in alias expansion for LEFT 
JOIN USING
+CREATE TABLE D(sk_cid INT, dt DATE, dm_sym VARCHAR, fhd DATE);
+(0 rows modified)
+
+!update
+
+CREATE TABLE F(sk_cid INT);
+(0 rows modified)
+
+!update
+
+CREATE TABLE S(sk_sid INT, sym VARCHAR);
+(0 rows modified)
+
+!update
+
+SELECT
+   d.dt as dtn,
+   fhd as sk_fhd
+FROM D
+JOIN S
+   ON S.sym = D.dm_sym
+LEFT JOIN F USING (sk_cid);
++-----+--------+
+| DTN | SK_FHD |
++-----+--------+
++-----+--------+
+(0 rows)
+
+!ok
 
 # Add tests for [CALCITE-6985] to verify AggregateMinMaxToLimitRule handles 
empty tables correctly
-!use blank
 create table t_empty (id int);
 (0 rows modified)
 

Reply via email to