Skip to content

Commit 5646842

Browse files
davyboyhayesDavid Hayes
andauthored
[Fix] #2443 - NPE In TableNamesFinder with AnalyticExpression (#2444)
* [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. * More aggressive NPE protection --------- Co-authored-by: David Hayes <dhayes@adobe.com>
1 parent 2b14156 commit 5646842

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -766,17 +766,22 @@ 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() != null
771+
&& analytic.getWindowElement().getRange().getStart().getExpression() != null) {
772+
analytic.getWindowElement().getRange().getStart().getExpression().accept(this,
773+
context);
774+
}
775+
if (analytic.getWindowElement().getRange().getEnd() != null
776+
&& analytic.getWindowElement().getRange().getEnd().getExpression() != null) {
777+
analytic.getWindowElement().getRange().getEnd().getExpression().accept(this,
778+
context);
779+
}
776780
}
777-
if (analytic.getWindowElement().getOffset() != null) {
781+
if (analytic.getWindowElement().getOffset() != null && analytic.getWindowElement().getOffset().getExpression() != null) {
778782
analytic.getWindowElement().getOffset().getExpression().accept(this, context);
779783
}
784+
780785
}
781786
return null;
782787
}

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)