Repository: tikv/tikv
File: components/tidb_query_executors/src/index_lookup_executor.rs (Rust)

Code before:
        iter.accessor.expect_get_local_region_storage(true);
        let task = block_on(iter.next_task()).unwrap();
        let expected_ranges = vec![make_scan_key_range(6, 7), make_scan_key_range(11, 13)];
        assert_eq!(task.storage, MockStorage(region, expected_ranges.clone()));
        assert_eq!(task.key_ranges, expected_ranges);
        assert_eq!(iter.cursor_in_handle_orders, 3);
        assert!(iter.left_rows.is_empty());

        // leader found, in this case, the key 14's point range is across the region
        // end, like:
        // |----------- region -----------|
        //          |key(13) -- key(14)----- point_range_end(14)|
        // We still need to add 14 to this region but should set the end of the key
        // range to the region end.
        // We get the key range: [key(13), region_end)
        iter.accessor.assert_no_exceptions();
        let region = make_region(2, encode_cmp_key(13), encode_cmp_key_and_push(14, 0u8));
        iter.accessor
            .expect_find_region(encode_cmp_key(13), region.clone(), StateRole::Leader);
        iter.accessor.expect_get_local_region_storage(true);
        let task = block_on(iter.next_task()).unwrap();
        let mut expected_ranges = vec![make_scan_key_range(13, 15)];
        // The end key of the range should be the region end because the point range end
        // of handle 14 exceeds the region.
        expected_ranges.last_mut().unwrap().end = Key::from_encoded(region.get_end_key().to_vec())
            .into_raw()
            .unwrap();
        assert_eq!(task.storage, MockStorage(region, expected_ranges.clone()));
        assert_eq!(task.key_ranges, expected_ranges);
        assert_eq!(iter.cursor_in_handle_orders, 5);
        assert!(iter.left_rows.is_empty());

        // leader found, in this case, the region start exceeds the point range
        // end of 14 but still before point range start of 15:
        // |------- region --------|
        //               |key(15)----- point_range_end(15)|
        // We need to get one range with [key(15), region_end)
        iter.accessor.assert_no_exceptions();
        let region = make_region(
            3,
            encode_cmp_key_and_push(14, 0u8),
            encode_cmp_key_and_push(15, 0u8),
        );
        iter.accessor
            .expect_find_region(encode_cmp_key(15), region.clone(), StateRole::Leader);
        iter.accessor.expect_get_local_region_storage(true);
        let task = block_on(iter.next_task()).unwrap();
        let mut expected_range = make_scan_key_range(15, 16);
        // The end key of the range should be the region end because the point range end
        // of handle 15 exceeds the region.
        expected_range.set_end(

Review comment (suggestion):
Inconsistency in method usage: the production code uses `to_raw()` (which borrows) while the test code uses `into_raw()` (which takes ownership). For consistency and to avoid unnecessary cloning in tests, consider using `to_raw()` consistently throughout, or `into_raw()` if ownership transfer is intentional.
```suggestion
        let region_end_key = Key::from_encoded(region.get_end_key().to_vec());
        expected_ranges.last_mut().unwrap().end = region_end_key
            .to_raw()
```

Code after:
        iter.accessor.expect_get_local_region_storage(true);
        let task = block_on(iter.next_task()).unwrap();
        let expected_ranges = vec![make_scan_key_range(6, 7), make_scan_key_range(11, 13)];
        assert_eq!(task.storage, MockStorage(region, expected_ranges.clone()));
        assert_eq!(task.key_ranges, expected_ranges);
        assert_eq!(iter.cursor_in_handle_orders, 3);
        assert!(iter.left_rows.is_empty());

        // leader found, in this case, the key 14's point range is across the region
        // end, like:
        // |----------- region -----------|
        //          |key(13) -- key(14)----- point_range_end(14)|
        // We still need to add 14 to this region but should set the end of the key
        // range to the region end.
        // We get the key range: [key(13), region_end)
        iter.accessor.assert_no_exceptions();
        let region = make_region(2, encode_cmp_key(13), encode_cmp_key_and_push(14, 0u8));
        iter.accessor
            .expect_find_region(encode_cmp_key(13), region.clone(), StateRole::Leader);
        iter.accessor.expect_get_local_region_storage(true);
        let task = block_on(iter.next_task()).unwrap();
        let mut expected_ranges = vec![make_scan_key_range(13, 15)];
        // The end key of the range should be the region end because the point range end
        // of handle 14 exceeds the region.
        expected_ranges.last_mut().unwrap().end = Key::from_encoded(region.get_end_key().to_vec())
            .to_raw()
            .unwrap();
        assert_eq!(task.storage, MockStorage(region, expected_ranges.clone()));
        assert_eq!(task.key_ranges, expected_ranges);
        assert_eq!(iter.cursor_in_handle_orders, 5);
        assert!(iter.left_rows.is_empty());

        // leader found, in this case, the region start exceeds the point range
        // end of 14 but still before point range start of 15:
        // |------- region --------|
        //               |key(15)----- point_range_end(15)|
        // We need to get one range with [key(15), region_end)
        iter.accessor.assert_no_exceptions();
        let region = make_region(
            3,
            encode_cmp_key_and_push(14, 0u8),
            encode_cmp_key_and_push(15, 0u8),
        );
        iter.accessor
            .expect_find_region(encode_cmp_key(15), region.clone(), StateRole::Leader);
        iter.accessor.expect_get_local_region_storage(true);
        let task = block_on(iter.next_task()).unwrap();
        let mut expected_range = make_scan_key_range(15, 16);
        // The end key of the range should be the region end because the point range end
        // of handle 15 exceeds the region.
        expected_range.set_end(