niebayes commented on code in PR #14723:
URL: https://github.com/apache/datafusion/pull/14723#discussion_r1961488850
##########
datafusion/substrait/src/serializer.rs:
##########
@@ -26,28 +26,39 @@ use substrait::proto::Plan;
use std::fs::OpenOptions;
use std::io::{Read, Write};
+use std::path::Path;
-#[allow(clippy::suspicious_open_options)]
-pub async fn serialize(sql: &str, ctx: &SessionContext, path: &str) ->
Result<()> {
+/// Plans a sql and serializes the generated logical plan to bytes.
+/// The bytes are then written into a file at `path`.
+///
+/// Returns an error if the file already exists.
+pub async fn serialize(
+ sql: &str,
+ ctx: &SessionContext,
+ path: impl AsRef<Path>,
+) -> Result<()> {
let protobuf_out = serialize_bytes(sql, ctx).await;
- let mut file = OpenOptions::new().create(true).write(true).open(path)?;
+
+ let mut file = OpenOptions::new().write(true).create_new(true).open(path)?;
file.write_all(&protobuf_out?)?;
Ok(())
}
+/// Plans a sql and serializes the generated logical plan to bytes.
pub async fn serialize_bytes(sql: &str, ctx: &SessionContext) ->
Result<Vec<u8>> {
let df = ctx.sql(sql).await?;
let plan = df.into_optimized_plan()?;
let proto = producer::to_substrait_plan(&plan, &ctx.state())?;
let mut protobuf_out = Vec::<u8>::new();
- proto.encode(&mut protobuf_out).map_err(|e| {
- DataFusionError::Substrait(format!("Failed to encode substrait plan:
{e}"))
- })?;
+ proto
+ .encode(&mut protobuf_out)
+ .map_err(|e| DataFusionError::Substrait(format!("Failed to encode
plan: {e}")))?;
Ok(protobuf_out)
}
-pub async fn deserialize(path: &str) -> Result<Box<Plan>> {
+/// Reads the file at `path` and deserializes a plan from the bytes.
+pub async fn deserialize(path: impl AsRef<Path>) -> Result<Box<Plan>> {
Review Comment:
I plan to contribute more to the datafusion-substrait crate. Personally, I
would like to reserve the breaking change in one of the following PR.
What do you think? @alamb
--
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]