Skip to content

Commit 8a11ae3

Browse files
committed
fix(feature): analyse ROWS FROM functions for purity
StatementFeatureVisitor only checked TableFunction#getFunction(), which is null for ROWS FROM (...), so every function listed there escaped the purity check. Iterate TableFunction#getFunctions() instead.
1 parent 26d3e02 commit 8a11ae3

2 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,11 +1018,18 @@ static final class FeatureFromItemVisitor extends FromItemVisitorAdapter<Void> {
10181018
this.analysis = analysis;
10191019
}
10201020

1021-
/** The inherited implementation is empty, so table functions escape the purity check. */
1021+
/**
1022+
* The inherited implementation is empty, so table functions escape the purity check.
1023+
* {@link TableFunction#getFunctions()} also covers {@code ROWS FROM (..)}, where
1024+
* {@link TableFunction#getFunction()} is null.
1025+
*/
10221026
@Override
10231027
public <S> Void visit(TableFunction tableFunction, S context) {
1024-
if (tableFunction.getFunction() != null) {
1025-
tableFunction.getFunction().accept(analysis.expressions, context);
1028+
List<Function> functions = tableFunction.getFunctions();
1029+
if (functions != null) {
1030+
for (Function function : functions) {
1031+
function.accept(analysis.expressions, context);
1032+
}
10261033
}
10271034
return super.visit(tableFunction, context);
10281035
}

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,88 @@ void allowListCollapsesTheUncertainty() throws JSQLParserException {
327327
}
328328
}
329329

330+
/**
331+
* {@code ROWS FROM (..)} keeps its functions in {@code getRowsFromFunctions()} and leaves
332+
* {@code getFunction()} null, so a check against the single function alone lets every one of
333+
* them through as harmless.
334+
*/
335+
@Nested
336+
@DisplayName("table functions in the FROM clause")
337+
class TableFunctions {
338+
339+
private StatementFeatures analyseNothingPure(String sql) throws JSQLParserException {
340+
Predicate<String> nothingIsPure = name -> false;
341+
return StatementFeatureVisitor.analyse(CCJSqlParserUtil.parse(sql), nothingIsPure);
342+
}
343+
344+
@Test
345+
void singleTableFunctionIsChecked() throws JSQLParserException {
346+
String sql = "SELECT * FROM pg_ls_dir('.')";
347+
348+
StatementFeatures impure = analyseNothingPure(sql);
349+
assertThat(impure.returnsResultSet()).isTrue();
350+
assertThat(impure.modifiesData()).isFalse();
351+
assertThat(impure.getUncertain()).contains(StmtFeature.MODIFIES_DATA,
352+
StmtFeature.MODIFIES_SCHEMA);
353+
assertThat(impure.getUnresolvedReferences()).containsExactly("pg_ls_dir");
354+
355+
StatementFeatures pure = analyse(sql, "pg_ls_dir");
356+
assertThat(pure.mayModifyData()).isFalse();
357+
assertThat(pure.getUnresolvedReferences()).isEmpty();
358+
}
359+
360+
@Test
361+
void rowsFromWithOneFunctionIsChecked() throws JSQLParserException {
362+
String sql = "SELECT * FROM ROWS FROM (pg_ls_dir('.'))";
363+
364+
StatementFeatures impure = analyseNothingPure(sql);
365+
assertThat(impure.returnsResultSet()).isTrue();
366+
assertThat(impure.modifiesData()).isFalse();
367+
assertThat(impure.getUncertain()).contains(StmtFeature.MODIFIES_DATA,
368+
StmtFeature.MODIFIES_SCHEMA);
369+
assertThat(impure.getUnresolvedReferences()).containsExactly("pg_ls_dir");
370+
371+
StatementFeatures pure = analyse(sql, "pg_ls_dir");
372+
assertThat(pure.mayModifyData()).isFalse();
373+
assertThat(pure.getUnresolvedReferences()).isEmpty();
374+
}
375+
376+
@Test
377+
void rowsFromChecksEveryFunction() throws JSQLParserException {
378+
String sql = "SELECT * FROM ROWS FROM (generate_series(1, 2), pg_ls_dir('.'))";
379+
380+
StatementFeatures impure = analyseNothingPure(sql);
381+
assertThat(impure.mayModifyData()).isTrue();
382+
assertThat(impure.getUnresolvedReferences())
383+
.containsExactlyInAnyOrder("generate_series", "pg_ls_dir");
384+
385+
StatementFeatures onlyFirstPure = analyse(sql, "generate_series");
386+
assertThat(onlyFirstPure.mayModifyData())
387+
.as("a pure first function must not hide the second")
388+
.isTrue();
389+
assertThat(onlyFirstPure.getUnresolvedReferences()).containsExactly("pg_ls_dir");
390+
391+
StatementFeatures pure = analyse(sql, "generate_series", "pg_ls_dir");
392+
assertThat(pure.mayModifyData()).isFalse();
393+
assertThat(pure.getUnresolvedReferences()).isEmpty();
394+
}
395+
396+
@Test
397+
void rowsFromWithOrdinalityIsChecked() throws JSQLParserException {
398+
String sql = "SELECT * FROM ROWS FROM (generate_series(1, 2), pg_ls_dir('.')) "
399+
+ "WITH ORDINALITY AS r(n, name, ord)";
400+
401+
StatementFeatures impure = analyseNothingPure(sql);
402+
assertThat(impure.mayModifyData()).isTrue();
403+
assertThat(impure.getUnresolvedReferences())
404+
.containsExactlyInAnyOrder("generate_series", "pg_ls_dir");
405+
406+
StatementFeatures pure = analyse(sql, "generate_series", "pg_ls_dir");
407+
assertThat(pure.mayModifyData()).isFalse();
408+
assertThat(pure.getUnresolvedReferences()).isEmpty();
409+
}
410+
}
411+
330412
@Nested
331413
@DisplayName("DDL and opaque statements")
332414
class PendingStatementOverrides {

0 commit comments

Comments
 (0)