|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Package parser - match_recognize.go |
| 10 | +// SQL:2016 MATCH_RECOGNIZE clause for row-pattern recognition (Snowflake, Oracle). |
| 11 | + |
| 12 | +package parser |
| 13 | + |
| 14 | +import ( |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/ajitpratap0/GoSQLX/pkg/models" |
| 18 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/ast" |
| 19 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 20 | +) |
| 21 | + |
| 22 | +// isMatchRecognizeKeyword returns true if the current token is the contextual |
| 23 | +// MATCH_RECOGNIZE keyword in a dialect that supports it. |
| 24 | +func (p *Parser) isMatchRecognizeKeyword() bool { |
| 25 | + if p.dialect != string(keywords.DialectSnowflake) && |
| 26 | + p.dialect != string(keywords.DialectOracle) { |
| 27 | + return false |
| 28 | + } |
| 29 | + return strings.EqualFold(p.currentToken.Token.Value, "MATCH_RECOGNIZE") |
| 30 | +} |
| 31 | + |
| 32 | +// parseMatchRecognize parses the MATCH_RECOGNIZE clause. The current token |
| 33 | +// must be MATCH_RECOGNIZE. |
| 34 | +// |
| 35 | +// Grammar: |
| 36 | +// |
| 37 | +// MATCH_RECOGNIZE ( |
| 38 | +// [PARTITION BY expr, ...] |
| 39 | +// [ORDER BY expr [ASC|DESC], ...] |
| 40 | +// [MEASURES measure_expr AS alias, ...] |
| 41 | +// [ONE ROW PER MATCH | ALL ROWS PER MATCH] |
| 42 | +// [AFTER MATCH SKIP ...] |
| 43 | +// PATTERN ( pattern_regex ) |
| 44 | +// DEFINE var AS condition, ... |
| 45 | +// ) |
| 46 | +func (p *Parser) parseMatchRecognize() (*ast.MatchRecognizeClause, error) { |
| 47 | + pos := p.currentLocation() |
| 48 | + p.advance() // Consume MATCH_RECOGNIZE |
| 49 | + |
| 50 | + if !p.isType(models.TokenTypeLParen) { |
| 51 | + return nil, p.expectedError("( after MATCH_RECOGNIZE") |
| 52 | + } |
| 53 | + p.advance() // Consume ( |
| 54 | + |
| 55 | + clause := &ast.MatchRecognizeClause{Pos: pos} |
| 56 | + |
| 57 | + // Parse sub-clauses in order. Each is optional except PATTERN and DEFINE. |
| 58 | + // PARTITION BY |
| 59 | + if p.isType(models.TokenTypePartition) { |
| 60 | + p.advance() // PARTITION |
| 61 | + if p.isType(models.TokenTypeBy) { |
| 62 | + p.advance() // BY |
| 63 | + } |
| 64 | + for { |
| 65 | + expr, err := p.parseExpression() |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + clause.PartitionBy = append(clause.PartitionBy, expr) |
| 70 | + if !p.isType(models.TokenTypeComma) { |
| 71 | + break |
| 72 | + } |
| 73 | + p.advance() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // ORDER BY |
| 78 | + if p.isType(models.TokenTypeOrder) { |
| 79 | + p.advance() // ORDER |
| 80 | + if p.isType(models.TokenTypeBy) { |
| 81 | + p.advance() // BY |
| 82 | + } |
| 83 | + for { |
| 84 | + expr, err := p.parseExpression() |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + entry := ast.OrderByExpression{Expression: expr, Ascending: true} |
| 89 | + if p.isType(models.TokenTypeAsc) { |
| 90 | + p.advance() |
| 91 | + } else if p.isType(models.TokenTypeDesc) { |
| 92 | + entry.Ascending = false |
| 93 | + p.advance() |
| 94 | + } |
| 95 | + clause.OrderBy = append(clause.OrderBy, entry) |
| 96 | + if !p.isType(models.TokenTypeComma) { |
| 97 | + break |
| 98 | + } |
| 99 | + p.advance() |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // MEASURES |
| 104 | + if strings.EqualFold(p.currentToken.Token.Value, "MEASURES") { |
| 105 | + p.advance() // MEASURES |
| 106 | + for { |
| 107 | + expr, err := p.parseExpression() |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + alias := "" |
| 112 | + if p.isType(models.TokenTypeAs) { |
| 113 | + p.advance() // AS |
| 114 | + alias = p.currentToken.Token.Value |
| 115 | + p.advance() // alias name |
| 116 | + } |
| 117 | + clause.Measures = append(clause.Measures, ast.MeasureDef{ |
| 118 | + Expr: expr, |
| 119 | + Alias: alias, |
| 120 | + }) |
| 121 | + if !p.isType(models.TokenTypeComma) { |
| 122 | + break |
| 123 | + } |
| 124 | + p.advance() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // ONE ROW PER MATCH / ALL ROWS PER MATCH |
| 129 | + if strings.EqualFold(p.currentToken.Token.Value, "ONE") { |
| 130 | + clause.RowsPerMatch = "ONE ROW PER MATCH" |
| 131 | + p.advance() // ONE |
| 132 | + p.advance() // ROW |
| 133 | + p.advance() // PER |
| 134 | + p.advance() // MATCH |
| 135 | + } else if p.isType(models.TokenTypeAll) { |
| 136 | + clause.RowsPerMatch = "ALL ROWS PER MATCH" |
| 137 | + p.advance() // ALL |
| 138 | + p.advance() // ROWS |
| 139 | + p.advance() // PER |
| 140 | + p.advance() // MATCH |
| 141 | + } |
| 142 | + |
| 143 | + // AFTER MATCH SKIP ... — consume as raw text until PATTERN or DEFINE |
| 144 | + if strings.EqualFold(p.currentToken.Token.Value, "AFTER") { |
| 145 | + var parts []string |
| 146 | + for { |
| 147 | + val := strings.ToUpper(p.currentToken.Token.Value) |
| 148 | + if val == "PATTERN" || val == "DEFINE" { |
| 149 | + break |
| 150 | + } |
| 151 | + if p.isType(models.TokenTypeEOF) || p.isType(models.TokenTypeRParen) { |
| 152 | + break |
| 153 | + } |
| 154 | + parts = append(parts, p.currentToken.Token.Value) |
| 155 | + p.advance() |
| 156 | + } |
| 157 | + clause.AfterMatch = strings.Join(parts, " ") |
| 158 | + } |
| 159 | + |
| 160 | + // PATTERN ( regex ) |
| 161 | + if strings.EqualFold(p.currentToken.Token.Value, "PATTERN") { |
| 162 | + p.advance() // PATTERN |
| 163 | + if !p.isType(models.TokenTypeLParen) { |
| 164 | + return nil, p.expectedError("( after PATTERN") |
| 165 | + } |
| 166 | + p.advance() // Consume ( |
| 167 | + |
| 168 | + // Collect pattern tokens as raw text until the matching ')' |
| 169 | + var patParts []string |
| 170 | + depth := 1 |
| 171 | + for depth > 0 { |
| 172 | + if p.isType(models.TokenTypeEOF) { |
| 173 | + return nil, p.expectedError(") to close PATTERN") |
| 174 | + } |
| 175 | + if p.isType(models.TokenTypeLParen) { |
| 176 | + depth++ |
| 177 | + patParts = append(patParts, "(") |
| 178 | + } else if p.isType(models.TokenTypeRParen) { |
| 179 | + depth-- |
| 180 | + if depth > 0 { |
| 181 | + patParts = append(patParts, ")") |
| 182 | + } |
| 183 | + } else { |
| 184 | + patParts = append(patParts, p.currentToken.Token.Value) |
| 185 | + } |
| 186 | + p.advance() |
| 187 | + } |
| 188 | + clause.Pattern = strings.Join(patParts, " ") |
| 189 | + } |
| 190 | + |
| 191 | + // DEFINE var AS condition, ... |
| 192 | + if strings.EqualFold(p.currentToken.Token.Value, "DEFINE") { |
| 193 | + p.advance() // DEFINE |
| 194 | + for { |
| 195 | + if p.isType(models.TokenTypeRParen) || p.isType(models.TokenTypeEOF) { |
| 196 | + break |
| 197 | + } |
| 198 | + name := p.currentToken.Token.Value |
| 199 | + p.advance() // variable name |
| 200 | + |
| 201 | + if !p.isType(models.TokenTypeAs) { |
| 202 | + return nil, p.expectedError("AS after pattern variable " + name) |
| 203 | + } |
| 204 | + p.advance() // AS |
| 205 | + |
| 206 | + cond, err := p.parseExpression() |
| 207 | + if err != nil { |
| 208 | + return nil, err |
| 209 | + } |
| 210 | + clause.Definitions = append(clause.Definitions, ast.PatternDef{ |
| 211 | + Name: name, |
| 212 | + Condition: cond, |
| 213 | + }) |
| 214 | + if !p.isType(models.TokenTypeComma) { |
| 215 | + break |
| 216 | + } |
| 217 | + p.advance() |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + // Expect closing ) |
| 222 | + if !p.isType(models.TokenTypeRParen) { |
| 223 | + return nil, p.expectedError(") to close MATCH_RECOGNIZE") |
| 224 | + } |
| 225 | + p.advance() // Consume ) |
| 226 | + |
| 227 | + return clause, nil |
| 228 | +} |
0 commit comments