Skip to content

Commit 03396b6

Browse files
authored
feat: add structured MATCH_RECOGNIZE support (#2634)
* feat: add structured MATCH_RECOGNIZE support * refactor: simplify row-pattern rendering and validation * refactor: isolate row-pattern validation visitor
1 parent 6312f9e commit 03396b6

61 files changed

Lines changed: 3112 additions & 184 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.

.codacy.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ engines:
44
exclude_paths:
55
# These fixtures use PostgreSQL grammar, not Transact-SQL.
66
- "src/test/resources/postgresql/**"
7+
# MATCH_RECOGNIZE fixtures use Oracle and BigQuery grammar.
8+
- "src/test/resources/net/sf/jsqlparser/statement/select/match-recognize/**"
9+
- "src/test/resources/net/sf/jsqlparser/statement/select/match-recognize-2350.sql"
710
exclude_paths:
811
- "site/**"

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: 48 additions & 58 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,73 +114,62 @@ public StructType add(Expression expression, String aliasName) {
113114
}
114115

115116
public StringBuilder appendTo(StringBuilder builder) {
116-
if (dialect != Dialect.DUCKDB && keyword != null) {
117-
builder.append(keyword);
118-
}
117+
return appendTo(builder, expression -> builder.append(expression));
118+
}
119119

120-
if (dialect != Dialect.DUCKDB && parameters != null && !parameters.isEmpty()) {
121-
builder.append("<");
122-
int i = 0;
123-
124-
for (Map.Entry<String, ColDataType> e : parameters) {
125-
if (0 < i++) {
126-
builder.append(",");
127-
}
128-
// optional name
129-
if (e.getKey() != null && !e.getKey().isEmpty()) {
130-
builder.append(e.getKey()).append(" ");
131-
}
132-
133-
// mandatory type
134-
builder.append(e.getValue());
120+
/** Renders current argument expressions through the caller's visitor. */
121+
public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressions) {
122+
if (dialect == Dialect.DUCKDB) {
123+
appendArguments(builder, expressions);
124+
appendTypeParameters(builder, true);
125+
} else {
126+
if (keyword != null) {
127+
builder.append(keyword);
135128
}
136-
137-
builder.append(">");
129+
appendTypeParameters(builder, false);
130+
appendArguments(builder, expressions);
138131
}
132+
return builder;
133+
}
139134

140-
if (arguments != null && !arguments.isEmpty()) {
141-
142-
if (dialect == Dialect.DUCKDB) {
143-
builder.append("{ ");
144-
int i = 0;
145-
for (SelectItem<?> e : arguments) {
146-
if (0 < i++) {
147-
builder.append(",");
148-
}
149-
builder.append(e.getAlias().getName());
150-
builder.append(":");
151-
builder.append(e.getExpression());
152-
}
153-
builder.append(" }");
154-
} else {
155-
builder.append("(");
156-
int i = 0;
157-
for (SelectItem<?> e : arguments) {
158-
if (0 < i++) {
159-
builder.append(",");
160-
}
161-
e.appendTo(builder);
162-
}
163-
164-
builder.append(")");
135+
private void appendTypeParameters(StringBuilder builder, boolean cast) {
136+
if (parameters == null || parameters.isEmpty()) {
137+
return;
138+
}
139+
builder.append(cast ? "::STRUCT( " : "<");
140+
int i = 0;
141+
for (Map.Entry<String, ColDataType> parameter : parameters) {
142+
if (i++ > 0) {
143+
builder.append(',');
144+
}
145+
if (cast || parameter.getKey() != null && !parameter.getKey().isEmpty()) {
146+
builder.append(parameter.getKey()).append(' ');
165147
}
148+
builder.append(parameter.getValue());
166149
}
150+
builder.append(cast ? ')' : '>');
151+
}
167152

168-
if (dialect == Dialect.DUCKDB && parameters != null && !parameters.isEmpty()) {
169-
builder.append("::STRUCT( ");
170-
int i = 0;
171-
172-
for (Map.Entry<String, ColDataType> e : parameters) {
173-
if (0 < i++) {
174-
builder.append(",");
175-
}
176-
builder.append(e.getKey()).append(" ");
177-
builder.append(e.getValue());
153+
private void appendArguments(StringBuilder builder, Consumer<Expression> expressions) {
154+
if (arguments == null || arguments.isEmpty()) {
155+
return;
156+
}
157+
boolean structLiteral = dialect == Dialect.DUCKDB;
158+
builder.append(structLiteral ? "{ " : "(");
159+
int i = 0;
160+
for (SelectItem<?> argument : arguments) {
161+
if (i++ > 0) {
162+
builder.append(',');
163+
}
164+
if (structLiteral) {
165+
builder.append(argument.getAlias().getName()).append(':');
166+
}
167+
expressions.accept(argument.getExpression());
168+
if (!structLiteral && argument.getAlias() != null) {
169+
builder.append(argument.getAlias());
178170
}
179-
builder.append(")");
180171
}
181-
182-
return builder;
172+
builder.append(structLiteral ? " }" : ")");
183173
}
184174

185175
@Override
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)