viirya opened a new pull request, #60551: URL: https://github.com/apache/spark/pull/60551
### What changes were proposed in this pull request? This PR adds a columnar scalar UDF: a user-defined scalar function that is evaluated one batch at a time, with columnar input and output, by an operator of its own. User-facing API (all `@Evolving`): - `ColumnarScalarFunction`: a JVM interface with one method, `ColumnVector eval(ColumnarBatch args)`. - `ColumnarUDFImplementation(kind, properties)`: an implementation of the function for an engine other than Spark. Spark does not interpret it and only carries it to the physical plan. - `ColumnarUserDefinedFunction`: the function definition. It holds the name, the argument and result types, an optional `ColumnarScalarFunction`, and any number of `ColumnarUDFImplementation`s. It can be applied to columns directly, or registered with `spark.udf.registerColumnar` to be called from SQL. Planning and execution: - `ColumnarUDF` is the expression of a call. It is `Unevaluable`. - `ExtractColumnarUDFs` extracts the calls from the operators that hold them into an `EvalColumnarUDFs` node below, as `ExtractPythonUDFs` does for Python UDFs. Arguments that are not plain columns are computed by a `Project` below the node, so that the node only reads columns of its child. - `EvalColumnarUDFsExec` is columnar only (`supportsColumnar = true`). For each input batch it calls every function with its argument columns, and outputs the input columns, as they are, followed by one column per call. It checks the type of each returned vector. Spark runs the JVM implementation. An engine that replaces Spark's physical operators through a `ColumnarRule` can replace `EvalColumnarUDFsExec` with an operator that runs one of the other implementations, and leave the node alone when it has none, in which case the query runs the JVM implementation. A function may have no JVM implementation; a query that calls it then fails with `COLUMNAR_UDF_NOT_EXECUTABLE`, and only when the node is executed, so that an engine has had the chance to replace it. Whoever provides several implementations of a function is responsible for them returning the same results. Not in this PR: - Calls over aggregate or window functions are not extracted. - The operator targets columnar input. Over a row-based child, Spark inserts its usual `RowToColumnarExec`. - A portable native ABI. The engine implementations are opaque to Spark. ### Why are the changes needed? Spark's scalar UDFs are evaluated row by row. Over a columnar source this costs a conversion per row, most visibly for strings, where each value is decoded to a Java `String` and encoded back. It also leaves native engines that replace Spark's operators without a function boundary to take over. Such an engine has to run the row-based function itself, or define a private kind of function that plain Spark cannot run, so that a query using it fails wherever the engine does not take the operator. With this API, a function is defined once with Spark, has a JVM implementation that always works, and can have a native implementation that an engine picks up. `ColumnarUDFBenchmark` (in this PR, 20M rows of Parquet, JDK 21, Apple M4 Max), ns per row: | function | Scala UDF | columnar UDF | built-in expression | scan and aggregate only | |---|---|---|---|---| | `add_one(bigint): bigint` | 9.6 | 7.2 (1.3X) | 6.5 | 6.4 | | `mix(bigint): bigint`, 8 rounds of a 64-bit mixer | 12.9 | 10.5 (1.2X) | | 6.3 | | `shout(string): string`, upper case and a suffix | 81.5 | 31.5 (2.6X) | 92.4 | 12.8 | | `fnv(string): bigint`, FNV-1a of the bytes | 51.9 | 25.0 (2.1X) | | 12.8 | To check that an engine can use the operator boundary, I prototyped the other side in Apache DataFusion Comet (about 110 lines of Scala, not part of this PR): a rule that turns `EvalColumnarUDFsExec` into a native projection calling the Rust implementation named by an implementation of kind `comet`. With the same four functions, each with a JVM and a Rust implementation of the same algorithm (Spark 4.2 with this change applied, JDK 17, same machine and data shape), ns per row for the whole query: | function | Comet, Scala UDF | Comet, columnar UDF with the Rust implementation | Comet, built-in expression | |---|---|---|---| | `add_one` | 9.4 | 7.7 | 6.1 | | `mix` | 13.1 | 9.1 | | | `shout` | 182.4 | 26.3 | 24.9 | | `fnv` | 53.1 | 25.1 | | When a function had only the JVM implementation, Comet left the node alone and the query ran the JVM implementation over Comet's batches. ### Does this PR introduce _any_ user-facing change? Yes. It adds the `@Evolving` API described above (`ColumnarScalarFunction`, `ColumnarUDFImplementation`, `ColumnarUserDefinedFunction`, `UDFRegistration.registerColumnar`) and two error conditions, `COLUMNAR_UDF_NOT_EXECUTABLE` and `COLUMNAR_UDF_RESULT_TYPE_MISMATCH`. Queries that do not use the new API are not affected. ### How was this patch tested? New `ColumnarUDFSuite`: calls over a row-based child and over a columnar scan (with no row to columnar transition), calls from SQL with an implicit cast, sibling and nested calls, arguments that are not columns, calls in a filter and filter push-down below a call, strings and nulls, a function that Spark cannot run, and a function that returns a vector of the wrong type. New `ColumnarUDFBenchmark`, with its results. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code This pull request and its description were written by Isaac. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
