This is an automated email from the ASF dual-hosted git repository.

comphead pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new e893a2eea4 Minor: Improve documentation in sql_to_plan example (#10582)
e893a2eea4 is described below

commit e893a2eea4dcbb7d079e68e996507d59e1e48952
Author: Andrew Lamb <[email protected]>
AuthorDate: Wed May 22 11:13:16 2024 -0400

    Minor: Improve documentation in sql_to_plan example (#10582)
---
 datafusion-examples/examples/plan_to_sql.rs | 62 +++++++++++++++--------------
 1 file changed, 33 insertions(+), 29 deletions(-)

diff --git a/datafusion-examples/examples/plan_to_sql.rs 
b/datafusion-examples/examples/plan_to_sql.rs
index 0e9485ba7f..8ac6746a31 100644
--- a/datafusion-examples/examples/plan_to_sql.rs
+++ b/datafusion-examples/examples/plan_to_sql.rs
@@ -22,18 +22,28 @@ use datafusion::sql::unparser::expr_to_sql;
 use datafusion_sql::unparser::dialect::CustomDialect;
 use datafusion_sql::unparser::{plan_to_sql, Unparser};
 
-/// This example demonstrates the programmatic construction of
-/// SQL using the DataFusion Expr [`Expr`] and LogicalPlan [`LogicalPlan`] API.
+/// This example demonstrates the programmatic construction of SQL strings 
using
+/// the DataFusion Expr [`Expr`] and LogicalPlan [`LogicalPlan`] API.
 ///
 ///
 /// The code in this example shows how to:
-/// 1. Create SQL from a variety of Expr and LogicalPlan: [`main`]`
-/// 2. Create a simple expression [`Exprs`] with fluent API
-/// and convert to sql: [`simple_expr_to_sql_demo`]
-/// 3. Create a simple expression [`Exprs`] with fluent API
-/// and convert to sql without escaping column names: 
[`simple_expr_to_sql_demo_no_escape`]
-/// 4. Create a simple expression [`Exprs`] with fluent API
-/// and convert to sql escaping column names a MySQL style: 
[`simple_expr_to_sql_demo_escape_mysql_style`]
+///
+/// 1. [`simple_expr_to_sql_demo`]: Create a simple expression [`Exprs`] with
+/// fluent API and convert to sql suitable for passing to another database
+///
+/// 2. [`simple_expr_to_sql_demo_no_escape`]  Create a simple expression
+/// [`Exprs`] with fluent API and convert to sql without escaping column names
+/// more suitable for displaying to humans.
+///
+/// 3. [`simple_expr_to_sql_demo_escape_mysql_style`]" Create a simple
+/// expression [`Exprs`] with fluent API and convert to sql escaping column
+/// names in MySQL style.
+///
+/// 4. [`simple_plan_to_sql_demo`]: Create a simple logical plan using the
+/// DataFrames API and convert to sql string.
+///
+/// 5. [`round_trip_plan_to_sql_demo`]: Create a logical plan from a SQL 
string, modify it using the
+/// DataFrames API and convert it back to a  sql string.
 
 #[tokio::main]
 async fn main() -> Result<()> {
@@ -41,8 +51,8 @@ async fn main() -> Result<()> {
     simple_expr_to_sql_demo()?;
     simple_expr_to_sql_demo_no_escape()?;
     simple_expr_to_sql_demo_escape_mysql_style()?;
-    simple_plan_to_sql_parquest_dataframe_demo().await?;
-    round_trip_plan_to_sql_parquest_dataframe_demo().await?;
+    simple_plan_to_sql_demo().await?;
+    round_trip_plan_to_sql_demo().await?;
     Ok(())
 }
 
@@ -50,8 +60,7 @@ async fn main() -> Result<()> {
 /// PostgreSQL style.
 fn simple_expr_to_sql_demo() -> Result<()> {
     let expr = col("a").lt(lit(5)).or(col("a").eq(lit(8)));
-    let ast = expr_to_sql(&expr)?;
-    let sql = format!("{}", ast);
+    let sql = expr_to_sql(&expr)?.to_string();
     assert_eq!(sql, r#"(("a" < 5) OR ("a" = 8))"#);
     Ok(())
 }
@@ -62,8 +71,7 @@ fn simple_expr_to_sql_demo_no_escape() -> Result<()> {
     let expr = col("a").lt(lit(5)).or(col("a").eq(lit(8)));
     let dialect = CustomDialect::new(None);
     let unparser = Unparser::new(&dialect);
-    let ast = unparser.expr_to_sql(&expr)?;
-    let sql = format!("{}", ast);
+    let sql = unparser.expr_to_sql(&expr)?.to_string();
     assert_eq!(sql, r#"((a < 5) OR (a = 8))"#);
     Ok(())
 }
@@ -74,16 +82,14 @@ fn simple_expr_to_sql_demo_escape_mysql_style() -> 
Result<()> {
     let expr = col("a").lt(lit(5)).or(col("a").eq(lit(8)));
     let dialect = CustomDialect::new(Some('`'));
     let unparser = Unparser::new(&dialect);
-    let ast = unparser.expr_to_sql(&expr)?;
-    let sql = format!("{}", ast);
+    let sql = unparser.expr_to_sql(&expr)?.to_string();
     assert_eq!(sql, r#"((`a` < 5) OR (`a` = 8))"#);
     Ok(())
 }
 
 /// DataFusion can convert a logic plan created using the DataFrames API to 
read from a parquet file
 /// to SQL, using column name escaping PostgreSQL style.
-async fn simple_plan_to_sql_parquest_dataframe_demo() -> Result<()> {
-    // create local execution context
+async fn simple_plan_to_sql_demo() -> Result<()> {
     let ctx = SessionContext::new();
 
     let testdata = datafusion::test_util::parquet_test_data();
@@ -95,9 +101,8 @@ async fn simple_plan_to_sql_parquest_dataframe_demo() -> 
Result<()> {
         .await?
         .select_columns(&["id", "int_col", "double_col", "date_string_col"])?;
 
-    let ast = plan_to_sql(df.logical_plan())?;
-
-    let sql = format!("{}", ast);
+    // Convert the data frame to a SQL string
+    let sql = plan_to_sql(df.logical_plan())?.to_string();
 
     assert_eq!(
         sql,
@@ -107,9 +112,9 @@ async fn simple_plan_to_sql_parquest_dataframe_demo() -> 
Result<()> {
     Ok(())
 }
 
-// DataFusion could parse a SQL into a DataFrame, adding a Filter, and 
converting that back to sql.
-async fn round_trip_plan_to_sql_parquest_dataframe_demo() -> Result<()> {
-    // create local execution context
+/// DataFusion can also be used to parse SQL, programmatically modify the query
+/// (in this case adding a filter) and then and converting back to SQL.
+async fn round_trip_plan_to_sql_demo() -> Result<()> {
     let ctx = SessionContext::new();
 
     let testdata = datafusion::test_util::parquet_test_data();
@@ -124,21 +129,20 @@ async fn round_trip_plan_to_sql_parquest_dataframe_demo() 
-> Result<()> {
 
     // create a logical plan from a SQL string and then programmatically add 
new filters
     let df = ctx
+        // Use SQL to read some data from the parquet file
         .sql(
             "SELECT int_col, double_col, CAST(date_string_col as VARCHAR) \
         FROM alltypes_plain",
         )
         .await?
+        // Add id > 1 and tinyint_col < double_col filter
         .filter(
             col("id")
                 .gt(lit(1))
                 .and(col("tinyint_col").lt(col("double_col"))),
         )?;
 
-    let ast = plan_to_sql(df.logical_plan())?;
-
-    let sql = format!("{}", ast);
-
+    let sql = plan_to_sql(df.logical_plan())?.to_string();
     assert_eq!(
         sql,
         r#"SELECT "alltypes_plain"."int_col", "alltypes_plain"."double_col", 
CAST("alltypes_plain"."date_string_col" AS VARCHAR) FROM "alltypes_plain" WHERE 
(("alltypes_plain"."id" > 1) AND ("alltypes_plain"."tinyint_col" < 
"alltypes_plain"."double_col"))"#


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to