Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #1604, added simple OVERLAPS support #1611

Merged
merged 2 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public interface ExpressionVisitor {

void visit(Between between);

void visit (OverlapsCondition overlapsCondition);

void visit(EqualsTo equalsTo);

void visit(GreaterThan greaterThan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public void visit(Between expr) {
expr.getBetweenExpressionEnd().accept(this);
}

public void visit(OverlapsCondition overlapsCondition) {
overlapsCondition.getLeft().accept(this);
overlapsCondition.getRight().accept(this);
}


@Override
public void visit(EqualsTo expr) {
visitBinaryExpression(expr);
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/OverlapsCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2022 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

public class OverlapsCondition extends ASTNodeAccessImpl implements Expression{


private ExpressionList left;
private ExpressionList right;


public OverlapsCondition(ExpressionList left, ExpressionList right) {
this.left = left;
this.right = right;
}

public ExpressionList getLeft() {
return left;
}

public ExpressionList getRight() {
return right;
}

@Override
public void accept(ExpressionVisitor expressionVisitor) {
expressionVisitor.visit(this);
}

@Override
public String toString() {
return String.format("%s OVERLAPS %s"
, left.toString()
, right.toString()
);
}
}
6 changes: 6 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ public void visit(Between between) {
between.getBetweenExpressionEnd().accept(this);
}

@Override
public void visit(OverlapsCondition overlapsCondition) {
overlapsCondition.getLeft().accept(this);
overlapsCondition.getRight().accept(this);
}

@Override
public void visit(Column tableColumn) {
if (allowColumnProcessing && tableColumn.getTable() != null && tableColumn.getTable().getName() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public void visit(Between between) {

}

@Override
public void visit(OverlapsCondition overlapsCondition) {
buffer.append(overlapsCondition.toString());
}

@Override
public void visit(EqualsTo equalsTo) {
visitOldOracleJoinBinaryExpression(equalsTo, " = ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public void visit(Between between) {
between.getBetweenExpressionEnd().accept(this);
}

@Override
public void visit(OverlapsCondition overlapsCondition) {
validateOptionalExpressionList(overlapsCondition.getLeft());
validateOptionalExpressionList(overlapsCondition.getRight());
}


@Override
public void visit(EqualsTo equalsTo) {
visitOldOracleJoinBinaryExpression(equalsTo, " = ");
Expand Down
16 changes: 16 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_OUTER:"OUTER">
| <K_OUTPUT:"OUTPUT">
| <K_OVER:"OVER">
| <K_OVERLAPS:"OVERLAPS">
| <K_OPTIMIZE: "OPTIMIZE" >
| <K_PARALLEL:"PARALLEL">
| <K_PARTITION:"PARTITION">
Expand Down Expand Up @@ -3307,6 +3308,20 @@ Expression Condition():
{ return not?new NotExpression(result, exclamationMarkNot):result; }
}

Expression OverlapsCondition():{
ExpressionList left = new ExpressionList();
ExpressionList right = new ExpressionList();
}
{
//As per the sql2003 standard, we need at least two items in the list if there is not explicit ROW prefix
//More than two expression are allowed per the sql2003 grammar.
"(" left = SimpleExpressionListAtLeastTwoItems() ")"
<K_OVERLAPS>
"(" right = SimpleExpressionListAtLeastTwoItems() ")"

{return new OverlapsCondition(left, right);}
}

Expression RegularCondition() #RegularCondition:
{
Expression result = null;
Expand Down Expand Up @@ -3383,6 +3398,7 @@ Expression SQLCondition():
(
result=ExistsExpression()
| LOOKAHEAD(InExpression()) result=InExpression()
| LOOKAHEAD(OverlapsCondition()) result=OverlapsCondition()
| left = SimpleExpression() { result = left; }
[ LOOKAHEAD(2) ((LOOKAHEAD(2) result=Between(left)
| LOOKAHEAD(IsNullExpression()) result=IsNullExpression(left)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2022 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

public class OverlapsConditionTest {

@Test
public void testOverlapsCondition() throws JSQLParserException {
TestUtils.assertExpressionCanBeParsedAndDeparsed("(t1.start, t1.end) overlaps (t2.start, t2.end)", true);

TestUtils.assertSqlCanBeParsedAndDeparsed("select * from dual where (start_one, end_one) overlaps (start_two, end_two)", true);

TestUtils.assertSqlCanBeParsedAndDeparsed("select * from t1 left join t2 on (t1.start, t1.end) overlaps (t2.start, t2.end)", true);

}
}