@@ -9,26 +9,36 @@ import (
99)
1010
1111type QueryCatalog struct {
12- catalog * catalog.Catalog
13- ctes map [string ]* Table
14- embeds rewrite.EmbedSet
12+ catalog * catalog.Catalog
13+ ctes map [string ]* Table
14+ fromClauses map [string ]* Table
15+ embeds rewrite.EmbedSet
1516}
1617
1718func (comp * Compiler ) buildQueryCatalog (c * catalog.Catalog , node ast.Node , embeds rewrite.EmbedSet ) (* QueryCatalog , error ) {
1819 var with * ast.WithClause
20+ var from * ast.List
1921 switch n := node .(type ) {
2022 case * ast.DeleteStmt :
2123 with = n .WithClause
2224 case * ast.InsertStmt :
2325 with = n .WithClause
2426 case * ast.UpdateStmt :
2527 with = n .WithClause
28+ from = n .FromClause
2629 case * ast.SelectStmt :
2730 with = n .WithClause
31+ from = n .FromClause
2832 default :
2933 with = nil
34+ from = nil
35+ }
36+ qc := & QueryCatalog {
37+ catalog : c ,
38+ ctes : map [string ]* Table {},
39+ fromClauses : map [string ]* Table {},
40+ embeds : embeds ,
3041 }
31- qc := & QueryCatalog {catalog : c , ctes : map [string ]* Table {}, embeds : embeds }
3242 if with != nil {
3343 for _ , item := range with .Ctes .Items {
3444 if cte , ok := item .(* ast.CommonTableExpr ); ok {
@@ -60,6 +70,37 @@ func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embed
6070 }
6171 }
6272 }
73+ if from != nil {
74+ for _ , item := range from .Items {
75+ if rs , ok := item .(* ast.RangeSubselect ); ok {
76+ cols , err := comp .outputColumns (qc , rs .Subquery )
77+ if err != nil {
78+ return nil , err
79+ }
80+ var names []string
81+ if rs .Alias .Colnames != nil {
82+ for _ , item := range rs .Alias .Colnames .Items {
83+ if val , ok := item .(* ast.String ); ok {
84+ names = append (names , val .Str )
85+ } else {
86+ names = append (names , "" )
87+ }
88+ }
89+ }
90+ rel := & ast.TableName {Name : * rs .Alias .Aliasname }
91+ for i := range cols {
92+ cols [i ].Table = rel
93+ if len (names ) > i {
94+ cols [i ].Name = names [i ]
95+ }
96+ }
97+ qc .fromClauses [* rs .Alias .Aliasname ] = & Table {
98+ Rel : rel ,
99+ Columns : cols ,
100+ }
101+ }
102+ }
103+ }
63104 return qc , nil
64105}
65106
0 commit comments