Skip to content

Commit abaeec6

Browse files
committed
Index CTEs when resolving queries
1 parent 2e0435c commit abaeec6

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

internal/compiler/resolve.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func dataType(n *ast.TypeName) string {
2121
}
2222
}
2323

24+
// resolveCatalogRefs
2425
func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar, args []paramRef, params *named.ParamSet, embeds rewrite.EmbedSet) ([]Parameter, error) {
2526
c := comp.catalog
2627

@@ -67,10 +68,14 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
6768
continue
6869
}
6970
// If the table name doesn't exist, first check if it's a CTE
70-
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
71+
qctab, qcerr := qc.GetTable(fqn)
72+
if qcerr != nil {
7173
return nil, err
7274
}
73-
continue
75+
table = catalog.Table{
76+
Rel: qctab.Rel,
77+
Columns: convertCompilerColumnsToCatalogColumns(qctab.Columns),
78+
}
7479
}
7580
err = indexTable(table)
7681
if err != nil {
@@ -635,3 +640,32 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
635640
}
636641
return a, nil
637642
}
643+
644+
func convertCompilerColumnsToCatalogColumns(qccols []*Column) []*catalog.Column {
645+
ret := make([]*catalog.Column, len(qccols))
646+
for i, v := range qccols {
647+
ret[i] = convertCompilerColumnToCatalogColumn(v)
648+
}
649+
return ret
650+
}
651+
652+
func convertCompilerColumnToCatalogColumn(qccol *Column) *catalog.Column {
653+
654+
var tn ast.TypeName
655+
if qccol.Type != nil {
656+
tn = *qccol.Type
657+
} else {
658+
tn = ast.TypeName{Name: qccol.DataType}
659+
}
660+
661+
return &catalog.Column{
662+
Name: qccol.Name,
663+
Type: tn,
664+
IsNotNull: qccol.NotNull,
665+
IsUnsigned: qccol.Unsigned,
666+
IsArray: qccol.IsArray,
667+
ArrayDims: qccol.ArrayDims,
668+
Comment: qccol.Comment,
669+
Length: qccol.Length,
670+
}
671+
}

0 commit comments

Comments
 (0)