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

Code before:
                    key_ranges.push(Self::new_key_range(key, point_range_end));
                }
            }

            scanned += 1;
            previous_handle = handle;
        }

        if let Some(key_range) = key_ranges.last_mut() {
            // The point_range_end may exceed the region end even if the key is in the
            // region. For example, the IntHandle has a point range end of
            // encode_row_key(v + 1), but the region end may be
            // encode_row_key(v) + b'\0' which is before the point range end. To
            // handle this case, just use the region end as the end of the key range to
            // avoid error.
            if !Self::is_key_before_or_eq_region_end(key_range.get_end(), end_exclusive) {
                let end_exclusive = Key::from_encoded(end_exclusive.to_vec());
                match end_exclusive.to_raw() {
                    Ok(end_key) => {
                        key_range.end = end_key;
                    }
                    Err(err) => {
                        // Set `region` to None to avoid generating a table task.
                        // The related rows will be looked up on the TiDB side.
                        region = None;
                        error!("failed to decode region end key"; "end_key" => ?end_exclusive);
                        debug_assert!(false, "region end should always be in MVCC format");
                    }
                }
            }
        }

        let task = match region {
            Some(region) => {
                match self
                    .accessor
                    .get_local_region_storage(&region, &key_ranges)
                    .await
                {
                    Ok(storage) => Some(TableTask {
                        storage,
                        key_ranges,
                    }),
                    Err(_) => {
                        // TODO: handle error
                        None
                    }
                }
            }
            _ => None,
        };

Review comment (bug):
The error variable `err` captured in the match arm is not used in the error handling. The error message should include the actual error details to help with debugging. Consider including the error in the log message.
```suggestion
                        error!("failed to decode region end key"; "end_key" => ?end_exclusive, "error" => ?err);
```

Code after:
                    key_ranges.push(Self::new_key_range(key, point_range_end));
                }
            }

            scanned += 1;
            previous_handle = handle;
        }

        if let Some(key_range) = key_ranges.last_mut() {
            // The point_range_end may exceed the region end even if the key is in the
            // region. For example, the IntHandle has a point range end of
            // encode_row_key(v + 1), but the region end may be
            // encode_row_key(v) + b'\0' which is before the point range end. To
            // handle this case, just use the region end as the end of the key range to
            // avoid error.
            if !Self::is_key_before_or_eq_region_end(key_range.get_end(), end_exclusive) {
                let end_exclusive = Key::from_encoded(end_exclusive.to_vec());
                match end_exclusive.to_raw() {
                    Ok(end_key) => {
                        key_range.end = end_key;
                    }
                    Err(err) => {
                        // Set `region` to None to avoid generating a table task.
                        // The related rows will be looked up on the TiDB side.
                        region = None;
                        error!("failed to decode region end key"; "end_key" => ?end_exclusive, "error" => ?err);
                        debug_assert!(false, "region end should always be in MVCC format");
                    }
                }
            }
        }

        let task = match region {
            Some(region) => {
                match self
                    .accessor
                    .get_local_region_storage(&region, &key_ranges)
                    .await
                {
                    Ok(storage) => Some(TableTask {
                        storage,
                        key_ranges,
                    }),
                    Err(_) => {
                        // TODO: handle error
                        None
                    }
                }
            }
            _ => None,
        };