Skip to content

Commit 03320a5

Browse files
committed
Fix CTE double-processing - only register names, process in processIndirect
The CTE's inner SELECT was being processed twice: 1. In WITH clause processing (visiting CTE's SELECT) 2. In processIndirect when CTE is referenced This caused issues because the annotated AST would have stale/conflicting data from the first pass when processIndirect tried to analyze it. Subqueries don't have this issue - they're processed only once in processIndirect. CTEs should follow the same pattern: just register the CTE name in the registry, and let processIndirect handle all the actual SELECT processing when the CTE is referenced.
1 parent 9310974 commit 03320a5

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

internal/stackql/astanalysis/earlyanalysis/ast_expand.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,14 @@ func (v *indirectExpandAstVisitor) Visit(node sqlparser.SQLNode) error {
216216
addIf(node.StraightJoinHint, sqlparser.StraightJoinHint)
217217
addIf(node.SQLCalcFoundRows, sqlparser.SQLCalcFoundRowsStr)
218218

219-
// Process CTEs (Common Table Expressions) if present
219+
// Register CTEs (Common Table Expressions) if present.
220+
// We only register CTE names here - the actual SELECT processing
221+
// happens in processIndirect when the CTE is referenced.
222+
// This mirrors how subqueries work (processed once in processIndirect).
220223
if node.With != nil {
221224
for _, cte := range node.With.CTEs {
222225
cteName := cte.Name.GetRawVal()
223226
v.cteRegistry[cteName] = cte
224-
// Process the CTE's select statement
225-
if cte.Select != nil {
226-
err := cte.Select.Accept(v)
227-
if err != nil {
228-
return err
229-
}
230-
}
231227
}
232228
}
233229

0 commit comments

Comments
 (0)