|
23 | 23 | import java.io.ByteArrayOutputStream; |
24 | 24 | import java.io.IOException; |
25 | 25 | import java.nio.channels.Channels; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
26 | 28 |
|
27 | 29 | import org.apache.arrow.memory.BufferAllocator; |
28 | 30 | import org.apache.arrow.memory.RootAllocator; |
29 | 31 | import org.apache.arrow.vector.VectorSchemaRoot; |
30 | 32 | import org.apache.arrow.vector.ipc.ArrowStreamWriter; |
31 | 33 | import org.apache.arrow.vector.ipc.ReadChannel; |
32 | 34 | import org.apache.arrow.vector.ipc.message.MessageSerializer; |
| 35 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 36 | +import org.apache.arrow.vector.types.pojo.Field; |
| 37 | +import org.apache.arrow.vector.types.pojo.FieldType; |
33 | 38 | import org.apache.arrow.vector.types.pojo.Schema; |
34 | 39 |
|
35 | 40 | /** |
@@ -293,6 +298,36 @@ public DataFrame readParquet(String path, ParquetReadOptions options) { |
293 | 298 | return new DataFrame(dfHandle); |
294 | 299 | } |
295 | 300 |
|
| 301 | + /** |
| 302 | + * Register a Java-implemented scalar UDF. After registration, the function can be invoked by SQL |
| 303 | + * via the UDF's name or referenced in DataFusion plans deserialised with {@link #fromProto}. |
| 304 | + * |
| 305 | + * <p>The UDF is registered with an exact signature: the runtime will reject calls whose argument |
| 306 | + * types do not match the declared {@link ScalarFunction#argTypes()} exactly. |
| 307 | + * |
| 308 | + * @throws RuntimeException if registration fails (e.g., name already registered with an |
| 309 | + * incompatible signature, schema serialisation failure). |
| 310 | + */ |
| 311 | + public void registerUdf(ScalarUdf udf) { |
| 312 | + if (nativeHandle == 0) { |
| 313 | + throw new IllegalStateException("SessionContext is closed"); |
| 314 | + } |
| 315 | + java.util.Objects.requireNonNull(udf, "udf"); |
| 316 | + ScalarFunction impl = udf.impl(); |
| 317 | + String name = udf.name(); |
| 318 | + ArrowType returnType = udf.returnType(); |
| 319 | + List<ArrowType> argTypes = udf.argTypes(); |
| 320 | + Volatility volatility = udf.volatility(); |
| 321 | + List<Field> fields = new ArrayList<>(argTypes.size() + 1); |
| 322 | + fields.add(new Field("return", FieldType.nullable(returnType), null)); |
| 323 | + for (int i = 0; i < argTypes.size(); i++) { |
| 324 | + fields.add(new Field("arg" + i, FieldType.nullable(argTypes.get(i)), null)); |
| 325 | + } |
| 326 | + Schema signatureSchema = new Schema(fields); |
| 327 | + byte[] signatureBytes = serializeSchemaIpc(signatureSchema); |
| 328 | + registerScalarUdf(nativeHandle, name, signatureBytes, volatility.code(), impl); |
| 329 | + } |
| 330 | + |
296 | 331 | private static byte[] serializeSchemaIpc(Schema schema) { |
297 | 332 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
298 | 333 | try (BufferAllocator allocator = new RootAllocator(); |
@@ -345,4 +380,7 @@ private static native long readJsonWithOptions( |
345 | 380 | long handle, String path, byte[] optionsBytes, byte[] schemaIpcBytes); |
346 | 381 |
|
347 | 382 | private static native void closeSessionContext(long handle); |
| 383 | + |
| 384 | + private static native void registerScalarUdf( |
| 385 | + long handle, String name, byte[] signatureSchemaBytes, byte volatility, ScalarFunction impl); |
348 | 386 | } |
0 commit comments