Skip to content

Commit 2391715

Browse files
committed
feat: add structured MATCH_RECOGNIZE support
1 parent 02b9d94 commit 2391715

60 files changed

Lines changed: 3003 additions & 87 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,14 @@ default void visit(ExtractExpression extractExpression) {
510510
this.visit(extractExpression, null);
511511
}
512512

513+
default <S> T visit(RowPatternFunction function, S context) {
514+
return function.getFunction().accept(this, context);
515+
}
516+
517+
default void visit(RowPatternFunction function) {
518+
visit(function, null);
519+
}
520+
513521
<S> T visit(IntervalExpression intervalExpression, S context);
514522

515523
default void visit(IntervalExpression intervalExpression) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.expression;
11+
12+
import java.util.function.Consumer;
13+
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
14+
15+
/** An explicit RUNNING or FINAL evaluation mode on a row-pattern function. */
16+
public class RowPatternFunction extends ASTNodeAccessImpl implements Expression {
17+
public enum EvaluationMode {
18+
RUNNING, FINAL
19+
}
20+
21+
private EvaluationMode evaluationMode;
22+
private Function function;
23+
24+
public RowPatternFunction(EvaluationMode evaluationMode, Function function) {
25+
this.evaluationMode = evaluationMode;
26+
this.function = function;
27+
}
28+
29+
public EvaluationMode getEvaluationMode() {
30+
return evaluationMode;
31+
}
32+
33+
public RowPatternFunction setEvaluationMode(EvaluationMode mode) {
34+
evaluationMode = mode;
35+
return this;
36+
}
37+
38+
public Function getFunction() {
39+
return function;
40+
}
41+
42+
public RowPatternFunction setFunction(Function function) {
43+
this.function = function;
44+
return this;
45+
}
46+
47+
@Override
48+
public <T, S> T accept(ExpressionVisitor<T> visitor, S context) {
49+
return visitor.visit(this, context);
50+
}
51+
52+
public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressions) {
53+
builder.append(evaluationMode).append(' ');
54+
expressions.accept(function);
55+
return builder;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
StringBuilder builder = new StringBuilder();
61+
return appendTo(builder, expression -> builder.append(expression)).toString();
62+
}
63+
}

src/main/java/net/sf/jsqlparser/expression/StructType.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818
import java.util.Map;
19+
import java.util.function.Consumer;
1920

2021
/*
2122
* STRUCT<T>
@@ -113,6 +114,11 @@ public StructType add(Expression expression, String aliasName) {
113114
}
114115

115116
public StringBuilder appendTo(StringBuilder builder) {
117+
return appendTo(builder, expression -> builder.append(expression));
118+
}
119+
120+
/** Renders current argument expressions through the caller's visitor. */
121+
public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressions) {
116122
if (dialect != Dialect.DUCKDB && keyword != null) {
117123
builder.append(keyword);
118124
}
@@ -148,7 +154,7 @@ public StringBuilder appendTo(StringBuilder builder) {
148154
}
149155
builder.append(e.getAlias().getName());
150156
builder.append(":");
151-
builder.append(e.getExpression());
157+
expressions.accept(e.getExpression());
152158
}
153159
builder.append(" }");
154160
} else {
@@ -158,7 +164,10 @@ public StringBuilder appendTo(StringBuilder builder) {
158164
if (0 < i++) {
159165
builder.append(",");
160166
}
161-
e.appendTo(builder);
167+
expressions.accept(e.getExpression());
168+
if (e.getAlias() != null) {
169+
builder.append(e.getAlias());
170+
}
162171
}
163172

164173
builder.append(")");
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/main/java/net/sf/jsqlparser/parser/feature/Feature.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public enum Feature {
7171
* "SELECT"
7272
*/
7373
select,
74+
/** Structured FROM row pattern recognition. */
75+
matchRecognize,
76+
/** BigQuery longest-match option. */
77+
matchRecognizeOptions,
7478
/**
7579
* "GROUP BY"
7680
*/

src/main/java/net/sf/jsqlparser/statement/select/FromItemVisitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ default <S> T visitJoins(Collection<Join> joins, S context) {
4242
return null;
4343
}
4444

45+
/**
46+
* Existing visitors continue through the input relation; adapters also visit clause
47+
* expressions.
48+
*/
49+
default <S> T visit(MatchRecognize matchRecognize, S context) {
50+
return matchRecognize.getInput().accept(this, context);
51+
}
52+
53+
default void visit(MatchRecognize matchRecognize) {
54+
visit(matchRecognize, null);
55+
}
56+
4557
<S> T visit(Table tableName, S context);
4658

4759
default void visit(Table tableName) {

src/main/java/net/sf/jsqlparser/statement/select/FromItemVisitorAdapter.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,34 @@ public <S> T visitJoins(Collection<Join> joins, S context) {
8585
return null;
8686
}
8787

88+
@Override
89+
public <S> T visit(MatchRecognize matchRecognize, S context) {
90+
matchRecognize.getInput().accept(this, context);
91+
matchRecognize
92+
.forEachExpression(expression -> expression.accept(expressionVisitor, context));
93+
PivotVisitorAdapter<T> pivots = new PivotVisitorAdapter<>(expressionVisitor);
94+
if (matchRecognize.getPivot() != null) {
95+
matchRecognize.getPivot().accept(pivots, context);
96+
}
97+
if (matchRecognize.getUnPivot() != null) {
98+
matchRecognize.getUnPivot().accept(pivots, context);
99+
}
100+
return null;
101+
}
102+
88103
@Override
89104
public <S> T visit(Table table, S context) {
90105
return null;
91106
}
92107

93108
@Override
94109
public <S> T visit(ParenthesedSelect select, S context) {
95-
return select.getPlainSelect().accept(selectVisitor, context);
110+
return select.getSelect().accept(selectVisitor, context);
96111
}
97112

98113
@Override
99114
public <S> T visit(LateralSubSelect lateralSubSelect, S context) {
100-
return lateralSubSelect.getPlainSelect().accept(selectVisitor, context);
115+
return lateralSubSelect.getSelect().accept(selectVisitor, context);
101116
}
102117

103118
@Override
@@ -108,7 +123,9 @@ public <S> T visit(TableFunction tableFunction, S context) {
108123

109124
@Override
110125
public <S> T visit(ParenthesedFromItem fromItem, S context) {
111-
return fromItem.getFromItem().accept(this, context);
126+
T result = fromItem.getFromItem().accept(this, context);
127+
visitJoins(fromItem.getJoins(), context);
128+
return result;
112129
}
113130

114131
@Override

0 commit comments

Comments
 (0)