normalizeToken maps a TokenWithSpan to a canonical form expected by the parser. Type mismatches that existed in the tokenizer are corrected here.
(t models.TokenWithSpan)
| 130 | // normalizeToken maps a TokenWithSpan to a canonical form expected by the |
| 131 | // parser. Type mismatches that existed in the tokenizer are corrected here. |
| 132 | func normalizeToken(t models.TokenWithSpan) models.TokenWithSpan { |
| 133 | // TokenTypeMul (*) is the same symbol as TokenTypeAsterisk for the |
| 134 | // parser. The parser explicitly checks both types wherever '*' can appear |
| 135 | // (SELECT list wildcard, table.* qualified wildcard, COUNT(*) argument). |
| 136 | // No remap is performed here to preserve the distinction for binary multiply. |
| 137 | |
| 138 | // Aggregate-function tokens (COUNT, SUM, AVG, MIN, MAX) must be treated |
| 139 | // as identifiers so they can be used as function-call names. |
| 140 | switch t.Token.Type { |
| 141 | case models.TokenTypeCount, models.TokenTypeSum, models.TokenTypeAvg, |
| 142 | models.TokenTypeMin, models.TokenTypeMax: |
| 143 | t.Token.Type = models.TokenTypeIdentifier |
| 144 | return t |
| 145 | } |
| 146 | |
| 147 | // Identifier tokens whose *value* is a keyword that the parser dispatches |
| 148 | // on by type need to be remapped. |
| 149 | if t.Token.Type == models.TokenTypeIdentifier { |
| 150 | if remapped := identifierKeywordType(t.Token.Value); remapped != models.TokenTypeUnknown { |
| 151 | t.Token.Type = remapped |
| 152 | } |
| 153 | return t |
| 154 | } |
| 155 | |
| 156 | // Generic TokenTypeKeyword tokens need a specific type for the parser. |
| 157 | if t.Token.Type == models.TokenTypeKeyword { |
| 158 | if remapped := keywordType(t.Token.Value); remapped != models.TokenTypeUnknown { |
| 159 | t.Token.Type = remapped |
| 160 | } |
| 161 | return t |
| 162 | } |
| 163 | |
| 164 | return t |
| 165 | } |
| 166 | |
| 167 | // toUpper converts s to uppercase using a pooled byte buffer for short strings. |
| 168 | func toUpper(s string) string { |
no test coverage detected