From 0c3406cec91ff8541ee3c0c9524441dcff3b6efe Mon Sep 17 00:00:00 2001 From: Christina Wallin Date: Tue, 28 Nov 2017 14:51:07 +0100 Subject: [PATCH] Add SHOW ROLE GRANTS syntax Extracted-From: https://github.com/prestodb/presto/pull/10904 --- .../io/prestosql/util/StatementUtils.java | 2 + .../antlr4/io/prestosql/sql/parser/SqlBase.g4 | 1 + .../java/io/prestosql/sql/SqlFormatter.java | 14 +++ .../io/prestosql/sql/parser/AstBuilder.java | 9 ++ .../io/prestosql/sql/tree/AstVisitor.java | 5 ++ .../io/prestosql/sql/tree/ShowRoleGrants.java | 89 +++++++++++++++++++ .../prestosql/sql/parser/TestSqlParser.java | 10 +++ .../sql/parser/TestStatementBuilder.java | 2 + 8 files changed, 132 insertions(+) create mode 100644 presto-parser/src/main/java/io/prestosql/sql/tree/ShowRoleGrants.java diff --git a/presto-main/src/main/java/io/prestosql/util/StatementUtils.java b/presto-main/src/main/java/io/prestosql/util/StatementUtils.java index c5ed6ed4ee8f..97860f7542c9 100644 --- a/presto-main/src/main/java/io/prestosql/util/StatementUtils.java +++ b/presto-main/src/main/java/io/prestosql/util/StatementUtils.java @@ -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; @@ -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); diff --git a/presto-parser/src/main/antlr4/io/prestosql/sql/parser/SqlBase.g4 b/presto-parser/src/main/antlr4/io/prestosql/sql/parser/SqlBase.g4 index 00e0e15abbb5..738767244fac 100644 --- a/presto-parser/src/main/antlr4/io/prestosql/sql/parser/SqlBase.g4 +++ b/presto-parser/src/main/antlr4/io/prestosql/sql/parser/SqlBase.g4 @@ -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 diff --git a/presto-parser/src/main/java/io/prestosql/sql/SqlFormatter.java b/presto-parser/src/main/java/io/prestosql/sql/SqlFormatter.java index eb83278592a4..ad85756bdad0 100644 --- a/presto-parser/src/main/java/io/prestosql/sql/SqlFormatter.java +++ b/presto-parser/src/main/java/io/prestosql/sql/SqlFormatter.java @@ -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; @@ -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) { diff --git a/presto-parser/src/main/java/io/prestosql/sql/parser/AstBuilder.java b/presto-parser/src/main/java/io/prestosql/sql/parser/AstBuilder.java index 7e654f208c0c..d887b588ff11 100644 --- a/presto-parser/src/main/java/io/prestosql/sql/parser/AstBuilder.java +++ b/presto-parser/src/main/java/io/prestosql/sql/parser/AstBuilder.java @@ -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; @@ -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) { diff --git a/presto-parser/src/main/java/io/prestosql/sql/tree/AstVisitor.java b/presto-parser/src/main/java/io/prestosql/sql/tree/AstVisitor.java index 22808d9249d2..0f1f2c7d7803 100644 --- a/presto-parser/src/main/java/io/prestosql/sql/tree/AstVisitor.java +++ b/presto-parser/src/main/java/io/prestosql/sql/tree/AstVisitor.java @@ -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); diff --git a/presto-parser/src/main/java/io/prestosql/sql/tree/ShowRoleGrants.java b/presto-parser/src/main/java/io/prestosql/sql/tree/ShowRoleGrants.java new file mode 100644 index 000000000000..6581c689fd10 --- /dev/null +++ b/presto-parser/src/main/java/io/prestosql/sql/tree/ShowRoleGrants.java @@ -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 catalog; + + public ShowRoleGrants(Optional catalog) + { + this(Optional.empty(), catalog); + } + + public ShowRoleGrants(NodeLocation location, Optional catalog) + { + this(Optional.of(location), catalog); + } + + public ShowRoleGrants(Optional location, Optional catalog) + { + super(location); + this.catalog = requireNonNull(catalog, "catalog is null"); + } + + public Optional getCatalog() + { + return catalog; + } + + @Override + public R accept(AstVisitor visitor, C context) + { + return visitor.visitShowRoleGrants(this, context); + } + + @Override + public List 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(); + } +} diff --git a/presto-parser/src/test/java/io/prestosql/sql/parser/TestSqlParser.java b/presto-parser/src/test/java/io/prestosql/sql/parser/TestSqlParser.java index 7b839a2dbcda..1f8cd20f8f7e 100644 --- a/presto-parser/src/test/java/io/prestosql/sql/parser/TestSqlParser.java +++ b/presto-parser/src/test/java/io/prestosql/sql/parser/TestSqlParser.java @@ -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; @@ -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() { diff --git a/presto-parser/src/test/java/io/prestosql/sql/parser/TestStatementBuilder.java b/presto-parser/src/test/java/io/prestosql/sql/parser/TestStatementBuilder.java index 46d966ebb4a8..4ca4d1d9f246 100644 --- a/presto-parser/src/test/java/io/prestosql/sql/parser/TestStatementBuilder.java +++ b/presto-parser/src/test/java/io/prestosql/sql/parser/TestStatementBuilder.java @@ -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\"");