Skip to content
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 @@ -638,6 +638,7 @@ public <S> Void visit(PragmaStatement pragmaStatement, S context) {
public <S> Void visit(AssertStatement assertStatement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.READS_DATA);
assertStatement.getExpression().accept(analysis.expressions, context);
return null;
}

Expand Down Expand Up @@ -1112,7 +1113,9 @@ private Void transactionOnly() {

@Override
public <S> Void visit(SetStatement set, S context) {
return sessionOnly();
sessionOnly();
// the assigned values are evaluated, as in SET @x = f()
return super.visit(set, context);
}

@Override
Expand Down Expand Up @@ -1147,7 +1150,14 @@ public <S> Void visit(AlterSession alterSession, S context) {

@Override
public <S> Void visit(DeclareStatement declareStatement, S context) {
return sessionOnly();
sessionOnly();
// the initial values are evaluated, as in DECLARE @x INT = f()
for (DeclareStatement.TypeDefExpr typeDefExpr : declareStatement.getTypeDefExprList()) {
if (typeDefExpr.getDefaultExpr() != null) {
typeDefExpr.getDefaultExpr().accept(analysis.expressions, context);
}
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,12 @@ private StatementFeatures analyseAllPureExcept(String sql, String impure)
"TABLE t ORDER BY pg_read_file('x')",
"TABLE t LIMIT length(pg_read_file('x'))",
"TABLE t OFFSET length(pg_read_file('x'))",
"TABLE t FETCH FIRST length(pg_read_file('x')) ROWS ONLY"})
"TABLE t FETCH FIRST length(pg_read_file('x')) ROWS ONLY",
"SET @x = pg_read_file('x')",
"SET @a = 1, @b = pg_read_file('x')",
"SET x = pg_read_file('x')",
"DECLARE @x INT = length(pg_read_file('x'))",
"ASSERT length(pg_read_file('x')) > 0"})
void functionIsChecked(String sql) throws JSQLParserException {
StatementFeatures impure = analyseAllPureExcept(sql, "pg_read_file");
assertThat(impure.getUnresolvedReferences()).containsExactly("pg_read_file");
Expand Down Expand Up @@ -765,6 +770,17 @@ void columnOutsideTheSequenceSyntaxIsNotChecked() throws JSQLParserException {
assertThat(f.getUnresolvedReferences()).isEmpty();
}

@ParameterizedTest(name = "{0}")
@ValueSource(strings = {
"SET @x = (SELECT a FROM t)",
"DECLARE @x INT = (SELECT a FROM t)"})
void assignedSubqueryIsRead(String sql) throws JSQLParserException {
StatementFeatures f = analyse(sql);
assertThat(f.is(StmtFeature.MODIFIES_SESSION)).isTrue();
assertThat(f.is(StmtFeature.READS_DATA)).isTrue();
assertThat(f.returnsResultSet()).isFalse();
}

@Test
void topLevelValuesReturnsRowsWithoutReading() throws JSQLParserException {
StatementFeatures f = analyse("VALUES (1, 2)");
Expand Down
Loading