Skip to content

Commit 74563f3

Browse files
improvements in the UDF API
1 parent 8d1333a commit 74563f3

File tree

2 files changed

+278
-15
lines changed

2 files changed

+278
-15
lines changed

src/main/java/io/tiledb/cloud/TileDBUDF.java

Lines changed: 263 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import com.google.gson.Gson;
44
import io.tiledb.cloud.rest_api.ApiException;
55
import io.tiledb.cloud.rest_api.api.UdfApi;
6-
import io.tiledb.cloud.rest_api.model.GenericUDF;
7-
import io.tiledb.cloud.rest_api.model.MultiArrayUDF;
8-
import io.tiledb.cloud.rest_api.model.ResultFormat;
6+
import io.tiledb.cloud.rest_api.model.*;
97
import io.tiledb.java.api.Pair;
108
import org.apache.arrow.compression.CommonsCompressionFactory;
119
import org.apache.arrow.memory.RootAllocator;
@@ -21,7 +19,11 @@
2119
import java.io.ByteArrayInputStream;
2220
import java.io.IOException;
2321
import java.util.ArrayList;
22+
import java.util.Arrays;
2423
import java.util.HashMap;
24+
import java.util.List;
25+
26+
import static io.tiledb.cloud.TileDBUtils.serializeArgs;
2527

2628
public class TileDBUDF {
2729
private TileDBClient tileDBClient;
@@ -32,7 +34,6 @@ public TileDBUDF(TileDBClient tileDBClient, String namespace) {
3234
this.tileDBClient = tileDBClient;
3335
this.namespace = namespace;
3436
this.apiInstance = new UdfApi(this.tileDBClient.getApiClient());
35-
3637
}
3738

3839
/**
@@ -57,6 +58,25 @@ public String executeGeneric(GenericUDF genericUDF, HashMap<String, Object> argu
5758
return null;
5859
}
5960

61+
/**
62+
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
63+
*
64+
* @param genericUDF The generic UDF definition
65+
* @return The result in String format
66+
*/
67+
public String executeGeneric(GenericUDF genericUDF){
68+
try {
69+
return apiInstance.submitGenericUDFString(namespace, genericUDF, "none");
70+
} catch (ApiException e) {
71+
System.err.println("Exception when calling UdfApi#submitGenericUDF");
72+
System.err.println("Status code: " + e.getCode());
73+
System.err.println("Reason: " + e.getResponseBody());
74+
System.err.println("Response headers: " + e.getResponseHeaders());
75+
e.printStackTrace();
76+
}
77+
return null;
78+
}
79+
6080
/**
6181
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
6282
*
@@ -70,6 +90,18 @@ public JSONObject executeGenericJSON(GenericUDF genericUDF, HashMap<String, Obje
7090
return new JSONObject(jsonString);
7191
}
7292

93+
/**
94+
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
95+
*
96+
* @param genericUDF The generic UDF definition
97+
* @return The result as a JSON object
98+
*/
99+
public JSONObject executeGenericJSON(GenericUDF genericUDF){
100+
genericUDF.setResultFormat(ResultFormat.JSON);
101+
String jsonString = this.executeGeneric(genericUDF);
102+
return new JSONObject(jsonString);
103+
}
104+
73105
/**
74106
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
75107
*
@@ -79,7 +111,19 @@ public JSONObject executeGenericJSON(GenericUDF genericUDF, HashMap<String, Obje
79111
*/
80112
public JSONArray executeGenericJSONArray(GenericUDF genericUDF, HashMap<String, Object> arguments){
81113
genericUDF.setResultFormat(ResultFormat.JSON);
82-
String jsonString = this.executeGeneric(genericUDF, arguments);
114+
String jsonString = this.executeGeneric(genericUDF, arguments);
115+
return new JSONArray(jsonString);
116+
}
117+
118+
/**
119+
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
120+
*
121+
* @param genericUDF The generic UDF definition
122+
* @return The result as a JSON array object
123+
*/
124+
public JSONArray executeGenericJSONArray(GenericUDF genericUDF){
125+
genericUDF.setResultFormat(ResultFormat.JSON);
126+
String jsonString = this.executeGeneric(genericUDF);
83127
return new JSONArray(jsonString);
84128
}
85129

@@ -90,7 +134,8 @@ public JSONArray executeGenericJSONArray(GenericUDF genericUDF, HashMap<String,
90134
* @param arguments The UDF arguments
91135
* @return A pair that consists of an ArrayList of all valueVectors and the number of batches read.
92136
*/
93-
public io.tiledb.java.api.Pair<ArrayList<ValueVector>, Integer> executeGenericArrow(GenericUDF genericUDF, HashMap<String, Object> arguments){
137+
public io.tiledb.java.api.Pair<ArrayList<ValueVector>, Integer> executeGenericArrow(GenericUDF genericUDF,
138+
HashMap<String, Object> arguments){
94139
String serializedArgs = serializeArgs(arguments);
95140
genericUDF.setArgument(serializedArgs);
96141
genericUDF.setResultFormat(ResultFormat.ARROW);
@@ -103,6 +148,24 @@ public io.tiledb.java.api.Pair<ArrayList<ValueVector>, Integer> executeGenericAr
103148
return null;
104149
}
105150

151+
/**
152+
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
153+
*
154+
* @param genericUDF The generic UDF definition
155+
* @return A pair that consists of an ArrayList of all valueVectors and the number of batches read.
156+
*/
157+
public io.tiledb.java.api.Pair<ArrayList<ValueVector>, Integer> executeGenericArrow(GenericUDF genericUDF){
158+
genericUDF.setResultFormat(ResultFormat.ARROW);
159+
try {
160+
byte[] bytes = apiInstance.submitGenericUDFBytes(namespace, genericUDF, "none");
161+
return TileDBUtils.createValueVectors(bytes);
162+
} catch (IOException | ApiException e) {
163+
e.printStackTrace();
164+
}
165+
return null;
166+
}
167+
168+
@Deprecated
106169
/**
107170
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
108171
*
@@ -130,6 +193,40 @@ public String executeSingleArray(MultiArrayUDF multiArrayUDF, HashMap<String, Ob
130193
return null;
131194
}
132195

196+
/**
197+
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
198+
*
199+
* @param genericUDF The UDF to run
200+
* @param array The Array to run the UDF on
201+
* @param queryRanges the Query ranges
202+
* @param xPayer Name of organization or user who should be charged for this request
203+
* @return The results as a String
204+
*/
205+
public String executeSingleArray(GenericUDF genericUDF, UDFArrayDetails array,
206+
QueryRanges queryRanges, String xPayer){
207+
List<UDFArrayDetails> arrays = new ArrayList<>();
208+
arrays.add(array);
209+
210+
MultiArrayUDF multiArrayUDF = new MultiArrayUDF();
211+
multiArrayUDF.setArgument(genericUDF.getArgument());
212+
multiArrayUDF.setUdfInfoName(genericUDF.getUdfInfoName());
213+
multiArrayUDF.setRanges(queryRanges);
214+
multiArrayUDF.setArrays(arrays);
215+
String[] split = breakdownFullURI(array.getUri());
216+
217+
try {
218+
return apiInstance.submitUDFString(split[0], split[1], multiArrayUDF, xPayer, "none", "");
219+
} catch (ApiException e) {
220+
System.err.println("Exception when calling UdfApi#submitUDF");
221+
System.err.println("Status code: " + e.getCode());
222+
System.err.println("Reason: " + e.getResponseBody());
223+
System.err.println("Response headers: " + e.getResponseHeaders());
224+
e.printStackTrace();
225+
}
226+
return null;
227+
}
228+
229+
@Deprecated
133230
/**
134231
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
135232
*
@@ -145,6 +242,42 @@ public JSONObject executeSingleArrayJSON(MultiArrayUDF multiArrayUDF, HashMap<St
145242
return new JSONObject(jsonString);
146243
}
147244

245+
/**
246+
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
247+
*
248+
* @param genericUDF The UDF to run
249+
* @param array The Array to run the UDF on
250+
* @param queryRanges the Query ranges
251+
* @param xPayer Name of organization or user who should be charged for this request
252+
* @return The results as a JSON Object
253+
*/
254+
public JSONObject executeSingleArrayJSON(GenericUDF genericUDF, UDFArrayDetails array,
255+
QueryRanges queryRanges, String xPayer){
256+
257+
List<UDFArrayDetails> arrays = new ArrayList<>();
258+
arrays.add(array);
259+
260+
MultiArrayUDF multiArrayUDF = new MultiArrayUDF();
261+
multiArrayUDF.setArgument(genericUDF.getArgument());
262+
multiArrayUDF.setUdfInfoName(genericUDF.getUdfInfoName());
263+
multiArrayUDF.setRanges(queryRanges);
264+
multiArrayUDF.setArrays(arrays);
265+
String[] split = breakdownFullURI(array.getUri());
266+
multiArrayUDF.setResultFormat(ResultFormat.JSON);
267+
String result = "";
268+
try {
269+
result = apiInstance.submitUDFString(split[0], split[1], multiArrayUDF, xPayer, "none", "");
270+
} catch (ApiException e) {
271+
System.err.println("Exception when calling UdfApi#submitUDF");
272+
System.err.println("Status code: " + e.getCode());
273+
System.err.println("Reason: " + e.getResponseBody());
274+
System.err.println("Response headers: " + e.getResponseHeaders());
275+
e.printStackTrace();
276+
}
277+
return new JSONObject(result);
278+
}
279+
280+
@Deprecated
148281
/**
149282
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
150283
*
@@ -154,12 +287,48 @@ public JSONObject executeSingleArrayJSON(MultiArrayUDF multiArrayUDF, HashMap<St
154287
* @param xPayer Name of organization or user who should be charged for this request
155288
* @return The results as a JSON array
156289
*/
157-
public JSONArray executeSingleArrayJSONArray(MultiArrayUDF multiArrayUDF, HashMap<String, Object> arguments, String arrayURI, String xPayer){
290+
public JSONArray executeSingleArrayJSONArray(MultiArrayUDF multiArrayUDF, HashMap<String,
291+
Object> arguments, String arrayURI, String xPayer){
158292
multiArrayUDF.setResultFormat(ResultFormat.JSON);
159293
String jsonString = this.executeSingleArray(multiArrayUDF, arguments, arrayURI, xPayer);
160294
return new JSONArray(jsonString);
161295
}
162296

297+
/**
298+
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
299+
*
300+
* @param genericUDF The UDF to run
301+
* @param array The Array to run the UDF on
302+
* @param queryRanges the Query ranges
303+
* @param xPayer Name of organization or user who should be charged for this request
304+
* @return The results as a JSON Object
305+
*/
306+
public JSONArray executeSingleArrayJSONArray(GenericUDF genericUDF, UDFArrayDetails array,
307+
QueryRanges queryRanges, String xPayer){
308+
List<UDFArrayDetails> arrays = new ArrayList<>();
309+
arrays.add(array);
310+
311+
MultiArrayUDF multiArrayUDF = new MultiArrayUDF();
312+
multiArrayUDF.setArgument(genericUDF.getArgument());
313+
multiArrayUDF.setUdfInfoName(genericUDF.getUdfInfoName());
314+
multiArrayUDF.setRanges(queryRanges);
315+
multiArrayUDF.setArrays(arrays);
316+
String[] split = breakdownFullURI(array.getUri());
317+
multiArrayUDF.setResultFormat(ResultFormat.JSON);
318+
String result = "";
319+
try {
320+
result = apiInstance.submitUDFString(split[0], split[1], multiArrayUDF, xPayer, "none", "");
321+
} catch (ApiException e) {
322+
System.err.println("Exception when calling UdfApi#submitUDF");
323+
System.err.println("Status code: " + e.getCode());
324+
System.err.println("Reason: " + e.getResponseBody());
325+
System.err.println("Response headers: " + e.getResponseHeaders());
326+
e.printStackTrace();
327+
}
328+
return new JSONArray(result);
329+
}
330+
331+
@Deprecated
163332
/**
164333
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
165334
*
@@ -185,6 +354,37 @@ public Pair<ArrayList<ValueVector>, Integer> executeSingleArrayArrow(MultiArrayU
185354
return null;
186355
}
187356

357+
/**
358+
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
359+
*
360+
* @param genericUDF The UDF to run
361+
* @param array The Array to run the UDF on
362+
* @param queryRanges the Query ranges
363+
* @param xPayer Name of organization or user who should be charged for this request
364+
* @return The results in arrow format
365+
*/
366+
public Pair<ArrayList<ValueVector>, Integer> executeSingleArrayArrow(GenericUDF genericUDF, UDFArrayDetails array,
367+
QueryRanges queryRanges, String xPayer){
368+
List<UDFArrayDetails> arrays = new ArrayList<>();
369+
arrays.add(array);
370+
371+
MultiArrayUDF multiArrayUDF = new MultiArrayUDF();
372+
multiArrayUDF.setArgument(genericUDF.getArgument());
373+
multiArrayUDF.setResultFormat(ResultFormat.ARROW);
374+
multiArrayUDF.setUdfInfoName(genericUDF.getUdfInfoName());
375+
multiArrayUDF.setRanges(queryRanges);
376+
multiArrayUDF.setArrays(arrays);
377+
String[] split = breakdownFullURI(array.getUri());
378+
379+
try {
380+
byte[] bytes = apiInstance.submitUDFBytes(split[0], split[1], multiArrayUDF, xPayer, "none", "");
381+
return TileDBUtils.createValueVectors(bytes);
382+
} catch (IOException | ApiException e) {
383+
e.printStackTrace();
384+
}
385+
return null;
386+
}
387+
188388
/**
189389
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
190390
*
@@ -207,6 +407,25 @@ public String executeMultiArray(MultiArrayUDF multiArrayUDF, HashMap<String, Obj
207407
return null;
208408
}
209409

410+
/**
411+
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
412+
*
413+
* @param multiArrayUDF The multiArrayUDF input object
414+
* @return The results as a String
415+
*/
416+
public String executeMultiArray(MultiArrayUDF multiArrayUDF){
417+
try {
418+
return apiInstance.submitMultiArrayUDFString(this.namespace, multiArrayUDF, "none");
419+
} catch (ApiException e) {
420+
System.err.println("Exception when calling UdfApi#submitMultiArrayUDFString");
421+
System.err.println("Status code: " + e.getCode());
422+
System.err.println("Reason: " + e.getResponseBody());
423+
System.err.println("Response headers: " + e.getResponseHeaders());
424+
e.printStackTrace();
425+
}
426+
return null;
427+
}
428+
210429
/**
211430
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
212431
*
@@ -220,6 +439,18 @@ public JSONObject executeMultiArrayJSON(MultiArrayUDF multiArrayUDF, HashMap<Str
220439
return new JSONObject(jsonString);
221440
}
222441

442+
/**
443+
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
444+
*
445+
* @param multiArrayUDF The multiArrayUDF input object
446+
* @return The results as JSON object
447+
*/
448+
public JSONObject executeMultiArrayJSON(MultiArrayUDF multiArrayUDF){
449+
multiArrayUDF.setResultFormat(ResultFormat.JSON);
450+
String jsonString = this.executeMultiArray(multiArrayUDF);
451+
return new JSONObject(jsonString);
452+
}
453+
223454
/**
224455
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
225456
*
@@ -233,6 +464,18 @@ public JSONArray executeMultiArrayJSONArray(MultiArrayUDF multiArrayUDF, HashMap
233464
return new JSONArray(jsonString);
234465
}
235466

467+
/**
468+
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
469+
*
470+
* @param multiArrayUDF The multiArrayUDF input object
471+
* @return The results as JSON Array
472+
*/
473+
public JSONArray executeMultiArrayJSONArray(MultiArrayUDF multiArrayUDF){
474+
multiArrayUDF.setResultFormat(ResultFormat.JSON);
475+
String jsonString = this.executeMultiArray(multiArrayUDF);
476+
return new JSONArray(jsonString);
477+
}
478+
236479
/**
237480
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
238481
*
@@ -255,15 +498,21 @@ public Pair<ArrayList<ValueVector>, Integer> executeMultiArrayArrow(MultiArrayUD
255498
}
256499

257500
/**
258-
* Serializes the arguments to a String
501+
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
259502
*
260-
* @param arguments The input arguments in a HashMap
261-
* @return The arguments in a String
503+
* @param multiArrayUDF The multiArrayUDF input object
504+
* @return The results in arrow format
262505
*/
263-
private String serializeArgs(HashMap<String, Object> arguments) {
264-
if (arguments == null || arguments.isEmpty()) return "";
265-
Gson gson = new Gson();
266-
return gson.toJson(arguments);
506+
public Pair<ArrayList<ValueVector>, Integer> executeMultiArrayArrow(MultiArrayUDF multiArrayUDF){
507+
multiArrayUDF.setResultFormat(ResultFormat.ARROW);
508+
509+
try {
510+
byte[] bytes = apiInstance.submitMultiArrayUDFBytes(this.namespace, multiArrayUDF, "none");
511+
return TileDBUtils.createValueVectors(bytes);
512+
} catch (IOException | ApiException e) {
513+
e.printStackTrace();
514+
}
515+
return null;
267516
}
268517

269518
/**

0 commit comments

Comments
 (0)