Rewrite SHOW ROLES as a select query
For SHOW ROLES, issue the query:
select role_name as "Role" from catalog.information_schema.roles;
@@ -58,6 +58,7 @@ import com.facebook.presto.sql.tree.ShowColumns;
import com.facebook.presto.sql.tree.ShowCreate;
import com.facebook.presto.sql.tree.ShowFunctions;
import com.facebook.presto.sql.tree.ShowGrants;
+import com.facebook.presto.sql.tree.ShowRoles;
import com.facebook.presto.sql.tree.ShowSchemas;
import com.facebook.presto.sql.tree.ShowSession;
import com.facebook.presto.sql.tree.ShowTables;
@@ -80,6 +81,7 @@ import java.util.Set;
import java.util.SortedMap;
import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_COLUMNS;
+import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_ROLES;
import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_SCHEMATA;
import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_TABLES;
import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_TABLE_PRIVILEGES;
@@ -120,6 +122,7 @@ import static com.facebook.presto.sql.tree.ShowCreate.Type.VIEW;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.String.format;
+import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
@@ -254,6 +257,19 @@ final class ShowQueriesRewrite
Optional.of(ordering(ascending("grantee"), ascending("table_name"))));
}
+ @Override
+ protected Node visitShowRoles(ShowRoles node, Void context)
+ {
+ if (!node.getCatalog().isPresent() && !session.getCatalog().isPresent()) {
+ throw new SemanticException(CATALOG_NOT_SPECIFIED, node, "Catalog must be specified when session catalog is not set");
+ }
+
+ String catalog = node.getCatalog().map(c -> c.getValue().toLowerCase(ENGLISH)).orElseGet(() -> session.getCatalog().get());
+ return simpleQuery(
+ selectList(aliasedName("role_name", "Role")),
+ from(catalog, TABLE_ROLES));
+ }
+
@Override
protected Node visitShowSchemas(ShowSchemas node, Void context)
{
