Skip to content

Commit f175c5f

Browse files
authored
Merge pull request #7 from RelationalAI/hnr-enhance-arrow-result
Enhance arrow parsing results type
2 parents 177341e + b2bed82 commit f175c5f

File tree

6 files changed

+46
-27
lines changed

6 files changed

+46
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ In order to use the `rai-sdk-java`, you need add this dependency to your project
8686
<dependency>
8787
<groupId>com.relationalai</groupId>
8888
<artifactId>rai-sdk</artifactId>
89-
<version>0.0.2</version>
89+
<version>0.1.0-alpha</version>
9090
</dependency>
9191

9292
You need also to point maven to the SDK GitHub packages repository in the project's POM:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<description>The RelationalAI Software Development Kit (SDK) for Java</description>
2222
<groupId>com.relationalai</groupId>
2323
<artifactId>rai-sdk-pom</artifactId>
24-
<version>0.0.2</version>
24+
<version>0.1.0-alpha</version>
2525
<packaging>pom</packaging>
2626
<url></url>
2727

rai-sdk-examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<parent>
2121
<groupId>com.relationalai</groupId>
2222
<artifactId>rai-sdk-pom</artifactId>
23-
<version>0.0.2</version>
23+
<version>0.1.0-alpha</version>
2424
</parent>
2525

2626
<name>RelationalAI SDK for Java Examples</name>

rai-sdk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<parent>
2121
<groupId>com.relationalai</groupId>
2222
<artifactId>rai-sdk-pom</artifactId>
23-
<version>0.0.2</version>
23+
<version>0.1.0-alpha</version>
2424
</parent>
2525

2626
<name>RelationalAI SDK for Java Package</name>

rai-sdk/src/main/java/com/relationalai/Client.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.databind.ObjectMapper;
2020
import com.jsoniter.any.Any;
2121
import org.apache.arrow.memory.RootAllocator;
22+
import org.apache.arrow.vector.FieldVector;
2223
import org.apache.arrow.vector.VectorSchemaRoot;
2324
import org.apache.arrow.vector.ipc.ArrowStreamReader;
2425
import org.apache.arrow.vector.types.pojo.Field;
@@ -306,6 +307,7 @@ private String parseMultipartResponse(HttpResponse<byte[]> response) throws IOEx
306307
result.add(out.toString(StandardCharsets.UTF_8));
307308
} else if (partContentType.toLowerCase().equals("application/vnd.apache.arrow.stream")) {
308309
result.add(parseArrowResponse(out));
310+
out.close();
309311
} else {
310312
throw new HttpError(statusCode, String.format("unknown part content type: %s", partContentType));
311313
}
@@ -317,27 +319,43 @@ private String parseMultipartResponse(HttpResponse<byte[]> response) throws IOEx
317319
}
318320

319321
private String parseArrowResponse(ByteArrayOutputStream out) throws IOException {
320-
Map<String, String> result = new HashMap<>();
322+
List<Object> output = new ArrayList<>();
321323

322-
RootAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
323324
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
324-
ArrowStreamReader reader = new ArrowStreamReader(in, allocator);
325-
326-
VectorSchemaRoot readBatch = reader.getVectorSchemaRoot();
327-
328-
for (Field f : readBatch.getSchema().getFields()) {
329-
result.put(f.getName(), String.valueOf(readBatch.getVector(f)));
330-
}
331-
332-
while(reader.loadNextBatch()) {
333-
readBatch = reader.getVectorSchemaRoot();
334-
335-
for (Field f : readBatch.getSchema().getFields()) {
336-
result.put(f.getName(), String.valueOf(readBatch.getVector(f)));
325+
List<FieldVector> fieldVectors = null;
326+
RootAllocator allocator = new RootAllocator(Long.MAX_VALUE);
327+
try(ArrowStreamReader arrowStreamReader = new ArrowStreamReader(in, allocator)){
328+
329+
VectorSchemaRoot root = arrowStreamReader.getVectorSchemaRoot();
330+
fieldVectors = root.getFieldVectors();
331+
332+
while(arrowStreamReader.loadNextBatch()) {
333+
for(FieldVector fieldVector : fieldVectors) {
334+
Map<String, List<Object>> col = new HashMap<>();
335+
336+
col.put(fieldVector.getField().getName(), new ArrayList<>());
337+
for (int i = 0; i < fieldVector.getValueCount(); i ++) {
338+
col.get(fieldVector.getField().getName()).add(fieldVector.getObject(i));
339+
}
340+
output.add(col);
341+
}
342+
}
343+
} finally {
344+
if (in != null ) {
345+
in.close();
337346
}
347+
if (fieldVectors != null) {
348+
for (FieldVector fieldVector : fieldVectors) {
349+
if (fieldVector != null) {
350+
fieldVector.close();
351+
}
352+
}
353+
}
354+
allocator.close();
338355
}
356+
339357
// serialize map to json string
340-
return new ObjectMapper().writeValueAsString(result);
358+
return new ObjectMapper().writeValueAsString(output);
341359
}
342360

343361
static void printRequest(HttpRequest request) {

rai-sdk/src/test/java/com/relationalai/ExecuteAsyncTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.relationalai;
1818

19+
import com.jsoniter.JsonIterator;
1920
import org.junit.jupiter.api.AfterAll;
2021
import org.junit.jupiter.api.Test;
2122
import org.junit.jupiter.api.TestInstance;
@@ -35,17 +36,17 @@ void testExecuteAsync() throws HttpError, InterruptedException, IOException {
3536
var query = "x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}";
3637

3738
var rsp = client.executeAsyncWait(databaseName, engineName, query, true);
39+
3840
assertEquals(
39-
rsp.get("results").toString(),
40-
"[{\"v1\":\"[1, 2, 3, 4, 5]\",\"v2\":\"[1, 4, 9, 16, 25]\",\"v3\":\"[1, 8, 27, 64, 125]\",\"v4\":\"[1, 16, 81, 256, 625]\"}]"
41-
);
41+
rsp.get("results"),
42+
JsonIterator.deserialize("[[{\"v1\":[1,2,3,4,5]},{\"v2\":[1,4,9,16,25]},{\"v3\":[1,8,27,64,125]},{\"v4\":[1,16,81,256,625]}]]"));
4243
assertEquals(
43-
rsp.get("metadata").toString(),
44-
"[{\"relationId\":\"/:output/Int64/Int64/Int64/Int64\",\"types\":[\":output\",\"Int64\",\"Int64\",\"Int64\",\"Int64\"]}]"
44+
rsp.get("metadata"),
45+
JsonIterator.deserialize("[{\"relationId\":\"/:output/Int64/Int64/Int64/Int64\",\"types\":[\":output\",\"Int64\",\"Int64\",\"Int64\",\"Int64\"]}]")
4546
);
4647
assertEquals(
47-
rsp.get("problems").toString(),
48-
"[]"
48+
rsp.get("problems"),
49+
JsonIterator.deserialize("[]")
4950
);
5051
}
5152

0 commit comments

Comments
 (0)