Skip to content

Commit

Permalink
Support multiple state filters on getAllQueryInfo API
Browse files Browse the repository at this point in the history
  • Loading branch information
sthandassery committed Dec 12, 2024
1 parent da79aed commit cd15023
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
20 changes: 10 additions & 10 deletions core/trino-main/src/main/java/io/trino/server/QueryResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.trino.server;

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import io.trino.dispatcher.DispatchManager;
import io.trino.execution.QueryInfo;
Expand Down Expand Up @@ -41,6 +40,8 @@
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static io.trino.connector.system.KillQueryProcedure.createKillQueryException;
import static io.trino.connector.system.KillQueryProcedure.createPreemptQueryException;
Expand Down Expand Up @@ -71,20 +72,19 @@ public QueryResource(DispatchManager dispatchManager, AccessControl accessContro

@ResourceSecurity(AUTHENTICATED_USER)
@GET
public List<BasicQueryInfo> getAllQueryInfo(@QueryParam("state") String stateFilter, @Context HttpServletRequest servletRequest, @Context HttpHeaders httpHeaders)
public List<BasicQueryInfo> getAllQueryInfo(@QueryParam("state") Set<String> stateFilters, @Context HttpServletRequest servletRequest, @Context HttpHeaders httpHeaders)
{
QueryState expectedState = stateFilter == null ? null : QueryState.valueOf(stateFilter.toUpperCase(Locale.ENGLISH));
Set<QueryState> expectedStates = stateFilters.stream()
.map(f -> f.toUpperCase(Locale.ENGLISH))
.map(QueryState::valueOf)
.collect(Collectors.toUnmodifiableSet());

List<BasicQueryInfo> queries = dispatchManager.getQueries();
queries = filterQueries(sessionContextFactory.extractAuthorizedIdentity(servletRequest, httpHeaders), queries, accessControl);

ImmutableList.Builder<BasicQueryInfo> builder = ImmutableList.builder();
for (BasicQueryInfo queryInfo : queries) {
if (stateFilter == null || queryInfo.getState() == expectedState) {
builder.add(queryInfo);
}
}
return builder.build();
return queries.stream()
.filter(q -> expectedStates.isEmpty() || expectedStates.contains(q.getState()))
.collect(Collectors.toUnmodifiableList());
}

@ResourceSecurity(AUTHENTICATED_USER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,17 @@ public void testGetQueryInfos()
assertThat(infos).isEmpty();
assertStateCounts(infos, 0, 0, 0);

infos = getQueryInfos("/v1/query?state=finished&state=failed&state=running");
assertThat(infos).hasSize(3);
assertStateCounts(infos, 2, 1, 0);

server.getAccessControl().deny(privilege("query", VIEW_QUERY));
try {
assertThat(getQueryInfos("/v1/query")).isEmpty();
assertThat(getQueryInfos("/v1/query?state=finished")).isEmpty();
assertThat(getQueryInfos("/v1/query?state=failed")).isEmpty();
assertThat(getQueryInfos("/v1/query?state=running")).isEmpty();
assertThat(getQueryInfos("/v1/query?state=finished&state=failed&state=running")).isEmpty();
}
finally {
server.getAccessControl().reset();
Expand Down

0 comments on commit cd15023

Please sign in to comment.