-
Notifications
You must be signed in to change notification settings - Fork 0
Dimitrisstaratzis/sc 22112/support arrow return type for serverless #9
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
Merged
DimitrisStaratzis
merged 3 commits into
master
from
dimitrisstaratzis/sc-22112/support-arrow-return-type-for-serverless
Oct 18, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| This file contains custom methods to add to the generated files. | ||
|
|
||
| SqlApi.java | ||
|
|
||
| /** | ||
| * | ||
| * Run a sql query | ||
| * @param namespace namespace to run task under is in (an organization name or user's username) (required) | ||
| * @param sql sql being submitted (required) | ||
| * @param acceptEncoding Encoding to use (optional) | ||
| * @return ApiResponse with byte[] | ||
| * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body | ||
| * @http.response.details | ||
| <table summary="Response Details" border="1"> | ||
| <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> | ||
| <tr><td> 200 </td><td> JSON results in array of objects form, if the query returns results </td><td> * X-TILEDB-CLOUD-TASK-ID - Task ID for just completed request <br> </td></tr> | ||
| <tr><td> 204 </td><td> SQL executed successfully </td><td> * X-TILEDB-CLOUD-TASK-ID - Task ID for just completed request <br> </td></tr> | ||
| <tr><td> 0 </td><td> error response </td><td> - </td></tr> | ||
| </table> | ||
| */ | ||
| public ApiResponse<byte[]> runSQLWithHttpInfoBytes(String namespace, SQLParameters sql, String acceptEncoding) throws ApiException { | ||
| okhttp3.Call localVarCall = runSQLValidateBeforeCall(namespace, sql, acceptEncoding, null); | ||
| Type localVarReturnType = new TypeToken<byte[]>(){}.getType(); | ||
| return localVarApiClient.execute(localVarCall, localVarReturnType); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * Run a sql query | ||
| * @param namespace namespace to run task under is in (an organization name or user's username) (required) | ||
| * @param sql sql being submitted (required) | ||
| * @param acceptEncoding Encoding to use (optional) | ||
| * @return byte[] | ||
| * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body | ||
| * @http.response.details | ||
| <table summary="Response Details" border="1"> | ||
| <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr> | ||
| <tr><td> 200 </td><td> JSON results in array of objects form, if the query returns results </td><td> * X-TILEDB-CLOUD-TASK-ID - Task ID for just completed request <br> </td></tr> | ||
| <tr><td> 204 </td><td> SQL executed successfully </td><td> * X-TILEDB-CLOUD-TASK-ID - Task ID for just completed request <br> </td></tr> | ||
| <tr><td> 0 </td><td> error response </td><td> - </td></tr> | ||
| </table> | ||
| */ | ||
| public byte[] runSQLBytes(String namespace, SQLParameters sql, String acceptEncoding) throws ApiException { | ||
| ApiResponse<byte[]> localVarResp = runSQLWithHttpInfoBytes(namespace, sql, acceptEncoding); | ||
| return localVarResp.getData(); | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package io.tiledb.cloud; | ||
|
|
||
| import io.tiledb.cloud.rest_api.ApiException; | ||
| import io.tiledb.cloud.rest_api.api.SqlApi; | ||
| import io.tiledb.cloud.rest_api.model.ResultFormat; | ||
| import io.tiledb.cloud.rest_api.model.SQLParameters; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.VectorSchemaRoot; | ||
| import org.apache.arrow.vector.ipc.ArrowStreamReader; | ||
| import org.apache.arrow.vector.types.pojo.Schema; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class TileDBSQL { | ||
| String namespace; | ||
|
|
||
| SQLParameters sql; | ||
|
|
||
| TileDBClient tileDBClient; | ||
|
|
||
| SqlApi apiInstance; | ||
|
|
||
| ArrayList<VectorSchemaRoot> readBatches; | ||
|
|
||
| List<Object> results; | ||
|
|
||
| /** | ||
| * | ||
| * @param tileDBClient The TileDBClient | ||
| * @param namespace namespace to run task under is in (an organization name or user's username) | ||
| * @param sql sql being submitted | ||
| */ | ||
| public TileDBSQL(TileDBClient tileDBClient, String namespace, SQLParameters sql) { | ||
| Objects.requireNonNull(tileDBClient, "TileDBClient can not be null"); | ||
| Objects.requireNonNull(namespace, "Namespace can not be null"); | ||
| Objects.requireNonNull(sql, "SQL parameters can not be null"); | ||
| this.namespace = namespace; | ||
| this.sql = sql; | ||
| this.tileDBClient = tileDBClient; | ||
| this.apiInstance = new SqlApi(this.tileDBClient.getApiClient()); | ||
| this.readBatches = new ArrayList<>(); | ||
| } | ||
|
|
||
| /** | ||
| * Exec an SQL query and get results in arrow format. | ||
| */ | ||
| public void execArrow(){ | ||
| try { | ||
| assert sql.getResultFormat() != null; | ||
| byte[] bytes = apiInstance.runSQLBytes(namespace, sql, sql.getResultFormat().toString()); | ||
| System.out.println(Arrays.toString(bytes)); | ||
|
|
||
| RootAllocator allocator = new RootAllocator(Long.MAX_VALUE); | ||
| try (ArrowStreamReader reader = new ArrowStreamReader(new ByteArrayInputStream(bytes), allocator)) { | ||
| while (reader.loadNextBatch()) { | ||
| // This will be loaded with new values on every call to loadNextBatch | ||
| VectorSchemaRoot readBatch = reader.getVectorSchemaRoot(); | ||
| readBatches.add(readBatch); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } catch (ApiException e) { | ||
| System.err.println("Exception when calling SqlApi#runSQL/runSQLBytes"); | ||
| System.err.println("Status code: " + e.getCode()); | ||
| System.err.println("Reason: " + e.getResponseBody()); | ||
| System.err.println("Response headers: " + e.getResponseHeaders()); | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Exec an SQL query and get results in any format except arrow. | ||
| * | ||
| * @return | ||
| */ | ||
| public void execStandard(){ | ||
| try { | ||
| assert sql.getResultFormat() != null; | ||
| results = apiInstance.runSQL(namespace, sql, sql.getResultFormat().toString()); | ||
| } catch (ApiException e) { | ||
| System.err.println("Exception when calling SqlApi#runSQL/runSQLBytes"); | ||
| System.err.println("Status code: " + e.getCode()); | ||
| System.err.println("Reason: " + e.getResponseBody()); | ||
| System.err.println("Response headers: " + e.getResponseHeaders()); | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Exec an SQL query | ||
| */ | ||
| public void exec(){ | ||
| if (this.sql.getResultFormat() == ResultFormat.ARROW){ | ||
| execArrow(); | ||
| }else { | ||
| execStandard(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the results in Arrow format | ||
| * @return | ||
| */ | ||
| public ArrayList<VectorSchemaRoot> getReadBatches() { | ||
| return readBatches; | ||
| } | ||
|
|
||
| /** | ||
| * Get the results as lists of Objects | ||
| * @return | ||
| */ | ||
| public List<Object> getResults() { | ||
| return results; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a follow up we should find a way to return these errors better to the user.