Repository navigation
Conversation
…rce::file` A crate that depends only on `datafusion` can reach `ExtensionDataSourceFromProto` and `ExtensionDataSinkFromProto` through the re-exported `source` and `sink` modules, but not `ExtensionFileSourceFromProto`, because the `file` module is not re-exported. Re-export the `file` module next to `source` and `sink`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… file source The per-type decoder registries for extension expressions, plans and file sources each have their own trait, and no example shows them together or shows what a library should export. Add the `extension_crate` example to the `proto` examples. A module stands in for an external crate with a custom `PhysicalExpr`, a custom `ExecutionPlan` and a custom `FileSource`, and one `register_all(&mut SessionConfig)` function that registers all three decoders. The example serializes a plan, decodes it in a session that only called `register_all`, executes it and checks the rows. Link the example from the 57.0.0 upgrade guide. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
alamb
left a comment
There was a problem hiding this comment.
Thank you @adriangb -- this looks great; I found it a little confusing but I think with a few edits it will be great
It would also be great to add a link to this example from the relevant trait (try_from_datasource_proto or whatever)
This also feels pretty similar to https://github.com/apache/datafusion/blame/main/datafusion-examples/examples/proto/extension_plan_registry.rs
Also the composed codec is also similar: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/proto/composed_extension_codec.rs
It would be great to somehow explain how these are all connected
Also according to the agent, parts of PhysicalExtensionCodec are now duplicated / redundant -- is the plan to remove this redundancy (e.g. deprecate some of the methods on PhysicalExtensionCodec?) Or do we plan to remove the entire PhysicalExtensionCodec ?
┌──────────────────────────────────────────┬──────────────────────────────────────────────────────────────────────────────────┬───────────────────────────────┐
│ PhysicalExtensionCodec method │ Registry replacement │ Forwarded by the composed │
│ │ │ codec │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode / try_encode (plans) │ ExtensionPlanFromProto │ Yes │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_expr / try_encode_expr │ ExtensionExprFromProto │ No │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_udf / try_encode_udf │ None │ Yes │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_udaf / try_encode_udaf │ None │ Yes │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_udwf / try_encode_udwf │ None │ No │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_higher_order_function / │ None │ No │
│ encode │ │ │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ (data sources, file sources, sinks) │ ExtensionDataSourceFromProto, ExtensionFileSourceFromProto, │ n/a │
│ │ ExtensionDataSinkFromProto │ │
└──────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────┴───────────────────────────────┘
|
|
||
| //! See `main.rs` for how to run it. | ||
| //! | ||
| //! How a crate outside DataFusion makes its physical plans serializable. The |
There was a problem hiding this comment.
I found this introduction pretty awkward -- maybe we can focus the intro with something like
This example shows how to create an extension crate where the extensions can be
serialized across the network in a distributed system.
(is that the main point?)
| //! | ||
| //! How a crate outside DataFusion makes its physical plans serializable. The | ||
| //! [`numbers_crate`] module stands in for that crate. It ships one of each | ||
| //! kind of extension, and one function that registers all of them: |
There was a problem hiding this comment.
I think it would help to list the extension types:
- data source
- Scalar Function
- etc
The plan is neat and helpful
There was a problem hiding this comment.
Maybe we should also say it doesn't support LogicalPlans? udf / udaf, etc?
There was a problem hiding this comment.
Done, both. It now includes a scalar UDF, since one without state round-trips by name. Functions with state are tracked in #28006.
| //! | Decode | its `Extension*FromProto` | Rebuilds itself from that state | | ||
| //! | ||
| //! An application that uses the crate calls `numbers_crate::register_all` on | ||
| //! each session that decodes plans. It needs no `PhysicalExtensionCodec`, and |
There was a problem hiding this comment.
I think the reference to PhysicalExtensionCodec here is confusing (it is referring to the old way of doing things)
| // The coordinator builds the plan and serializes it. | ||
| // `DefaultPhysicalExtensionCodec` encodes and decodes nothing, so every | ||
| // custom type below goes through its own hook. | ||
| let codec = DefaultPhysicalExtensionCodec {}; |
There was a problem hiding this comment.
I found this confusing -- I was expecting that the coordiator would just call register_all as well. Why does it have to mess with DefaultPhysicalExtensionCodec ?
There was a problem hiding this comment.
It did not need to. The example now uses physical_plan_to_bytes / physical_plan_from_bytes with no codec. The coordinator does not call register_all either: each type encodes itself, so only the decoding side registers.
| worker.task_ctx().as_ref(), | ||
| &codec, | ||
| )?; | ||
| assert_eq!(plan_text(&decoded), plan_text(&plan)); |
There was a problem hiding this comment.
But the same codec was used on the coordinator 🤔
| &batches | ||
| ); | ||
|
|
||
| // The same function works on a session that already exists. |
There was a problem hiding this comment.
I don't understand the point of the rest of this example -- you just illustrated above how to register the functions and the plan is decoded
| }; | ||
| use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec; | ||
|
|
||
| pub async fn extension_crate() -> Result<()> { |
There was a problem hiding this comment.
I recommend making this clearer by splitting the work done on the coordinator into a function
coordinator() -> Bytes {
...
}And then the worker
fn worker(plan: Bytes) { .
...
}That way it would be clear you are just sharing bytes across the network, not other state
…and worker Review found the example hard to follow: it passed a codec that does nothing, it shared more than bytes between the two sides, and it did not say how it relates to the other proto examples. Use the codec-free `physical_plan_to_bytes` / `physical_plan_from_bytes`. Split the flow into `coordinator() -> Bytes` and `worker(bytes)`. Add a scalar UDF to the crate, so `register_all` takes a `SessionContext` and registers the function and the decoders. Drop the existing-session and unregistered-session cases. Say in the `proto` examples index which example to start with, and link the example from the three `Extension*FromProto` traits it uses. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
adriangb
left a comment
There was a problem hiding this comment.
Thanks for the review, all addressed.
On the codec table: the four rows with no replacement are all the same case, a function that carries state. I filed #28006 and have a draft prototype covering all four kinds (scalar, aggregate, window, higher-order) in #28009.
I do not plan to remove PhysicalExtensionCodec any time soon, but I do think long term we should look at deprecating and removing it. But it seems both systems can co-exist and we need to finish absorbing all of the use cases and letting the ecosystem migrate.
|
|
||
| //! See `main.rs` for how to run it. | ||
| //! | ||
| //! How a crate outside DataFusion makes its physical plans serializable. The |
| //! | ||
| //! How a crate outside DataFusion makes its physical plans serializable. The | ||
| //! [`numbers_crate`] module stands in for that crate. It ships one of each | ||
| //! kind of extension, and one function that registers all of them: |
There was a problem hiding this comment.
Done, both. It now includes a scalar UDF, since one without state round-trips by name. Functions with state are tracked in #28006.
| //! | Decode | its `Extension*FromProto` | Rebuilds itself from that state | | ||
| //! | ||
| //! An application that uses the crate calls `numbers_crate::register_all` on | ||
| //! each session that decodes plans. It needs no `PhysicalExtensionCodec`, and |
| // The coordinator builds the plan and serializes it. | ||
| // `DefaultPhysicalExtensionCodec` encodes and decodes nothing, so every | ||
| // custom type below goes through its own hook. | ||
| let codec = DefaultPhysicalExtensionCodec {}; |
There was a problem hiding this comment.
It did not need to. The example now uses physical_plan_to_bytes / physical_plan_from_bytes with no codec. The coordinator does not call register_all either: each type encodes itself, so only the decoding side registers.
| worker.task_ctx().as_ref(), | ||
| &codec, | ||
| )?; | ||
| assert_eq!(plan_text(&decoded), plan_text(&plan)); |
| &batches | ||
| ); | ||
|
|
||
| // The same function works on a session that already exists. |
| }; | ||
| use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec; | ||
|
|
||
| pub async fn extension_crate() -> Result<()> { |
Which issue does this PR close?
Rationale for this change
Extension expressions, plans and file sources each have a per-type decoder trait (
ExtensionExprFromProto,ExtensionPlanFromProto,ExtensionFileSourceFromProto). No example shows them together, and no example shows what a library should export so that an application can register all of its types in one call.What changes are included in this PR?
A new
extension_crateexample in theprotoexamples. It shows how to write an extension crate whose extensions can be serialized and sent across the network. Thenumbers_cratemodule stands in for that crate:The crate exports one function that registers everything a serialized plan can refer to:
The example has two functions, and only bytes pass between them:
datafusion_datasource::fileasdatafusion::datasource::fileCommit 1 is needed because examples use only the
datafusioncrate.ExtensionDataSourceFromProtoandExtensionDataSinkFromProtoare reachable through the re-exportedsourceandsinkmodules, butExtensionFileSourceFromProtois not, becausefileis not re-exported.The module doc of the
protoexamples now says which example to start with, and the threeExtension*FromPrototraits link to this example.What is the testing strategy for this PR?
The example checks itself, and CI runs it. The worker decodes the plan in a session that only called
register_all, executes it against a real file, and the example asserts the exact rows:Are there any user-facing changes?
One new public path:
datafusion::datasource::file(a re-export ofdatafusion_datasource::file). There are no breaking changes.🤖 Generated with Claude Code