Skip to content

Commit

Permalink
Add SHOW ROLE GRANTS syntax
Browse files Browse the repository at this point in the history
Extracted-From: prestodb/presto#10904
  • Loading branch information
cawallin authored and sopel39 committed Jan 29, 2019
1 parent 5d9615c commit 0c3406c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import io.prestosql.sql.tree.ShowCreate;
import io.prestosql.sql.tree.ShowFunctions;
import io.prestosql.sql.tree.ShowGrants;
import io.prestosql.sql.tree.ShowRoleGrants;
import io.prestosql.sql.tree.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
Expand Down Expand Up @@ -87,6 +88,7 @@ private StatementUtils() {}
builder.put(ShowFunctions.class, QueryType.DESCRIBE);
builder.put(ShowGrants.class, QueryType.DESCRIBE);
builder.put(ShowRoles.class, QueryType.DESCRIBE);
builder.put(ShowRoleGrants.class, QueryType.DESCRIBE);
builder.put(ShowSchemas.class, QueryType.DESCRIBE);
builder.put(ShowSession.class, QueryType.DESCRIBE);
builder.put(ShowStats.class, QueryType.DESCRIBE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ statement
| SHOW STATS FOR qualifiedName #showStats
| SHOW STATS FOR '(' querySpecification ')' #showStatsForQuery
| SHOW CURRENT? ROLES ((FROM | IN) identifier)? #showRoles
| SHOW ROLE GRANTS ((FROM | IN) identifier)? #showRoleGrants
| DESCRIBE qualifiedName #showColumns
| DESC qualifiedName #showColumns
| SHOW FUNCTIONS #showFunctions
Expand Down
14 changes: 14 additions & 0 deletions presto-parser/src/main/java/io/prestosql/sql/SqlFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import io.prestosql.sql.tree.ShowCreate;
import io.prestosql.sql.tree.ShowFunctions;
import io.prestosql.sql.tree.ShowGrants;
import io.prestosql.sql.tree.ShowRoleGrants;
import io.prestosql.sql.tree.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
Expand Down Expand Up @@ -1277,6 +1278,19 @@ protected Void visitShowRoles(ShowRoles node, Integer context)
return null;
}

@Override
protected Void visitShowRoleGrants(ShowRoleGrants node, Integer context)
{
builder.append("SHOW ROLE GRANTS");

if (node.getCatalog().isPresent()) {
builder.append(" FROM ")
.append(node.getCatalog().get());
}

return null;
}

@Override
public Void visitSetPath(SetPath node, Integer indent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
import io.prestosql.sql.tree.ShowCreate;
import io.prestosql.sql.tree.ShowFunctions;
import io.prestosql.sql.tree.ShowGrants;
import io.prestosql.sql.tree.ShowRoleGrants;
import io.prestosql.sql.tree.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
Expand Down Expand Up @@ -946,6 +947,14 @@ public Node visitShowRoles(SqlBaseParser.ShowRolesContext context)
context.CURRENT() != null);
}

@Override
public Node visitShowRoleGrants(SqlBaseParser.ShowRoleGrantsContext context)
{
return new ShowRoleGrants(
getLocation(context),
getIdentifierIfPresent(context.identifier()));
}

@Override
public Node visitSetPath(SqlBaseParser.SetPathContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,11 @@ protected R visitShowRoles(ShowRoles node, C context)
return visitStatement(node, context);
}

protected R visitShowRoleGrants(ShowRoleGrants node, C context)
{
return visitStatement(node, context);
}

protected R visitSetPath(SetPath node, C context)
{
return visitStatement(node, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.sql.tree;

import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public class ShowRoleGrants
extends Statement
{
private final Optional<Identifier> catalog;

public ShowRoleGrants(Optional<Identifier> catalog)
{
this(Optional.empty(), catalog);
}

public ShowRoleGrants(NodeLocation location, Optional<Identifier> catalog)
{
this(Optional.of(location), catalog);
}

public ShowRoleGrants(Optional<NodeLocation> location, Optional<Identifier> catalog)
{
super(location);
this.catalog = requireNonNull(catalog, "catalog is null");
}

public Optional<Identifier> getCatalog()
{
return catalog;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
return visitor.visitShowRoleGrants(this, context);
}

@Override
public List<Node> getChildren()
{
return ImmutableList.of();
}

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

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
ShowRoleGrants o = (ShowRoleGrants) obj;
return Objects.equals(catalog, o.catalog);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("catalog", catalog)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import io.prestosql.sql.tree.ShowCatalogs;
import io.prestosql.sql.tree.ShowColumns;
import io.prestosql.sql.tree.ShowGrants;
import io.prestosql.sql.tree.ShowRoleGrants;
import io.prestosql.sql.tree.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
Expand Down Expand Up @@ -1462,6 +1463,15 @@ public void testShowRoles()
new ShowRoles(Optional.of(new Identifier("foo")), true));
}

@Test
public void testShowRoleGrants()
{
assertStatement("SHOW ROLE GRANTS",
new ShowRoleGrants(Optional.empty(), Optional.empty()));
assertStatement("SHOW ROLE GRANTS FROM catalog",
new ShowRoleGrants(Optional.of(new Identifier("catalog"))));
}

@Test
public void testSetPath()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ public void testStatementBuilder()
printStatement("show roles from foo");
printStatement("show current roles");
printStatement("show current roles from foo");
printStatement("show role grants");
printStatement("show role grants from foo");

printStatement("prepare p from select * from (select * from T) \"A B\"");

Expand Down

0 comments on commit 0c3406c

Please sign in to comment.