-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexpressions_literal.go
More file actions
413 lines (361 loc) · 12.2 KB
/
expressions_literal.go
File metadata and controls
413 lines (361 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2026 GoSQLX Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package parser - expressions_literal.go
// Primary expression parsing: literals, identifiers, qualified names, subqueries,
// and parenthesized expressions.
package parser
import (
"fmt"
"strings"
goerrors "github.com/ajitpratap0/GoSQLX/pkg/errors"
"github.com/ajitpratap0/GoSQLX/pkg/models"
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
"github.com/ajitpratap0/GoSQLX/pkg/sql/keywords"
)
// parsePrimaryExpression parses a primary expression (literals, identifiers, function calls)
func (p *Parser) parsePrimaryExpression() (ast.Expression, error) {
// Handle unary minus/plus (-6, -3.14, +x)
if p.isType(models.TokenTypeMinus) || p.isType(models.TokenTypePlus) {
unaryPos := p.currentLocation()
var op ast.UnaryOperator
if p.isType(models.TokenTypeMinus) {
op = ast.Minus
} else {
op = ast.Plus
}
p.advance() // Consume -/+
expr, err := p.parsePrimaryExpression()
if err != nil {
return nil, err
}
return &ast.UnaryExpression{
Operator: op,
Expr: expr,
Pos: unaryPos,
}, nil
}
if p.isType(models.TokenTypeCase) {
// Handle CASE expressions (both simple and searched forms)
return p.parseCaseExpression()
}
if p.isType(models.TokenTypeCast) {
// Handle CAST(expr AS type) expressions
return p.parseCastExpression()
}
// TRY_CAST(expr AS type) — Snowflake, SQL Server, BigQuery. Tokenized as
// an identifier (the snowflake keyword table is not wired into the
// tokenizer); detect by name when followed by '('.
if (p.isType(models.TokenTypeIdentifier) || p.isType(models.TokenTypeKeyword)) &&
strings.EqualFold(p.currentToken.Token.Value, "TRY_CAST") &&
p.peekToken().Token.Type == models.TokenTypeLParen {
return p.parseTryCastExpression()
}
if p.isType(models.TokenTypeInterval) {
// Handle INTERVAL 'value' expressions
return p.parseIntervalExpression()
}
if p.isType(models.TokenTypeArray) {
// Handle ARRAY[...] or ARRAY(SELECT ...) constructor
return p.parseArrayConstructor()
}
// ClickHouse array literal: [expr, expr, ...] without the ARRAY keyword.
// Gated to ClickHouse to avoid colliding with subscript-style usage in other
// dialects.
if p.isType(models.TokenTypeLBracket) && p.dialect == string(keywords.DialectClickHouse) {
return p.parseBracketArrayLiteral()
}
// Handle MySQL VALUES(column) helper in ON DUPLICATE KEY UPDATE.
// VALUES is normally a DML keyword, but inside ON DUPLICATE KEY UPDATE it acts
// as a scalar function that returns the value that was attempted for insertion.
// e.g.: INSERT INTO t (id, name) VALUES (1, 'Alice') ON DUPLICATE KEY UPDATE name=VALUES(name)
if p.isType(models.TokenTypeValues) && p.peekToken().Token.Type == models.TokenTypeLParen {
valuesPos := p.currentLocation()
p.advance() // Consume VALUES
funcCall, err := p.parseFunctionCall("VALUES")
if err != nil {
return nil, err
}
if funcCall.Pos.IsZero() {
funcCall.Pos = valuesPos
}
return funcCall, nil
}
// Handle keywords that can be used as function names (IF, REPLACE, FIRST, LAST, etc.)
if (p.isType(models.TokenTypeIf) || p.isType(models.TokenTypeReplace) ||
p.isType(models.TokenTypeFirst) || p.isType(models.TokenTypeLast)) && p.peekToken().Token.Type == models.TokenTypeLParen {
kwPos := p.currentLocation()
identName := p.currentToken.Token.Value
p.advance()
funcCall, err := p.parseFunctionCall(identName)
if err != nil {
return nil, err
}
if funcCall.Pos.IsZero() {
funcCall.Pos = kwPos
}
return funcCall, nil
}
if p.isType(models.TokenTypeIdentifier) || p.isType(models.TokenTypeDoubleQuotedString) || ((p.dialect == string(keywords.DialectSQLServer) || p.dialect == string(keywords.DialectClickHouse)) && p.isNonReservedKeyword()) {
// Handle identifiers and function calls
// Double-quoted strings are treated as identifiers in SQL (e.g., "column_name")
// Non-reserved keywords (TARGET, SOURCE, etc.) can also be used as identifiers
identPos := p.currentLocation()
identName := p.currentToken.Token.Value
p.advance()
// Check for function call (identifier followed by parentheses)
if p.isType(models.TokenTypeLParen) {
// This is a function call
funcCall, err := p.parseFunctionCall(identName)
if err != nil {
return nil, err
}
// Assign position of function name
if funcCall.Pos.IsZero() {
funcCall.Pos = identPos
}
// MySQL MATCH(...) AGAINST(...) full-text search
if strings.EqualFold(identName, "MATCH") && strings.EqualFold(p.currentToken.Token.Value, "AGAINST") {
return p.parseMatchAgainst(funcCall)
}
return funcCall, nil
}
// Handle regular identifier or qualified identifier (table.column or table.*)
ident := &ast.Identifier{Name: identName, Pos: identPos}
// Check for qualified identifier (table.column) or qualified asterisk (table.*)
if p.isType(models.TokenTypePeriod) {
p.advance() // Consume .
if p.isType(models.TokenTypeAsterisk) || p.isType(models.TokenTypeMul) {
// Handle table.* (qualified asterisk).
// Both TokenTypeAsterisk and TokenTypeMul represent '*'.
ident = &ast.Identifier{
Table: ident.Name,
Name: "*",
Pos: identPos,
}
p.advance()
} else if p.isIdentifier() || p.isNonReservedKeyword() {
// Handle table.column (qualified identifier).
// isNonReservedKeyword covers reserved words valid as column
// names after a dot, e.g. table.KEY, schema.INDEX, alias.VIEW.
ident = &ast.Identifier{
Table: ident.Name,
Name: p.currentToken.Token.Value,
Pos: identPos,
}
p.advance()
} else {
return nil, goerrors.InvalidSyntaxError(
"expected column name or * after table qualifier",
p.currentLocation(),
"Use table.column or table.* syntax",
)
}
}
// Check for array subscript or slice syntax: identifier[...]
// This handles: arr[1], arr[1][2], arr[1:3], arr[2:], arr[:5]
if p.isType(models.TokenTypeLBracket) {
return p.parseArrayAccessExpression(ident)
}
return ident, nil
}
if p.isType(models.TokenTypeAsterisk) || p.isType(models.TokenTypeMul) {
// Handle asterisk (e.g., in COUNT(*) or SELECT *).
// Both TokenTypeAsterisk and TokenTypeMul represent '*' from the tokenizer.
p.advance()
return &ast.Identifier{Name: "*"}, nil
}
if p.isStringLiteral() {
// Handle string literals
value := p.currentToken.Token.Value
p.advance()
return &ast.LiteralValue{Value: value, Type: "string"}, nil
}
if p.isNumericLiteral() {
// Handle numeric literals (int or float)
value := p.currentToken.Token.Value
litType := "int"
if strings.ContainsAny(value, ".eE") {
litType = "float"
}
p.advance()
return &ast.LiteralValue{Value: value, Type: litType}, nil
}
if p.isBooleanLiteral() {
// Handle boolean literals (uses O(1) switch instead of O(n) isAnyType)
value := p.currentToken.Token.Value
p.advance()
return &ast.LiteralValue{Value: value, Type: "bool"}, nil
}
if p.isType(models.TokenTypePlaceholder) {
// Handle SQL placeholders (e.g., $1, $2 for PostgreSQL; @param for SQL Server)
value := p.currentToken.Token.Value
p.advance()
return &ast.LiteralValue{Value: value, Type: "placeholder"}, nil
}
if p.isType(models.TokenTypeNull) {
// Handle NULL literal
p.advance()
return &ast.LiteralValue{Value: nil, Type: "null"}, nil
}
if p.isType(models.TokenTypeLParen) {
// Handle parenthesized expression or subquery
parenPos := p.currentLocation()
p.advance() // Consume (
// Check if this is a subquery (starts with SELECT or WITH)
if p.isType(models.TokenTypeSelect) || p.isType(models.TokenTypeWith) {
// Parse subquery
subquery, err := p.parseSubquery()
if err != nil {
return nil, goerrors.InvalidSyntaxError(
fmt.Sprintf("failed to parse subquery: %v", err),
p.currentLocation(),
"",
)
}
// Expect closing parenthesis
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
return &ast.SubqueryExpression{Subquery: subquery, Pos: parenPos}, nil
}
// Regular parenthesized expression - could be tuple (a, b, c) or single (expr)
expr, err := p.parseExpression()
if err != nil {
return nil, err
}
// Check if this is a tuple (has comma after first expression)
if p.isType(models.TokenTypeComma) {
// This is a tuple expression (col1, col2, ...)
tuple := ast.GetTupleExpression()
tuple.Expressions = append(tuple.Expressions, expr)
for p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
nextExpr, err := p.parseExpression()
if err != nil {
return nil, err
}
tuple.Expressions = append(tuple.Expressions, nextExpr)
}
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
return tuple, nil
}
// Expect closing parenthesis for single expression
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
// Check for array subscript or slice on parenthesized expression
// This handles: (expr)[1], (SELECT arr)[2:3]
if p.isType(models.TokenTypeLBracket) {
return p.parseArrayAccessExpression(expr)
}
return expr, nil
}
if p.isType(models.TokenTypeExists) {
// Handle EXISTS (subquery)
p.advance() // Consume EXISTS
// Expect opening parenthesis
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
// Parse the subquery
subquery, err := p.parseSubquery()
if err != nil {
return nil, goerrors.InvalidSyntaxError(
fmt.Sprintf("failed to parse EXISTS subquery: %v", err),
p.currentLocation(),
"",
)
}
// Expect closing parenthesis
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
return &ast.ExistsExpression{Subquery: subquery}, nil
}
if p.isType(models.TokenTypeNot) {
// Handle NOT expression (NOT EXISTS, NOT boolean)
notPos := p.currentLocation()
p.advance() // Consume NOT
if p.isType(models.TokenTypeExists) {
// NOT EXISTS (subquery)
p.advance() // Consume EXISTS
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
subquery, err := p.parseSubquery()
if err != nil {
return nil, goerrors.InvalidSyntaxError(
fmt.Sprintf("failed to parse NOT EXISTS subquery: %v", err),
p.currentLocation(),
"",
)
}
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
// Return NOT EXISTS as a BinaryExpression with NOT flag
return &ast.BinaryExpression{
Left: &ast.ExistsExpression{Subquery: subquery},
Operator: "NOT",
Right: nil,
Not: true,
Pos: notPos,
}, nil
}
// NOT followed by other expression (boolean negation)
// Parse at comparison level for proper precedence: NOT (a > b), NOT active
expr, err := p.parseComparisonExpression()
if err != nil {
return nil, err
}
return &ast.UnaryExpression{
Operator: ast.Not,
Expr: expr,
Pos: notPos,
}, nil
}
return nil, goerrors.UnexpectedTokenError(
p.currentToken.Token.Type.String(),
p.currentToken.Token.Value,
p.currentLocation(),
"",
)
}
// parseSubquery parses a subquery (SELECT or WITH statement).
// Expects current token to be SELECT or WITH.
func (p *Parser) parseSubquery() (ast.Statement, error) {
if p.isType(models.TokenTypeWith) {
// WITH statement handles its own token consumption
return p.parseWithStatement()
}
if p.isType(models.TokenTypeSelect) {
p.advance() // Consume SELECT
return p.parseSelectWithSetOperations()
}
return nil, goerrors.ExpectedTokenError(
"SELECT or WITH",
p.currentToken.Token.Type.String(),
p.currentLocation(),
"",
)
}