parseGroupingSets parses GROUPING SETS(...) in GROUP BY clause Allows explicit specification of grouping sets Example: GROUPING SETS((a, b), (a), ()) generates exactly those three grouping sets
()
| 98 | // Allows explicit specification of grouping sets |
| 99 | // Example: GROUPING SETS((a, b), (a), ()) generates exactly those three grouping sets |
| 100 | func (p *Parser) parseGroupingSets() (*ast.GroupingSetsExpression, error) { |
| 101 | // Handle both "GROUPING SETS" as compound keyword or separate tokens |
| 102 | if p.currentToken.Token.Value == "GROUPING SETS" { |
| 103 | p.advance() // Consume "GROUPING SETS" compound token |
| 104 | } else if p.isType(models.TokenTypeGrouping) { |
| 105 | p.advance() // Consume GROUPING |
| 106 | // Check for SETS - using literal comparison as fallback since SETS is not a standalone token type |
| 107 | if p.currentToken.Token.Value != "SETS" && !p.isType(models.TokenTypeSets) { |
| 108 | return nil, p.expectedError("SETS after GROUPING") |
| 109 | } |
| 110 | p.advance() // Consume SETS |
| 111 | } |
| 112 | |
| 113 | if !p.isType(models.TokenTypeLParen) { |
| 114 | return nil, p.expectedError("( after GROUPING SETS") |
| 115 | } |
| 116 | p.advance() // Consume ( |
| 117 | |
| 118 | // Parse comma-separated grouping sets |
| 119 | sets := make([][]ast.Expression, 0) |
| 120 | for { |
| 121 | // Each set is either: |
| 122 | // 1. A parenthesized list: (col1, col2) |
| 123 | // 2. An empty set: () |
| 124 | // 3. A single column without parens: col1 (treated as (col1)) |
| 125 | |
| 126 | var set []ast.Expression |
| 127 | if p.isType(models.TokenTypeLParen) { |
| 128 | p.advance() // Consume ( |
| 129 | // Parse expressions in this set |
| 130 | set = make([]ast.Expression, 0) |
| 131 | // Handle empty set: () |
| 132 | if !p.isType(models.TokenTypeRParen) { |
| 133 | for { |
| 134 | expr, err := p.parseExpression() |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | set = append(set, expr) |
| 139 | |
| 140 | if p.isType(models.TokenTypeRParen) { |
| 141 | break |
| 142 | } |
| 143 | if !p.isType(models.TokenTypeComma) { |
| 144 | return nil, p.expectedError(", or ) in grouping set") |
| 145 | } |
| 146 | p.advance() // Consume comma |
| 147 | } |
| 148 | } |
| 149 | p.advance() // Consume ) |
| 150 | } else { |
| 151 | // Single column without parens |
| 152 | expr, err := p.parseExpression() |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | set = []ast.Expression{expr} |
| 157 | } |
no test coverage detected