|
| 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 | +# Scalar UDFs |
| 21 | + |
| 22 | +A scalar UDF is a Java-implemented SQL function that operates on one row at a |
| 23 | +time, expressed in vectorised form: each invocation receives a batch of input |
| 24 | +columns and returns a single output column of the same length. |
| 25 | + |
| 26 | +## Implement |
| 27 | + |
| 28 | +Implement the `ScalarUdf` interface: |
| 29 | + |
| 30 | +```java |
| 31 | +import java.util.List; |
| 32 | +import org.apache.arrow.memory.BufferAllocator; |
| 33 | +import org.apache.arrow.vector.FieldVector; |
| 34 | +import org.apache.arrow.vector.IntVector; |
| 35 | +import org.apache.datafusion.ScalarUdf; |
| 36 | + |
| 37 | +public final class AddOne implements ScalarUdf { |
| 38 | + @Override |
| 39 | + public FieldVector evaluate(BufferAllocator allocator, List<FieldVector> args) { |
| 40 | + IntVector in = (IntVector) args.get(0); |
| 41 | + IntVector out = new IntVector("add_one", allocator); |
| 42 | + out.allocateNew(in.getValueCount()); |
| 43 | + for (int i = 0; i < in.getValueCount(); i++) { |
| 44 | + if (in.isNull(i)) out.setNull(i); |
| 45 | + else out.set(i, in.get(i) + 1); |
| 46 | + } |
| 47 | + out.setValueCount(in.getValueCount()); |
| 48 | + return out; |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Allocate any new vectors — including the result — from the supplied |
| 54 | +`BufferAllocator`. The input vectors are read-only views; do not close them. |
| 55 | +Ownership of the returned vector transfers to the framework on return. |
| 56 | + |
| 57 | +## Register |
| 58 | + |
| 59 | +```java |
| 60 | +try (SessionContext ctx = new SessionContext()) { |
| 61 | + ctx.registerUdf( |
| 62 | + "add_one", |
| 63 | + new AddOne(), |
| 64 | + new ArrowType.Int(32, true), |
| 65 | + List.of(new ArrowType.Int(32, true)), |
| 66 | + Volatility.IMMUTABLE); |
| 67 | + |
| 68 | + try (DataFrame df = ctx.sql("SELECT add_one(x) FROM t"); |
| 69 | + ArrowReader r = df.collect(allocator)) { |
| 70 | + // ... |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +The signature is exact: a call must match the declared `argTypes` exactly. Use |
| 76 | +`Volatility.IMMUTABLE` for pure functions, `STABLE` for functions that are |
| 77 | +deterministic within a single query, and `VOLATILE` for non-deterministic |
| 78 | +functions. |
| 79 | + |
| 80 | +## Errors |
| 81 | + |
| 82 | +If the UDF throws, the exception class and message surface in the |
| 83 | +`RuntimeException` raised from `collect()`. If the returned vector is `null`, |
| 84 | +has the wrong row count, or the wrong type, the runtime raises a |
| 85 | +`RuntimeException` with a descriptive message. |
| 86 | + |
| 87 | +## Threading |
| 88 | + |
| 89 | +DataFusion may invoke a UDF concurrently from multiple worker threads. If the |
| 90 | +implementation carries mutable state, the implementation must synchronize it. |
| 91 | + |
| 92 | +## Limitations (v1) |
| 93 | + |
| 94 | +- Scalar UDFs only — no aggregates, window functions, or table functions. |
| 95 | +- Exact-signature only — no variadic or polymorphic argument lists. |
| 96 | +- No nullable-argument short-circuiting; null inputs are passed through to the |
| 97 | + UDF as nulls in the input vector. |
0 commit comments