Skip to content

Commit e24d775

Browse files
committed
fix cli json print
1 parent 1e352c3 commit e24d775

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

datafusion-cli/src/print_format.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,21 @@ impl fmt::Display for PrintFormat {
7171
}
7272

7373
fn print_batches_to_json<J: JsonFormat>(batches: &[RecordBatch]) -> Result<String> {
74+
use arrow::io::json::write as json_write;
75+
7476
if batches.is_empty() {
7577
return Ok("{}".to_string());
7678
}
7779
let mut bytes = vec![];
78-
let schema = batches[0].schema();
79-
let names = schema
80-
.fields
81-
.iter()
82-
.map(|f| f.name.clone())
83-
.collect::<Vec<String>>();
84-
for batch in batches {
85-
arrow::io::json::write::serialize(
86-
&names,
87-
batch.columns(),
88-
J::default(),
89-
&mut bytes,
90-
);
91-
}
80+
81+
let format = J::default();
82+
let blocks = json_write::Serializer::new(
83+
batches.into_iter().map(|r| Ok(r.clone())),
84+
vec![],
85+
format,
86+
);
87+
json_write::write(&mut bytes, format, blocks)?;
88+
9289
let formatted = String::from_utf8(bytes)
9390
.map_err(|e| DataFusionError::Execution(e.to_string()))?;
9491
Ok(formatted)

0 commit comments

Comments
 (0)