Skip to content

Commit d674161

Browse files
committed
refactor: extract CTE processing into helper method to reduce nesting complexity
Move CTE reference handling into separate processCTEReference method to satisfy nestif linter complexity requirements.
1 parent 45ab752 commit d674161

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

internal/stackql/astanalysis/earlyanalysis/ast_expand.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,38 @@ func (v *indirectExpandAstVisitor) IsReadOnly() bool {
180180
return v.selectCount > 0 && v.mutateCount == 0
181181
}
182182

183+
// processCTEReference handles CTE references by converting them to subquery indirects.
184+
// Returns true if the table name was a CTE reference and was processed, false otherwise.
185+
func (v *indirectExpandAstVisitor) processCTEReference(
186+
node *sqlparser.AliasedTableExpr,
187+
tableName string,
188+
) (bool, error) {
189+
cteSubquery, isCTE := v.cteRegistry[tableName]
190+
if !isCTE {
191+
return false, nil
192+
}
193+
// Use the CTE name as the effective alias if no explicit alias is set
194+
effectiveAlias := node.As.GetRawVal()
195+
if effectiveAlias == "" {
196+
effectiveAlias = tableName
197+
}
198+
// Create a synthetic AliasedTableExpr with the CTE subquery
199+
syntheticExpr := &sqlparser.AliasedTableExpr{
200+
Expr: cteSubquery,
201+
As: sqlparser.NewTableIdent(effectiveAlias),
202+
}
203+
sq := internaldto.NewSubqueryDTO(syntheticExpr, cteSubquery)
204+
indirect, err := astindirect.NewSubqueryIndirect(sq)
205+
if err != nil {
206+
return true, nil //nolint:nilerr //TODO: investigate
207+
}
208+
err = v.processIndirect(syntheticExpr, indirect)
209+
if err != nil {
210+
return true, nil //nolint:nilerr //TODO: investigate
211+
}
212+
return true, nil
213+
}
214+
183215
func (v *indirectExpandAstVisitor) ContainsAnalyticsCacheMaterial() bool {
184216
return v.containsAnalyticsCacheMaterial
185217
}
@@ -797,28 +829,11 @@ func (v *indirectExpandAstVisitor) Visit(node sqlparser.SQLNode) error {
797829
return nil
798830
case sqlparser.TableName:
799831
// Check if this table name is a CTE reference
800-
tableName := n.GetRawVal()
801-
if cteSubquery, isCTE := v.cteRegistry[tableName]; isCTE {
802-
// Process CTE reference as a subquery
803-
// Use the CTE name as the effective alias if no explicit alias is set
804-
effectiveAlias := node.As.GetRawVal()
805-
if effectiveAlias == "" {
806-
effectiveAlias = tableName
807-
}
808-
// Create a synthetic AliasedTableExpr with the CTE subquery
809-
syntheticExpr := &sqlparser.AliasedTableExpr{
810-
Expr: cteSubquery,
811-
As: sqlparser.NewTableIdent(effectiveAlias),
812-
}
813-
sq := internaldto.NewSubqueryDTO(syntheticExpr, cteSubquery)
814-
indirect, err := astindirect.NewSubqueryIndirect(sq)
815-
if err != nil {
816-
return nil //nolint:nilerr //TODO: investigate
817-
}
818-
err = v.processIndirect(syntheticExpr, indirect)
819-
if err != nil {
820-
return nil //nolint:nilerr //TODO: investigate
821-
}
832+
processed, err := v.processCTEReference(node, n.GetRawVal())
833+
if err != nil {
834+
return err
835+
}
836+
if processed {
822837
return nil
823838
}
824839
}

0 commit comments

Comments
 (0)