Skip to content

Commit e5d0051

Browse files
author
David Hayes
committed
[Fix] #2443 - NPE In TableNamesFinder with AnalyticExpression
If you attempt to tree walk an AnalyticExpression in TableNamesFinder with an SQL that has a window function without a range and without an offset, such as `SELECT c, SUM(COUNT(*)) OVER (ORDER BY c ASC ROWS UNBOUNDED PRECEDING) FROM tbl GROUP BY c`, an NPE is thrown. This adds defensive checks around the range and offset expressions being null.
1 parent 2b14156 commit e5d0051

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -766,17 +766,20 @@ public <S> Void visit(AnalyticExpression analytic, S context) {
766766
}
767767

768768
if (analytic.getWindowElement() != null) {
769-
if (analytic.getWindowElement().getRange().getStart().getExpression() != null) {
770-
analytic.getWindowElement().getRange().getStart().getExpression().accept(this,
771-
context);
772-
}
773-
if (analytic.getWindowElement().getRange().getEnd().getExpression() != null) {
774-
analytic.getWindowElement().getRange().getEnd().getExpression().accept(this,
775-
context);
769+
if (analytic.getWindowElement().getRange() != null) {
770+
if (analytic.getWindowElement().getRange().getStart().getExpression() != null) {
771+
analytic.getWindowElement().getRange().getStart().getExpression().accept(this,
772+
context);
773+
}
774+
if (analytic.getWindowElement().getRange().getEnd().getExpression() != null) {
775+
analytic.getWindowElement().getRange().getEnd().getExpression().accept(this,
776+
context);
777+
}
776778
}
777-
if (analytic.getWindowElement().getOffset() != null) {
779+
if (analytic.getWindowElement().getOffset() != null && analytic.getWindowElement().getOffset().getExpression() != null) {
778780
analytic.getWindowElement().getOffset().getExpression().accept(this, context);
779781
}
782+
780783
}
781784
return null;
782785
}

src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,4 +771,12 @@ void testJsonTable() throws JSQLParserException {
771771

772772
}
773773

774+
@Test
775+
void testWindowExpressionWithNoRangeAndNoOffsetDoesNotThrowException() {
776+
String sqlStr = "SELECT c, SUM(COUNT(*)) OVER (ORDER BY c ASC ROWS UNBOUNDED PRECEDING) FROM tbl GROUP BY c";
777+
778+
assertThatCode(() -> TablesNamesFinder.findTables(sqlStr))
779+
.doesNotThrowAnyException();
780+
}
781+
774782
}

0 commit comments

Comments
 (0)