Skip to content

Commit 7757d63

Browse files
add catalog as part of the table path in plan_to_sql (#10612)
* add catalog as part of the table path in plan_to_sql * Update datafusion/sql/src/unparser/plan.rs Co-authored-by: Phillip LeBlanc <[email protected]> --------- Co-authored-by: Phillip LeBlanc <[email protected]>
1 parent 7bd4b53 commit 7757d63

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

datafusion/sql/src/unparser/plan.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ impl Unparser<'_> {
140140
LogicalPlan::TableScan(scan) => {
141141
let mut builder = TableRelationBuilder::default();
142142
let mut table_parts = vec![];
143+
if let Some(catalog_name) = scan.table_name.catalog() {
144+
table_parts.push(self.new_ident(catalog_name.to_string()));
145+
}
143146
if let Some(schema_name) = scan.table_name.schema() {
144147
table_parts
145148
.push(self.new_ident_quoted_if_needs(schema_name.to_string()));
@@ -505,3 +508,35 @@ impl From<BuilderError> for DataFusionError {
505508
DataFusionError::External(Box::new(e))
506509
}
507510
}
511+
512+
#[cfg(test)]
513+
mod test {
514+
use crate::unparser::plan_to_sql;
515+
use arrow::datatypes::{DataType, Field, Schema};
516+
use datafusion_expr::{col, logical_plan::table_scan};
517+
#[test]
518+
fn test_table_references_in_plan_to_sql() {
519+
fn test(table_name: &str, expected_sql: &str) {
520+
let schema = Schema::new(vec![
521+
Field::new("id", DataType::Utf8, false),
522+
Field::new("value", DataType::Utf8, false),
523+
]);
524+
let plan = table_scan(Some(table_name), &schema, None)
525+
.unwrap()
526+
.project(vec![col("id"), col("value")])
527+
.unwrap()
528+
.build()
529+
.unwrap();
530+
let sql = plan_to_sql(&plan).unwrap();
531+
532+
assert_eq!(format!("{}", sql), expected_sql)
533+
}
534+
535+
test("catalog.schema.table", "SELECT \"catalog\".\"schema\".\"table\".\"id\", \"catalog\".\"schema\".\"table\".\"value\" FROM \"catalog\".\"schema\".\"table\"");
536+
test("schema.table", "SELECT \"schema\".\"table\".\"id\", \"schema\".\"table\".\"value\" FROM \"schema\".\"table\"");
537+
test(
538+
"table",
539+
"SELECT \"table\".\"id\", \"table\".\"value\" FROM \"table\"",
540+
);
541+
}
542+
}

0 commit comments

Comments
 (0)