Skip to content

Commit

Permalink
Fix round trip of DROP ROLE statement
Browse files Browse the repository at this point in the history
Also, migrate assertStatement to ParserAssert.
  • Loading branch information
ebyhr authored and wendigo committed Sep 14, 2024
1 parent e118d4f commit 1f60c83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,11 @@ protected Void visitCreateRole(CreateRole node, Integer indent)
@Override
protected Void visitDropRole(DropRole node, Integer indent)
{
builder.append("DROP ROLE ").append(formatName(node.getName()));
builder.append("DROP ROLE ");
if (node.isExists()) {
builder.append("IF EXISTS ");
}
builder.append(formatName(node.getName()));
node.getCatalog().ifPresent(catalog -> builder
.append(" IN ")
.append(formatName(catalog)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ public boolean equals(Object o)
}
DropRole dropRole = (DropRole) o;
return Objects.equals(name, dropRole.name) &&
Objects.equals(catalog, dropRole.catalog);
Objects.equals(catalog, dropRole.catalog) &&
exists == dropRole.exists;
}

@Override
public int hashCode()
{
return Objects.hash(name, catalog);
return Objects.hash(name, catalog, exists);
}

@Override
Expand All @@ -78,6 +79,7 @@ public String toString()
return toStringHelper(this)
.add("name", name)
.add("catalog", catalog)
.add("exists", exists)
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4754,12 +4754,18 @@ public void testCreateRole()
@Test
public void testDropRole()
{
assertStatement("DROP ROLE role", new DropRole(location(1, 1), new Identifier("role"), Optional.empty(), false));
assertStatement("DROP ROLE IF EXISTS role", new DropRole(location(1, 1), new Identifier("role"), Optional.empty(), false));
assertStatement("DROP ROLE \"role\"", new DropRole(location(1, 1), new Identifier("role"), Optional.empty(), false));
assertStatement("DROP ROLE \"ro le\"", new DropRole(location(1, 1), new Identifier("ro le"), Optional.empty(), false));
assertStatement("DROP ROLE \"!@#$%^&*'ад\"\"мін\"", new DropRole(location(1, 1), new Identifier("!@#$%^&*'ад\"мін"), Optional.empty(), false));
assertStatement("DROP ROLE role IN my_catalog", new DropRole(location(1, 1), new Identifier("role"), Optional.of(new Identifier("my_catalog")), false));
assertThat(statement("DROP ROLE role")).isEqualTo(
new DropRole(location(1, 1), new Identifier(location(1, 11), "role", false), Optional.empty(), false));
assertThat(statement("DROP ROLE IF EXISTS role")).isEqualTo(
new DropRole(location(1, 1), new Identifier(location(1, 21), "role", false), Optional.empty(), true));
assertThat(statement("DROP ROLE \"role\"")).isEqualTo(
new DropRole(location(1, 1), new Identifier(location(1, 11), "role", true), Optional.empty(), false));
assertThat(statement("DROP ROLE \"ro le\"")).isEqualTo(
new DropRole(location(1, 1), new Identifier(location(1, 11), "ro le", true), Optional.empty(), false));
assertThat(statement("DROP ROLE \"!@#$%^&*'ад\"\"мін\"")).isEqualTo(
new DropRole(location(1, 1), new Identifier(location(1, 11), "!@#$%^&*'ад\"мін", true), Optional.empty(), false));
assertThat(statement("DROP ROLE role IN my_catalog")).isEqualTo(
new DropRole(location(1, 1), new Identifier(location(1, 11), "role", false), Optional.of(new Identifier(location(1, 19), "my_catalog", false)), false));
}

@Test
Expand Down

0 comments on commit 1f60c83

Please sign in to comment.