diff --git a/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java b/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java index fc616e0a7..2c66d8729 100644 --- a/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java +++ b/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java @@ -773,7 +773,8 @@ public Void visit(Alter alter, S context) { analysis.unresolved("unsupported"); } } - return null; + // DEFAULT, CHECK and USING expressions are evaluated against the existing rows + return super.visit(alter, context); } @Override @@ -1477,7 +1478,8 @@ public Void visit(CreateDomain statement, S context) { public Void visit(AlterDomain statement, S context) { analysis.claimTopLevel(); analysis.certain(StmtFeature.MODIFIES_SCHEMA); - return null; + // an added CHECK constraint is evaluated against every column of the domain + return super.visit(statement, context); } @Override diff --git a/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java b/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java index 9c2209162..0d61f23d6 100644 --- a/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java @@ -295,6 +295,26 @@ void repairPartitionMayModifyDataLikeRepairTable() throws JSQLParserException { assertThat(f.mayModifyData()).isTrue(); } + /** + * PostgreSQL evaluates these expressions against the rows already stored in the table, or in + * the columns of the domain, while the statement runs, so a function called there may write. + */ + @ParameterizedTest + @ValueSource(strings = {"ALTER TABLE t ALTER COLUMN a TYPE int USING purge_audit(a)", + "ALTER TABLE t ADD COLUMN c int DEFAULT purge_audit()", + "ALTER TABLE t ADD CONSTRAINT c CHECK (purge_audit(a) > 0)", + "ALTER DOMAIN d ADD CONSTRAINT c CHECK (purge_audit(VALUE) > 0)"}) + void alterExpressionIsChecked(String sql) throws JSQLParserException { + StatementFeatures impure = analyse(sql); + assertThat(impure.modifiesSchema()).isTrue(); + assertThat(impure.mayModifyData()).isTrue(); + assertThat(impure.getUnresolvedReferences()).containsExactly("purge_audit"); + + StatementFeatures pure = analyse(sql, "purge_audit"); + assertThat(pure.mayModifyData()).isFalse(); + assertThat(pure.getUnresolvedReferences()).isEmpty(); + } + // ---- the cases a flag union gets wrong -------------------------------------------------- @Test