Skip to content

Commit ba9291f

Browse files
fix: check LATERAL VIEW generator functions in adapter and validator (#2956)
1 parent e2d2f2c commit ba9291f

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/select/SelectVisitorAdapter.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,11 @@ public <S> T visit(PlainSelect plainSelect, S context) {
201201
fromItemVisitor.visitTables(plainSelect.getIntoTables(), context);
202202
fromItemVisitor.visitFromItem(plainSelect.getFromItem(), context);
203203

204-
// if (plainSelect.getLateralViews() != null) {
205-
// //@todo: implement this
206-
// }
204+
if (plainSelect.getLateralViews() != null) {
205+
for (LateralView lateralView : plainSelect.getLateralViews()) {
206+
expressionVisitor.visitExpression(lateralView.getGeneratorFunction(), context);
207+
}
208+
}
207209

208210
fromItemVisitor.visitJoins(plainSelect.getJoins(), context);
209211

‎src/main/java/net/sf/jsqlparser/util/validation/validator/SelectValidator.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ public <S> Void visit(PlainSelect plainSelect, S context) {
175175
validateOptionalFromItem(plainSelect.getFromItem());
176176
validateOptionalFromItems(plainSelect.getIntoTables());
177177
validateOptionalJoins(plainSelect.getJoins());
178+
if (isNotEmpty(plainSelect.getLateralViews())) {
179+
plainSelect.getLateralViews()
180+
.forEach(view -> validateOptionalExpression(view.getGeneratorFunction()));
181+
}
178182

179183
// after the FROM items, to correctly recognize aliased tables
180184
if (plainSelect.getDistinct() != null

‎src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,9 @@ private StatementFeatures analyseAllPureExcept(String sql, String impure)
767767
"FROM t |> UNION ALL (SELECT pg_read_file('x'))",
768768
"FROM t |> PIVOT (sum(pg_read_file('x')) FOR a IN (1))",
769769
"FROM t |> WINDOW sum(pg_read_file('x')) OVER () AS w",
770-
"SELECT * FROM (FROM t |> WHERE length(pg_read_file('x')) > 0)"})
770+
"SELECT * FROM (FROM t |> WHERE length(pg_read_file('x')) > 0)",
771+
"SELECT a FROM t LATERAL VIEW explode(pg_read_file('x')) v AS c",
772+
"SELECT a FROM t LATERAL VIEW OUTER explode(pg_read_file('x')) v AS c"})
771773
void functionIsChecked(String sql) throws JSQLParserException {
772774
StatementFeatures impure = analyseAllPureExcept(sql, "pg_read_file");
773775
assertThat(impure.getUnresolvedReferences()).containsExactly("pg_read_file");

‎src/test/java/net/sf/jsqlparser/util/validation/validator/SelectValidatorTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ public void testValidateTableFunctionArguments(String sql) {
268268
"SELECT a FROM t OFFSET upper(a) ROWS",
269269
"SELECT a FROM t FETCH FIRST upper(a) ROWS ONLY",
270270
"SELECT a FROM t UNION SELECT b FROM u LIMIT upper(a)",
271-
"SELECT * FROM t PIVOT (max(upper(a)) FOR b IN (1))"})
271+
"SELECT * FROM t PIVOT (max(upper(a)) FOR b IN (1))",
272+
"SELECT a FROM t LATERAL VIEW explode(b) v AS c"})
272273
public void testValidateFunctionOutsideWhere(String sql) {
273274
validateNotAllowed(sql, 1, 1,
274275
new FeaturesAllowed(Feature.values()).remove(Feature.function), Feature.function);

0 commit comments

Comments
 (0)