diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rs index a175f99a90d8..84fd0e2982a7 100644 --- a/datafusion-cli/src/exec.rs +++ b/datafusion-cli/src/exec.rs @@ -239,6 +239,7 @@ async fn exec_and_print( let df = ctx.execute_logical_plan(plan).await?; let physical_plan = df.create_physical_plan().await?; + let schema = physical_plan.schema(); if is_plan_streaming(&physical_plan)? { let stream = execute_stream(physical_plan, task_ctx.clone())?; @@ -252,7 +253,7 @@ async fn exec_and_print( print_options.format = PrintFormat::Table; } let results = collect(physical_plan, task_ctx.clone()).await?; - print_options.print_batches(&results, now)?; + print_options.print_batches_with_empty_result(&results, schema, now)?; } } diff --git a/datafusion-cli/src/print_options.rs b/datafusion-cli/src/print_options.rs index f8cd9b3258ef..dd99d913b49c 100644 --- a/datafusion-cli/src/print_options.rs +++ b/datafusion-cli/src/print_options.rs @@ -23,7 +23,8 @@ use std::time::Instant; use crate::print_format::PrintFormat; -use arrow::record_batch::RecordBatch; +use arrow::datatypes::SchemaRef; +use arrow::record_batch::{RecordBatch, RecordBatchOptions}; use datafusion::common::DataFusionError; use datafusion::error::Result; use datafusion::physical_plan::RecordBatchStream; @@ -94,6 +95,23 @@ fn get_timing_info_str( } impl PrintOptions { + pub fn print_batches_with_empty_result( + &self, + batches: &[RecordBatch], + schema: SchemaRef, + query_start_time: Instant, + ) -> Result<()> { + if batches.len() == 0 { + let option = RecordBatchOptions::new().with_match_field_names(false); + + return self.print_batches( + &[RecordBatch::try_new_with_options(schema, vec![], &option)?], + query_start_time, + ); + }; + return self.print_batches(batches, query_start_time); + } + /// Print the batches to stdout using the specified format pub fn print_batches( &self, diff --git a/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs b/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs index a86c76b9b6dd..a4e2c7cb3924 100644 --- a/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs +++ b/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs @@ -209,8 +209,10 @@ async fn scalar_udf_zero_params() -> Result<()> { let result = plan_and_collect(&ctx, "select get_100() from t where a=999").await?; let expected = [ - "++", // - "++", + "+-----------+", + "| get_100() |", + "+-----------+", + "+-----------+", ]; assert_batches_eq!(expected, &result); Ok(())