|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.parser; |
| 11 | + |
| 12 | +import java.util.ArrayDeque; |
| 13 | +import java.util.Deque; |
| 14 | + |
| 15 | +/** |
| 16 | + * Recognizes the BigQuery PATTERN lexical region even during JavaCC lookahead. The required initial |
| 17 | + * ORDER BY (or optional PARTITION BY) distinguishes a clause from an ordinary function named |
| 18 | + * MATCH_RECOGNIZE. Quoted tokens never change this state. |
| 19 | + */ |
| 20 | +final class RowPatternTokenContext implements CCJSqlParserConstants { |
| 21 | + private final Deque<Frame> frames = new ArrayDeque<>(); |
| 22 | + private int depth; |
| 23 | + private boolean pendingClause; |
| 24 | + |
| 25 | + private static final class Frame { |
| 26 | + private final int depth; |
| 27 | + private int phase; |
| 28 | + private int patternDepth = -1; |
| 29 | + private boolean pendingPattern; |
| 30 | + private boolean patternComplete; |
| 31 | + |
| 32 | + private Frame(int depth) { |
| 33 | + this.depth = depth; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + boolean isIdle() { |
| 38 | + return frames.isEmpty() && !pendingClause; |
| 39 | + } |
| 40 | + |
| 41 | + boolean isInPattern() { |
| 42 | + return !frames.isEmpty() && frames.peek().patternDepth >= 0; |
| 43 | + } |
| 44 | + |
| 45 | + void accept(Token token) { |
| 46 | + if (token.kind == OPENING_BRACKET) { |
| 47 | + depth++; |
| 48 | + if (pendingClause) { |
| 49 | + frames.push(new Frame(depth)); |
| 50 | + pendingClause = false; |
| 51 | + return; |
| 52 | + } |
| 53 | + if (!frames.isEmpty() && frames.peek().pendingPattern) { |
| 54 | + frames.peek().patternDepth = depth; |
| 55 | + frames.peek().pendingPattern = false; |
| 56 | + } |
| 57 | + } else if (token.kind == CLOSING_BRACKET) { |
| 58 | + if (!frames.isEmpty()) { |
| 59 | + Frame frame = frames.peek(); |
| 60 | + if (frame.patternDepth == depth) { |
| 61 | + frame.patternDepth = -1; |
| 62 | + frame.patternComplete = true; |
| 63 | + } |
| 64 | + if (frame.depth == depth) { |
| 65 | + frames.pop(); |
| 66 | + } |
| 67 | + } |
| 68 | + depth--; |
| 69 | + } else if (!frames.isEmpty() && depth == frames.peek().depth) { |
| 70 | + Frame frame = frames.peek(); |
| 71 | + if (frame.phase == 0) { |
| 72 | + frame.phase = token.kind == K_ORDER || token.kind == K_PARTITION ? 1 : -1; |
| 73 | + } else if (frame.phase == 1) { |
| 74 | + frame.phase = token.kind == K_BY ? 2 : -1; |
| 75 | + } else if (frame.phase == 2 && !frame.patternComplete) { |
| 76 | + frame.pendingPattern = token.kind == S_IDENTIFIER |
| 77 | + && "PATTERN".equalsIgnoreCase(token.image); |
| 78 | + } |
| 79 | + } |
| 80 | + pendingClause = token.kind == S_IDENTIFIER |
| 81 | + && "MATCH_RECOGNIZE".equalsIgnoreCase(token.image); |
| 82 | + } |
| 83 | +} |
0 commit comments