Skip to content

Commit

Permalink
fix: make analytic expression visitor null-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Jan 10, 2024
1 parent bc16618 commit caf17a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import net.sf.jsqlparser.statement.select.UnPivot;
import net.sf.jsqlparser.statement.select.WithItem;

import java.util.Optional;

@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.UncommentedEmptyMethodBody"})
public class ExpressionVisitorAdapter
implements ExpressionVisitor, PivotVisitor, SelectItemVisitor {
Expand Down Expand Up @@ -382,11 +384,13 @@ public void visit(AnalyticExpression expr) {
element.getExpression().accept(this);
}
}

if (expr.getWindowElement() != null) {
expr.getWindowElement().getRange().getStart().getExpression().accept(this);
expr.getWindowElement().getRange().getEnd().getExpression().accept(this);
expr.getWindowElement().getOffset().getExpression().accept(this);
Optional.ofNullable(expr.getWindowElement().getRange()).map(WindowRange::getStart)
.map(WindowOffset::getExpression).ifPresent(e -> e.accept(this));
Optional.ofNullable(expr.getWindowElement().getRange()).map(WindowRange::getEnd)
.map(WindowOffset::getExpression).ifPresent(e -> e.accept(this));
Optional.ofNullable(expr.getWindowElement().getOffset())
.map(WindowOffset::getExpression).ifPresent(e -> e.accept(this));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,13 @@ public void visit(AllTableColumns all) {
assertNotNull(holder[0]);
assertEquals("a.*", holder[0].toString());
}

@Test
public void testAnalyticExpressionWithPartialWindowElement() throws JSQLParserException {
ExpressionVisitorAdapter adapter = new ExpressionVisitorAdapter();
Expression expression = CCJSqlParserUtil.parseExpression(
"SUM(\"Spent\") OVER (PARTITION BY \"ID\" ORDER BY \"Name\" ASC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)");

expression.accept(adapter);
}
}

0 comments on commit caf17a2

Please sign in to comment.