Skip to content

Commit 02c948d

Browse files
authored
[MINOR]: Limit stream replace with slice (#9303)
* Initial commit * Minor changes
1 parent 91f3eb2 commit 02c948d

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

datafusion/physical-plan/src/limit.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ use crate::{
2929
DisplayFormatType, Distribution, EquivalenceProperties, ExecutionPlan, Partitioning,
3030
};
3131

32-
use arrow::array::ArrayRef;
3332
use arrow::datatypes::SchemaRef;
34-
use arrow::record_batch::{RecordBatch, RecordBatchOptions};
33+
use arrow::record_batch::RecordBatch;
3534
use datafusion_common::stats::Precision;
3635
use datafusion_common::{internal_err, DataFusionError, Result};
3736
use datafusion_execution::TaskContext;
@@ -507,26 +506,15 @@ impl LimitStream {
507506
//
508507
self.fetch -= batch.num_rows();
509508
Some(batch)
510-
} else {
509+
} else if batch.num_rows() >= self.fetch {
511510
let batch_rows = self.fetch;
512511
self.fetch = 0;
513512
self.input = None; // clear input so it can be dropped early
514513

515-
let limited_columns: Vec<ArrayRef> = batch
516-
.columns()
517-
.iter()
518-
.map(|col| col.slice(0, col.len().min(batch_rows)))
519-
.collect();
520-
let options =
521-
RecordBatchOptions::new().with_row_count(Option::from(batch_rows));
522-
Some(
523-
RecordBatch::try_new_with_options(
524-
batch.schema(),
525-
limited_columns,
526-
&options,
527-
)
528-
.unwrap(),
529-
)
514+
// It is guaranteed that batch_rows is <= batch.num_rows
515+
Some(batch.slice(0, batch_rows))
516+
} else {
517+
unreachable!()
530518
}
531519
}
532520
}
@@ -575,6 +563,7 @@ mod tests {
575563
use crate::{common, test};
576564

577565
use crate::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy};
566+
use arrow_array::RecordBatchOptions;
578567
use arrow_schema::Schema;
579568
use datafusion_physical_expr::expressions::col;
580569
use datafusion_physical_expr::PhysicalExpr;

0 commit comments

Comments
 (0)