fix: preserve None projection semantics across FFI boundary in ForeignTableProvider::scan#20393
fix: preserve None projection semantics across FFI boundary in ForeignTableProvider::scan#20393Kontinuation wants to merge 1 commit intoapache:mainfrom
Conversation
…nTableProvider::scan ForeignTableProvider::scan() converted projection: None (meaning 'all columns') into an empty RVec<usize> via unwrap_or_default() before passing it across the FFI boundary. On the receiving side, scan_fn_wrapper always wrapped the received RVec in Some(...), passing Some(&vec![]) to the inner TableProvider::scan(). This means 'project zero columns' -- the opposite of the intended 'all columns'. Fix by changing the FFI function signature's projections parameter from RVec<usize> to ROption<RVec<usize>>, matching how limit already uses ROption<usize> for the same None-vs-value distinction. Update both the sender (ForeignTableProvider::scan) and receiver (scan_fn_wrapper) to properly convert between Option and ROption. Add test that verifies scan(projection=None) returns all columns.
ea8800a to
34563b3
Compare
|
cc @timsaucer ! |
timsaucer
left a comment
There was a problem hiding this comment.
Thank you for a very clear and concise solution!
One alternative we could do that would not be breaking for the FFI boundary would be on the Foreign side to check for Some([]) and return an empty batch executor and then on the local side (wrapped fn) change an empty projections into None.
|
What shall we do with this PR? Is it ready to merge? Should we try @timsaucer 's alternate suggestion? |
|
I had a hard time figuring out how to construct an execution plan producing RecordBatches with zero columns while matching the total number of records in the foreign table. This can be done but in a cumbersome way. I don't think we can simply return an empty batch executor for such cases, since we still need to produce batches with no columns. Another approach without breaking the ABI is to expand The "select all" projection represented by I think we should either stick to the current approach to make the FFI APIs look the same as local APIs for better consistency and less mental burden, or expand |
Which issue does this PR close?
N/A (newly discovered bug)
This is originally found in apache/sedona-db when working on a custom plan node: apache/sedona-db#611 (comment)
Rationale for this change
ForeignTableProvider::scan()converts aNoneprojection (meaning "return all columns") into an emptyRVec<usize>before passing it across the FFI boundary. On the receiving side,scan_fn_wrapperalways wraps the receivedRVecinSome(...), passingSome(&vec![])to the innerTableProvider::scan(). This means "project zero columns" — the exact opposite of the intended "project all columns."The root cause is that the
FFI_TableProvider::scanfunction signature usesRVec<usize>for the projections parameter. SinceRVec<usize>cannot representNone, theNonevs. empty-vec distinction is lost at the FFI boundary.What changes are included in this PR?
Three coordinated changes in
datafusion/ffi/src/table_provider.rs:FFI struct definition: Changed
scanfunction pointer signature fromRVec<usize>toROption<RVec<usize>>for the projections parameter, matching howlimitalready usesROption<usize>for the sameNone-vs-value distinction.Receiver side (
scan_fn_wrapper): ConvertsROption<RVec<usize>>via.into_option().map(...)and passesprojections.as_ref()to the inner provider, preservingNonesemantics.Sender side (
ForeignTableProvider::scan): ConvertsOption<&Vec<usize>>toROption<RVec<usize>>via.into()instead of usingunwrap_or_default().Plus a new unit test
test_scan_with_none_projection_returns_all_columnsthat directly exercises the FFI round-trip withprojection=Noneand verifies all 3 columns are returned.Also fixed the existing
test_aggregationtest to setlibrary_marker_id = mock_foreign_marker_idso it actually exercises the FFI path instead of taking the local bypass.How are these changes tested?
test_scan_with_none_projection_returns_all_columns: creates a 3-column MemTable, wraps it through FFI with the foreign marker set, callsscan(None), and asserts 3 columns are returned (previously returned 0).Are these changes safe?
This is a breaking FFI ABI change to the
FFI_TableProvider::scanfunction pointer signature. However:abi_stablecrate's#[derive(StableAbi)]generates layout checks at dylib load time, so mismatched dylibs will be caught at load rather than causing silent corruption.FFI_TableProvidervia::new()or::new_with_ffi_codec(), which internally wire upscan_fn_wrapper— nobody constructs thescanfunction pointer manually.