Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,13 @@ dependencies {
implementation group: 'org.apache.oltu.oauth2', name: 'org.apache.oltu.oauth2.client', version: '1.0.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation "jakarta.annotation:jakarta.annotation-api:$jakarta_annotation_version"
implementation group: 'org.apache.arrow', name: 'arrow-vector', version: '9.0.0'
runtimeOnly group: 'org.apache.arrow', name: 'arrow-memory-netty', version: '9.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'org.mockito:mockito-core:3.12.4'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation 'junit:junit:4.13.1'

}

import org.gradle.plugins.signing.Sign
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/io/tiledb/cloud/CustomCode.txt
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&#39;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();
}


121 changes: 121 additions & 0 deletions src/main/java/io/tiledb/cloud/TileDBSQL.java
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");
Copy link
Member

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.

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;
}
}
43 changes: 43 additions & 0 deletions src/main/java/io/tiledb/cloud/rest_api/api/SqlApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ public List<Object> runSQL(String namespace, SQLParameters sql, String acceptEnc
return localVarResp.getData();
}

/**
*
* Run a sql query
* @param namespace namespace to run task under is in (an organization name or user&#39;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();
}

/**
*
* Run a sql query
Expand All @@ -196,6 +217,28 @@ public ApiResponse<List<Object>> runSQLWithHttpInfo(String namespace, SQLParamet
return localVarApiClient.execute(localVarCall, localVarReturnType);
}

/**
*
* Run a sql query
* @param namespace namespace to run task under is in (an organization name or user&#39;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);
}

/**
* (asynchronously)
* Run a sql query
Expand Down