adriangb opened a new pull request, #28009:
URL: https://github.com/apache/datafusion/pull/28009

   ## Which issue does this PR close?
   
   - Prototype for [#28006](https://github.com/apache/datafusion/issues/28006). 
It does not close the issue.
   
   This PR is a draft to support the design discussion. It is not ready to 
merge.
   
   ## Rationale for this change
   
   A scalar UDF whose `ScalarUDFImpl` has fields (a compiled regex, a model 
handle, a lookup table) cannot round-trip through `datafusion-proto` without a 
`PhysicalExtensionCodec`. Plans, physical expressions, data sources, file 
sources and sinks can now serialize themselves and register a decoder in the 
`ProtoDecoderRegistry`. Stateful UDFs are the remaining gap, see 
[#28006](https://github.com/apache/datafusion/issues/28006).
   
   This prototype checks that the same pattern works for scalar UDFs, and finds 
where it does not fit.
   
   ## What changes are included in this PR?
   
   The UDF writes its own state, and a decoder registered under a type name 
rebuilds it:
   
   ```rust
   // Encode side: datafusion-expr, no feature flag.
   impl ScalarUDFImpl for RegexMatch {
       fn try_encode_state(&self) -> Result<Option<ScalarUDFState>> {
           Ok(Some(ScalarUDFState {
               type_name: Self::NAME,
               payload: self.pattern.as_bytes().to_vec(),
           }))
       }
   }
   
   // Decode side: datafusion-execution, behind a new `proto` feature.
   impl ScalarUdfFromProto for RegexMatch {
       const NAME: &'static str = "my_crate.RegexMatch";
   
       fn try_from_proto(
           payload: &[u8],
           name: &str,
           ctx: &TaskContext,
       ) -> Result<Arc<ScalarUDF>> { /* ... */ }
   }
   
   config.update_extension(RegexMatch::register_proto)?;
   ```
   
   | Commit | Content |
   |---|---|
   | 1 | Adds `optional string fun_type_name = 8` to `PhysicalScalarUdfNode`. 
The generated code is mechanical (`regen.sh`). |
   | 2 | Adds `ScalarUDFImpl::try_encode_state`, `ScalarUDFState`, the 
`ScalarUdfFromProto` decoder trait, and the wiring in the physical expression 
serde. |
   | 3 | Moves `ScalarUdfFromProto` to `datafusion-execution`, so that the 
decoder takes a `&TaskContext`. |
   
   Decode precedence:
   
   | `fun_type_name` | `fun_definition` | Decoder |
   |---|---|---|
   | present | any | The registry only. An unregistered name is an error. |
   | absent | present | The `PhysicalExtensionCodec`, as today |
   | absent | absent | Lookup by name in the session, as today |
   
   The type name is separate from the SQL name because one type can back 
several registered functions. It also lets the decoder tell a self-encoded 
payload from a codec payload.
   
   New public items:
   
   - `datafusion_expr::ScalarUDFImpl::try_encode_state` (provided method, 
default `Ok(None)`)
   - `datafusion_expr::ScalarUDF::try_encode_state`
   - `datafusion_expr::ScalarUDFState`
   - `datafusion_execution::proto::ScalarUdfFromProto`
   - The `proto` feature of `datafusion-execution`
   - The `fun_type_name` field of `PhysicalScalarUdfNode`
   
   Open questions:
   
   - Logical plans: `ScalarUDFExprNode` needs the same field. This prototype 
covers physical plans only.
   - `AsyncScalarUDF`: the wrapper forwards the encode hook, but the decoder 
must wrap the function again.
   - FFI: `ForeignScalarUDF` needs an ABI slot to forward the hook.
   - The bytes-only `encode_udf` path in `physical_plan/mod.rs` has no slot for 
the type name. It returns a `not_impl_err` for a self-encoding UDF.
   - Aggregate and window UDFs can follow the same shape. This prototype does 
not include them.
   - Crate placement: the encode hook is in `datafusion-expr` and the decoder 
trait is in `datafusion-execution`.
   
   ## What is the testing strategy for this PR?
   
   `datafusion/proto/tests/cases/plans/udf_registry.rs` adds six round-trip 
tests:
   
   - `one_type_two_sql_names_round_trip`
   - `unregistered_type_name_errors`
   - `stateless_udf_round_trips_by_name`
   - `aliased_stateful_udf_round_trips`
   - `async_wrapped_stateful_udf_keeps_state`
   - `codec_written_definition_still_uses_codec`
   
   ## Are there any user-facing changes?
   
   Yes, all additive: the public items listed above. Existing serialized plans 
decode as before, because an absent `fun_type_name` keeps the current behavior.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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]

Reply via email to