mbrobbel commented on code in PR #14723:
URL: https://github.com/apache/datafusion/pull/14723#discussion_r1959330030
##########
datafusion/substrait/src/serializer.rs:
##########
@@ -26,9 +26,30 @@ use substrait::proto::Plan;
use std::fs::OpenOptions;
use std::io::{Read, Write};
+use std::path::Path;
-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 and is not empty.
+pub async fn serialize(
+ sql: &str,
+ ctx: &SessionContext,
+ path: impl AsRef<Path>,
+) -> Result<()> {
let protobuf_out = serialize_bytes(sql, ctx).await;
+
+ match std::fs::metadata(path.as_ref()) {
+ Ok(meta) if meta.len() > 0 => {
+ return Err(DataFusionError::Substrait(format!(
+ "Failed to encode substrait plan: the file {} already exists
and is not empty",
Review Comment:
You could remove `substrait` here because it's already part of the error
prefix
https://github.com/apache/datafusion/blob/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/datafusion/common/src/error.rs#L517
```suggestion
"Failed to encode plan: the file {} already exists and is
not empty",
```
##########
datafusion/substrait/src/serializer.rs:
##########
@@ -26,9 +26,30 @@ use substrait::proto::Plan;
use std::fs::OpenOptions;
use std::io::{Read, Write};
+use std::path::Path;
-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 and is not empty.
+pub async fn serialize(
+ sql: &str,
+ ctx: &SessionContext,
+ path: impl AsRef<Path>,
+) -> Result<()> {
let protobuf_out = serialize_bytes(sql, ctx).await;
+
+ match std::fs::metadata(path.as_ref()) {
+ Ok(meta) if meta.len() > 0 => {
+ return Err(DataFusionError::Substrait(format!(
+ "Failed to encode substrait plan: the file {} already exists
and is not empty",
+ path.as_ref().display())
+ ));
+ }
+ // Ignores other cases.
+ _ => {}
+ }
Review Comment:
```suggestion
if std::fs::metadata(path.as_ref()).is_ok_and(|meta| meta.len() > 0) {
return Err(DataFusionError::Substrait(format!(
"Failed to encode substrait plan: the file {} already exists and
is not empty",
path.as_ref().display())
));
}
```
##########
datafusion/substrait/src/serializer.rs:
##########
@@ -50,6 +72,7 @@ pub async fn serialize_bytes(sql: &str, ctx: &SessionContext)
-> Result<Vec<u8>>
Ok(protobuf_out)
}
+/// Reads the file at `path` and deserializes a plan from the bytes.
pub async fn deserialize(path: &str) -> Result<Box<Plan>> {
Review Comment:
```suggestion
/// Reads the file at `path` and deserializes a plan from the bytes.
pub async fn deserialize(path: impl AsRef<Path>) -> Result<Box<Plan>> {
```
--
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]