@@ -246,6 +246,79 @@ void validateConstraintIsStructuredWithoutThePostgreSqlDialect() throws JSQLPars
246246 }
247247 }
248248
249+ @ ParameterizedTest
250+ @ ValueSource (strings = {
251+ "OWNER TO bob" , "OWNER TO CURRENT_USER" , "SET (fillfactor=70)" ,
252+ "SET (toast.autovacuum_enabled=false,fillfactor=70)" , "RESET (fillfactor)" ,
253+ "REPLICA IDENTITY FULL" , "REPLICA IDENTITY USING INDEX ix" , "SET TABLESPACE pg_default" ,
254+ "SET ACCESS METHOD heap" , "SET WITHOUT CLUSTER" ,
255+ "SET WITHOUT OIDS" , "SET LOGGED" , "SET UNLOGGED" , "CLUSTER ON ix" , "OF public.typ" ,
256+ "NOT OF" , "INHERIT p" , "NO INHERIT p" , "ENABLE TRIGGER ALL" , "DISABLE TRIGGER USER" ,
257+ "DISABLE TRIGGER trg" , "ENABLE ALWAYS TRIGGER trg" , "ENABLE REPLICA TRIGGER trg" ,
258+ "ALTER CONSTRAINT ck INHERIT" , "ALTER CONSTRAINT ck NO INHERIT" ,
259+ "ALTER COLUMN c SET (n_distinct=10)" , "ALTER COLUMN c RESET (n_distinct)" })
260+ void postgreSqlTableActionsAreStructuredWithoutADialect (String action )
261+ throws JSQLParserException {
262+ String sql = "ALTER TABLE t " + action + ", ADD COLUMN z INT" ;
263+ Alter alter = (Alter ) assertSqlCanBeParsedAndDeparsed (sql , true );
264+
265+ List <AlterExpression > expressions = alter .getAlterExpressions ();
266+ assertEquals (2 , expressions .size ());
267+ RelationAlterAction withoutDialect =
268+ assertInstanceOf (RelationAlterAction .class , expressions .get (0 ));
269+ RelationAlterAction withPostgreSql =
270+ (RelationAlterAction ) ((Alter ) parse (sql )).getAlterExpressions ().get (0 );
271+ assertEquals (withPostgreSql .getKind (), withoutDialect .getKind ());
272+ assertEquals (withPostgreSql .toString (), withoutDialect .toString ());
273+ assertEquals (AlterOperation .ADD , expressions .get (1 ).getOperation ());
274+ assertRoundTrip (alter );
275+ }
276+
277+ @ Test
278+ void setSchemaIsStructuredWithoutADialect () throws JSQLParserException {
279+ // PostgreSQL takes SET SCHEMA only as the sole action of the statement.
280+ Alter alter = (Alter ) assertSqlCanBeParsedAndDeparsed ("ALTER TABLE t SET SCHEMA s2" , true );
281+ RelationAlterAction action =
282+ assertInstanceOf (RelationAlterAction .class , alter .getAlterExpressions ().get (0 ));
283+ assertEquals (RelationAlterAction .Kind .SET_SCHEMA , action .getKind ());
284+ assertEquals ("s2" , action .getValue ());
285+ }
286+
287+ @ Test
288+ void otherDialectsKeepTheGenericReadingOfPostgreSqlTableActions () throws JSQLParserException {
289+ for (Dialect dialect : new Dialect [] {Dialect .MYSQL , Dialect .SQLSERVER }) {
290+ Alter alter = (Alter ) assertSqlCanBeParsedAndDeparsed ("ALTER TABLE t OWNER TO bob" ,
291+ true , p -> p .withDialect (dialect ));
292+ assertEquals (AlterOperation .UNSPECIFIC ,
293+ alter .getAlterExpressions ().get (0 ).getOperation ());
294+ }
295+ }
296+
297+ @ ParameterizedTest
298+ @ ValueSource (strings = {"ALTER TABLE t CLUSTER BY (a)" , "ALTER TABLE t DISABLE TRIGGER t1, t2" ,
299+ "ALTER TABLE t ENABLE TRIGGER t1, t2, ADD COLUMN z INT" })
300+ void otherProductsFormsKeepTheirReadingWithoutADialect (String sql )
301+ throws JSQLParserException {
302+ // Snowflake's CLUSTER BY and SQL Server's trigger lists are not PostgreSQL actions.
303+ Alter alter = (Alter ) assertSqlCanBeParsedAndDeparsed (sql , true );
304+ assertEquals (1 , alter .getAlterExpressions ().size ());
305+ assertEquals (AlterOperation .UNSPECIFIC ,
306+ alter .getAlterExpressions ().get (0 ).getOperation ());
307+ }
308+
309+ @ ParameterizedTest
310+ @ ValueSource (strings = {"ALTER TABLE t ALTER COLUMN c SET STATISTICS 100" ,
311+ "ALTER TABLE t ALTER COLUMN c SET STORAGE EXTERNAL" ,
312+ "ALTER TABLE t ALTER COLUMN c SET COMPRESSION lz4" ,
313+ "ALTER TABLE t ALTER COLUMN c DROP EXPRESSION" })
314+ void columnActionsOfTheGenericBranchKeepTheirModelWithoutADialect (String sql )
315+ throws JSQLParserException {
316+ Alter alter = (Alter ) assertSqlCanBeParsedAndDeparsed (sql , true );
317+ AlterExpression expression = alter .getAlterExpressions ().get (0 );
318+ assertFalse (expression instanceof RelationAlterAction );
319+ assertEquals (AlterOperation .ALTER , expression .getOperation ());
320+ }
321+
249322 private static Statement parse (String sql ) throws JSQLParserException {
250323 return CCJSqlParserUtil .parse (sql , p -> p .withDialect (Dialect .POSTGRESQL ));
251324 }
0 commit comments