@@ -1987,6 +1987,9 @@ Status SegmentIterator::_next_batch_internal(vectorized::Block* block) {
1987
1987
if (UNLIKELY (!_lazy_inited)) {
1988
1988
RETURN_IF_ERROR (_lazy_init ());
1989
1989
_lazy_inited = true ;
1990
+ // If the row bitmap size is smaller than block_row_max, there's no need to reserve that many column rows.
1991
+ auto nrows_reserve_limit =
1992
+ std::min (_row_bitmap.cardinality (), uint64_t (_opts.block_row_max ));
1990
1993
if (_lazy_materialization_read || _opts.record_rowids || _is_need_expr_eval) {
1991
1994
_block_rowids.resize (_opts.block_row_max );
1992
1995
}
@@ -2011,7 +2014,7 @@ Status SegmentIterator::_next_batch_internal(vectorized::Block* block) {
2011
2014
storage_column_type->is_nullable (), _opts.io_ctx .reader_type ));
2012
2015
_current_return_columns[cid]->set_rowset_segment_id (
2013
2016
{_segment->rowset_id (), _segment->id ()});
2014
- _current_return_columns[cid]->reserve (_opts. block_row_max );
2017
+ _current_return_columns[cid]->reserve (nrows_reserve_limit );
2015
2018
} else if (i >= block->columns ()) {
2016
2019
// if i >= block->columns means the column and not the pred_column means `column i` is
2017
2020
// a delete condition column. but the column is not effective in the segment. so we just
@@ -2022,7 +2025,7 @@ Status SegmentIterator::_next_batch_internal(vectorized::Block* block) {
2022
2025
// TODO: skip read the not effective delete column to speed up segment read.
2023
2026
_current_return_columns[cid] =
2024
2027
Schema::get_data_type_ptr (*column_desc)->create_column ();
2025
- _current_return_columns[cid]->reserve (_opts. block_row_max );
2028
+ _current_return_columns[cid]->reserve (nrows_reserve_limit );
2026
2029
}
2027
2030
}
2028
2031
@@ -2047,7 +2050,8 @@ Status SegmentIterator::_next_batch_internal(vectorized::Block* block) {
2047
2050
if (_can_opt_topn_reads ()) {
2048
2051
nrows_read_limit = std::min (static_cast <uint32_t >(_opts.topn_limit ), nrows_read_limit);
2049
2052
}
2050
-
2053
+ // If the row bitmap size is smaller than nrows_read_limit, there's no need to reserve that many column rows.
2054
+ nrows_read_limit = std::min (_row_bitmap.cardinality (), uint64_t (nrows_read_limit));
2051
2055
DBUG_EXECUTE_IF (" segment_iterator.topn_opt_1" , {
2052
2056
if (nrows_read_limit != 1 ) {
2053
2057
return Status::Error<ErrorCode::INTERNAL_ERROR>(" topn opt 1 execute failed: {}" ,
0 commit comments