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

alamb 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 1ce546168d Fix `plan_to_sql`: Add wildcard projection to SELECT 
statement if no projection was set (#11744)
1ce546168d is described below

commit 1ce546168de23137dbe30b70b7f948d131681a00
Author: Yasser Latreche <[email protected]>
AuthorDate: Thu Aug 1 13:31:34 2024 +0200

    Fix `plan_to_sql`: Add wildcard projection to SELECT statement if no 
projection was set (#11744)
    
    * Fix: Add wildcard projection to SELECT statement if no projection is set
    
    * run cargo fmt
---
 datafusion/sql/src/unparser/plan.rs       |  8 ++++++++
 datafusion/sql/tests/cases/plan_to_sql.rs | 24 ++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/datafusion/sql/src/unparser/plan.rs 
b/datafusion/sql/src/unparser/plan.rs
index b30e109881..e08f25d3c2 100644
--- a/datafusion/sql/src/unparser/plan.rs
+++ b/datafusion/sql/src/unparser/plan.rs
@@ -142,6 +142,14 @@ impl Unparser<'_> {
             return Ok(*body);
         }
 
+        // If no projection is set, add a wildcard projection to the select
+        // which will be translated to `SELECT *` in the SQL statement
+        if !select_builder.already_projected() {
+            select_builder.projection(vec![ast::SelectItem::Wildcard(
+                ast::WildcardAdditionalOptions::default(),
+            )]);
+        }
+
         let mut twj = select_builder.pop_from().unwrap();
         twj.relation(relation_builder);
         select_builder.push_from(twj);
diff --git a/datafusion/sql/tests/cases/plan_to_sql.rs 
b/datafusion/sql/tests/cases/plan_to_sql.rs
index a52333e54f..d1ac7a0c96 100644
--- a/datafusion/sql/tests/cases/plan_to_sql.rs
+++ b/datafusion/sql/tests/cases/plan_to_sql.rs
@@ -449,6 +449,30 @@ fn test_table_references_in_plan_to_sql() {
     );
 }
 
+#[test]
+fn test_table_scan_with_no_projection_in_plan_to_sql() {
+    fn test(table_name: &str, expected_sql: &str) {
+        let schema = Schema::new(vec![
+            Field::new("id", DataType::Utf8, false),
+            Field::new("value", DataType::Utf8, false),
+        ]);
+
+        let plan = table_scan(Some(table_name), &schema, None)
+            .unwrap()
+            .build()
+            .unwrap();
+        let sql = plan_to_sql(&plan).unwrap();
+        assert_eq!(format!("{}", sql), expected_sql)
+    }
+
+    test(
+        "catalog.schema.table",
+        "SELECT * FROM catalog.\"schema\".\"table\"",
+    );
+    test("schema.table", "SELECT * FROM \"schema\".\"table\"");
+    test("table", "SELECT * FROM \"table\"");
+}
+
 #[test]
 fn test_pretty_roundtrip() -> Result<()> {
     let schema = Schema::new(vec![


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

Reply via email to