alamb commented on code in PR #13880:
URL: https://github.com/apache/datafusion/pull/13880#discussion_r1896789436


##########
datafusion-examples/examples/plan_to_sql.rs:
##########
@@ -152,3 +172,144 @@ async fn round_trip_plan_to_sql_demo() -> Result<()> {
 
     Ok(())
 }
+
+#[derive(Debug, PartialEq, Eq, Hash, PartialOrd)]
+struct MyLogicalPlan {
+    input: LogicalPlan,
+}
+
+impl UserDefinedLogicalNodeCore for MyLogicalPlan {
+    fn name(&self) -> &str {
+        "MyLogicalPlan"
+    }
+
+    fn inputs(&self) -> Vec<&LogicalPlan> {
+        vec![&self.input]
+    }
+
+    fn schema(&self) -> &DFSchemaRef {
+        self.input.schema()
+    }
+
+    fn expressions(&self) -> Vec<Expr> {
+        vec![]
+    }
+
+    fn fmt_for_explain(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "MyLogicalPlan")
+    }
+
+    fn with_exprs_and_inputs(
+        &self,
+        _exprs: Vec<Expr>,
+        inputs: Vec<LogicalPlan>,
+    ) -> Result<Self> {
+        Ok(MyLogicalPlan {
+            input: inputs.into_iter().next().unwrap(),
+        })
+    }
+}
+
+struct PlanToStatement {}
+impl UserDefinedLogicalNodeUnparser for PlanToStatement {
+    fn unparse_to_statement(
+        &self,
+        node: &dyn UserDefinedLogicalNode,
+        unparser: &Unparser,
+    ) -> Result<UnparseToStatementResult> {
+        if let Some(plan) = node.as_any().downcast_ref::<MyLogicalPlan>() {
+            let input = unparser.plan_to_sql(&plan.input)?;
+            Ok(UnparseToStatementResult::Modified(input))
+        } else {
+            Ok(UnparseToStatementResult::Unmodified)
+        }
+    }
+}
+
+/// This example demonstrates how to unparse a custom logical plan as a 
statement.
+/// The custom logical plan is a simple extension of the logical plan that 
reads from a parquet file.
+/// It can be unparse as a statement that reads from the same parquet file.
+async fn unparse_my_logical_plan_as_statement() -> Result<()> {

Review Comment:
   this is very cool



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to