Skip to content

Commit

Permalink
[fix apache#8904] Improved output of datafuson-cli with empty results
Browse files Browse the repository at this point in the history
Signed-off-by: tangruilin <[email protected]>
  • Loading branch information
Tangruilin committed Feb 3, 2024
1 parent c843226 commit f8f218c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand All @@ -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)?;
}
}

Expand Down
20 changes: 19 additions & 1 deletion datafusion-cli/src/print_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit f8f218c

Please sign in to comment.