Skip to content

Commit

Permalink
Fix SHOW ROLES where there are no roles to display
Browse files Browse the repository at this point in the history
  • Loading branch information
kokosing committed Sep 23, 2021
1 parent 390f2fd commit e38dbf0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
import static io.trino.sql.QueryUtil.aliasedName;
import static io.trino.sql.QueryUtil.aliasedNullToEmpty;
import static io.trino.sql.QueryUtil.ascending;
import static io.trino.sql.QueryUtil.emptyQuery;
import static io.trino.sql.QueryUtil.equal;
import static io.trino.sql.QueryUtil.functionCall;
import static io.trino.sql.QueryUtil.identifier;
Expand Down Expand Up @@ -299,18 +300,14 @@ protected Node visitShowRoles(ShowRoles node, Void context)
List<Expression> rows = enabledRoles.stream()
.map(role -> row(new StringLiteral(role)))
.collect(toList());
return simpleQuery(
selectList(new AllColumns()),
aliased(new Values(rows), "role_name", ImmutableList.of("Role")));
return singleColumnValues(rows, "Role");
}
else {
accessControl.checkCanShowRoles(session.toSecurityContext(), catalog);
List<Expression> rows = metadata.listRoles(session, catalog).stream()
.map(role -> row(new StringLiteral(role)))
.collect(toList());
return simpleQuery(
selectList(new AllColumns()),
aliased(new Values(rows), "role_name", ImmutableList.of("Role")));
return singleColumnValues(rows, "Role");
}
}

Expand All @@ -325,10 +322,19 @@ protected Node visitShowRoleGrants(ShowRoleGrants node, Void context)
.map(roleGrant -> row(new StringLiteral(roleGrant.getRoleName())))
.collect(toList());

return singleColumnValues(rows, "Role Grants");
}

private Query singleColumnValues(List<Expression> rows, String columnName)
{
List<String> columns = ImmutableList.of(columnName);
if (rows.isEmpty()) {
return emptyQuery(columns);
}
return simpleQuery(
selectList(new AllColumns()),
aliased(new Values(rows), "role_grants", ImmutableList.of("Role Grants")),
ordering(ascending("Role Grants")));
aliased(new Values(rows), "relation", columns),
ordering(ascending(columnName)));
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions core/trino-parser/src/main/java/io/trino/sql/QueryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.trino.sql.tree.Identifier;
import io.trino.sql.tree.LogicalBinaryExpression;
import io.trino.sql.tree.Node;
import io.trino.sql.tree.NullLiteral;
import io.trino.sql.tree.Offset;
import io.trino.sql.tree.OrderBy;
import io.trino.sql.tree.QualifiedName;
Expand Down Expand Up @@ -267,6 +268,25 @@ public static Query singleValueQuery(String columnName, boolean value)
aliased(values, "t", ImmutableList.of(columnName)));
}

// TODO pass column types
public static Query emptyQuery(List<String> columns)
{
Select select = selectList(columns.stream()
.map(column -> new SingleColumn(new NullLiteral(), QueryUtil.identifier(column)))
.toArray(SelectItem[]::new));
Optional<Expression> where = Optional.of(FALSE_LITERAL);
return query(new QuerySpecification(
select,
Optional.empty(),
where,
Optional.empty(),
Optional.empty(),
ImmutableList.of(),
Optional.empty(),
Optional.empty(),
Optional.empty()));
}

public static Query query(QueryBody body)
{
return new Query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,13 @@ public void testNoCatalogIsNeededInSessionForShowRoles()
assertQuery(session, "SHOW CURRENT ROLES FROM mock", "VALUES 'alice_role'");
assertQuery(session, "SELECT * FROM mock.information_schema.applicable_roles", "SELECT 'alice', 'USER', 'alice_role', 'NO'");
}

@Test
public void testEmptyRoles()
{
assertQueryReturnsEmptyResult("SHOW ROLES");
assertQueryReturnsEmptyResult("SHOW ROLE GRANTS");
assertQueryReturnsEmptyResult("SHOW CURRENT ROLES");
assertQueryReturnsEmptyResult("SELECT * FROM information_schema.applicable_roles");
}
}

0 comments on commit e38dbf0

Please sign in to comment.