Repository navigation
Conversation
A function that serializes its own state needs a place on the wire for the key of the decoder that rebuilds it. The existing `fun_definition` bytes belong to the `PhysicalExtensionCodec`, so a reader cannot tell who wrote them. Add `optional string fun_type_name` to `PhysicalScalarUdfNode`, `PhysicalHigherOrderUdfNode`, `PhysicalAggregateExprNode` and `PhysicalWindowExprNode`. Every writer sets it to `None` and the scalar reader ignores it, so this commit changes no behavior. Mechanical: `pbjson.rs` and `prost.rs` are regenerated from `datafusion.proto`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Three call sites in `to_proto.rs` and three in `ConverterPlanEncoder` each repeat the codec call and the "empty bytes mean by name" rule. The self-encoded state added later needs one place per function kind to take precedence over the codec. Move that logic into `encode_scalar_udf`, `encode_aggregate_udf`, `encode_window_udf` and `encode_higher_order_udf`. Each returns `(fun_definition, fun_type_name)`. The type name is always `None` here, so the wire output does not change. Refactor: no behavior change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`AggregateExec` and the window operators encode their functions through `ExecutionPlanEncodeCtx::encode_udaf` and `encode_udwf`, which return only bytes. A self-encoded function also needs its decoder type name written to and read from the plan node. Add `EncodedFunction` (payload plus type name) and the `encode_udaf_definition`, `encode_udwf_definition`, `decode_udaf_definition` and `decode_udwf_definition` methods to the encode and decode traits and contexts. The aggregate and window nodes use them. The provided encoders write no type name, and the provided decoders reject one, so every existing plan round-trips as before. Refactor: no behavior change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A function that carries per-instance state (a model path, a compiled
regex, a config value) cannot be rebuilt from its name. Today the only way
to ship that state is a `PhysicalExtensionCodec` that knows every such
function.
Add `UDFState { type_name, payload }` and a `try_encode_state` hook to
`ScalarUDFImpl`, `AggregateUDFImpl`, `WindowUDFImpl` and
`HigherOrderUDFImpl`. The default returns `Ok(None)`. The `ScalarUDF`,
`AggregateUDF`, `WindowUDF` and `HigherOrderUDF` wrappers, the alias
wrappers and `AsyncScalarUDF` forward to the inner function.
Nothing calls the hook yet.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The decoding side needs a typed way to turn a `UDFState` payload back into a function. The decoder takes a `TaskContext`, so it cannot live in `datafusion-expr`. Add the `proto` feature and module to `datafusion-execution`, with `ScalarUdfFromProto`, `AggregateUdfFromProto`, `WindowUdfFromProto` and `HigherOrderUdfFromProto`. Each registers a constructor in the session's `ProtoDecoderRegistry` under `NAME`. The hidden `decode_*` functions and the `*_names` functions are the lookups that `datafusion-proto` uses. `datafusion-proto` and the `proto` feature of `datafusion-physical-plan` turn the feature on. Nothing decodes with these yet. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…oders An `ExecutionPlanEncode` implementation that only writes bytes would drop the state of a function that implements `try_encode_state`. The decoder would then resolve the function by name and build the wrong instance. Make the provided `encode_udaf_definition` and `encode_udwf_definition` ask the function for its state first and fall back to the bytes-only method. Add `impl From<UDFState> for EncodedFunction`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A scalar UDF with per-instance state needs a `PhysicalExtensionCodec` that knows it. The function now has `try_encode_state` and the session has `ScalarUdfFromProto` decoders, but the serializer uses neither. Encode: `encode_scalar_udf` asks the function for its state before the codec and writes the type name to `fun_type_name`. The bytes-only `ExecutionPlanEncode::encode_udf` has no slot for the type name, so it returns a not-implemented error for such a function. Decode: a node with `fun_type_name` goes to the `ProtoDecoderRegistry` of the session. An unregistered type name is an error that lists the registered names. A decoder that returns a function with another SQL name is an error. A node without `fun_type_name` decodes as before. `udf_registry.rs` holds the round-trip tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Apply the scalar UDF design to aggregate UDFs. Encode: `encode_aggregate_udf` asks the function for its state before the codec. The bytes-only `ExecutionPlanEncode::encode_udaf` returns a not-implemented error for a self-encoded function and points to `encode_udaf_definition`. Decode: `ConverterPlanDecoder` overrides `decode_udaf_definition`. This covers `AggregateExec` and aggregates used as window functions. A node with `fun_type_name` goes to the `AggregateUdfFromProto` decoder in the session's `ProtoDecoderRegistry`, with the same errors as the scalar path. A node without it decodes as before. `udaf_registry.rs` holds the round-trip tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Apply the scalar UDF design to window UDFs. Encode: `encode_window_udf` asks the function for its state before the codec. The bytes-only `ExecutionPlanEncode::encode_udwf` returns a not-implemented error for a self-encoded function and points to `encode_udwf_definition`. Decode: `ConverterPlanDecoder` overrides `decode_udwf_definition`. A node with `fun_type_name` goes to the `WindowUdfFromProto` decoder in the session's `ProtoDecoderRegistry`, with the same errors as the scalar path. A node without it decodes as before. `udwf_registry.rs` holds the round-trip tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Apply the scalar UDF design to higher-order functions. Encode: `encode_higher_order_udf` asks the function for its state before the codec. Decode: Higher-order functions are expressions, so the expression decoder handles them and the plan channel does not change. A node with `fun_type_name` goes to the `HigherOrderUdfFromProto` decoder in the session's `ProtoDecoderRegistry`, with the same errors as the scalar path. A node without it decodes as before. `hof_registry.rs` holds the round-trip tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…bytes The round-trip tests use the default codec, which writes nothing for a function. They do not show what happens when a codec and a function both want to write the definition. Add unit tests with `PayloadCodec`, which writes bytes for every function: the bytes-only encoders reject a self-encoded function with an exact message, and the definition encoders prefer the function's state over the codec bytes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
adriangb
force-pushed
the
proto-udf-registry-prototype
branch
from
December 5, 2026 04:55
65db2e5 to
dffd0e9
Compare
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?
This PR is a draft to support the design discussion. It is not ready to merge.
Rationale for this change
The problem
A function with fields cannot be rebuilt from its name. Take a UDF that multiplies by a factor:
datafusion-protowrites the namescale_by_7. The receiving node has no way to learn the7. Today the only fix is aPhysicalExtensionCodecthat knowsScaleBy:One codec must know every stateful function of every crate in the process. Two libraries that each ship a codec cannot be combined without a third codec that wraps both.
Plans, physical expressions, data sources, file sources and sinks already left this model: each type writes itself and registers a decoder in the
ProtoDecoderRegistryof the session. Stateful functions are the remaining gap.The proposal
The function writes its own state. The session holds the decoder. No codec.
This prototype checks that the pattern works for all four function kinds that
PhysicalExtensionCodechandles:try_encode_udf/try_decode_udfScalarUDFImpl::try_encode_stateScalarUdfFromPrototry_encode_udaf/try_decode_udafAggregateUDFImpl::try_encode_stateAggregateUdfFromPrototry_encode_udwf/try_decode_udwfWindowUDFImpl::try_encode_stateWindowUdfFromPrototry_encode_higher_order_function/try_decode_higher_order_functionHigherOrderUDFImpl::try_encode_stateHigherOrderUdfFromProtoWhat changes are included in this PR?
How the pieces fit
flowchart LR subgraph enc["Encoding process"] F["ScaleBy { factor: 7 }<br/>registered as scale_by_7"] H["try_encode_state()<br/><i>datafusion-expr</i>"] F --> H end subgraph wire["Proto node"] N["name = scale_by_7"] T["fun_type_name = my_crate.ScaleBy<br/><b>new field</b>"] D["fun_definition = payload bytes"] end subgraph dec["Decoding process"] R["ProtoDecoderRegistry<br/>in SessionConfig"] P["ScaleBy::try_from_proto(payload, name, ctx)<br/><i>datafusion-execution</i>"] G["ScaleBy { factor: 7 }<br/>named scale_by_7"] R --> P --> G end H -- "UDFState.type_name" --> T H -- "UDFState.payload" --> D T -- "selects the decoder" --> R D --> P N --> PThe wire has two names on purpose:
namescale_by_7fun_type_namemy_crate.ScaleByOne type can back many instances (
scale_by_7,scale_by_3), so the SQL name cannot be the registry key.Who decodes a function node
fun_type_namealso tells the reader who wrotefun_definition. The rule is the same for all four kinds:flowchart TD A{"fun_type_name present?"} A -- yes --> B{"decoder registered<br/>under that name?"} B -- yes --> C["Registry decoder.<br/>Error if the result has another SQL name."] B -- no --> E["Error that lists the registered type names.<br/>No codec fallback."] A -- no --> F{"fun_definition present?"} F -- yes --> G["PhysicalExtensionCodec, as today"] F -- no --> H["Lookup by name in the session, as today"]On encode, the state of the function wins over the codec. A function that returns
Ok(None)(the default) takes the codec path as before.The plan channel
Scalar and higher-order functions are expression nodes that only
datafusion-protoencodes. Aggregate and window functions are different:AggregateExecand the window plans encode them throughExecutionPlanEncodeCtx, whose methods return bytes only.encode_udaf,encode_udwf,encode_udf(bytes only)encode_udaf_definition,encode_udwf_definitionHow to review
The commits are ordered so that the first three change no behavior and the next two only add API. Each commit builds and passes its tests.
optional string fun_type_nameto the four function nodes. Every writer setsNone.pbjson.rsandprost.rsare generated.encode_*helper per function kind. No behavior change.EncodedFunctionand the*_definitionmethods to the plan channel.AggregateExecand the window plans use them. The type name is alwaysNone. No behavior change.UDFStateandtry_encode_stateto the four function traits and their wrappers. Nothing calls the hook.protofeature ofdatafusion-executionwith the four*FromProtodecoder traits. Nothing decodes with them.encode_udaf_definitionandencode_udwf_definitionask the function for its state first.decode_self_encodedhelper and its errors. Tests inudf_registry.rs.udaf_registry.rs.udwf_registry.rs.hof_registry.rs.The four "Change" commits for the function kinds have the same shape. Read the scalar one closely and skim the other three.
New public items
datafusion-exprUDFState.try_encode_stateonScalarUDFImpl,AggregateUDFImpl,WindowUDFImpl,HigherOrderUDFImpl(provided methods, defaultOk(None)) and onScalarUDF,AggregateUDF,WindowUDF,HigherOrderUDF.datafusion-executionprotofeature. Indatafusion_execution::proto:ScalarUdfFromProto,AggregateUdfFromProto,WindowUdfFromProto,HigherOrderUdfFromProto, andscalar_udf_names,aggregate_udf_names,window_udf_names,higher_order_udf_names. Hidden, for internal use only:decode_scalar_udf,decode_aggregate_udf,decode_window_udf,decode_higher_order_udf.datafusion-physical-planproto::EncodedFunctionandimpl From<UDFState> for EncodedFunction.encode_udaf_definitionandencode_udwf_definitiononExecutionPlanEncodeCtx.decode_udaf_definitionanddecode_udwf_definitiononExecutionPlanDecodeCtx.datafusion-proto-modelsfun_type_namefield onPhysicalScalarUdfNode,PhysicalAggregateExprNode,PhysicalWindowExprNodeandPhysicalHigherOrderUdfNode.Open questions
try_encode_stateloses the state. The alias wrappers andAsyncScalarUDFforward it.AsyncScalarUDFalso needs the decoder to wrap the function again.datafusion-expr. The decoder traits are indatafusion-execution, because the decoder takes aTaskContext.What is the testing strategy for this PR?
The core test, in
udf_registry.rs: one type, two instances, no codec, and the decoding session does not have the functions registered by name.Four files in
datafusion/proto/tests/cases/plans/hold 23 round-trip tests that assert exact output values:ProjectionExecAggregateExecWindowAggExecProjectionExecUnit tests in
datafusion-protoanddatafusion-physical-plancover the plan channel:encode_udf,encode_udafandencode_udwfreject a self-encoded function with an exact message.Known gap: the state in the higher-order test does not change the query output. The test checks the decoded state directly.
Are there any user-facing changes?
Yes, all additive: the public items listed above.
fun_type_namekeeps the current behavior.try_encode_statetakes the codec path.fun_type_namefield numbers and thetry_encode_statesignature.🤖 Generated with Claude Code