@@ -413,6 +413,11 @@ protected SqlNode getTop() {
413413 return parentCursorMap .get (columnListParamName );
414414 }
415415
416+ private boolean expandSelectItem (final SqlNode selectItem , SqlSelect select ,
417+ RelDataType targetType , List <SqlNode > selectItems , Set <String > aliases ,
418+ List <Map .Entry <String , RelDataType >> fields , boolean includeSystemVars ) {
419+ return expandSelectItem (selectItem , select , targetType , selectItems , aliases , fields , includeSystemVars , false );
420+ }
416421 /**
417422 * If <code>selectItem</code> is "*" or "TABLE.*", expands it and returns
418423 * true; otherwise writes the unexpanded item.
@@ -427,10 +432,15 @@ protected SqlNode getTop() {
427432 */
428433 private boolean expandSelectItem (final SqlNode selectItem , SqlSelect select ,
429434 RelDataType targetType , List <SqlNode > selectItems , Set <String > aliases ,
430- List <Map .Entry <String , RelDataType >> fields , boolean includeSystemVars ) {
435+ List <Map .Entry <String , RelDataType >> fields , boolean includeSystemVars , boolean ignoreImplicitName ) {
431436 final SelectScope scope = (SelectScope ) getWhereScope (select );
432437 if (expandStar (selectItems , aliases , fields , includeSystemVars , scope ,
433438 selectItem )) {
439+ if (ignoreImplicitName ) {
440+ selectItems .removeIf (sqlNode -> sqlNode .toString ().contains (IMPLICIT_COL_NAME ));
441+ aliases .remove (IMPLICIT_COL_NAME );
442+ fields .removeIf (entry -> entry .getKey ().equalsIgnoreCase (IMPLICIT_COL_NAME ));
443+ }
434444 return true ;
435445 }
436446
@@ -1062,40 +1072,45 @@ private SqlNode validateScopedExpression(
10621072 }
10631073
10641074 @ Override public void validateQuery (SqlNode node , SqlValidatorScope scope ,
1065- RelDataType targetRowType ) {
1075+ RelDataType targetRowType , boolean ignoreImplicitName ) {
10661076 final SqlValidatorNamespace ns = getNamespaceOrThrow (node , scope );
10671077 if (node .getKind () == SqlKind .TABLESAMPLE ) {
10681078 List <SqlNode > operands = ((SqlCall ) node ).getOperandList ();
10691079 SqlSampleSpec sampleSpec = SqlLiteral .sampleValue (operands .get (1 ));
10701080 if (sampleSpec instanceof SqlSampleSpec .SqlTableSampleSpec ) {
10711081 validateFeature (RESOURCE .sQLFeature_T613 (), node .getParserPosition ());
10721082 } else if (sampleSpec
1073- instanceof SqlSampleSpec .SqlSubstitutionSampleSpec ) {
1083+ instanceof SqlSampleSpec .SqlSubstitutionSampleSpec ) {
10741084 validateFeature (RESOURCE .sQLFeatureExt_T613_Substitution (),
1075- node .getParserPosition ());
1085+ node .getParserPosition ());
10761086 }
10771087 }
10781088
1079- validateNamespace (ns , targetRowType );
1089+ validateNamespace (ns , targetRowType , ignoreImplicitName );
10801090 switch (node .getKind ()) {
1081- case EXTEND :
1082- // Until we have a dedicated namespace for EXTEND
1083- deriveType (requireNonNull (scope , "scope" ), node );
1084- break ;
1085- default :
1086- break ;
1091+ case EXTEND :
1092+ // Until we have a dedicated namespace for EXTEND
1093+ deriveType (requireNonNull (scope , "scope" ), node );
1094+ break ;
1095+ default :
1096+ break ;
10871097 }
10881098 if (node == top ) {
10891099 validateModality (node );
10901100 }
10911101 validateAccess (
1092- node ,
1093- ns .getTable (),
1094- SqlAccessEnum .SELECT );
1102+ node ,
1103+ ns .getTable (),
1104+ SqlAccessEnum .SELECT );
10951105
10961106 validateSnapshot (node , scope , ns );
10971107 }
10981108
1109+ @ Override public void validateQuery (SqlNode node , SqlValidatorScope scope ,
1110+ RelDataType targetRowType ) {
1111+ validateQuery (node , scope , targetRowType , false );
1112+ }
1113+
10991114 /**
11001115 * Validates a namespace.
11011116 *
@@ -1112,6 +1127,15 @@ protected void validateNamespace(final SqlValidatorNamespace namespace,
11121127 }
11131128 }
11141129
1130+ protected void validateNamespace (final SqlValidatorNamespace namespace ,
1131+ RelDataType targetRowType , boolean ignoreImplicitName ) {
1132+ namespace .validate (targetRowType , ignoreImplicitName );
1133+ SqlNode node = namespace .getNode ();
1134+ if (node != null ) {
1135+ setValidatedNodeType (node , namespace .getType ());
1136+ }
1137+ }
1138+
11151139 @ Override public SqlValidatorScope getEmptyScope () {
11161140 return new EmptyScope (this );
11171141 }
@@ -3796,6 +3820,12 @@ private RelDataType validateCommonInputJoinColumn(SqlIdentifier id,
37963820 return field .getType ();
37973821 }
37983822
3823+ protected void validateSelect (
3824+ SqlSelect select ,
3825+ RelDataType targetRowType ) {
3826+ validateSelect (select , targetRowType , false );
3827+ }
3828+
37993829 /**
38003830 * Validates a SELECT statement.
38013831 *
@@ -3805,7 +3835,7 @@ private RelDataType validateCommonInputJoinColumn(SqlIdentifier id,
38053835 */
38063836 protected void validateSelect (
38073837 SqlSelect select ,
3808- RelDataType targetRowType ) {
3838+ RelDataType targetRowType , boolean ignoreImplicitName ) {
38093839 assert targetRowType != null ;
38103840 // Namespace is either a select namespace or a wrapper around one.
38113841 final SelectNamespace ns =
@@ -3877,7 +3907,7 @@ protected void validateSelect(
38773907 // depend on the GROUP BY list, or the window function might reference
38783908 // window name in the WINDOW clause etc.
38793909 final RelDataType rowType =
3880- validateSelectList (selectItems , select , targetRowType );
3910+ validateSelectList (selectItems , select , targetRowType , ignoreImplicitName );
38813911 ns .setType (rowType );
38823912 validateHavingClause (select );
38833913
@@ -4642,7 +4672,7 @@ protected void validateHavingClause(SqlSelect select) {
46424672 protected RelDataType validateSelectList (
46434673 final SqlNodeList selectItems ,
46444674 SqlSelect select ,
4645- RelDataType targetRowType ) {
4675+ RelDataType targetRowType , boolean ignoreImplicitName ) {
46464676 // First pass, ensure that aliases are unique. "*" and "TABLE.*" items
46474677 // are ignored.
46484678
@@ -4676,7 +4706,7 @@ protected RelDataType validateSelectList(
46764706 expandedSelectItems ,
46774707 aliases ,
46784708 fieldList ,
4679- false );
4709+ false , ignoreImplicitName );
46804710 }
46814711 }
46824712 expandSelectItemWithNotInGroupBy (expandedSelectItems , selectScope );
@@ -4869,7 +4899,7 @@ protected RelDataType createTargetRowType(
48694899 final SqlNode source = insert .getSource ();
48704900 if (source instanceof SqlSelect ) {
48714901 final SqlSelect sqlSelect = (SqlSelect ) source ;
4872- validateSelect (sqlSelect , targetRowType );
4902+ validateSelect (sqlSelect , targetRowType , true );
48734903 } else {
48744904 final SqlValidatorScope scope = scopes .get (source );
48754905 requireNonNull (scope , "scope" );
0 commit comments