-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmatch_recognize.go
More file actions
228 lines (208 loc) · 5.66 KB
/
match_recognize.go
File metadata and controls
228 lines (208 loc) · 5.66 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
// 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
// Package parser - match_recognize.go
// SQL:2016 MATCH_RECOGNIZE clause for row-pattern recognition (Snowflake, Oracle).
package parser
import (
"strings"
"github.com/ajitpratap0/GoSQLX/pkg/models"
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
"github.com/ajitpratap0/GoSQLX/pkg/sql/keywords"
)
// isMatchRecognizeKeyword returns true if the current token is the contextual
// MATCH_RECOGNIZE keyword in a dialect that supports it.
func (p *Parser) isMatchRecognizeKeyword() bool {
if p.dialect != string(keywords.DialectSnowflake) &&
p.dialect != string(keywords.DialectOracle) {
return false
}
return strings.EqualFold(p.currentToken.Token.Value, "MATCH_RECOGNIZE")
}
// parseMatchRecognize parses the MATCH_RECOGNIZE clause. The current token
// must be MATCH_RECOGNIZE.
//
// Grammar:
//
// MATCH_RECOGNIZE (
// [PARTITION BY expr, ...]
// [ORDER BY expr [ASC|DESC], ...]
// [MEASURES measure_expr AS alias, ...]
// [ONE ROW PER MATCH | ALL ROWS PER MATCH]
// [AFTER MATCH SKIP ...]
// PATTERN ( pattern_regex )
// DEFINE var AS condition, ...
// )
func (p *Parser) parseMatchRecognize() (*ast.MatchRecognizeClause, error) {
pos := p.currentLocation()
p.advance() // Consume MATCH_RECOGNIZE
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("( after MATCH_RECOGNIZE")
}
p.advance() // Consume (
clause := &ast.MatchRecognizeClause{Pos: pos}
// Parse sub-clauses in order. Each is optional except PATTERN and DEFINE.
// PARTITION BY
if p.isType(models.TokenTypePartition) {
p.advance() // PARTITION
if p.isType(models.TokenTypeBy) {
p.advance() // BY
}
for {
expr, err := p.parseExpression()
if err != nil {
return nil, err
}
clause.PartitionBy = append(clause.PartitionBy, expr)
if !p.isType(models.TokenTypeComma) {
break
}
p.advance()
}
}
// ORDER BY
if p.isType(models.TokenTypeOrder) {
p.advance() // ORDER
if p.isType(models.TokenTypeBy) {
p.advance() // BY
}
for {
expr, err := p.parseExpression()
if err != nil {
return nil, err
}
entry := ast.OrderByExpression{Expression: expr, Ascending: true}
if p.isType(models.TokenTypeAsc) {
p.advance()
} else if p.isType(models.TokenTypeDesc) {
entry.Ascending = false
p.advance()
}
clause.OrderBy = append(clause.OrderBy, entry)
if !p.isType(models.TokenTypeComma) {
break
}
p.advance()
}
}
// MEASURES
if strings.EqualFold(p.currentToken.Token.Value, "MEASURES") {
p.advance() // MEASURES
for {
expr, err := p.parseExpression()
if err != nil {
return nil, err
}
alias := ""
if p.isType(models.TokenTypeAs) {
p.advance() // AS
alias = p.currentToken.Token.Value
p.advance() // alias name
}
clause.Measures = append(clause.Measures, ast.MeasureDef{
Expr: expr,
Alias: alias,
})
if !p.isType(models.TokenTypeComma) {
break
}
p.advance()
}
}
// ONE ROW PER MATCH / ALL ROWS PER MATCH
if strings.EqualFold(p.currentToken.Token.Value, "ONE") {
clause.RowsPerMatch = "ONE ROW PER MATCH"
p.advance() // ONE
p.advance() // ROW
p.advance() // PER
p.advance() // MATCH
} else if p.isType(models.TokenTypeAll) {
clause.RowsPerMatch = "ALL ROWS PER MATCH"
p.advance() // ALL
p.advance() // ROWS
p.advance() // PER
p.advance() // MATCH
}
// AFTER MATCH SKIP ... — consume as raw text until PATTERN or DEFINE
if strings.EqualFold(p.currentToken.Token.Value, "AFTER") {
var parts []string
for {
val := strings.ToUpper(p.currentToken.Token.Value)
if val == "PATTERN" || val == "DEFINE" {
break
}
if p.isType(models.TokenTypeEOF) || p.isType(models.TokenTypeRParen) {
break
}
parts = append(parts, p.currentToken.Token.Value)
p.advance()
}
clause.AfterMatch = strings.Join(parts, " ")
}
// PATTERN ( regex )
if strings.EqualFold(p.currentToken.Token.Value, "PATTERN") {
p.advance() // PATTERN
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("( after PATTERN")
}
p.advance() // Consume (
// Collect pattern tokens as raw text until the matching ')'
var patParts []string
depth := 1
for depth > 0 {
if p.isType(models.TokenTypeEOF) {
return nil, p.expectedError(") to close PATTERN")
}
if p.isType(models.TokenTypeLParen) {
depth++
patParts = append(patParts, "(")
} else if p.isType(models.TokenTypeRParen) {
depth--
if depth > 0 {
patParts = append(patParts, ")")
}
} else {
patParts = append(patParts, p.currentToken.Token.Value)
}
p.advance()
}
clause.Pattern = strings.Join(patParts, " ")
}
// DEFINE var AS condition, ...
if strings.EqualFold(p.currentToken.Token.Value, "DEFINE") {
p.advance() // DEFINE
for {
if p.isType(models.TokenTypeRParen) || p.isType(models.TokenTypeEOF) {
break
}
name := p.currentToken.Token.Value
p.advance() // variable name
if !p.isType(models.TokenTypeAs) {
return nil, p.expectedError("AS after pattern variable " + name)
}
p.advance() // AS
cond, err := p.parseExpression()
if err != nil {
return nil, err
}
clause.Definitions = append(clause.Definitions, ast.PatternDef{
Name: name,
Condition: cond,
})
if !p.isType(models.TokenTypeComma) {
break
}
p.advance()
}
}
// Expect closing )
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(") to close MATCH_RECOGNIZE")
}
p.advance() // Consume )
return clause, nil
}