-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
Signed-off-by: Vamsi Manohar <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.opensearch.sql.protocol.response; | ||
|
||
import java.util.Collection; | ||
import lombok.Getter; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.executor.ExecutionEngine; | ||
import org.opensearch.sql.executor.pagination.Cursor; | ||
|
||
/** AsyncQueryResult for async query APIs. */ | ||
public class AsyncQueryResult extends QueryResult { | ||
|
||
@Getter private final String status; | ||
|
||
public AsyncQueryResult( | ||
String status, | ||
ExecutionEngine.Schema schema, | ||
Collection<ExprValue> exprValues, | ||
Cursor cursor) { | ||
super(schema, exprValues, cursor); | ||
this.status = status; | ||
} | ||
|
||
public AsyncQueryResult( | ||
String status, ExecutionEngine.Schema schema, Collection<ExprValue> exprValues) { | ||
super(schema, exprValues); | ||
this.status = status; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.protocol.response.format; | ||
|
||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Singular; | ||
import org.opensearch.sql.protocol.response.AsyncQueryResult; | ||
import org.opensearch.sql.protocol.response.QueryResult; | ||
|
||
/** | ||
* JSON response format with schema header and data rows. For example, | ||
* | ||
* <pre> | ||
* { | ||
* "schema": [ | ||
* { | ||
* "name": "name", | ||
* "type": "string" | ||
* } | ||
* ], | ||
* "datarows": [ | ||
* ["John"], | ||
* ["Smith"] | ||
* ], | ||
* "total": 2, | ||
* "size": 2 | ||
* } | ||
* </pre> | ||
*/ | ||
public class SimpleJsonAsyncQueryResponseFormatter extends JsonResponseFormatter<AsyncQueryResult> { | ||
|
||
public SimpleJsonAsyncQueryResponseFormatter(Style style) { | ||
super(style); | ||
} | ||
Check warning on line 40 in protocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java Codecov / codecov/patchprotocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java#L39-L40
|
||
|
||
@Override | ||
public Object buildJsonObject(AsyncQueryResult response) { | ||
JsonResponse.JsonResponseBuilder json = JsonResponse.builder(); | ||
json.status(response.getStatus()); | ||
json.total(response.size()).size(response.size()); | ||
Check warning on line 46 in protocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java Codecov / codecov/patchprotocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java#L44-L46
|
||
|
||
response.columnNameTypes().forEach((name, type) -> json.column(new Column(name, type))); | ||
Check warning on line 48 in protocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java Codecov / codecov/patchprotocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java#L48
|
||
|
||
json.datarows(fetchDataRows(response)); | ||
return json.build(); | ||
Check warning on line 51 in protocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java Codecov / codecov/patchprotocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java#L50-L51
|
||
} | ||
|
||
private Object[][] fetchDataRows(QueryResult response) { | ||
Object[][] rows = new Object[response.size()][]; | ||
int i = 0; | ||
Check warning on line 56 in protocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java Codecov / codecov/patchprotocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java#L55-L56
|
||
for (Object[] values : response) { | ||
rows[i++] = values; | ||
} | ||
return rows; | ||
Check warning on line 60 in protocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java Codecov / codecov/patchprotocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonAsyncQueryResponseFormatter.java#L58-L60
|
||
} | ||
|
||
/** org.json requires these inner data classes be public (and static) */ | ||
@Builder | ||
@Getter | ||
public static class JsonResponse { | ||
|
||
private final String status; | ||
|
||
@Singular("column") | ||
private final List<Column> schema; | ||
|
||
private final Object[][] datarows; | ||
|
||
private long total; | ||
private long size; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public static class Column { | ||
private final String name; | ||
private final String type; | ||
} | ||
} |