fix(udf): pass batch row count to ScalarFunction.evaluate#57
Closed
LantaoJin wants to merge 1 commit into
Closed
Conversation
Add an int rowCount parameter to ScalarFunction.evaluate. JniBridge already receives the value from the native side as expectedRowCount for post-call validation; now it is also forwarded into evaluate. For UDFs with at least one argument the value matches what the body could read from args.get(0).getValueCount(). For nullary UDFs (args is empty), this is the only channel that communicates the batch row count, making it possible to implement Volatility.VOLATILE nullary functions like random() / now().
This was referenced May 18, 2026
Open
Contributor
Author
|
Superseded by upstream #64; nullary row count is now available via |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
ScalarFunction.evaluate(BufferAllocator, List<FieldVector>)(introduced in #46) is the contract every Java-implemented scalar UDF must satisfy. It must return aFieldVectorwhosegetValueCount()matches the batch row count DataFusion is driving through the operator tree.For UDFs with at least one argument, the body can read
args.get(0).getValueCount()to learn how many rows to produce. For nullary UDFs -- zero arguments, e.g. analogs ofrandom(),pi(),now()--argsis the empty list, and the body has no other channel to learn the row count.The native side already knows the value:
ScalarFunctionArgs::number_rowsis read atnative/src/udf.rs:100, used to materialise scalar arg columns at:106. The Java bridge (JniBridge.invokeScalarUdf) receives it but only uses it after the fact, to validate the returned vector's length. It is never communicated toimpl.evaluate(...).The result: any nullary UDF that DataFusion does not constant-fold (anything declared
Volatility.VOLATILE, orSTABLEcalls in plans the optimizer cannot fold) trips the post-hoc row-count validation as soon as it runs over a batch with more than one row.What changes are included in this PR?
ScalarFunction.evaluate(BufferAllocator allocator, List<FieldVector> args, int rowCount)— adds a third parameter carrying the per-batch row count. Source-breaking signature change to a public interface. The repo is pre-release; only five existing implementations needed an unused-parameter update (four test UDFs inScalarUdfTest, one inexamples/AddOneExample).JniBridge.invokeScalarUdf(core/src/main/java/org/apache/datafusion/internal/JniBridge.java) now forwards the existingexpectedRowCountparameter intoimpl.evaluate(...). Post-call validation against the same value is unchanged.Are these changes tested?
yes
Are there any user-facing changes?
Yes, a source-breaking signature change to
ScalarFunction.evaluate. Implementations of the interface need to add anint rowCountparameter to theirevaluateoverride. Bodies that ignore it remain identical otherwise.Before:
After: