Skip to content

Commit 3d97c8e

Browse files
authored
fix: avoid send empty record batch to client (apache#796)
1 parent e01e891 commit 3d97c8e

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

common_types/src/record_batch.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.
1+
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
22

33
//! Record batch
44
@@ -234,23 +234,33 @@ impl RecordBatch {
234234
&self.schema
235235
}
236236

237+
#[inline]
238+
pub fn is_empty(&self) -> bool {
239+
self.num_rows() == 0
240+
}
241+
237242
// REQUIRE: index is valid
243+
#[inline]
238244
pub fn column(&self, index: usize) -> &ColumnBlock {
239245
&self.data.column_blocks[index]
240246
}
241247

248+
#[inline]
242249
pub fn num_columns(&self) -> usize {
243250
self.schema.num_columns()
244251
}
245252

253+
#[inline]
246254
pub fn num_rows(&self) -> usize {
247255
self.data.num_rows()
248256
}
249257

258+
#[inline]
250259
pub fn as_arrow_record_batch(&self) -> &ArrowRecordBatch {
251260
&self.data.arrow_record_batch
252261
}
253262

263+
#[inline]
254264
pub fn into_arrow_record_batch(self) -> ArrowRecordBatch {
255265
self.data.arrow_record_batch
256266
}

components/arrow_ext/src/ipc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.
1+
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
22

33
//! Utilities for `RecordBatch` serialization using Arrow IPC
44
@@ -109,6 +109,7 @@ impl RecordBatchesEncoder {
109109
let stream_writer = if let Some(v) = &mut self.stream_writer {
110110
v
111111
} else {
112+
// TODO: pre-allocate the buffer.
112113
let buffer: Vec<u8> = Vec::new();
113114
let stream_writer =
114115
StreamWriter::try_new(buffer, &batch.schema()).context(ArrowError)?;

server/src/proxy/grpc/sql_query.rs

+4
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ impl QueryResponseWriter {
453453
}
454454

455455
pub fn write(&mut self, batch: &RecordBatch) -> Result<()> {
456+
if batch.is_empty() {
457+
return Ok(());
458+
}
459+
456460
self.encoder
457461
.write(batch.as_arrow_record_batch())
458462
.box_err()

0 commit comments

Comments
 (0)