/// Applies finishing actions to a row set,
/// and unrolls it to a unary representation.
Applies finishing actions to a row set,
and unrolls it to a unary representation.
Applies finishing actions to a row set and unrolls it to a unary representation .
pub fn finish(&self, mut rows: Vec<(Row, NonZeroUsize)>) -> Vec<Row> {
        let mut left_datum_vec = mz_repr::DatumVec::new();
        let mut right_datum_vec = mz_repr::DatumVec::new();
        let sort_by = |(left, _): &(Row, _), (right, _): &(Row, _)| {
            let left_datums = left_datum_vec.borrow_with(left);
            let right_datums = right_datum_vec.borrow_with(right);
            compare_columns(&self.order_by, &left_datums, &right_datums, || {
                left.cmp(&right)
            })
        };
        rows.sort_by(sort_by);
        let (offset_nth_row, offset_kth_copy) = self.find_offset(&rows);
        // Adjust the first returned row's count to account for the offset.
        if let Some((_, nth_diff)) = rows.get_mut(offset_nth_row) {
            // Justification for `unwrap`:
            // we only get here if we return from `get_offset`
            // having found something to return,
            // which can only happen if the count of
            // this row is greater than the offset remaining.
            *nth_diff = NonZeroUsize::new(nth_diff.get() - offset_kth_copy).unwrap();
        }
        let limit = self.limit.unwrap_or(std::usize::MAX);
        // The code below is logically equivalent to:
        //
        // let mut total = 0;
        // for (_, count) in &rows[offset_nth_row..] {
        //     total += count.get();
        // }
        // let return_size = std::cmp::min(total, limit);
        //
        // but it breaks early if the limit is reached, instead of scanning the entire code.
        let return_size = rows[offset_nth_row..]
            .iter()
            .try_fold(0, |sum, (_, count)| {
                let new_sum = sum + count.get();
                if new_sum > limit {
                    None
                } else {
                    Some(new_sum)
                }
            })
            .unwrap_or(limit);
        let mut ret = Vec::with_capacity(return_size);
        let mut remaining = limit;
        let mut row_buf = Row::default();
        let mut datum_vec = mz_repr::DatumVec::new();
        for (row, count) in &rows[offset_nth_row..] {
            if remaining == 0 {
                break;
            }
            let count = std::cmp::min(count.get(), remaining);
            for _ in 0..count {
                let new_row = {
                    let datums = datum_vec.borrow_with(&row);
                    row_buf
                        .packer()
                        .extend(self.project.iter().map(|i| &datums[*i]));
                    row_buf.clone()
                };
                ret.push(new_row);
            }
            remaining -= count;
        }
        ret
    }
pub fn finish ( & self , mut rows : Vec < ( Row , NonZeroUsize ) > ) -> Vec < Row > { let mut left_datum_vec = mz_repr :: DatumVec :: new ( ) ; let mut right_datum_vec = mz_repr :: DatumVec :: new ( ) ; let sort_by = | ( left , _ ) : & ( Row , _ ) , ( right , _ ) : & ( Row , _ ) | { let left_datums = left_datum_vec . borrow_with ( left ) ; let right_datums = right_datum_vec . borrow_with ( right ) ; compare_columns ( & self . order_by , & left_datums , & right_datums , | | { left . cmp ( & right ) } ) } ; rows . sort_by ( sort_by ) ; let ( offset_nth_row , offset_kth_copy ) = self . find_offset ( & rows ) ; // Adjust the first returned row's count to account for the offset. if let Some ( ( _ , nth_diff ) ) = rows . get_mut ( offset_nth_row ) { // Justification for `unwrap`: // we only get here if we return from `get_offset` // having found something to return, // which can only happen if the count of // this row is greater than the offset remaining. * nth_diff = NonZeroUsize :: new ( nth_diff . get ( ) - offset_kth_copy ) . unwrap ( ) ; } let limit = self . limit . unwrap_or ( std :: usize :: MAX ) ; // The code below is logically equivalent to: // // let mut total = 0; // for (_, count) in &rows[offset_nth_row..] { //     total += count.get(); // } // let return_size = std::cmp::min(total, limit); // // but it breaks early if the limit is reached, instead of scanning the entire code. let return_size = rows [ offset_nth_row .. ] . iter ( ) . try_fold ( 0 , | sum , ( _ , count ) | { let new_sum = sum + count . get ( ) ; if new_sum > limit { None } else { Some ( new_sum ) } } ) . unwrap_or ( limit ) ; let mut ret = Vec :: with_capacity ( return_size ) ; let mut remaining = limit ; let mut row_buf = Row :: default ( ) ; let mut datum_vec = mz_repr :: DatumVec :: new ( ) ; for ( row , count ) in & rows [ offset_nth_row .. ] { if remaining == 0 { break ; } let count = std :: cmp :: min ( count . get ( ) , remaining ) ; for _ in 0 .. count { let new_row = { let datums = datum_vec . borrow_with ( & row ) ; row_buf . packer ( ) . extend ( self . project . iter ( ) . map ( | i | & datums [ * i ] ) ) ; row_buf . clone ( ) } ; ret . push ( new_row ) ; } remaining -= count ; } ret }
Applies finishing actions to a row set,
and unrolls it to a unary representation.
Applies finishing actions to a row set and unrolls it to a unary representation .
&self
rows
Vec<(Row, NonZeroUsize)>