|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet.udf; |
| 21 | + |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | + |
| 24 | +import org.apache.arrow.c.ArrowArray; |
| 25 | +import org.apache.arrow.c.ArrowSchema; |
| 26 | +import org.apache.arrow.c.Data; |
| 27 | +import org.apache.arrow.memory.BufferAllocator; |
| 28 | +import org.apache.arrow.vector.FieldVector; |
| 29 | +import org.apache.arrow.vector.ValueVector; |
| 30 | + |
| 31 | +/** |
| 32 | + * JNI entry point for native execution to invoke a {@link CometUDF}. Matches the static-method |
| 33 | + * pattern used by CometScalarSubquery so the native side can dispatch via |
| 34 | + * call_static_method_unchecked. |
| 35 | + */ |
| 36 | +public class CometUdfBridge { |
| 37 | + |
| 38 | + // Process-wide cache of UDF instances keyed by class name. CometUDF |
| 39 | + // implementations are required to be stateless (see CometUDF), so a |
| 40 | + // single shared instance per class is safe across native worker threads. |
| 41 | + private static final ConcurrentHashMap<String, CometUDF> INSTANCES = new ConcurrentHashMap<>(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Called from native via JNI. |
| 45 | + * |
| 46 | + * @param udfClassName fully-qualified class name implementing CometUDF |
| 47 | + * @param inputArrayPtrs addresses of pre-allocated FFI_ArrowArray structs (one per input) |
| 48 | + * @param inputSchemaPtrs addresses of pre-allocated FFI_ArrowSchema structs (one per input) |
| 49 | + * @param outArrayPtr address of pre-allocated FFI_ArrowArray for the result |
| 50 | + * @param outSchemaPtr address of pre-allocated FFI_ArrowSchema for the result |
| 51 | + */ |
| 52 | + public static void evaluate( |
| 53 | + String udfClassName, |
| 54 | + long[] inputArrayPtrs, |
| 55 | + long[] inputSchemaPtrs, |
| 56 | + long outArrayPtr, |
| 57 | + long outSchemaPtr) { |
| 58 | + CometUDF udf = |
| 59 | + INSTANCES.computeIfAbsent( |
| 60 | + udfClassName, |
| 61 | + name -> { |
| 62 | + try { |
| 63 | + // Resolve via the executor's context classloader so user-supplied UDF jars |
| 64 | + // (added via spark.jars / --jars) are visible. |
| 65 | + ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
| 66 | + if (cl == null) { |
| 67 | + cl = CometUdfBridge.class.getClassLoader(); |
| 68 | + } |
| 69 | + return (CometUDF) |
| 70 | + Class.forName(name, true, cl).getDeclaredConstructor().newInstance(); |
| 71 | + } catch (ReflectiveOperationException e) { |
| 72 | + throw new RuntimeException("Failed to instantiate CometUDF: " + name, e); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + BufferAllocator allocator = org.apache.comet.package$.MODULE$.CometArrowAllocator(); |
| 77 | + |
| 78 | + ValueVector[] inputs = new ValueVector[inputArrayPtrs.length]; |
| 79 | + ValueVector result = null; |
| 80 | + try { |
| 81 | + for (int i = 0; i < inputArrayPtrs.length; i++) { |
| 82 | + ArrowArray inArr = ArrowArray.wrap(inputArrayPtrs[i]); |
| 83 | + ArrowSchema inSch = ArrowSchema.wrap(inputSchemaPtrs[i]); |
| 84 | + inputs[i] = Data.importVector(allocator, inArr, inSch, null); |
| 85 | + } |
| 86 | + |
| 87 | + result = udf.evaluate(inputs); |
| 88 | + if (!(result instanceof FieldVector)) { |
| 89 | + throw new RuntimeException( |
| 90 | + "CometUDF.evaluate() must return a FieldVector, got: " + result.getClass().getName()); |
| 91 | + } |
| 92 | + // Result length must match the longest input. Scalar (length-1) inputs |
| 93 | + // are allowed to be shorter, but a vector input bounds the output. |
| 94 | + int expectedLen = 0; |
| 95 | + for (ValueVector v : inputs) { |
| 96 | + expectedLen = Math.max(expectedLen, v.getValueCount()); |
| 97 | + } |
| 98 | + if (result.getValueCount() != expectedLen) { |
| 99 | + throw new RuntimeException( |
| 100 | + "CometUDF.evaluate() returned " |
| 101 | + + result.getValueCount() |
| 102 | + + " rows, expected " |
| 103 | + + expectedLen); |
| 104 | + } |
| 105 | + ArrowArray outArr = ArrowArray.wrap(outArrayPtr); |
| 106 | + ArrowSchema outSch = ArrowSchema.wrap(outSchemaPtr); |
| 107 | + Data.exportVector(allocator, (FieldVector) result, null, outArr, outSch); |
| 108 | + } finally { |
| 109 | + for (ValueVector v : inputs) { |
| 110 | + if (v != null) { |
| 111 | + try { |
| 112 | + v.close(); |
| 113 | + } catch (RuntimeException ignored) { |
| 114 | + // do not mask the original throwable |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + if (result != null) { |
| 119 | + try { |
| 120 | + result.close(); |
| 121 | + } catch (RuntimeException ignored) { |
| 122 | + // do not mask the original throwable |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments