Skip to content

Commit 2979877

Browse files
committed
refactor: isolate row-pattern validation visitor
1 parent daa0561 commit 2979877

1 file changed

Lines changed: 59 additions & 51 deletions

File tree

src/main/java/net/sf/jsqlparser/util/validation/validator/MatchRecognizeValidator.java

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void validate(MatchRecognize match) {
4646
validateRowOutput(match, oracle);
4747
Set<String> used = validateVariables(match, bigQuery, oracle);
4848
validateTargets(match, used, bigQuery);
49-
validatePattern(match, bigQuery, oracle);
49+
match.getPattern().accept(new PatternValidator(match, bigQuery, oracle), null);
5050
match.getMeasures()
5151
.forEach(measure -> checkFunctions(measure.getExpression(), bigQuery, false));
5252
match.getDefinitions()
@@ -131,64 +131,72 @@ private void validateTargets(MatchRecognize match, Set<String> used, boolean big
131131
}
132132
}
133133

134-
private void validatePattern(MatchRecognize match, boolean bigQuery, boolean oracle) {
135-
match.getPattern().accept(new RowPatternVisitorAdapter<Void>() {
136-
@Override
137-
public <S> Void visit(RowPattern.Quantified quantified, S context) {
138-
checkBound(quantified.getLowerBound(), bigQuery);
139-
checkBound(quantified.getUpperBound(), bigQuery);
140-
if (quantified.getLowerBound() instanceof LongValue
141-
&& quantified.getUpperBound() instanceof LongValue
142-
&& ((LongValue) quantified.getLowerBound()).getBigIntegerValue()
143-
.compareTo(((LongValue) quantified.getUpperBound())
144-
.getBigIntegerValue()) > 0) {
145-
error("Row pattern upper bound is smaller than its lower bound");
146-
}
147-
if (bigQuery && quantified.getType() == RowPattern.Quantified.Type.EXACT
148-
&& quantified.isReluctant()) {
149-
error("BigQuery does not support reluctant fixed quantifiers");
150-
}
151-
if (oracle) {
152-
Expression maximum = quantified.getType() == RowPattern.Quantified.Type.EXACT
153-
? quantified.getLowerBound()
154-
: quantified.getUpperBound();
155-
if (maximum instanceof LongValue
156-
&& ((LongValue) maximum).getBigIntegerValue().signum() == 0) {
157-
error("Oracle row pattern quantifier maximum must be positive");
158-
}
134+
private class PatternValidator extends RowPatternVisitorAdapter<Void> {
135+
private final MatchRecognize match;
136+
private final boolean bigQuery;
137+
private final boolean oracle;
138+
139+
private PatternValidator(MatchRecognize match, boolean bigQuery, boolean oracle) {
140+
this.match = match;
141+
this.bigQuery = bigQuery;
142+
this.oracle = oracle;
143+
}
144+
145+
@Override
146+
public <S> Void visit(RowPattern.Quantified quantified, S context) {
147+
checkBound(quantified.getLowerBound(), bigQuery);
148+
checkBound(quantified.getUpperBound(), bigQuery);
149+
if (quantified.getLowerBound() instanceof LongValue
150+
&& quantified.getUpperBound() instanceof LongValue
151+
&& ((LongValue) quantified.getLowerBound()).getBigIntegerValue()
152+
.compareTo(((LongValue) quantified.getUpperBound())
153+
.getBigIntegerValue()) > 0) {
154+
error("Row pattern upper bound is smaller than its lower bound");
155+
}
156+
if (bigQuery && quantified.getType() == RowPattern.Quantified.Type.EXACT
157+
&& quantified.isReluctant()) {
158+
error("BigQuery does not support reluctant fixed quantifiers");
159+
}
160+
if (oracle) {
161+
Expression maximum = quantified.getType() == RowPattern.Quantified.Type.EXACT
162+
? quantified.getLowerBound()
163+
: quantified.getUpperBound();
164+
if (maximum instanceof LongValue
165+
&& ((LongValue) maximum).getBigIntegerValue().signum() == 0) {
166+
error("Oracle row pattern quantifier maximum must be positive");
159167
}
160-
return super.visit(quantified, context);
161168
}
169+
return super.visit(quantified, context);
170+
}
162171

163-
@Override
164-
public <S> Void visit(RowPattern.Operation operation, S context) {
165-
if (oracle && operation.getType() == RowPattern.Operation.Type.ALTERNATION
166-
&& operation.getPatterns().stream()
167-
.anyMatch(RowPattern.Empty.class::isInstance)) {
168-
error("Oracle requires a pattern on each side of an alternative");
169-
}
170-
return super.visit(operation, context);
172+
@Override
173+
public <S> Void visit(RowPattern.Operation operation, S context) {
174+
if (oracle && operation.getType() == RowPattern.Operation.Type.ALTERNATION
175+
&& operation.getPatterns().stream()
176+
.anyMatch(RowPattern.Empty.class::isInstance)) {
177+
error("Oracle requires a pattern on each side of an alternative");
171178
}
179+
return super.visit(operation, context);
180+
}
172181

173-
@Override
174-
public <S> Void visit(RowPattern.Exclusion exclusion, S context) {
175-
if (bigQuery) {
176-
error("BigQuery does not support row pattern exclusion");
177-
}
178-
if (match.getEmptyMatchMode() == MatchRecognize.EmptyMatchMode.WITH_UNMATCHED) {
179-
error("Row pattern exclusion cannot be combined with WITH UNMATCHED ROWS");
180-
}
181-
return super.visit(exclusion, context);
182+
@Override
183+
public <S> Void visit(RowPattern.Exclusion exclusion, S context) {
184+
if (bigQuery) {
185+
error("BigQuery does not support row pattern exclusion");
186+
}
187+
if (match.getEmptyMatchMode() == MatchRecognize.EmptyMatchMode.WITH_UNMATCHED) {
188+
error("Row pattern exclusion cannot be combined with WITH UNMATCHED ROWS");
182189
}
190+
return super.visit(exclusion, context);
191+
}
183192

184-
@Override
185-
public <S> Void visit(RowPattern.Permute permute, S context) {
186-
if (bigQuery) {
187-
error("BigQuery does not support PERMUTE");
188-
}
189-
return super.visit(permute, context);
193+
@Override
194+
public <S> Void visit(RowPattern.Permute permute, S context) {
195+
if (bigQuery) {
196+
error("BigQuery does not support PERMUTE");
190197
}
191-
}, null);
198+
return super.visit(permute, context);
199+
}
192200
}
193201

194202
private void checkFunctions(Expression expression, boolean bigQuery, boolean definition) {

0 commit comments

Comments
 (0)