-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add page information to SqlStatementResource API #14512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.druid.msq.sql.entity; | ||
|
|
||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Contains information about a single page in the results. | ||
| */ | ||
| public class PageInformation | ||
| { | ||
| @Nullable | ||
| private final Long numRows; | ||
| @Nullable | ||
| private final Long sizeInBytes; | ||
| private final long id; | ||
|
|
||
| @JsonCreator | ||
| public PageInformation( | ||
| @JsonProperty("numRows") @Nullable Long numRows, | ||
| @JsonProperty("sizeInBytes") @Nullable Long sizeInBytes, | ||
| @JsonProperty("id") long id | ||
| ) | ||
| { | ||
| this.numRows = numRows; | ||
| this.sizeInBytes = sizeInBytes; | ||
| this.id = id; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| @Nullable | ||
| public Long getNumRows() | ||
| { | ||
| return numRows; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| @Nullable | ||
| public Long getSizeInBytes() | ||
| { | ||
| return sizeInBytes; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public long getId() | ||
| { | ||
| return id; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) | ||
| { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| PageInformation that = (PageInformation) o; | ||
| return id == that.id && Objects.equals(numRows, that.numRows) && Objects.equals( | ||
| sizeInBytes, | ||
| that.sizeInBytes | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| return Objects.hash(numRows, sizeInBytes, id); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return "PageInformation{" + | ||
| "numRows=" + numRows + | ||
| ", sizeInBytes=" + sizeInBytes + | ||
| ", id=" + id + | ||
| '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
|
|
||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.io.CountingOutputStream; | ||
| import com.google.common.util.concurrent.ListenableFuture; | ||
|
|
@@ -46,6 +47,7 @@ | |
| import org.apache.druid.msq.sql.MSQTaskSqlEngine; | ||
| import org.apache.druid.msq.sql.SqlStatementState; | ||
| import org.apache.druid.msq.sql.entity.ColumnNameAndTypes; | ||
| import org.apache.druid.msq.sql.entity.PageInformation; | ||
| import org.apache.druid.msq.sql.entity.ResultSetInformation; | ||
| import org.apache.druid.msq.sql.entity.SqlStatementResult; | ||
| import org.apache.druid.msq.util.SqlStatementResourceHelper; | ||
|
|
@@ -268,8 +270,7 @@ public Response doGetStatus( | |
| @Produces(MediaType.APPLICATION_JSON) | ||
| public Response doGetResults( | ||
| @PathParam("id") final String queryId, | ||
| @QueryParam("offset") Long offset, | ||
| @QueryParam("numRows") Long numberOfRows, | ||
| @QueryParam("page") Long page, | ||
| @Context final HttpServletRequest req | ||
| ) | ||
| { | ||
|
|
@@ -284,28 +285,16 @@ public Response doGetResults( | |
| } | ||
| final AuthenticationResult authenticationResult = AuthorizationUtils.authenticationResultFromRequest(req); | ||
|
|
||
| if (offset != null && offset < 0) { | ||
| if (page != null && page < 0) { | ||
| return buildNonOkResponse( | ||
| DruidException.forPersona(DruidException.Persona.USER) | ||
| .ofCategory(DruidException.Category.INVALID_INPUT) | ||
| .build( | ||
| "offset cannot be negative. Please pass a positive number." | ||
| ) | ||
| ); | ||
| } | ||
| if (numberOfRows != null && numberOfRows < 0) { | ||
| return buildNonOkResponse( | ||
| DruidException.forPersona(DruidException.Persona.USER) | ||
| .ofCategory(DruidException.Category.INVALID_INPUT) | ||
| .build( | ||
| "numRows cannot be negative. Please pass a positive number." | ||
| "Page cannot be negative. Please pass a positive number." | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the page does not exist its also an error.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added this check below as "if page > 0" for the report since we don't really have a list of pages to check the index for yet and manually creating a list of a single page to check this doesn't make sense |
||
| final long start = offset == null ? 0 : offset; | ||
| final long last = SqlStatementResourceHelper.getLastIndex(numberOfRows, start); | ||
|
|
||
| TaskStatusResponse taskResponse = contactOverlord(overlordClient.taskStatus(queryId)); | ||
| if (taskResponse == null) { | ||
| return Response.status(Response.Status.NOT_FOUND).build(); | ||
|
|
@@ -343,8 +332,21 @@ public Response doGetResults( | |
| if (!signature.isPresent()) { | ||
| return Response.ok().build(); | ||
| } | ||
| Optional<List<Object>> results = SqlStatementResourceHelper.getResults(SqlStatementResourceHelper.getPayload( | ||
| contactOverlord(overlordClient.taskReportAsMap(queryId)))); | ||
|
|
||
| if (page != null && page > 0) { | ||
| // Results from task report are only present as one page. | ||
| return buildNonOkResponse( | ||
| DruidException.forPersona(DruidException.Persona.USER) | ||
| .ofCategory(DruidException.Category.INVALID_INPUT) | ||
| .build("Page number is out of range of the results.") | ||
| ); | ||
| } | ||
|
|
||
| Optional<List<Object>> results = SqlStatementResourceHelper.getResults( | ||
| SqlStatementResourceHelper.getPayload( | ||
| contactOverlord(overlordClient.taskReportAsMap(queryId)) | ||
| ) | ||
| ); | ||
|
|
||
| return Response.ok((StreamingOutput) outputStream -> { | ||
| CountingOutputStream os = new CountingOutputStream(outputStream); | ||
|
|
@@ -353,7 +355,7 @@ public Response doGetResults( | |
| List<ColumnNameAndTypes> rowSignature = signature.get(); | ||
| writer.writeResponseStart(); | ||
|
|
||
| for (long k = start; k < Math.min(last, results.get().size()); k++) { | ||
| for (long k = 0; k < results.get().size(); k++) { | ||
| writer.writeRowStart(); | ||
| for (int i = 0; i < rowSignature.size(); i++) { | ||
| writer.writeRowField( | ||
|
|
@@ -575,13 +577,19 @@ private Optional<ResultSetInformation> getSampleResults( | |
| isSelectQuery | ||
| ); | ||
| return Optional.of(new ResultSetInformation( | ||
| null, | ||
| // since the rows can be sampled, get the number of rows from counters | ||
| rowsAndSize.orElse(new Pair<>(null, null)).lhs, | ||
| rowsAndSize.orElse(new Pair<>(null, null)).rhs, | ||
| null, | ||
| dataSource, | ||
| // only populate sample results in case a select query is successful | ||
| isSelectQuery ? SqlStatementResourceHelper.getResults(payload).orElse(null) : null | ||
| isSelectQuery ? SqlStatementResourceHelper.getResults(payload).orElse(null) : null, | ||
| ImmutableList.of( | ||
| new PageInformation( | ||
| rowsAndSize.orElse(new Pair<>(null, null)).lhs, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be populate from the counters. so its returns pageInformation.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might need to be handled at least partially in the next change, as counters don't really mean anything for results if the destination is the task report. |
||
| rowsAndSize.orElse(new Pair<>(null, null)).rhs, | ||
| 0 | ||
| ) | ||
| ) | ||
| )); | ||
| } else { | ||
| return Optional.empty(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.