parseSelectColumnList parses the comma-separated column/expression list in SELECT.
()
| 303 | |
| 304 | // parseSelectColumnList parses the comma-separated column/expression list in SELECT. |
| 305 | func (p *Parser) parseSelectColumnList() ([]ast.Expression, error) { |
| 306 | // Guard: SELECT immediately followed by FROM is an error. |
| 307 | if p.isType(models.TokenTypeFrom) { |
| 308 | return nil, goerrors.ExpectedTokenError( |
| 309 | "column expression", |
| 310 | "FROM", |
| 311 | p.currentLocation(), |
| 312 | "SELECT requires at least one column expression before FROM", |
| 313 | ) |
| 314 | } |
| 315 | |
| 316 | columns := make([]ast.Expression, 0, 8) |
| 317 | for { |
| 318 | var expr ast.Expression |
| 319 | |
| 320 | if p.isType(models.TokenTypeAsterisk) || p.isType(models.TokenTypeMul) { |
| 321 | // Both TokenTypeAsterisk and TokenTypeMul represent '*' in SQL. |
| 322 | // The tokenizer may produce either depending on context. |
| 323 | expr = &ast.Identifier{Name: "*"} |
| 324 | p.advance() |
| 325 | } else { |
| 326 | var err error |
| 327 | expr, err = p.parseExpression() |
| 328 | if err != nil { |
| 329 | return nil, err |
| 330 | } |
| 331 | |
| 332 | // Optional alias: AS name or implicit name (non-identifier expressions only) |
| 333 | if p.isType(models.TokenTypeAs) { |
| 334 | p.advance() // Consume AS |
| 335 | if !p.isIdentifier() { |
| 336 | return nil, p.expectedError("alias name after AS") |
| 337 | } |
| 338 | alias := p.currentToken.Token.Value |
| 339 | p.advance() |
| 340 | expr = &ast.AliasedExpression{Expr: expr, Alias: alias} |
| 341 | } else if p.canBeAlias() { |
| 342 | if _, ok := expr.(*ast.Identifier); !ok { |
| 343 | alias := p.currentToken.Token.Value |
| 344 | p.advance() |
| 345 | expr = &ast.AliasedExpression{Expr: expr, Alias: alias} |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | columns = append(columns, expr) |
| 351 | |
| 352 | if !p.isType(models.TokenTypeComma) { |
| 353 | break |
| 354 | } |
| 355 | p.advance() // Consume comma |
| 356 | } |
| 357 | return columns, nil |
| 358 | } |
no test coverage detected