1919
2020package org .apache .datafusion .internal ;
2121
22+ import java .util .ArrayList ;
2223import java .util .List ;
2324
2425import org .apache .arrow .c .ArrowArray ;
2728import org .apache .arrow .memory .RootAllocator ;
2829import org .apache .arrow .vector .FieldVector ;
2930import org .apache .arrow .vector .VectorSchemaRoot ;
31+ import org .apache .datafusion .ColumnarValue ;
3032import org .apache .datafusion .ScalarFunction ;
33+ import org .apache .datafusion .ScalarFunctionArgs ;
3134
3235/** Internal trampoline invoked from native code on every UDF call. Not part of the public API. */
3336public final class JniBridge {
@@ -40,54 +43,100 @@ public final class JniBridge {
4043
4144 private JniBridge () {}
4245
46+ /** argKind byte signalling a {@link ColumnarValue.Array} arg. */
47+ private static final byte KIND_ARRAY = 0 ;
48+
49+ /** argKind byte signalling a {@link ColumnarValue.Scalar} arg. */
50+ private static final byte KIND_SCALAR = 1 ;
51+
4352 /**
4453 * Invoke a scalar UDF for one batch. Called from native code; not for application use.
4554 *
46- * @param impl the registered {@link ScalarFunction} implementation
47- * @param argsArrayAddr address of a populated {@code FFI_ArrowArray} struct holding the input
48- * batch as a struct array (one field per UDF argument)
49- * @param argsSchemaAddr address of the matching {@code FFI_ArrowSchema}
50- * @param resultArrayAddr address of an empty {@code FFI_ArrowArray} the bridge writes into
51- * @param resultSchemaAddr address of an empty {@code FFI_ArrowSchema} the bridge writes into
52- * @param expectedRowCount the row count the result vector must have
55+ * <p>Args arrive split into two struct arrays: {@code arrayArgs*} of length {@code rowCount}
56+ * holding the {@link ColumnarValue.Array} arguments in their relative order, and {@code
57+ * scalarArgs*} of length 1 holding the {@link ColumnarValue.Scalar} arguments. {@code argKinds}
58+ * records the original positional order so the bridge can interleave them back into a single
59+ * {@code List<ColumnarValue>} for the user.
60+ *
61+ * @return {@link #KIND_ARRAY} if the UDF returned an Array, {@link #KIND_SCALAR} if it returned a
62+ * Scalar. The native caller uses this to reconstruct the right {@code ColumnarValue} variant.
5363 */
54- public static void invokeScalarUdf (
64+ public static byte invokeScalarUdf (
5565 ScalarFunction impl ,
56- long argsArrayAddr ,
57- long argsSchemaAddr ,
66+ long arrayArgsArrayAddr ,
67+ long arrayArgsSchemaAddr ,
68+ long scalarArgsArrayAddr ,
69+ long scalarArgsSchemaAddr ,
70+ byte [] argKinds ,
5871 long resultArrayAddr ,
5972 long resultSchemaAddr ,
60- int expectedRowCount ) {
61- ArrowArray argsArr = ArrowArray .wrap (argsArrayAddr );
62- ArrowSchema argsSch = ArrowSchema .wrap (argsSchemaAddr );
73+ int rowCount ) {
74+ ArrowArray arrayArr = ArrowArray .wrap (arrayArgsArrayAddr );
75+ ArrowSchema arraySch = ArrowSchema .wrap (arrayArgsSchemaAddr );
76+ ArrowArray scalarArr = ArrowArray .wrap (scalarArgsArrayAddr );
77+ ArrowSchema scalarSch = ArrowSchema .wrap (scalarArgsSchemaAddr );
6378 ArrowArray resultArr = ArrowArray .wrap (resultArrayAddr );
6479 ArrowSchema resultSch = ArrowSchema .wrap (resultSchemaAddr );
6580
66- try (VectorSchemaRoot root = Data .importVectorSchemaRoot (ALLOCATOR , argsArr , argsSch , null )) {
67- List <FieldVector > argVectors = root .getFieldVectors ();
81+ try (VectorSchemaRoot arrayRoot =
82+ Data .importVectorSchemaRoot (ALLOCATOR , arrayArr , arraySch , null );
83+ VectorSchemaRoot scalarRoot =
84+ Data .importVectorSchemaRoot (ALLOCATOR , scalarArr , scalarSch , null )) {
6885
69- FieldVector result = impl .evaluate (ALLOCATOR , argVectors );
86+ List <FieldVector > arrayFields = arrayRoot .getFieldVectors ();
87+ List <FieldVector > scalarFields = scalarRoot .getFieldVectors ();
88+
89+ List <ColumnarValue > args = new ArrayList <>(argKinds .length );
90+ int arrayIdx = 0 ;
91+ int scalarIdx = 0 ;
92+ for (byte kind : argKinds ) {
93+ if (kind == KIND_ARRAY ) {
94+ args .add (ColumnarValue .array (arrayFields .get (arrayIdx ++)));
95+ } else if (kind == KIND_SCALAR ) {
96+ args .add (ColumnarValue .scalar (scalarFields .get (scalarIdx ++)));
97+ } else {
98+ throw new IllegalStateException ("Unknown argKind byte: " + kind );
99+ }
100+ }
101+
102+ ColumnarValue result = impl .evaluate (ALLOCATOR , new ScalarFunctionArgs (args , rowCount ));
70103
71104 if (result == null ) {
72105 throw new IllegalStateException ("ScalarFunction.evaluate returned null" );
73106 }
74- if (result .getValueCount () != expectedRowCount ) {
107+
108+ FieldVector resultVec = result .vector ();
109+ byte resultKind ;
110+ int expectedLen ;
111+ if (result instanceof ColumnarValue .Array ) {
112+ resultKind = KIND_ARRAY ;
113+ expectedLen = rowCount ;
114+ } else {
115+ resultKind = KIND_SCALAR ;
116+ expectedLen = 1 ;
117+ }
118+
119+ if (resultVec .getValueCount () != expectedLen ) {
75120 try {
76121 throw new IllegalStateException (
77- "ScalarFunction.evaluate returned vector with "
78- + result .getValueCount ()
122+ "ScalarFunction.evaluate returned "
123+ + (resultKind == KIND_ARRAY ? "Array" : "Scalar" )
124+ + " vector with "
125+ + resultVec .getValueCount ()
79126 + " rows; expected "
80- + expectedRowCount );
127+ + expectedLen );
81128 } finally {
82- result .close ();
129+ resultVec .close ();
83130 }
84131 }
85132
86133 try {
87- Data .exportVector (ALLOCATOR , result , null , resultArr , resultSch );
134+ Data .exportVector (ALLOCATOR , resultVec , null , resultArr , resultSch );
88135 } finally {
89- result .close ();
136+ resultVec .close ();
90137 }
138+
139+ return resultKind ;
91140 }
92141 }
93142}
0 commit comments