-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparser.go
More file actions
709 lines (635 loc) · 22 KB
/
Copy pathparser.go
File metadata and controls
709 lines (635 loc) · 22 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// Package parser provides a recursive descent SQL parser that converts tokens into an Abstract Syntax Tree (AST).
// It supports comprehensive SQL features including SELECT, INSERT, UPDATE, DELETE, DDL operations,
// Common Table Expressions (CTEs), set operations (UNION, EXCEPT, INTERSECT), and window functions.
//
// Phase 2 Features (v1.2.0+):
// - Common Table Expressions (WITH clause) with recursive support
// - Set operations: UNION, UNION ALL, EXCEPT, INTERSECT
// - Multiple CTE definitions in single query
// - CTE column specifications
// - Left-associative set operation parsing
// - Integration of CTEs with set operations
//
// Phase 2.5 Features (v1.3.0+):
// - Window functions with OVER clause support
// - PARTITION BY and ORDER BY in window specifications
// - Window frame clauses (ROWS/RANGE with bounds)
// - Ranking functions: ROW_NUMBER(), RANK(), DENSE_RANK(), NTILE()
// - Analytic functions: LAG(), LEAD(), FIRST_VALUE(), LAST_VALUE()
// - Function call parsing with parentheses and arguments
// - Integration with existing SELECT statement parsing
package parser
import (
"context"
"fmt"
"strings"
"sync"
"github.com/ajitpratap0/GoSQLX/pkg/models"
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
"github.com/ajitpratap0/GoSQLX/pkg/sql/token"
)
// parserPool provides object pooling for Parser instances to reduce allocations.
// This significantly improves performance in high-throughput scenarios.
var parserPool = sync.Pool{
New: func() interface{} {
return &Parser{}
},
}
// GetParser returns a Parser instance from the pool.
// The caller must call PutParser when done to return it to the pool.
func GetParser() *Parser {
return parserPool.Get().(*Parser)
}
// PutParser returns a Parser instance to the pool after resetting it.
// This should be called after parsing is complete to enable reuse.
func PutParser(p *Parser) {
if p != nil {
p.Reset()
parserPool.Put(p)
}
}
// Reset clears the parser state for reuse from the pool.
func (p *Parser) Reset() {
p.tokens = nil
p.currentPos = 0
p.currentToken = token.Token{}
p.depth = 0
p.ctx = nil
}
// MaxRecursionDepth defines the maximum allowed recursion depth for parsing operations.
// This prevents stack overflow from deeply nested expressions, CTEs, or other recursive structures.
const MaxRecursionDepth = 100
// modelTypeUnset is the zero value for ModelType, indicating the type was not set.
// Used for fast path checks: tokens with ModelType set use O(1) switch dispatch.
const modelTypeUnset models.TokenType = 0
// Parser represents a SQL parser
type Parser struct {
tokens []token.Token
currentPos int
currentToken token.Token
depth int // Current recursion depth
ctx context.Context // Optional context for cancellation support
}
// Parse parses the tokens into an AST
// Uses fast ModelType (int) comparisons for hot path optimization
func (p *Parser) Parse(tokens []token.Token) (*ast.AST, error) {
p.tokens = tokens
p.currentPos = 0
if len(tokens) > 0 {
p.currentToken = tokens[0]
}
// Get a pre-allocated AST from the pool
result := ast.NewAST()
// Pre-allocate statements slice based on a reasonable estimate
estimatedStmts := 1 // Most SQL queries have just one statement
if len(tokens) > 100 {
estimatedStmts = 2 // For larger inputs, allocate more
}
result.Statements = make([]ast.Statement, 0, estimatedStmts)
// Parse statements using ModelType (int) comparisons for speed
for p.currentPos < len(tokens) && !p.isType(models.TokenTypeEOF) {
// Skip semicolons between statements
if p.isType(models.TokenTypeSemicolon) {
p.advance()
continue
}
stmt, err := p.parseStatement()
if err != nil {
// Clean up the AST on error
ast.ReleaseAST(result)
return nil, err
}
result.Statements = append(result.Statements, stmt)
// Optionally consume semicolon after statement
if p.isType(models.TokenTypeSemicolon) {
p.advance()
}
}
// Check if we got any statements
if len(result.Statements) == 0 {
ast.ReleaseAST(result)
return nil, fmt.Errorf("no SQL statements found")
}
return result, nil
}
// ParseContext parses the tokens into an AST with context support for cancellation.
// It checks the context at strategic points (every statement and expression) to enable fast cancellation.
// Returns context.Canceled or context.DeadlineExceeded when the context is cancelled.
//
// This method is useful for:
// - Long-running parsing operations that need to be cancellable
// - Implementing timeouts for parsing
// - Graceful shutdown scenarios
//
// Example:
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// astNode, err := parser.ParseContext(ctx, tokens)
// if err == context.DeadlineExceeded {
// // Handle timeout
// }
func (p *Parser) ParseContext(ctx context.Context, tokens []token.Token) (*ast.AST, error) {
// Check context before starting
if err := ctx.Err(); err != nil {
return nil, err
}
// Store context for use during parsing
p.ctx = ctx
defer func() { p.ctx = nil }() // Clear context when done
p.tokens = tokens
p.currentPos = 0
if len(tokens) > 0 {
p.currentToken = tokens[0]
}
// Get a pre-allocated AST from the pool
result := ast.NewAST()
// Pre-allocate statements slice based on a reasonable estimate
estimatedStmts := 1 // Most SQL queries have just one statement
if len(tokens) > 100 {
estimatedStmts = 2 // For larger inputs, allocate more
}
result.Statements = make([]ast.Statement, 0, estimatedStmts)
// Parse statements using ModelType (int) comparisons for speed
for p.currentPos < len(tokens) && !p.isType(models.TokenTypeEOF) {
// Check context before each statement
if err := ctx.Err(); err != nil {
// Clean up the AST on error
ast.ReleaseAST(result)
return nil, fmt.Errorf("parsing cancelled: %w", err)
}
// Skip semicolons between statements
if p.isType(models.TokenTypeSemicolon) {
p.advance()
continue
}
stmt, err := p.parseStatement()
if err != nil {
// Clean up the AST on error
ast.ReleaseAST(result)
return nil, err
}
result.Statements = append(result.Statements, stmt)
// Optionally consume semicolon after statement
if p.isType(models.TokenTypeSemicolon) {
p.advance()
}
}
// Check if we got any statements
if len(result.Statements) == 0 {
ast.ReleaseAST(result)
return nil, fmt.Errorf("no SQL statements found")
}
return result, nil
}
// Release releases any resources held by the parser
func (p *Parser) Release() {
// Reset internal state to avoid memory leaks
p.tokens = nil
p.currentPos = 0
p.currentToken = token.Token{}
p.depth = 0
p.ctx = nil
}
// parseStatement parses a single SQL statement
// Uses O(1) switch dispatch on ModelType (compiles to jump table) for optimal performance
func (p *Parser) parseStatement() (ast.Statement, error) {
// Check context if available
if p.ctx != nil {
if err := p.ctx.Err(); err != nil {
return nil, fmt.Errorf("parsing cancelled: %w", err)
}
}
// Fast path: O(1) switch dispatch on ModelType (compiles to jump table)
// This replaces the previous O(n) isAnyType + O(n) matchType approach
if p.currentToken.ModelType != modelTypeUnset {
switch p.currentToken.ModelType {
case models.TokenTypeWith:
return p.parseWithStatement()
case models.TokenTypeSelect:
p.advance()
return p.parseSelectWithSetOperations()
case models.TokenTypeInsert:
p.advance()
return p.parseInsertStatement()
case models.TokenTypeUpdate:
p.advance()
return p.parseUpdateStatement()
case models.TokenTypeDelete:
p.advance()
return p.parseDeleteStatement()
case models.TokenTypeAlter:
p.advance()
return p.parseAlterTableStmt()
case models.TokenTypeMerge:
p.advance()
return p.parseMergeStatement()
case models.TokenTypeCreate:
p.advance()
return p.parseCreateStatement()
case models.TokenTypeDrop:
p.advance()
return p.parseDropStatement()
case models.TokenTypeRefresh:
p.advance()
return p.parseRefreshStatement()
case models.TokenTypeTruncate:
p.advance()
return p.parseTruncateStatement()
}
// ModelType set but not a statement keyword - fall through to fallback
}
// Fallback: string comparison for tokens without ModelType (e.g., tests)
// or tokens with ModelType that aren't statement starters (e.g., operators)
if p.isType(models.TokenTypeWith) {
return p.parseWithStatement()
}
if p.matchType(models.TokenTypeSelect) {
return p.parseSelectWithSetOperations()
}
if p.matchType(models.TokenTypeInsert) {
return p.parseInsertStatement()
}
if p.matchType(models.TokenTypeUpdate) {
return p.parseUpdateStatement()
}
if p.matchType(models.TokenTypeDelete) {
return p.parseDeleteStatement()
}
if p.matchType(models.TokenTypeAlter) {
return p.parseAlterTableStmt()
}
if p.matchType(models.TokenTypeMerge) {
return p.parseMergeStatement()
}
if p.matchType(models.TokenTypeCreate) {
return p.parseCreateStatement()
}
if p.matchType(models.TokenTypeDrop) {
return p.parseDropStatement()
}
if p.matchType(models.TokenTypeRefresh) {
return p.parseRefreshStatement()
}
if p.matchType(models.TokenTypeTruncate) {
return p.parseTruncateStatement()
}
return nil, p.expectedError("statement")
}
// NewParser creates a new parser
func NewParser() *Parser {
return &Parser{}
}
// matchToken checks if the current token matches the expected type
func (p *Parser) matchToken(expected token.Type) bool {
// Convert both to strings for comparison
expectedStr := string(expected)
currentStr := string(p.currentToken.Type)
if currentStr == expectedStr {
p.advance()
return true
}
return false
}
// advance moves to the next token
func (p *Parser) advance() {
p.currentPos++
if p.currentPos < len(p.tokens) {
p.currentToken = p.tokens[p.currentPos]
}
}
// peekToken returns the next token without advancing the parser position.
// Returns an empty token if at the end of input.
func (p *Parser) peekToken() token.Token {
nextPos := p.currentPos + 1
if nextPos < len(p.tokens) {
return p.tokens[nextPos]
}
return token.Token{}
}
// =============================================================================
// ModelType-based Helper Methods (Phase 2 - Fast Int Comparisons)
// =============================================================================
// These methods use int-based ModelType comparisons which are significantly
// faster than string comparisons (~0.24ns vs ~3.4ns). Use these for hot paths.
// They include fallback to string-based Type comparison for backward compatibility
// with tests that create tokens directly without setting ModelType.
// modelTypeToString maps ModelType to expected string Type for fallback comparison.
// This comprehensive map enables isType() to work with tokens that don't have ModelType set
// (e.g., tokens created in tests without using the tokenizer).
// NOTE: Only TokenTypes that exist in models package are included here.
var modelTypeToString = map[models.TokenType]token.Type{
// Special tokens
models.TokenTypeEOF: token.EOF,
models.TokenTypeSemicolon: token.SEMICOLON,
models.TokenTypeIdentifier: "IDENT",
// Punctuation and operators
models.TokenTypeComma: token.COMMA,
models.TokenTypeLParen: "(",
models.TokenTypeRParen: ")",
models.TokenTypeEq: "=",
models.TokenTypeLt: "<",
models.TokenTypeGt: ">",
models.TokenTypeNeq: "!=",
models.TokenTypeLtEq: "<=",
models.TokenTypeGtEq: ">=",
models.TokenTypeDot: ".",
models.TokenTypeAsterisk: "*",
// Core SQL keywords
models.TokenTypeSelect: token.SELECT,
models.TokenTypeFrom: token.FROM,
models.TokenTypeWhere: token.WHERE,
models.TokenTypeInsert: token.INSERT,
models.TokenTypeUpdate: token.UPDATE,
models.TokenTypeDelete: token.DELETE,
models.TokenTypeInto: "INTO",
models.TokenTypeValues: "VALUES",
models.TokenTypeSet: "SET",
models.TokenTypeAs: "AS",
models.TokenTypeOn: "ON",
// DDL keywords
models.TokenTypeCreate: "CREATE",
models.TokenTypeAlter: token.ALTER,
models.TokenTypeDrop: token.DROP,
models.TokenTypeTruncate: "TRUNCATE",
models.TokenTypeTable: "TABLE",
models.TokenTypeIndex: "INDEX",
models.TokenTypeView: "VIEW",
models.TokenTypePrimary: "PRIMARY",
models.TokenTypeForeign: "FOREIGN",
models.TokenTypeUnique: "UNIQUE",
models.TokenTypeCheck: "CHECK",
models.TokenTypeConstraint: "CONSTRAINT",
models.TokenTypeDefault: "DEFAULT",
models.TokenTypeReferences: "REFERENCES",
models.TokenTypeCascade: "CASCADE",
models.TokenTypeRestrict: "RESTRICT",
models.TokenTypeMaterialized: "MATERIALIZED",
models.TokenTypeReplace: "REPLACE",
models.TokenTypeCollate: "COLLATE",
// Clause keywords
models.TokenTypeGroup: "GROUP",
models.TokenTypeBy: "BY",
models.TokenTypeHaving: "HAVING",
models.TokenTypeOrder: "ORDER",
models.TokenTypeAsc: "ASC",
models.TokenTypeDesc: "DESC",
models.TokenTypeLimit: "LIMIT",
models.TokenTypeOffset: "OFFSET",
models.TokenTypeDistinct: "DISTINCT",
// JOIN keywords
models.TokenTypeJoin: "JOIN",
models.TokenTypeInner: "INNER",
models.TokenTypeLeft: "LEFT",
models.TokenTypeRight: "RIGHT",
models.TokenTypeFull: "FULL",
models.TokenTypeOuter: "OUTER",
models.TokenTypeCross: "CROSS",
models.TokenTypeNatural: "NATURAL",
models.TokenTypeUsing: "USING",
// Set operations
models.TokenTypeUnion: "UNION",
models.TokenTypeExcept: "EXCEPT",
models.TokenTypeIntersect: "INTERSECT",
models.TokenTypeAll: "ALL",
// Logical operators
models.TokenTypeAnd: "AND",
models.TokenTypeOr: "OR",
models.TokenTypeNot: "NOT",
// Comparison operators
models.TokenTypeIs: "IS",
models.TokenTypeIn: "IN",
models.TokenTypeLike: "LIKE",
models.TokenTypeBetween: "BETWEEN",
models.TokenTypeExists: "EXISTS",
models.TokenTypeAny: "ANY",
// NULL and boolean
models.TokenTypeNull: "NULL",
models.TokenTypeTrue: "TRUE",
models.TokenTypeFalse: "FALSE",
// Window function keywords
models.TokenTypeOver: "OVER",
models.TokenTypePartition: "PARTITION",
models.TokenTypeRows: "ROWS",
models.TokenTypeRange: "RANGE",
models.TokenTypeUnbounded: "UNBOUNDED",
models.TokenTypePreceding: "PRECEDING",
models.TokenTypeFollowing: "FOLLOWING",
models.TokenTypeCurrent: "CURRENT",
models.TokenTypeRow: "ROW",
models.TokenTypeNulls: "NULLS",
models.TokenTypeFirst: "FIRST",
models.TokenTypeLast: "LAST",
models.TokenTypeFilter: "FILTER",
// Placeholder token - maps to "PLACEHOLDER" for tests that create tokens manually
models.TokenTypePlaceholder: "PLACEHOLDER",
// CTE keywords
models.TokenTypeWith: token.WITH,
models.TokenTypeRecursive: "RECURSIVE",
// CASE expression
models.TokenTypeCase: "CASE",
models.TokenTypeWhen: "WHEN",
models.TokenTypeThen: "THEN",
models.TokenTypeElse: "ELSE",
models.TokenTypeEnd: "END",
// MERGE keywords
models.TokenTypeMerge: "MERGE",
models.TokenTypeMatched: "MATCHED",
models.TokenTypeSource: "SOURCE",
models.TokenTypeTarget: "TARGET",
// Grouping keywords
models.TokenTypeRollup: "ROLLUP",
models.TokenTypeCube: "CUBE",
models.TokenTypeGrouping: "GROUPING",
models.TokenTypeGroupingSets: "GROUPING SETS",
models.TokenTypeSets: "SETS",
// Data types
models.TokenTypeInt: "INT",
models.TokenTypeInteger: "INTEGER",
models.TokenTypeVarchar: "VARCHAR",
models.TokenTypeText: "TEXT",
models.TokenTypeBoolean: "BOOLEAN",
// FETCH clause keywords (SQL-99 F861, F862)
models.TokenTypeFetch: "FETCH",
models.TokenTypeNext: "NEXT",
models.TokenTypeTies: "TIES",
models.TokenTypePercent: "PERCENT",
models.TokenTypeOnly: "ONLY",
// Other keywords
models.TokenTypeIf: "IF",
models.TokenTypeRefresh: "REFRESH",
models.TokenTypeTo: "TO",
}
// isType checks if the current token's ModelType matches the expected type.
// Falls back to string comparison if ModelType is not set (for backward compatibility).
func (p *Parser) isType(expected models.TokenType) bool {
// Fast path: use int comparison if ModelType is set
if p.currentToken.ModelType != modelTypeUnset {
return p.currentToken.ModelType == expected
}
// Fallback: string comparison for tokens without ModelType
if str, ok := modelTypeToString[expected]; ok {
return p.currentToken.Type == str
}
return false
}
// isAnyType checks if the current token's ModelType matches any of the given types.
// More efficient than multiple isType calls when checking many alternatives.
func (p *Parser) isAnyType(types ...models.TokenType) bool {
for _, t := range types {
if p.isType(t) {
return true
}
}
return false
}
// matchType checks if the current token's ModelType matches and advances if true.
// Returns true if matched (and advanced), false otherwise.
func (p *Parser) matchType(expected models.TokenType) bool {
if p.isType(expected) {
p.advance()
return true
}
return false
}
// isComparisonOperator checks if the current token is a comparison operator using O(1) switch.
// This is a hot path optimization for expression parsing.
func (p *Parser) isComparisonOperator() bool {
// Fast path: use ModelType switch for O(1) lookup
if p.currentToken.ModelType != modelTypeUnset {
switch p.currentToken.ModelType {
case models.TokenTypeEq, models.TokenTypeLt, models.TokenTypeGt,
models.TokenTypeNeq, models.TokenTypeLtEq, models.TokenTypeGtEq:
return true
}
return false
}
// Fallback: string comparison for tokens without ModelType (e.g., tests)
switch p.currentToken.Type {
case "=", "<", ">", "!=", "<=", ">=", "<>":
return true
}
return false
}
// isQuantifier checks if the current token is ANY or ALL using O(1) switch.
// This is used for subquery quantifier operators like "= ANY (...)".
func (p *Parser) isQuantifier() bool {
// Fast path: use ModelType switch for O(1) lookup
if p.currentToken.ModelType != modelTypeUnset {
switch p.currentToken.ModelType {
case models.TokenTypeAny, models.TokenTypeAll:
return true
}
return false
}
// Fallback: string comparison for tokens without ModelType
upper := strings.ToUpper(p.currentToken.Literal)
return upper == "ANY" || upper == "ALL"
}
// isBooleanLiteral checks if the current token is TRUE or FALSE using O(1) switch.
// This is used for parsing boolean literal values in expressions.
func (p *Parser) isBooleanLiteral() bool {
// Fast path: use ModelType switch for O(1) lookup
if p.currentToken.ModelType != modelTypeUnset {
switch p.currentToken.ModelType {
case models.TokenTypeTrue, models.TokenTypeFalse:
return true
}
return false
}
// Fallback: string comparison for tokens without ModelType
upper := strings.ToUpper(p.currentToken.Literal)
return upper == "TRUE" || upper == "FALSE"
}
// =============================================================================
// expectedError returns an error for unexpected token
func (p *Parser) expectedError(expected string) error {
return fmt.Errorf("expected %s, got %s", expected, p.currentToken.Type)
}
// parseIdent parses an identifier
func (p *Parser) parseIdent() *ast.Identifier {
if p.currentToken.Type != token.IDENT && p.currentToken.Type != "IDENT" {
return nil
}
ident := &ast.Identifier{Name: p.currentToken.Literal}
p.advance()
return ident
}
// parseIdentAsString parses an identifier and returns its name as a string
func (p *Parser) parseIdentAsString() string {
ident := p.parseIdent()
if ident == nil {
return ""
}
return ident.Name
}
// parseObjectName parses an object name (possibly qualified)
func (p *Parser) parseObjectName() ast.ObjectName {
ident := p.parseIdent()
if ident == nil {
return ast.ObjectName{}
}
return ast.ObjectName{Name: ident.Name}
}
// parseStringLiteral parses a string literal
func (p *Parser) parseStringLiteral() string {
if p.currentToken.Type != token.STRING && p.currentToken.Type != "STRING" {
return ""
}
value := p.currentToken.Literal
p.advance()
return value
}
// Accepts IDENT or non-reserved keywords that can be used as table names
func (p *Parser) parseTableReference() (*ast.TableReference, error) {
// Accept IDENT or keywords that can be used as table names
if p.currentToken.Type != "IDENT" && !p.isNonReservedKeyword() {
return nil, p.expectedError("table name")
}
tableName := p.currentToken.Literal
p.advance()
return &ast.TableReference{Name: tableName}, nil
}
// isNonReservedKeyword checks if current token is a non-reserved keyword
// that can be used as a table or column name
func (p *Parser) isNonReservedKeyword() bool {
// These keywords can be used as table/column names in most SQL dialects
// Check uppercase version of token type for case-insensitive matching
upperType := strings.ToUpper(string(p.currentToken.Type))
switch upperType {
case "TARGET", "SOURCE", "MATCHED", "VALUE", "NAME", "TYPE", "STATUS":
return true
default:
return false
}
}
// canBeAlias checks if current token can be used as an alias
// Aliases can be IDENT or certain non-reserved keywords
func (p *Parser) canBeAlias() bool {
return p.currentToken.Type == "IDENT" || p.isNonReservedKeyword()
}
// parseAlterTableStmt is a simplified version for the parser implementation
// It delegates to the more comprehensive parseAlterStatement in alter.go
func (p *Parser) parseAlterTableStmt() (ast.Statement, error) {
// We've already consumed the ALTER token in matchToken
// This is just a placeholder that delegates to the main implementation
return p.parseAlterStatement()
}
// isJoinKeyword checks if current token is a JOIN-related keyword
func (p *Parser) isJoinKeyword() bool {
switch p.currentToken.Type {
case "JOIN", "INNER", "LEFT", "RIGHT", "FULL", "CROSS":
return true
default:
return false
}
}
// parseWithStatement parses a WITH statement (Common Table Expression).
// It supports both simple and recursive CTEs, multiple CTE definitions, and column specifications.
//
// Examples:
//
// WITH sales_summary AS (SELECT region, total FROM sales) SELECT * FROM sales_summary
// WITH RECURSIVE emp_tree AS (SELECT emp_id FROM employees) SELECT * FROM emp_tree
// WITH first AS (SELECT * FROM t1), second AS (SELECT * FROM first) SELECT * FROM second
// WITH summary(region, total) AS (SELECT region, SUM(amount) FROM sales GROUP BY region) SELECT * FROM summary