Skip to content

Commit b226c63

Browse files
committed
fix: CTE table references now correctly modify AST for downstream resolution
The CTE implementation was storing an indirect but not modifying the original AST node. This caused GetHIDs to still see a TableName type instead of a Subquery type, resulting in "could not locate table" errors. Fix: Modify the original node.Expr to be the CTE's Subquery instead of creating a synthetic expression. This ensures downstream code correctly recognizes the CTE reference as a subquery. Also re-enables the CTE robot test.
1 parent 6c5f6c8 commit b226c63

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

internal/stackql/astanalysis/earlyanalysis/ast_expand.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,19 @@ func (v *indirectExpandAstVisitor) processCTEReference(
190190
if !isCTE {
191191
return false
192192
}
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
193+
// Modify the original node to replace the TableName with the CTE subquery
194+
// This is critical: downstream code (GetHIDs) checks node.Expr type to identify subqueries
195+
node.Expr = cteSubquery
196+
// Set the alias to the CTE name if no explicit alias was provided
197+
if node.As.IsEmpty() {
198+
node.As = sqlparser.NewTableIdent(tableName)
197199
}
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)
200+
sq := internaldto.NewSubqueryDTO(node, cteSubquery)
204201
indirect, err := astindirect.NewSubqueryIndirect(sq)
205202
if err != nil {
206203
return true //nolint:nilerr //TODO: investigate
207204
}
208-
_ = v.processIndirect(syntheticExpr, indirect) //nolint:errcheck // errors handled via indirect pattern
205+
_ = v.processIndirect(node, indirect) //nolint:errcheck // errors handled via indirect pattern
209206
return true
210207
}
211208

test/robot/functional/stackql_mocked_from_cmd_line.robot

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,17 @@ Window Function RANK Over AcceleratorTypes
152152
... ${SELECT_ACCELERATOR_TYPES_RANK}
153153
... ${SELECT_ACCELERATOR_TYPES_RANK_EXPECTED}
154154

155-
# CTE test temporarily disabled for debugging - CTE support may need more work
156-
# CTE Simple Select Over AcceleratorTypes
157-
# Should StackQL Exec Inline Equal
158-
# ... ${STACKQL_EXE}
159-
# ... ${OKTA_SECRET_STR}
160-
# ... ${GITHUB_SECRET_STR}
161-
# ... ${K8S_SECRET_STR}
162-
# ... ${REGISTRY_NO_VERIFY_CFG_STR}
163-
# ... ${AUTH_CFG_STR}
164-
# ... ${SQL_BACKEND_CFG_STR_CANONICAL}
165-
# ... ${SELECT_ACCELERATOR_TYPES_SIMPLE_CTE}
166-
# ... ${SELECT_ACCELERATOR_TYPES_SIMPLE_CTE_EXPECTED}
155+
CTE Simple Select Over AcceleratorTypes
156+
Should StackQL Exec Inline Equal
157+
... ${STACKQL_EXE}
158+
... ${OKTA_SECRET_STR}
159+
... ${GITHUB_SECRET_STR}
160+
... ${K8S_SECRET_STR}
161+
... ${REGISTRY_NO_VERIFY_CFG_STR}
162+
... ${AUTH_CFG_STR}
163+
... ${SQL_BACKEND_CFG_STR_CANONICAL}
164+
... ${SELECT_ACCELERATOR_TYPES_SIMPLE_CTE}
165+
... ${SELECT_ACCELERATOR_TYPES_SIMPLE_CTE_EXPECTED}
167166

168167
Google Machine Types Select Paginated
169168
Should Horrid Query StackQL Inline Equal

0 commit comments

Comments
 (0)