Skip to content

Commit

Permalink
Add SHOW ROLES to the parser
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 34ffbaa commit f147d32
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 1 deletion.
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.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
import io.prestosql.sql.tree.ShowStats;
Expand Down Expand Up @@ -85,6 +86,7 @@ private StatementUtils() {}
builder.put(ShowCreate.class, QueryType.DESCRIBE);
builder.put(ShowFunctions.class, QueryType.DESCRIBE);
builder.put(ShowGrants.class, QueryType.DESCRIBE);
builder.put(ShowRoles.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 @@ -99,6 +99,7 @@ statement
| SHOW COLUMNS (FROM | IN) qualifiedName #showColumns
| SHOW STATS FOR qualifiedName #showStats
| SHOW STATS FOR '(' querySpecification ')' #showStatsForQuery
| SHOW ROLES ((FROM | IN) identifier)? #showRoles
| DESCRIBE qualifiedName #showColumns
| DESC qualifiedName #showColumns
| SHOW FUNCTIONS #showFunctions
Expand Down Expand Up @@ -651,6 +652,7 @@ RESTRICT: 'RESTRICT';
REVOKE: 'REVOKE';
RIGHT: 'RIGHT';
ROLE: 'ROLE';
ROLES: 'ROLES';
ROLLBACK: 'ROLLBACK';
ROLLUP: 'ROLLUP';
ROW: 'ROW';
Expand Down
15 changes: 14 additions & 1 deletion 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.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
import io.prestosql.sql.tree.ShowStats;
Expand Down Expand Up @@ -1259,12 +1260,24 @@ public Void visitShowGrants(ShowGrants node, Integer indent)
return null;
}

@Override
protected Void visitShowRoles(ShowRoles node, Integer context)
{
builder.append("SHOW ROLES");

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

return null;
}

@Override
public Void visitSetPath(SetPath node, Integer indent)
{
builder.append("SET PATH ");
builder.append(Joiner.on(", ").join(node.getPathSpecification().getPath()));

return null;
}

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.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
import io.prestosql.sql.tree.ShowStats;
Expand Down Expand Up @@ -936,6 +937,14 @@ public Node visitShowGrants(SqlBaseParser.ShowGrantsContext context)
tableName);
}

@Override
public Node visitShowRoles(SqlBaseParser.ShowRolesContext context)
{
return new ShowRoles(
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 @@ -637,6 +637,11 @@ protected R visitShowGrants(ShowGrants node, C context)
return visitStatement(node, context);
}

protected R visitShowRoles(ShowRoles node, C context)
{
return visitStatement(node, context);
}

protected R visitSetPath(SetPath node, C context)
{
return visitStatement(node, context);
Expand Down
89 changes: 89 additions & 0 deletions presto-parser/src/main/java/io/prestosql/sql/tree/ShowRoles.java
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 ShowRoles
extends Statement
{
private final Optional<Identifier> catalog;

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

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

public ShowRoles(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.visitShowRoles(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;
}
ShowRoles o = (ShowRoles) 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.ShowRoles;
import io.prestosql.sql.tree.ShowSchemas;
import io.prestosql.sql.tree.ShowSession;
import io.prestosql.sql.tree.ShowStats;
Expand Down Expand Up @@ -1442,6 +1443,18 @@ public void testShowGrants()
new ShowGrants(false, Optional.empty()));
}

@Test
public void testShowRoles()
throws Exception
{
assertStatement("SHOW ROLES",
new ShowRoles(Optional.empty()));
assertStatement("SHOW ROLES FROM foo",
new ShowRoles(Optional.of(new Identifier("foo"))));
assertStatement("SHOW ROLES IN foo",
new ShowRoles(Optional.of(new Identifier("foo"))));
}

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

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

Expand Down

0 comments on commit f147d32

Please sign in to comment.