alamb commented on code in PR #28004:
URL: https://github.com/apache/datafusion/pull/28004#discussion_r4792079382
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! How a crate outside DataFusion makes its physical plans serializable. The
Review 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?)
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! 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:
+//!
+//! ```text
+//! RepeatExec: times=2 <- custom ExecutionPlan
+//! ProjectionExec: [ScaleExpr(value)] <- custom PhysicalExpr in a
built-in plan
+//! DataSourceExec <- built-in, written by DataFusion
+//! FileScanConfig <- built-in, written by DataFusion
+//! NumbersSource <- custom FileSource
+//! ```
+//!
+//! Each custom type does two things:
+//!
+//! | Step | Where | What
|
+//!
|--------|------------------------------|--------------------------------------------|
+//! | Encode | its `try_to_proto*` hook | Writes its name and its own
state |
+//! | 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
Review Comment:
I think the reference to PhysicalExtensionCodec here is confusing (it is
referring to the old way of doing things)
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! 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:
+//!
+//! ```text
+//! RepeatExec: times=2 <- custom ExecutionPlan
+//! ProjectionExec: [ScaleExpr(value)] <- custom PhysicalExpr in a
built-in plan
+//! DataSourceExec <- built-in, written by DataFusion
+//! FileScanConfig <- built-in, written by DataFusion
+//! NumbersSource <- custom FileSource
+//! ```
+//!
+//! Each custom type does two things:
+//!
+//! | Step | Where | What
|
+//!
|--------|------------------------------|--------------------------------------------|
+//! | Encode | its `try_to_proto*` hook | Writes its name and its own
state |
+//! | 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
+//! it does not need to know which types the crate contains.
+
+use std::sync::Arc;
+
+use datafusion::arrow::array::{Int64Array, RecordBatch};
+use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use datafusion::common::{Result, assert_batches_eq};
+use datafusion::physical_plan::{ExecutionPlan, collect, displayable};
+use datafusion::prelude::{SessionConfig, SessionContext};
+use datafusion_proto::bytes::{
+ physical_plan_from_bytes_with_extension_codec,
+ physical_plan_to_bytes_with_extension_codec,
+};
+use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec;
+
+pub async fn extension_crate() -> Result<()> {
+ let dir = tempfile::tempdir()?;
+ let path = dir.path().join("data.numbers");
+ std::fs::write(&path, "1\n2\n3\n")?;
+ let file_size = std::fs::metadata(&path)?.len();
+
+ // 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 {};
Review 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` ?
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! 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:
+//!
+//! ```text
+//! RepeatExec: times=2 <- custom ExecutionPlan
+//! ProjectionExec: [ScaleExpr(value)] <- custom PhysicalExpr in a
built-in plan
+//! DataSourceExec <- built-in, written by DataFusion
+//! FileScanConfig <- built-in, written by DataFusion
+//! NumbersSource <- custom FileSource
+//! ```
+//!
+//! Each custom type does two things:
+//!
+//! | Step | Where | What
|
+//!
|--------|------------------------------|--------------------------------------------|
+//! | Encode | its `try_to_proto*` hook | Writes its name and its own
state |
+//! | 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
+//! it does not need to know which types the crate contains.
+
+use std::sync::Arc;
+
+use datafusion::arrow::array::{Int64Array, RecordBatch};
+use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use datafusion::common::{Result, assert_batches_eq};
+use datafusion::physical_plan::{ExecutionPlan, collect, displayable};
+use datafusion::prelude::{SessionConfig, SessionContext};
+use datafusion_proto::bytes::{
+ physical_plan_from_bytes_with_extension_codec,
+ physical_plan_to_bytes_with_extension_codec,
+};
+use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec;
+
+pub async fn extension_crate() -> Result<()> {
Review Comment:
I recommend making this clearer by splitting the work done on the
coordinator into a function
```rust
coordinator() -> Bytes {
...
}
```
And then the worker
```rust
fn worker(plan: Bytes) { .
...
}
```
That way it would be clear you are just sharing bytes across the network,
not other state
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! 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:
Review Comment:
Maybe we should also say it doesn't support LogicalPlans? udf / udaf, etc?
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! 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:
+//!
+//! ```text
+//! RepeatExec: times=2 <- custom ExecutionPlan
+//! ProjectionExec: [ScaleExpr(value)] <- custom PhysicalExpr in a
built-in plan
+//! DataSourceExec <- built-in, written by DataFusion
+//! FileScanConfig <- built-in, written by DataFusion
+//! NumbersSource <- custom FileSource
+//! ```
+//!
+//! Each custom type does two things:
+//!
+//! | Step | Where | What
|
+//!
|--------|------------------------------|--------------------------------------------|
+//! | Encode | its `try_to_proto*` hook | Writes its name and its own
state |
+//! | 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
+//! it does not need to know which types the crate contains.
+
+use std::sync::Arc;
+
+use datafusion::arrow::array::{Int64Array, RecordBatch};
+use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use datafusion::common::{Result, assert_batches_eq};
+use datafusion::physical_plan::{ExecutionPlan, collect, displayable};
+use datafusion::prelude::{SessionConfig, SessionContext};
+use datafusion_proto::bytes::{
+ physical_plan_from_bytes_with_extension_codec,
+ physical_plan_to_bytes_with_extension_codec,
+};
+use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec;
+
+pub async fn extension_crate() -> Result<()> {
+ let dir = tempfile::tempdir()?;
+ let path = dir.path().join("data.numbers");
+ std::fs::write(&path, "1\n2\n3\n")?;
+ let file_size = std::fs::metadata(&path)?.len();
+
+ // 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 {};
+ let plan = numbers_crate::scaled_and_repeated(path.to_str().unwrap(),
file_size)?;
+ let bytes = physical_plan_to_bytes_with_extension_codec(Arc::clone(&plan),
&codec)?;
+ println!(
+ "Serialized this plan to {} bytes:\n{}",
+ bytes.len(),
+ plan_text(&plan)
+ );
+
+ // The worker registers the crate once, when it builds its session.
+ let mut config = SessionConfig::new();
+ numbers_crate::register_all(&mut config)?;
+ let worker = SessionContext::new_with_config(config);
+
+ let decoded = physical_plan_from_bytes_with_extension_codec(
+ &bytes,
+ worker.task_ctx().as_ref(),
+ &codec,
+ )?;
+ assert_eq!(plan_text(&decoded), plan_text(&plan));
+
+ let batches = collect(decoded, worker.task_ctx()).await?;
+ assert_batches_eq!(
+ [
+ "+--------+",
+ "| scaled |",
+ "+--------+",
+ "| 10 |",
+ "| 20 |",
+ "| 30 |",
+ "| 10 |",
+ "| 20 |",
+ "| 30 |",
+ "+--------+",
+ ],
+ &batches
+ );
+
+ // The same function works on a session that already exists.
Review 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
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! 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:
Review Comment:
I think it would help to list the extension types:
1. data source
2. Scalar Function
3. etc
The plan is neat and helpful
##########
datafusion-examples/examples/proto/extension_crate.rs:
##########
@@ -0,0 +1,595 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! See `main.rs` for how to run it.
+//!
+//! 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:
+//!
+//! ```text
+//! RepeatExec: times=2 <- custom ExecutionPlan
+//! ProjectionExec: [ScaleExpr(value)] <- custom PhysicalExpr in a
built-in plan
+//! DataSourceExec <- built-in, written by DataFusion
+//! FileScanConfig <- built-in, written by DataFusion
+//! NumbersSource <- custom FileSource
+//! ```
+//!
+//! Each custom type does two things:
+//!
+//! | Step | Where | What
|
+//!
|--------|------------------------------|--------------------------------------------|
+//! | Encode | its `try_to_proto*` hook | Writes its name and its own
state |
+//! | 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
+//! it does not need to know which types the crate contains.
+
+use std::sync::Arc;
+
+use datafusion::arrow::array::{Int64Array, RecordBatch};
+use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use datafusion::common::{Result, assert_batches_eq};
+use datafusion::physical_plan::{ExecutionPlan, collect, displayable};
+use datafusion::prelude::{SessionConfig, SessionContext};
+use datafusion_proto::bytes::{
+ physical_plan_from_bytes_with_extension_codec,
+ physical_plan_to_bytes_with_extension_codec,
+};
+use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec;
+
+pub async fn extension_crate() -> Result<()> {
+ let dir = tempfile::tempdir()?;
+ let path = dir.path().join("data.numbers");
+ std::fs::write(&path, "1\n2\n3\n")?;
+ let file_size = std::fs::metadata(&path)?.len();
+
+ // 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 {};
+ let plan = numbers_crate::scaled_and_repeated(path.to_str().unwrap(),
file_size)?;
+ let bytes = physical_plan_to_bytes_with_extension_codec(Arc::clone(&plan),
&codec)?;
+ println!(
+ "Serialized this plan to {} bytes:\n{}",
+ bytes.len(),
+ plan_text(&plan)
+ );
+
+ // The worker registers the crate once, when it builds its session.
+ let mut config = SessionConfig::new();
+ numbers_crate::register_all(&mut config)?;
+ let worker = SessionContext::new_with_config(config);
+
+ let decoded = physical_plan_from_bytes_with_extension_codec(
+ &bytes,
+ worker.task_ctx().as_ref(),
+ &codec,
+ )?;
+ assert_eq!(plan_text(&decoded), plan_text(&plan));
Review Comment:
But the same codec was used on the coordinator 🤔
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]