Skip to content

Commit

Permalink
fix cli json print
Browse files Browse the repository at this point in the history
  • Loading branch information
houqp committed Jan 20, 2022
1 parent 1e352c3 commit aaaed9b
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions datafusion-cli/src/print_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,21 @@ impl fmt::Display for PrintFormat {
}

fn print_batches_to_json<J: JsonFormat>(batches: &[RecordBatch]) -> Result<String> {
use arrow::io::json::write as json_write;

if batches.is_empty() {
return Ok("{}".to_string());
}
let mut bytes = vec![];
let schema = batches[0].schema();
let names = schema
.fields
.iter()
.map(|f| f.name.clone())
.collect::<Vec<String>>();
for batch in batches {
arrow::io::json::write::serialize(
&names,
batch.columns(),
J::default(),
&mut bytes,
);
}

let format = J::default();
let blocks = json_write::Serializer::new(
batches.iter().map(|r| Ok(r.clone())),
vec![],
format,
);
json_write::write(&mut bytes, format, blocks)?;

let formatted = String::from_utf8(bytes)
.map_err(|e| DataFusionError::Execution(e.to_string()))?;
Ok(formatted)
Expand Down

0 comments on commit aaaed9b

Please sign in to comment.