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


##########
datafusion-examples/examples/plan_to_sql.rs:
##########
@@ -77,3 +81,132 @@ fn simple_expr_to_sql_demo_escape_mysql_style() -> 
Result<()> {
     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
+    let ctx = SessionContext::new(); // define the query using the DataFrame 
trait
+
+    let testdata = datafusion::test_util::parquet_test_data();
+    let df = ctx
+        .read_parquet(
+            &format!("{testdata}/alltypes_plain.parquet"),
+            ParquetReadOptions::default(),
+        )
+        .await?
+        .select_columns(&["id", "int_col", "double_col", "date_string_col"])?;
+
+    let ast = plan_to_sql(df.logical_plan())?;
+
+    let sql = format!("{}", ast);
+
+    assert_eq!(
+        sql,
+        r#"SELECT "?table?"."id", "?table?"."int_col", "?table?"."double_col", 
"?table?"."date_string_col" FROM "?table?""#
+    );
+
+    Ok(())
+}
+
+/// DataFusion can convert a logic plan created using the DataFrames API to 
read from a csv file

Review Comment:
   I am not sure we need to have both parquet and csv demos -- one or the other 
serves as a good demonstration I think



##########
datafusion-examples/examples/plan_to_sql.rs:
##########
@@ -77,3 +81,132 @@ fn simple_expr_to_sql_demo_escape_mysql_style() -> 
Result<()> {
     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
+    let ctx = SessionContext::new(); // define the query using the DataFrame 
trait
+
+    let testdata = datafusion::test_util::parquet_test_data();
+    let df = ctx
+        .read_parquet(
+            &format!("{testdata}/alltypes_plain.parquet"),
+            ParquetReadOptions::default(),
+        )
+        .await?
+        .select_columns(&["id", "int_col", "double_col", "date_string_col"])?;
+
+    let ast = plan_to_sql(df.logical_plan())?;
+
+    let sql = format!("{}", ast);
+
+    assert_eq!(
+        sql,
+        r#"SELECT "?table?"."id", "?table?"."int_col", "?table?"."double_col", 
"?table?"."date_string_col" FROM "?table?""#
+    );
+
+    Ok(())
+}
+
+/// DataFusion can convert a logic plan created using the DataFrames API to 
read from a csv file
+/// to SQL, using column name escaping PostgreSQL style.
+async fn simple_plan_to_sql_csv_dataframe_demo() -> Result<()> {
+    // create local execution context
+    let ctx = SessionContext::new(); // define the query using the DataFrame 
trait
+
+    let testdata = datafusion::test_util::arrow_test_data();
+    let df = ctx
+        .read_csv(
+            &format!("{testdata}/csv/aggregate_test_100.csv"),
+            CsvReadOptions::default(),
+        )
+        .await?
+        .select(vec![col("c1"), min(col("c12")), max(col("c12"))])?;
+
+    let ast = plan_to_sql(df.logical_plan())?;
+
+    let sql = format!("{}", ast);
+
+    assert_eq!(
+        sql,
+        r#"SELECT "?table?"."c1", MIN("?table?"."c12"), MAX("?table?"."c12") 
FROM "?table?""#
+    );
+
+    Ok(())
+}
+
+async fn round_trip_plan_to_sql_parquest_dataframe_demo() -> Result<()> {
+    // create local execution context
+    let ctx = SessionContext::new(); // define the query using the DataFrame 
trait
+
+    let testdata = datafusion::test_util::parquet_test_data();
+
+    // register parquet file with the execution context
+    ctx.register_parquet(
+        "alltypes_plain",
+        &format!("{testdata}/alltypes_plain.parquet"),
+        ParquetReadOptions::default(),
+    )
+    .await?;
+
+    // execute the query

Review Comment:
   I don't think this executes a query -- it does something more like
   
   ```suggestion
       // create a logical plan from a SQL string and then programmatically add 
new filters
   ```



-- 
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]

Reply via email to