Skip to content

Commit 4e74673

Browse files
fix: walk the expressions of ALTER TABLE and ALTER DOMAIN actions (#2950)
1 parent 443adc2 commit 4e74673

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,8 @@ public <S> Void visit(Alter alter, S context) {
773773
analysis.unresolved("unsupported");
774774
}
775775
}
776-
return null;
776+
// DEFAULT, CHECK and USING expressions are evaluated against the existing rows
777+
return super.visit(alter, context);
777778
}
778779

779780
@Override
@@ -1477,7 +1478,8 @@ public <S> Void visit(CreateDomain statement, S context) {
14771478
public <S> Void visit(AlterDomain statement, S context) {
14781479
analysis.claimTopLevel();
14791480
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
1480-
return null;
1481+
// an added CHECK constraint is evaluated against every column of the domain
1482+
return super.visit(statement, context);
14811483
}
14821484

14831485
@Override

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,26 @@ void repairPartitionMayModifyDataLikeRepairTable() throws JSQLParserException {
295295
assertThat(f.mayModifyData()).isTrue();
296296
}
297297

298+
/**
299+
* PostgreSQL evaluates these expressions against the rows already stored in the table, or in
300+
* the columns of the domain, while the statement runs, so a function called there may write.
301+
*/
302+
@ParameterizedTest
303+
@ValueSource(strings = {"ALTER TABLE t ALTER COLUMN a TYPE int USING purge_audit(a)",
304+
"ALTER TABLE t ADD COLUMN c int DEFAULT purge_audit()",
305+
"ALTER TABLE t ADD CONSTRAINT c CHECK (purge_audit(a) > 0)",
306+
"ALTER DOMAIN d ADD CONSTRAINT c CHECK (purge_audit(VALUE) > 0)"})
307+
void alterExpressionIsChecked(String sql) throws JSQLParserException {
308+
StatementFeatures impure = analyse(sql);
309+
assertThat(impure.modifiesSchema()).isTrue();
310+
assertThat(impure.mayModifyData()).isTrue();
311+
assertThat(impure.getUnresolvedReferences()).containsExactly("purge_audit");
312+
313+
StatementFeatures pure = analyse(sql, "purge_audit");
314+
assertThat(pure.mayModifyData()).isFalse();
315+
assertThat(pure.getUnresolvedReferences()).isEmpty();
316+
}
317+
298318
// ---- the cases a flag union gets wrong --------------------------------------------------
299319

300320
@Test

0 commit comments

Comments
 (0)