123123import org .apache .calcite .util .trace .CalciteTrace ;
124124
125125import com .google .common .collect .ImmutableList ;
126- import com .google .common .collect .ImmutableMap ;
127126import com .google .common .collect .ImmutableSet ;
128127import 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 {
0 commit comments