Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ impl Unparser<'_> {
LogicalPlan::TableScan(scan) => {
let mut builder = TableRelationBuilder::default();
let mut table_parts = vec![];
if let Some(catalog_name) = scan.table_name.catalog() {
table_parts.push(self.new_ident(catalog_name.to_string()));
}
if let Some(schema_name) = scan.table_name.schema() {
table_parts.push(self.new_ident(schema_name.to_string()));
}
Expand Down Expand Up @@ -502,3 +505,35 @@ impl From<BuilderError> for DataFusionError {
DataFusionError::External(Box::new(e))
}
}

#[cfg(test)]
mod test {
use crate::unparser::plan_to_sql;
use arrow::datatypes::{DataType, Field, Schema};
use datafusion_expr::{col, logical_plan::table_scan};
#[test]
fn test_table_references_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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is testing plan_to_sql it might make more sense to put it with the rest of the tests for plan_to_sql: https://github.com/apache/datafusion/blob/main/datafusion/sql/tests/sql_integration.rs#L4669

However, the fact that those tests don't have "plan_to_sql" in their name is confusing too.

Maybe we could consolidate the tests into something like datafusion/sql/tests/sql_integration/plan_to_sql.rs 🤔 (as a follow on PR)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #10635 to track

.unwrap()
.project(vec![col("id"), col("value")])
.unwrap()
.build()
.unwrap();
let sql = plan_to_sql(&plan).unwrap();

assert_eq!(format!("{}", sql), expected_sql)
}

test("catalog.schema.table", "SELECT \"catalog\".\"schema\".\"table\".\"id\", \"catalog\".\"schema\".\"table\".\"value\" FROM \"catalog\".\"schema\".\"table\"");
test("schema.table", "SELECT \"schema\".\"table\".\"id\", \"schema\".\"table\".\"value\" FROM \"schema\".\"table\"");
test(
"table",
"SELECT \"table\".\"id\", \"table\".\"value\" FROM \"table\"",
);
}
}