MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / parseCommonTableExpr

Method parseCommonTableExpr

pkg/sql/parser/cte.go:113–222  ·  view source on GitHub ↗

parseCommonTableExpr parses a single Common Table Expression. It handles CTE name, optional column list, AS keyword, and the CTE query in parentheses. Syntax: cte_name [(column_list)] AS (query)

()

Source from the content-addressed store, hash-verified

111//
112// Syntax: cte_name [(column_list)] AS (query)
113func (p *Parser) parseCommonTableExpr() (*ast.CommonTableExpr, error) {
114 // Check recursion depth to prevent stack overflow in recursive CTEs
115 // This is critical since CTEs can call parseStatement which leads back to more CTEs
116 p.depth++
117 defer func() { p.depth-- }()
118
119 if p.depth > MaxRecursionDepth {
120 return nil, goerrors.InvalidCTEError(
121 fmt.Sprintf("maximum recursion depth exceeded (%d) - CTE too deeply nested", MaxRecursionDepth),
122 p.currentLocation(),
123 "",
124 )
125 }
126
127 // Parse CTE name (supports double-quoted identifiers)
128 if !p.isIdentifier() {
129 return nil, p.expectedError("CTE name")
130 }
131 cteNamePos := p.currentLocation()
132 name := p.currentToken.Token.Value
133 p.advance()
134
135 // Parse optional column list
136 var columns []string
137 if p.isType(models.TokenTypeLParen) {
138 p.advance() // Consume (
139
140 for {
141 if !p.isIdentifier() {
142 return nil, p.expectedError("column name")
143 }
144 columns = append(columns, p.currentToken.Token.Value)
145 p.advance()
146
147 if p.isType(models.TokenTypeComma) {
148 p.advance() // Consume comma
149 continue
150 }
151 break
152 }
153
154 if !p.isType(models.TokenTypeRParen) {
155 return nil, p.expectedError(")")
156 }
157 p.advance() // Consume )
158 }
159
160 // Parse AS keyword
161 if !p.isType(models.TokenTypeAs) {
162 return nil, p.expectedError("AS")
163 }
164 p.advance()
165
166 // Parse optional MATERIALIZED / NOT MATERIALIZED
167 // Syntax: AS [NOT] MATERIALIZED (query)
168 var materialized *bool
169 if p.isType(models.TokenTypeNot) {
170 p.advance() // Consume NOT

Callers 1

parseWithStatementMethod · 0.95

Calls 7

currentLocationMethod · 0.95
isIdentifierMethod · 0.95
expectedErrorMethod · 0.95
advanceMethod · 0.95
isTypeMethod · 0.95
parseStatementMethod · 0.95

Tested by

no test coverage detected