Skip to content

Commit 9310974

Browse files
committed
Call processIndirect for CTE references to get proper column context
The root cause of the "no such column: name" error was that CTEs were not calling processIndirect(), unlike subqueries. This meant the CTE's selCtx was never set, and column lookups would fail. processIndirect runs the CTE's inner SELECT through the primitive generation pipeline, which sets up the proper column metadata with both API names and user aliases - enabling column name resolution.
1 parent ddc4a54 commit 9310974

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

internal/stackql/astanalysis/earlyanalysis/ast_expand.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,14 +842,17 @@ func (v *indirectExpandAstVisitor) Visit(node sqlparser.SQLNode) error {
842842
// Check if this is a CTE reference.
843843
cteName := node.Name.GetRawVal()
844844
if cte, isCTE := v.cteRegistry[cteName]; isCTE {
845-
// This is a CTE reference - create and register an indirect.
846-
// The CTE's inner SELECT has already been visited when processing
847-
// the WITH clause, so the API tables within it will be processed.
845+
// This is a CTE reference - create and process an indirect.
846+
// We need to call processIndirect to get the proper column context
847+
// from the CTE's SELECT statement.
848848
indirect, err := astindirect.NewCTEIndirect(cte)
849849
if err != nil {
850850
return err
851851
}
852-
v.annotatedAST.SetIndirect(node, indirect)
852+
err = v.processIndirect(node, indirect)
853+
if err != nil {
854+
return err
855+
}
853856
return nil
854857
}
855858
containsBackendMaterial := v.handlerCtx.GetDBMSInternalRouter().ExprIsRoutable(node)

0 commit comments

Comments
 (0)