Skip to content

fix: make analytic expression visitor null-safe #1944

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

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 @@ -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,19 @@ 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);
/*
* Visit expressions from the range and offset of the window element. Do this using
* optional chains, because several things down the tree can be null e.g. the
* expression. So, null-safe versions of e.g.:
* 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);
}
}