Skip to content
Open
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
36 changes: 28 additions & 8 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5457,6 +5457,18 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
}
}

/**
* SQL Server's {@code ENABLE | DISABLE TRIGGER t1, t2} names several triggers in one action. PostgreSQL
* takes one trigger per action, so a plain name after the comma, rather than the next action, is the list.
*/
private boolean isTriggerNameListAhead() {
Token next = getToken(5);
return ",".equals(getToken(4).image)
&& (next.kind == S_IDENTIFIER || next.kind == S_QUOTED_IDENTIFIER)
&& !"OWNER".equalsIgnoreCase(next.image) && !"INHERIT".equalsIgnoreCase(next.image)
&& !"REPLICA".equalsIgnoreCase(next.image);
}

private boolean isPostgreSqlRelationActionAhead() {
Token first = getToken(1);
if ((first.kind == K_ENABLE || first.kind == K_DISABLE)
Expand All @@ -5470,12 +5482,19 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
}
// VALIDATE CONSTRAINT starts no other ALTER TABLE action in any dialect.
if (first.kind == K_VALIDATE && getToken(2).kind == K_CONSTRAINT) { return true; }
if (!Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect))) { return false; }
// The other actions are PostgreSQL's: read them with that dialect and without one, never with another.
// Otherwise the generic fallback takes the action and every action after it as one unstructured text.
String dialect = getAsString(Feature.dialect);
boolean postgreSql = Dialect.POSTGRESQL.name().equals(dialect);
if (!postgreSql && dialect != null) { return false; }
if (first.kind == K_VALIDATE || first.kind == K_RESET || isKeywordAhead("INHERIT")
|| isKeywordAhead("REPLICA") || isKeywordAhead("OWNER") || isKeywordAhead("CLUSTER")
|| isKeywordAhead("REPLICA") || isKeywordAhead("OWNER")
// Without a dialect only CLUSTER ON is PostgreSQL's; CLUSTER BY belongs to other products.
|| isKeywordAhead("CLUSTER") && (postgreSql || getToken(2).kind == K_ON)
|| first.kind == K_OF || first.kind == K_NOT && getToken(2).kind == K_OF
|| (first.kind == K_ENABLE || first.kind == K_DISABLE)
&& (getToken(2).kind == K_TRIGGER || getToken(2).kind == K_ALWAYS
&& (getToken(2).kind == K_TRIGGER && (postgreSql || !isTriggerNameListAhead())
|| getToken(2).kind == K_ALWAYS
|| "REPLICA".equalsIgnoreCase(getToken(2).image))) { return true; }
if (first.kind == K_NO) { return "INHERIT".equalsIgnoreCase(getToken(2).image); }
if (first.kind == K_SET) {
Expand All @@ -5490,12 +5509,13 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
|| getToken(4).kind == K_NO && "INHERIT".equalsIgnoreCase(getToken(5).image);
}
int offset = getToken(2).kind == K_COLUMN ? 4 : 3;
return getToken(offset).kind == K_RESET
|| getToken(offset).kind == K_DROP && "EXPRESSION".equalsIgnoreCase(getToken(offset + 1).image)
|| getToken(offset).kind == K_SET && (getToken(offset + 1).kind == OPENING_BRACKET
|| getToken(offset + 1).kind == K_STORAGE
if (getToken(offset).kind == K_RESET
|| getToken(offset).kind == K_SET && getToken(offset + 1).kind == OPENING_BRACKET) { return true; }
// Without a dialect the generic ALTER COLUMN branch already reads these column actions.
return postgreSql && (getToken(offset).kind == K_DROP && "EXPRESSION".equalsIgnoreCase(getToken(offset + 1).image)
|| getToken(offset).kind == K_SET && (getToken(offset + 1).kind == K_STORAGE
|| "COMPRESSION".equalsIgnoreCase(getToken(offset + 1).image)
|| "STATISTICS".equalsIgnoreCase(getToken(offset + 1).image));
|| "STATISTICS".equalsIgnoreCase(getToken(offset + 1).image)));
}

private void validateRelationAction(AlterRelation.ObjectType type, RelationAlterAction action) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,79 @@ void validateConstraintIsStructuredWithoutThePostgreSqlDialect() throws JSQLPars
}
}

@ParameterizedTest
@ValueSource(strings = {
"OWNER TO bob", "OWNER TO CURRENT_USER", "SET (fillfactor=70)",
"SET (toast.autovacuum_enabled=false,fillfactor=70)", "RESET (fillfactor)",
"REPLICA IDENTITY FULL", "REPLICA IDENTITY USING INDEX ix", "SET TABLESPACE pg_default",
"SET ACCESS METHOD heap", "SET WITHOUT CLUSTER",
"SET WITHOUT OIDS", "SET LOGGED", "SET UNLOGGED", "CLUSTER ON ix", "OF public.typ",
"NOT OF", "INHERIT p", "NO INHERIT p", "ENABLE TRIGGER ALL", "DISABLE TRIGGER USER",
"DISABLE TRIGGER trg", "ENABLE ALWAYS TRIGGER trg", "ENABLE REPLICA TRIGGER trg",
"ALTER CONSTRAINT ck INHERIT", "ALTER CONSTRAINT ck NO INHERIT",
"ALTER COLUMN c SET (n_distinct=10)", "ALTER COLUMN c RESET (n_distinct)"})
void postgreSqlTableActionsAreStructuredWithoutADialect(String action)
throws JSQLParserException {
String sql = "ALTER TABLE t " + action + ", ADD COLUMN z INT";
Alter alter = (Alter) assertSqlCanBeParsedAndDeparsed(sql, true);

List<AlterExpression> expressions = alter.getAlterExpressions();
assertEquals(2, expressions.size());
RelationAlterAction withoutDialect =
assertInstanceOf(RelationAlterAction.class, expressions.get(0));
RelationAlterAction withPostgreSql =
(RelationAlterAction) ((Alter) parse(sql)).getAlterExpressions().get(0);
assertEquals(withPostgreSql.getKind(), withoutDialect.getKind());
assertEquals(withPostgreSql.toString(), withoutDialect.toString());
assertEquals(AlterOperation.ADD, expressions.get(1).getOperation());
assertRoundTrip(alter);
}

@Test
void setSchemaIsStructuredWithoutADialect() throws JSQLParserException {
// PostgreSQL takes SET SCHEMA only as the sole action of the statement.
Alter alter = (Alter) assertSqlCanBeParsedAndDeparsed("ALTER TABLE t SET SCHEMA s2", true);
RelationAlterAction action =
assertInstanceOf(RelationAlterAction.class, alter.getAlterExpressions().get(0));
assertEquals(RelationAlterAction.Kind.SET_SCHEMA, action.getKind());
assertEquals("s2", action.getValue());
}

@Test
void otherDialectsKeepTheGenericReadingOfPostgreSqlTableActions() throws JSQLParserException {
for (Dialect dialect : new Dialect[] {Dialect.MYSQL, Dialect.SQLSERVER}) {
Alter alter = (Alter) assertSqlCanBeParsedAndDeparsed("ALTER TABLE t OWNER TO bob",
true, p -> p.withDialect(dialect));
assertEquals(AlterOperation.UNSPECIFIC,
alter.getAlterExpressions().get(0).getOperation());
}
}

@ParameterizedTest
@ValueSource(strings = {"ALTER TABLE t CLUSTER BY (a)", "ALTER TABLE t DISABLE TRIGGER t1, t2",
"ALTER TABLE t ENABLE TRIGGER t1, t2, ADD COLUMN z INT"})
void otherProductsFormsKeepTheirReadingWithoutADialect(String sql)
throws JSQLParserException {
// Snowflake's CLUSTER BY and SQL Server's trigger lists are not PostgreSQL actions.
Alter alter = (Alter) assertSqlCanBeParsedAndDeparsed(sql, true);
assertEquals(1, alter.getAlterExpressions().size());
assertEquals(AlterOperation.UNSPECIFIC,
alter.getAlterExpressions().get(0).getOperation());
}

@ParameterizedTest
@ValueSource(strings = {"ALTER TABLE t ALTER COLUMN c SET STATISTICS 100",
"ALTER TABLE t ALTER COLUMN c SET STORAGE EXTERNAL",
"ALTER TABLE t ALTER COLUMN c SET COMPRESSION lz4",
"ALTER TABLE t ALTER COLUMN c DROP EXPRESSION"})
void columnActionsOfTheGenericBranchKeepTheirModelWithoutADialect(String sql)
throws JSQLParserException {
Alter alter = (Alter) assertSqlCanBeParsedAndDeparsed(sql, true);
AlterExpression expression = alter.getAlterExpressions().get(0);
assertFalse(expression instanceof RelationAlterAction);
assertEquals(AlterOperation.ALTER, expression.getOperation());
}

private static Statement parse(String sql) throws JSQLParserException {
return CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL));
}
Expand Down
Loading