Skip to content

feat: give PhysicalExtensionCodec the full decode context - #25318

Open
namanjain24-sudo wants to merge 26 commits into
apache:mainfrom
namanjain24-sudo:extension-codec-decode-context
Open

namanjain24-sudo wants to merge 26 commits into
apache:mainfrom
namanjain24-sudo:extension-codec-decode-context

Conversation

@namanjain24-sudo

@namanjain24-sudo namanjain24-sudo commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

A custom ExecutionPlan whose codec decodes its own expressions fails to deserialize when one of those expressions is a ScalarSubqueryExpr. It fails even when the plan sits inside the ScalarSubqueryExec that owns the results:

Internal("ScalarSubqueryExpr can only be deserialized as part of a surrounding ScalarSubqueryExec")

The decoder does track the results of the enclosing ScalarSubqueryExec in PhysicalPlanDecodeContext. But PhysicalExtensionCodec::try_decode only receives ctx.task_ctx(), so a codec has to build a fresh PhysicalPlanDecodeContext::new(..) to decode its expressions, and that fresh context has no results.

What changes are included in this PR?

This takes the non-breaking option from the issue:

  • PhysicalExtensionCodec::try_decode_with_ctx receives the full PhysicalPlanDecodeContext. Its default implementation calls try_decode(ctx.task_ctx()), so existing codecs compile and behave as before.
  • Extension plans are now decoded through try_decode_with_ctx.
  • ComposedPhysicalExtensionCodec forwards try_decode_with_ctx to the codec that encoded the node. Without this, composing codecs would drop the context again.
  • On the FFI side, FFI_PhysicalExtensionCodec gains a try_decode_with_ctx function pointer that also forwards the caller's active scalar-subquery results scope (if any) across the boundary through a new FFI_ScalarSubqueryResults handle, so a ScalarSubqueryExpr decoded by a foreign codec shares the same populated results as the host's ScalarSubqueryExec. A codec that only implements the new method therefore also works through FFI. For codecs that only implement try_decode, nothing changes.

I went with the new method rather than changing try_decode's signature because of the API health policy. The breaking change is simpler if reviewers prefer it, and I'm happy to switch.

This touches the same function as the draft #24631, which adds a per-type registry in front of the codec. That PR's registry path already passes decoders the full context (through ConverterPlanDecoder), but its codec fallback still calls try_decode(ctx.task_ctx()), which is the line this PR changes. Whichever lands second needs a one-line rebase.

What is the testing strategy for this PR?

New tests in datafusion/proto/tests/cases/plans/scalar_subquery.rs use an extension plan whose codec decodes its own expressions through try_decode_with_ctx. Its try_decode returns an error, so a caller that drops the context fails the test:

  • a ScalarSubqueryExpr inside the extension plan shares the enclosing ScalarSubqueryExec's results, with both DefaultPhysicalProtoConverter and DeduplicatingProtoConverter
  • the same through ComposedPhysicalExtensionCodec
  • nested ScalarSubqueryExecs: each expression binds to its own scope and not to the other
  • without an enclosing ScalarSubqueryExec, decoding still fails with the existing error

A new FFI test checks that a codec that only decodes through try_decode_with_ctx works behind FFI_PhysicalExtensionCodec. A second, cross-library integration test (datafusion/ffi/tests/ffi_physical_extension_codec.rs, gated by the integration-tests feature) builds that codec inside a separately dlopen'd copy of the datafusion-ffi cdylib, so the scalar-subquery results handle crosses a genuine FFI boundary and exercises ForeignScalarSubqueryResultsBackend for real, rather than only the in-process mock_foreign_marker_id path the unit test uses.

I checked that each part of the change is needed:

  • The first test fails on main with the error from the issue.
  • With the call site reverted to try_decode, all four new proto tests fail.
  • With ComposedPhysicalExtensionCodec::try_decode_with_ctx removed, only the composed-codec test fails.
  • With the FFI wrapper reverted, the new FFI test fails.

Existing suites pass: datafusion-proto (17 lib, 265 integration, 4 doc tests) and datafusion-ffi (123 lib tests, plus the new cross-library test under --features integration-tests). ./dev/rust_lint.sh is clean.

Are there any user-facing changes?

This has two separate kinds of API impact:

  • Rust API (non-breaking): PhysicalExtensionCodec::try_decode_with_ctx is a new provided method. Existing implementations keep compiling and behave the same.
  • FFI ABI (breaking): FFI_PhysicalExtensionCodec is #[repr(C)] and gains a try_decode_with_ctx function-pointer field, appended after the existing fields so a producer compiled against the old layout still sees every field it knows about at its original offset. The struct's size still changes, so this is an ABI break: a producer and a consumer built against different layouts of this struct must not be mixed — both sides need to be rebuilt against the same DataFusion version. This should carry the api change label; I don't have permission to add labels on this repo, so flagging it here for a maintainer to apply.

The same two-sided split applies to the FFI_ScalarSubqueryResults handle added for the scalar-subquery results forwarding described above.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api change Changes the API exposed to users of the crate documentation Improvements or additions to documentation ffi Changes to the ffi crate logical-expr Logical plan and expressions proto Related to proto crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PhysicalExtensionCodec should take a PhysicalPlanDecodeContext instead of a TaskContext

5 participants