Skip to content

Commit 236a50b

Browse files
Closes #1604, added simple OVERLAPS support (#1611)
1 parent 2619ce0 commit 236a50b

File tree

8 files changed

+116
-0
lines changed

8 files changed

+116
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public interface ExpressionVisitor {
6969

7070
void visit(Between between);
7171

72+
void visit (OverlapsCondition overlapsCondition);
73+
7274
void visit(EqualsTo equalsTo);
7375

7476
void visit(GreaterThan greaterThan);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ public void visit(Between expr) {
161161
expr.getBetweenExpressionEnd().accept(this);
162162
}
163163

164+
public void visit(OverlapsCondition overlapsCondition) {
165+
overlapsCondition.getLeft().accept(this);
166+
overlapsCondition.getRight().accept(this);
167+
}
168+
169+
164170
@Override
165171
public void visit(EqualsTo expr) {
166172
visitBinaryExpression(expr);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2022 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 net.sf.jsqlparser.expression.operators.relational.ExpressionList;
13+
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
14+
15+
public class OverlapsCondition extends ASTNodeAccessImpl implements Expression{
16+
17+
18+
private ExpressionList left;
19+
private ExpressionList right;
20+
21+
22+
public OverlapsCondition(ExpressionList left, ExpressionList right) {
23+
this.left = left;
24+
this.right = right;
25+
}
26+
27+
public ExpressionList getLeft() {
28+
return left;
29+
}
30+
31+
public ExpressionList getRight() {
32+
return right;
33+
}
34+
35+
@Override
36+
public void accept(ExpressionVisitor expressionVisitor) {
37+
expressionVisitor.visit(this);
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return String.format("%s OVERLAPS %s"
43+
, left.toString()
44+
, right.toString()
45+
);
46+
}
47+
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ public void visit(Between between) {
189189
between.getBetweenExpressionEnd().accept(this);
190190
}
191191

192+
@Override
193+
public void visit(OverlapsCondition overlapsCondition) {
194+
overlapsCondition.getLeft().accept(this);
195+
overlapsCondition.getRight().accept(this);
196+
}
197+
192198
@Override
193199
public void visit(Column tableColumn) {
194200
if (allowColumnProcessing && tableColumn.getTable() != null && tableColumn.getTable().getName() != null) {

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public void visit(Between between) {
107107

108108
}
109109

110+
@Override
111+
public void visit(OverlapsCondition overlapsCondition) {
112+
buffer.append(overlapsCondition.toString());
113+
}
114+
110115
@Override
111116
public void visit(EqualsTo equalsTo) {
112117
visitOldOracleJoinBinaryExpression(equalsTo, " = ");

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public void visit(Between between) {
7979
between.getBetweenExpressionEnd().accept(this);
8080
}
8181

82+
@Override
83+
public void visit(OverlapsCondition overlapsCondition) {
84+
validateOptionalExpressionList(overlapsCondition.getLeft());
85+
validateOptionalExpressionList(overlapsCondition.getRight());
86+
}
87+
88+
8289
@Override
8390
public void visit(EqualsTo equalsTo) {
8491
visitOldOracleJoinBinaryExpression(equalsTo, " = ");

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
320320
| <K_OUTER:"OUTER">
321321
| <K_OUTPUT:"OUTPUT">
322322
| <K_OVER:"OVER">
323+
| <K_OVERLAPS:"OVERLAPS">
323324
| <K_OPTIMIZE: "OPTIMIZE" >
324325
| <K_PARALLEL:"PARALLEL">
325326
| <K_PARTITION:"PARTITION">
@@ -3307,6 +3308,20 @@ Expression Condition():
33073308
{ return not?new NotExpression(result, exclamationMarkNot):result; }
33083309
}
33093310

3311+
Expression OverlapsCondition():{
3312+
ExpressionList left = new ExpressionList();
3313+
ExpressionList right = new ExpressionList();
3314+
}
3315+
{
3316+
//As per the sql2003 standard, we need at least two items in the list if there is not explicit ROW prefix
3317+
//More than two expression are allowed per the sql2003 grammar.
3318+
"(" left = SimpleExpressionListAtLeastTwoItems() ")"
3319+
<K_OVERLAPS>
3320+
"(" right = SimpleExpressionListAtLeastTwoItems() ")"
3321+
3322+
{return new OverlapsCondition(left, right);}
3323+
}
3324+
33103325
Expression RegularCondition() #RegularCondition:
33113326
{
33123327
Expression result = null;
@@ -3383,6 +3398,7 @@ Expression SQLCondition():
33833398
(
33843399
result=ExistsExpression()
33853400
| LOOKAHEAD(InExpression()) result=InExpression()
3401+
| LOOKAHEAD(OverlapsCondition()) result=OverlapsCondition()
33863402
| left = SimpleExpression() { result = left; }
33873403
[ LOOKAHEAD(2) ((LOOKAHEAD(2) result=Between(left)
33883404
| LOOKAHEAD(IsNullExpression()) result=IsNullExpression(left)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2022 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 net.sf.jsqlparser.JSQLParserException;
13+
import net.sf.jsqlparser.test.TestUtils;
14+
import org.junit.jupiter.api.Test;
15+
16+
public class OverlapsConditionTest {
17+
18+
@Test
19+
public void testOverlapsCondition() throws JSQLParserException {
20+
TestUtils.assertExpressionCanBeParsedAndDeparsed("(t1.start, t1.end) overlaps (t2.start, t2.end)", true);
21+
22+
TestUtils.assertSqlCanBeParsedAndDeparsed("select * from dual where (start_one, end_one) overlaps (start_two, end_two)", true);
23+
24+
TestUtils.assertSqlCanBeParsedAndDeparsed("select * from t1 left join t2 on (t1.start, t1.end) overlaps (t2.start, t2.end)", true);
25+
26+
}
27+
}

0 commit comments

Comments
 (0)