syntax: groupByClause? (WITH (CUBE | ROLLUP))? (WITH TOTALS)?
(pos Pos)
| 494 | |
| 495 | // syntax: groupByClause? (WITH (CUBE | ROLLUP))? (WITH TOTALS)? |
| 496 | func (p *Parser) parseGroupByClause(pos Pos) (*GroupByClause, error) { |
| 497 | if err := p.expectKeyword(KeywordGroup); err != nil { |
| 498 | return nil, err |
| 499 | } |
| 500 | if err := p.expectKeyword(KeywordBy); err != nil { |
| 501 | return nil, err |
| 502 | } |
| 503 | |
| 504 | var expr Expr |
| 505 | var err error |
| 506 | aggregateType := "" |
| 507 | switch { |
| 508 | case p.matchKeyword(KeywordCube) || p.matchKeyword(KeywordRollup): |
| 509 | aggregateType = p.last().String |
| 510 | _ = p.lexer.consumeToken() |
| 511 | expr, err = p.parseFunctionParams(p.Pos()) |
| 512 | case p.tryConsumeKeywords(KeywordGrouping, KeywordSets): |
| 513 | aggregateType = "GROUPING SETS" |
| 514 | expr, err = p.parseFunctionParams(p.Pos()) |
| 515 | case p.tryConsumeKeywords(KeywordAll): |
| 516 | aggregateType = "ALL" |
| 517 | default: |
| 518 | expr, err = p.parseColumnExprListWithLParen(p.Pos()) |
| 519 | } |
| 520 | if err != nil { |
| 521 | return nil, err |
| 522 | } |
| 523 | groupBy := &GroupByClause{ |
| 524 | GroupByPos: pos, |
| 525 | AggregateType: aggregateType, |
| 526 | Expr: expr, |
| 527 | } |
| 528 | |
| 529 | // parse WITH CUBE, ROLLUP, TOTALS |
| 530 | for p.tryConsumeKeywords(KeywordWith) { |
| 531 | switch { |
| 532 | case p.tryConsumeKeywords(KeywordCube): |
| 533 | groupBy.WithCube = true |
| 534 | case p.tryConsumeKeywords(KeywordRollup): |
| 535 | groupBy.WithRollup = true |
| 536 | case p.tryConsumeKeywords(KeywordTotals): |
| 537 | groupBy.WithTotals = true |
| 538 | default: |
| 539 | return nil, fmt.Errorf("expected CUBE, ROLLUP or TOTALS, got %s", p.lastTokenKind()) |
| 540 | } |
| 541 | } |
| 542 | groupBy.GroupByEnd = p.Pos() |
| 543 | |
| 544 | return groupBy, nil |
| 545 | } |
| 546 | |
| 547 | func (p *Parser) tryParseLimitAfterLimitByClause(pos Pos) (*LimitClause, error) { |
| 548 | if !p.matchKeyword(KeywordLimit) { |
no test coverage detected