Skip to content

Apache DataFusion SQL Query Engine

distinguish none and empty projection (#14116)

Xiangpeng Hao53dab469d11a
diff --git a/datafusion/sql/src/unparser/plan.rs b/datafusion/sql/src/unparser/plan.rs
index 2bad683dc1..2e02baa4f7 100644
--- a/datafusion/sql/src/unparser/plan.rs
+++ b/datafusion/sql/src/unparser/plan.rs
@@ -41,7 +41,7 @@ use crate::utils::UNNEST_PLACEHOLDER;
use datafusion_common::{
internal_err, not_impl_err,
tree_node::{TransformedResult, TreeNode},
- Column, DataFusionError, Result, TableReference,
+ Column, DataFusionError, Result, ScalarValue, TableReference,
};
use datafusion_expr::expr::OUTER_REFERENCE_COLUMN_PREFIX;
use datafusion_expr::{
@@ -919,23 +919,29 @@ impl Unparser<'_> {
// information included in the TableScan node.
if !already_projected {
if let Some(project_vec) = &table_scan.projection {
- let project_columns = project_vec
- .iter()
- .cloned()
- .map(|i| {
- let schema = table_scan.source.schema();
- let field = schema.field(i);
- if alias.is_some() {
- Column::new(alias.clone(), field.name().clone())
- } else {
- Column::new(
- Some(table_scan.table_name.clone()),
- field.name().clone(),
- )
- }
- })
- .collect::<Vec<_>>();
- builder = builder.project(project_columns)?;
+ if project_vec.is_empty() {
+ builder = builder.project(vec![Expr::Literal(
+ ScalarValue::Int64(Some(1)),
+ )])?;
+ } else {
+ let project_columns = project_vec
+ .iter()
+ .cloned()
+ .map(|i| {
+ let schema = table_scan.source.schema();
+ let field = schema.field(i);
+ if alias.is_some() {
+ Column::new(alias.clone(), field.name().clone())
+ } else {
+ Column::new(
+ Some(table_scan.table_name.clone()),
+ field.name().clone(),
+ )
+ }
+ })
+ .collect::<Vec<_>>();
+ builder = builder.project(project_columns)?;
+ };
}
}
diff --git a/datafusion/sql/tests/cases/plan_to_sql.rs b/datafusion/sql/tests/cases/plan_to_sql.rs
index 94b4df59ef..80af4d367b 100644
--- a/datafusion/sql/tests/cases/plan_to_sql.rs
+++ b/datafusion/sql/tests/cases/plan_to_sql.rs
@@ -792,7 +792,7 @@ fn test_table_references_in_plan_to_sql() {
}
#[test]
-fn test_table_scan_with_no_projection_in_plan_to_sql() {
+fn test_table_scan_with_none_projection_in_plan_to_sql() {
fn test(table_name: &str, expected_sql: &str) {
let schema = Schema::new(vec![
Field::new("id", DataType::Utf8, false),
@@ -815,6 +815,30 @@ fn test_table_scan_with_no_projection_in_plan_to_sql() {
test("table", r#"SELECT * FROM "table""#);
}
+#[test]
+fn test_table_scan_with_empty_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, Some(vec![]))
+ .unwrap()
+ .build()
+ .unwrap();
+ let sql = plan_to_sql(&plan).unwrap();
+ assert_eq!(sql.to_string(), expected_sql)
+ }
+
+ test(
+ "catalog.schema.table",
+ r#"SELECT 1 FROM "catalog"."schema"."table""#,
+ );
+ test("schema.table", r#"SELECT 1 FROM "schema"."table""#);
+ test("table", r#"SELECT 1 FROM "table""#);
+}
+
#[test]
fn test_pretty_roundtrip() -> Result<()> {
let schema = Schema::new(vec![