@@ -21,43 +21,63 @@ func dataType(n *ast.TypeName) string {
2121 }
2222}
2323
24- func (comp * Compiler ) resolveCatalogRefs (qc * QueryCatalog , rvs []* ast. RangeVar , args []paramRef , params * named.ParamSet , embeds rewrite.EmbedSet ) ([]Parameter , error ) {
24+ func (comp * Compiler ) resolveCatalogRefs (qc * QueryCatalog , scopedRVs []scopedRangeVar , args []paramRef , params * named.ParamSet , embeds rewrite.EmbedSet ) ([]Parameter , error ) {
2525 c := comp .catalog
2626
27+ scopeMap := make (map [* string ][]* ast.TableName )
28+ outerAliasMap := map [string ]* ast.TableName {}
2729 aliasMap := map [string ]* ast.TableName {}
30+ tableNameMap := map [string ]* ast.TableName {}
2831 // TODO: Deprecate defaultTable
2932 var defaultTable * ast.TableName
3033 var tables []* ast.TableName
31-
3234 typeMap := map [string ]map [string ]map [string ]* catalog.Column {}
33- indexTable := func (table catalog.Table ) error {
34- tables = append (tables , table .Rel )
35+
36+ indexTableWithColumns := func (rel * ast.TableName , cols []* catalog.Column ) error {
37+ tables = append (tables , rel )
38+ tableNameMap [rel .Name ] = rel
3539 if defaultTable == nil {
36- defaultTable = table . Rel
40+ defaultTable = rel
3741 }
38- schema := table . Rel .Schema
42+ schema := rel .Schema
3943 if schema == "" {
4044 schema = c .DefaultSchema
4145 }
4246 if _ , exists := typeMap [schema ]; ! exists {
4347 typeMap [schema ] = map [string ]map [string ]* catalog.Column {}
4448 }
45- typeMap [schema ][table .Rel .Name ] = map [string ]* catalog.Column {}
46- for _ , c := range table .Columns {
47- cc := c
48- typeMap [schema ][table .Rel .Name ][c .Name ] = cc
49+ typeMap [schema ][rel.Name ] = map [string ]* catalog.Column {}
50+ for _ , c := range cols {
51+ typeMap [schema ][rel.Name ][c .Name ] = c
4952 }
5053 return nil
5154 }
5255
53- for _ , rv := range rvs {
56+ indexTable := func (table catalog.Table ) error {
57+ return indexTableWithColumns (table .Rel , table .Columns )
58+ }
59+
60+ indexCTE := func (cte * Table ) error {
61+ catalogCols := convertColumnsToCatalog (cte .Columns )
62+ return indexTableWithColumns (cte .Rel , catalogCols )
63+ }
64+
65+ for _ , srv := range scopedRVs {
66+ rv := srv .rv
67+ scope := srv .cteName
5468 if rv .Relname == nil {
5569 continue
5670 }
5771 fqn , err := ParseTableName (rv )
5872 if err != nil {
5973 return nil , err
6074 }
75+
76+ scopeMap [scope ] = append (scopeMap [scope ], fqn )
77+ if scope == nil && rv .Alias != nil {
78+ outerAliasMap [* rv .Alias .Aliasname ] = fqn
79+ }
80+
6181 if _ , found := aliasMap [fqn .Name ]; found {
6282 continue
6383 }
@@ -67,9 +87,16 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
6787 continue
6888 }
6989 // If the table name doesn't exist, first check if it's a CTE
70- if _ , qcerr := qc .GetTable (fqn ); qcerr != nil {
90+ cte , qcerr := qc .GetTable (fqn )
91+ if qcerr != nil {
7192 return nil , err
7293 }
94+ if err := indexCTE (cte ); err != nil {
95+ return nil , err
96+ }
97+ if rv .Alias != nil {
98+ aliasMap [* rv .Alias .Aliasname ] = fqn
99+ }
73100 continue
74101 }
75102 err = indexTable (table )
@@ -89,7 +116,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
89116 continue
90117 }
91118
92- if alias , ok := aliasMap [embed .Table .Name ]; ok {
119+ if alias , ok := outerAliasMap [embed .Table .Name ]; ok {
93120 embed .Table = alias
94121 continue
95122 }
@@ -195,24 +222,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
195222 panic ("too many field items: " + strconv .Itoa (len (items )))
196223 }
197224
198- search := tables
225+ search := scopeMap [ ref . cteName ]
199226 if alias != "" {
200227 if original , ok := aliasMap [alias ]; ok {
201228 search = []* ast.TableName {original }
229+ } else if tableName , ok := tableNameMap [alias ]; ok {
230+ search = []* ast.TableName {tableName }
202231 } else {
203- var located bool
204- for _ , fqn := range tables {
205- if fqn .Name == alias {
206- located = true
207- search = []* ast.TableName {fqn }
208- }
209- }
210- if ! located {
211- return nil , & sqlerr.Error {
212- Code : "42703" ,
213- Message : fmt .Sprintf ("table alias %q does not exist" , alias ),
214- Location : node .Location ,
215- }
232+ return nil , & sqlerr.Error {
233+ Code : "42703" ,
234+ Message : fmt .Sprintf ("table alias %q does not exist" , alias ),
235+ Location : node .Location ,
216236 }
217237 }
218238 }
@@ -573,12 +593,8 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
573593 if alias != "" {
574594 if original , ok := aliasMap [alias ]; ok {
575595 search = []* ast.TableName {original }
576- } else {
577- for _ , fqn := range tables {
578- if fqn .Name == alias {
579- search = []* ast.TableName {fqn }
580- }
581- }
596+ } else if tableName , ok := tableNameMap [alias ]; ok {
597+ search = []* ast.TableName {tableName }
582598 }
583599 }
584600
0 commit comments