@@ -530,13 +530,11 @@ private void from(From_itemContext fromItem) {
530530 }
531531 } else if ((primary = fromItem .from_primary ()) != null ) {
532532 Alias_clauseContext alias = primary .alias_clause ();
533- List <Function_callContext > functions = primary .function_call ();
534533 Schema_qualified_nameContext table ;
535534 Table_subqueryContext subquery ;
536535
537- if (!functions .isEmpty ()) {
538- functions .forEach (e -> function (e , primary .alias ));
539- } else if ((table = primary .schema_qualified_name ()) != null ) {
536+ fromFunctionCommon (primary );
537+ if ((table = primary .schema_qualified_name ()) != null ) {
540538 addNameReference (table , alias );
541539 if (primary .TABLESAMPLE () != null ) {
542540 ValueExpr vex = new ValueExpr (this );
@@ -571,20 +569,62 @@ private void from(From_itemContext fromItem) {
571569 }
572570 }
573571
574- private void function ( Function_callContext function , IdentifierContext alias ) {
572+ private void fromFunctionCommon ( From_primaryContext from ) {
575573 boolean oldLateral = lateralAllowed ;
576574 try {
577- lateralAllowed = true ;
575+ if (from .function_call () != null ) {
576+ function (from .function_call (), from .alias , from .from_function_column_def ());
577+ } else if (from .from_rows_with_alias () != null ) {
578+ fromRowsFunction (from .from_rows_with_alias ());
579+ }
580+ } finally {
581+ lateralAllowed = oldLateral ;
582+ }
583+ }
584+
585+ private void fromRowsFunction (From_rows_with_aliasContext fromRows ) {
586+ for (var function : fromRows .function_call ()) {
578587 ValueExpr vexFunc = new ValueExpr (this );
579588 Pair <String , String > func = vexFunc .function (function );
580589 if (func .getFirst () != null ) {
581- String funcAlias = alias == null ? func .getFirst () : alias .getText ();
582- addReference (funcAlias , null );
583- complexNamespace .put (funcAlias ,
584- List .of (new Pair <>(funcAlias , func .getSecond ())));
590+ String funcName = func .getFirst ();
591+ addReference (funcName , null );
585592 }
586- } finally {
587- lateralAllowed = oldLateral ;
593+ }
594+
595+ List <Pair <String , String >> colPairs = new ArrayList <>();
596+ fromRows .column_alias
597+ .forEach (identifier -> colPairs .add (new ModPair <>(identifier .getText (), TypesSetManually .COLUMN )));
598+
599+ if (!fromRows .from_function_column_def ().isEmpty ()) {
600+ // safe to take any since they all must have the same types
601+ var cols = fromRows .from_function_column_def (0 );
602+ for (int i = 0 ; i < cols .column_alias .size () && i < colPairs .size (); i ++) {
603+ colPairs .set (i , new Pair <>(colPairs .get (i ).getFirst (), cols .data_type ().get (i ).getText ()));
604+ }
605+ }
606+
607+ var alias = fromRows .alias .getText ();
608+ addReference (alias , null );
609+ complexNamespace .put (alias , colPairs );
610+ }
611+
612+ private void function (Function_callContext function , IdentifierContext alias ,
613+ From_function_column_defContext definition ) {
614+ ValueExpr vexFunc = new ValueExpr (this );
615+ Pair <String , String > func = vexFunc .function (function );
616+ if (func .getFirst () != null ) {
617+ String funcAlias = alias == null ? func .getFirst () : alias .getText ();
618+ addReference (funcAlias , null );
619+ List <Pair <String , String >> colPairs = new ArrayList <>();
620+ if (definition != null ) {
621+ for (int i = 0 ; i < definition .column_alias .size (); i ++) {
622+ colPairs .add (new Pair <>(definition .column_alias .get (i ).getText (), definition .data_type ().get (i ).getText ()));
623+ }
624+ } else {
625+ colPairs .add (new Pair <>(funcAlias , func .getSecond ()));
626+ }
627+ complexNamespace .put (funcAlias , colPairs );
588628 }
589629 }
590630}
0 commit comments