Skip to content

Commit 897a1cd

Browse files
committed
Add CTEType handling to FROM clause rewriting
The FROM clause rewriter had no case for CTEType, causing CTE references to hit the default error case "unknown indirect type". CTEs are now handled like views - the inner SELECT is executed and wrapped with the CTE name as an alias: ( inner_select ) AS "cte_name"
1 parent 03320a5 commit 897a1cd

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

internal/stackql/astvisit/from_rewrite.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,12 @@ func (v *standardFromRewriteAstVisitor) Visit(node sqlparser.SQLNode) error {
671671
templateString := ` ( %s ) `
672672
v.rewrittenQuery = templateString
673673
v.indirectContexts = append(v.indirectContexts, indirect.GetSelectContext())
674+
case astindirect.CTEType:
675+
// CTEs are handled like views - the inner SELECT is executed
676+
// and results are wrapped with the CTE name as alias.
677+
templateString := fmt.Sprintf(` ( %%s ) AS "%s" `, name)
678+
v.rewrittenQuery = templateString
679+
v.indirectContexts = append(v.indirectContexts, indirect.GetSelectContext())
674680
case astindirect.MaterializedViewType, astindirect.PhysicalTableType:
675681
refString := fmt.Sprintf(` %s `, name)
676682
isQuoted, _ := regexp.MatchString(`^".*"$`, name)

0 commit comments

Comments
 (0)