Skip to content
Merged
Show file tree
Hide file tree
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: 2 additions & 33 deletions datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ impl Unparser<'_> {
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()));
table_parts
.push(self.new_ident_quoted_if_needs(catalog_name.to_string()));
}
if let Some(schema_name) = scan.table_name.schema() {
table_parts
Expand Down Expand Up @@ -515,35 +516,3 @@ 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)
.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\"",
);
}
}
27 changes: 27 additions & 0 deletions datafusion/sql/tests/sql_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use datafusion_common::{
assert_contains, plan_err, DFSchema, DataFusionError, ParamValues, Result,
ScalarValue, TableReference,
};
use datafusion_expr::{col, table_scan};
use datafusion_expr::{
logical_plan::{LogicalPlan, Prepare},
AggregateUDF, ColumnarValue, ScalarUDF, ScalarUDFImpl, Signature, TableSource,
Expand Down Expand Up @@ -4799,6 +4800,32 @@ fn test_unnest_logical_plan() -> Result<()> {
Ok(())
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

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)
.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\"",
);
}

#[cfg(test)]
#[ctor::ctor]
fn init() {
Expand Down