-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: preserve provided arguments during FFI object construction #24723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timsaucer
merged 1 commit into
apache:main
from
timsaucer:fix/ffi-constructor-argument-drop
Aug 28, 2026
+202
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -280,6 +280,15 @@ impl Drop for FFI_PhysicalExtensionCodec { | |
|
|
||
| impl FFI_PhysicalExtensionCodec { | ||
| /// Creates a new [`FFI_PhysicalExtensionCodec`]. | ||
| /// | ||
| /// If `codec` is already foreign, this re-exports its original FFI handle | ||
| /// rather than adding another wrapper layer. The handle still adopts the | ||
| /// `task_ctx_provider` supplied here, so it is never silently discarded and | ||
| /// an imported codec can be rebound to a different session. | ||
| /// | ||
| /// `runtime` is only honored when a new wrapper is created. An | ||
| /// already-foreign handle keeps the runtime of the library that owns it, | ||
| /// because that value lives in private data this side cannot reach. | ||
| pub fn new( | ||
| codec: Arc<dyn PhysicalExtensionCodec>, | ||
| runtime: Option<Handle>, | ||
|
|
@@ -288,7 +297,9 @@ impl FFI_PhysicalExtensionCodec { | |
| if let Some(codec) = (Arc::clone(&codec) as Arc<dyn Any>) | ||
| .downcast_ref::<ForeignPhysicalExtensionCodec>() | ||
| { | ||
| return codec.0.clone(); | ||
| let mut codec = codec.0.clone(); | ||
| codec.task_ctx_provider = task_ctx_provider.into(); | ||
| return codec; | ||
|
Comment on lines
+300
to
+302
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core of the PR. |
||
| } | ||
|
|
||
| let task_ctx_provider = task_ctx_provider.into(); | ||
|
|
@@ -449,7 +460,9 @@ pub(crate) mod tests { | |
| }; | ||
|
|
||
| use crate::execution_plan::tests::EmptyExec; | ||
| use crate::proto::physical_extension_codec::FFI_PhysicalExtensionCodec; | ||
| use crate::proto::physical_extension_codec::{ | ||
| FFI_PhysicalExtensionCodec, ForeignPhysicalExtensionCodec, | ||
| }; | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct TestExtensionCodec; | ||
|
|
@@ -710,4 +723,34 @@ pub(crate) mod tests { | |
| let foreign_codec: Arc<dyn PhysicalExtensionCodec> = (&ffi_codec).into(); | ||
| assert!(!arc_ptr_eq(&foreign_codec, &codec)); | ||
| } | ||
|
|
||
| /// Importing a codec and re-wrapping it with a different task context | ||
| /// provider must rebind the handle. See | ||
| /// <https://github.com/apache/datafusion/issues/24722>. | ||
| #[test] | ||
| fn ffi_physical_extension_codec_rebind_adopts_task_ctx_provider() { | ||
| let (_ctx_a, provider_a) = crate::util::tests::test_session_and_ctx(); | ||
| let (ctx_b, provider_b) = crate::util::tests::test_session_and_ctx(); | ||
|
|
||
| let mut ffi_codec = FFI_PhysicalExtensionCodec::new( | ||
| Arc::new(TestExtensionCodec {}) as Arc<dyn PhysicalExtensionCodec>, | ||
| None, | ||
| provider_a, | ||
| ); | ||
| ffi_codec.library_marker_id = crate::mock_foreign_marker_id; | ||
|
|
||
| let imported: Arc<dyn PhysicalExtensionCodec> = (&ffi_codec).into(); | ||
| assert!( | ||
| (Arc::clone(&imported) as Arc<dyn std::any::Any>) | ||
| .downcast_ref::<ForeignPhysicalExtensionCodec>() | ||
| .is_some() | ||
| ); | ||
|
|
||
| let rebound = FFI_PhysicalExtensionCodec::new(imported, None, provider_b); | ||
|
|
||
| let task_ctx: Arc<TaskContext> = (&rebound.task_ctx_provider) | ||
| .try_into() | ||
| .expect("rebound codec resolves"); | ||
| assert_eq!(task_ctx.session_id(), ctx_b.task_ctx().session_id()); | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -564,14 +564,26 @@ impl FFI_TableProvider { | |
| ) | ||
| } | ||
|
|
||
| /// Creates an [`FFI_TableProvider`] using a prebuilt FFI logical codec. | ||
| /// | ||
| /// If `provider` is already foreign, this re-exports its original FFI | ||
| /// handle rather than adding another wrapper layer. The handle still adopts | ||
| /// the `logical_codec` supplied here, so it is never silently discarded and | ||
| /// an imported provider can be rebound to a different session. | ||
| /// | ||
| /// `runtime` is only honored when a new wrapper is created. An | ||
| /// already-foreign handle keeps the runtime of the library that owns it, | ||
| /// because that value lives in private data this side cannot reach. | ||
| pub fn new_with_ffi_codec( | ||
| provider: Arc<dyn TableProvider>, | ||
| can_support_pushdown_filters: bool, | ||
| runtime: Option<Handle>, | ||
| logical_codec: FFI_LogicalExtensionCodec, | ||
| ) -> Self { | ||
| if let Some(provider) = provider.downcast_ref::<ForeignTableProvider>() { | ||
| return provider.0.clone(); | ||
| let mut provider = provider.0.clone(); | ||
| provider.logical_codec = logical_codec; | ||
|
timsaucer marked this conversation as resolved.
|
||
| return provider; | ||
|
Comment on lines
+584
to
+586
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core of the PR. |
||
| } | ||
| let private_data = Box::new(ProviderPrivateData { provider, runtime }); | ||
|
|
||
|
|
@@ -1271,4 +1283,40 @@ mod tests { | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Re-wrapping an imported provider with a rebuilt logical codec must adopt | ||
| /// that codec. See <https://github.com/apache/datafusion/issues/24722>. | ||
| #[test] | ||
| fn test_rebind_foreign_table_provider_adopts_logical_codec() -> Result<()> { | ||
| let (_ctx_a, provider_a) = crate::util::tests::test_session_and_ctx(); | ||
| let (ctx_b, provider_b) = crate::util::tests::test_session_and_ctx(); | ||
|
|
||
| let mut ffi_provider = FFI_TableProvider::new( | ||
| create_test_table_provider()?, | ||
| true, | ||
| None, | ||
| provider_a, | ||
| None, | ||
| ); | ||
| ffi_provider.library_marker_id = crate::mock_foreign_marker_id; | ||
|
|
||
| let imported: Arc<dyn TableProvider> = (&ffi_provider).into(); | ||
| assert!(imported.downcast_ref::<ForeignTableProvider>().is_some()); | ||
|
|
||
| // Rebuild the codec against session B and re-wrap. | ||
| let codec_b = FFI_LogicalExtensionCodec::new( | ||
| Arc::new(DefaultLogicalExtensionCodec {}), | ||
| None, | ||
| provider_b, | ||
| ); | ||
| let rebound = | ||
| FFI_TableProvider::new_with_ffi_codec(imported, true, None, codec_b); | ||
|
|
||
| let task_ctx: Arc<TaskContext> = (&rebound.logical_codec.task_ctx_provider) | ||
| .try_into() | ||
| .expect("rebound provider's codec resolves"); | ||
| assert_eq!(task_ctx.session_id(), ctx_b.task_ctx().session_id()); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the core of the PR.