Skip to content

Functions that carry state cannot be serialized without an extension codec #28006

Backlog

Description

@adriangb

Is your feature request related to a problem or challenge?

Who this is for: people who ship plans between processes with datafusion-proto, and whose plans call functions that carry state.

After #27941, a crate can make its own ExecutionPlan, PhysicalExpr, DataSource, FileSource and DataSink types serializable without a PhysicalExtensionCodec. Functions are the remaining gap.

A function is serialized as its name. The decoding side looks that name up in its session. This works only if the name fully identifies the function. It does not when the instance carries state:

/// `scale_by(x)` returns `x * factor`.
struct ScaleBy {
    factor: i64,
}

impl ScalarUDFImpl for ScaleBy { /* ... */ }

// The coordinator plans a query that calls an instance with factor = 7.
// The worker receives only the name "scale_by".
Example of a stateful function State that the name does not carry
A function configured per query, such as scale_by above The configured value
A function with a pattern or expression compiled at plan time The compiled pattern
A model inference function, predict(x) Which model
A lookup function, geo_lookup(ip) Which table or version to read
A function built from a closure that captures a value The captured value. The worker's session never has this function.

What happens today:

Worker session Result
Has no function with that name Decoding fails
Has a function with that name and different state Decoding succeeds and the query returns wrong results, with no error

The only fix today is a codec: implement try_encode_udf and try_decode_udf on a PhysicalExtensionCodec, and on a LogicalExtensionCodec for logical plans. That brings back the problems that the per-type registries removed for the other kinds:

  • An application that combines functions from several crates must compose their codecs, in the same order on both sides (#24331).
  • ComposedPhysicalExtensionCodec forwards scalar and aggregate functions, but not window functions or higher-order functions.
  • The crate that owns the function cannot make it "just work". Every application must wire the codec.

The same gap exists for aggregate functions, window functions and higher-order functions.

Describe the solution you'd like

A crate that defines a stateful function can make it serializable by itself, in the same way as its plans and expressions:

  • The function writes its own state.
  • The decoding session rebuilds the function from that state, after the crate's one registration call. The application writes no codec.
  • A function that does this never decodes to a same-named function with different state. If the decoding side cannot rebuild it, decoding fails with an error that names the function.

Requirements that a solution has to meet:

Requirement Reason
One function type can appear under several SQL names with different state For example scale_by_2 and scale_by_3 from one struct
Functions that serialize by name today keep working unchanged Most functions have no state
Applications that use a codec for functions today keep working unchanged Existing users
Wrapped functions (aliases, async functions, FFI) keep their state, or fail with an error A wrapper that hides the state gives wrong results silently
It covers logical plans as well as physical plans Functions appear in both

Describe alternatives you've considered

  1. Keep the codec as the only way. It works today, but it keeps the composition and ordering problem for every application that uses such a crate.
  2. Tell authors to put all state in the function name. This only works for small state, and it pollutes the session's function namespace.
  3. Tell authors to pass state as literal arguments. This changes the function's signature and does not work for state that is not a scalar value.

Additional context

  • A review of the code and a prototype for scalar functions in physical plans show that this is feasible. I can share the details in the discussion or in a PR.
  • This is the last kind that still requires a codec for types that a crate owns. It is therefore also input for the question of what PhysicalExtensionCodec should be responsible for in the long term, which came up in #28004.

Activity

  1. added
    enhancement
    on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions