Repository: pola-rs/polars
File: crates/polars-ops/src/series/ops/index.rs (Rust)

Code before:
        .collect();

    Ok(out)
}

/// Convert arbitrary integer Series into IdxCa, using `target_len` as logical length.
///
/// - All OOB indices are mapped to null in `convert`.
/// - We track null counts before and after:
///   - if `null_on_oob == true`, extra nulls are expected and we just return.
///   - if `null_on_oob == false` and new nulls appear, we raise OutOfBounds.
pub fn convert_to_unsigned_index(
    s: &Series,
    target_len: usize,
    null_on_oob: bool,
) -> PolarsResult<IdxCa> {
    let dtype = s.dtype();
    polars_ensure!(
        dtype.is_integer(),
        InvalidOperation: "expected integers as index"
    );

    // Ensure the logical length fits in IdxSize at all.
    polars_ensure!(
        (target_len as u128) <= (IdxSize::MAX as u128),
        OutOfBounds: "gather indices are out of bounds"
    );

    let in_nulls = s.null_count();

    // Normalize to IdxCa with all OOB indices already mapped to nulls.
    let idx: IdxCa = match dtype {
        DataType::Int64 => {
            let ca = s.i64().unwrap();
            convert(ca, target_len)?
        },
        DataType::Int32 => {
            let ca = s.i32().unwrap();
            convert(ca, target_len)?
        },
        #[cfg(feature = "dtype-i16")]
        DataType::Int16 => {
            let ca = s.i16().unwrap();
            convert(ca, target_len)?
        },
        #[cfg(feature = "dtype-i8")]
        DataType::Int8 => {
            let ca = s.i8().unwrap();
            convert(ca, target_len)?
        },


Review comment (suggestion):
This error message needs to be different - we didn't even inspect the error message. I think this could just be an assert as this should never be violated.

Code after:
                        debug_assert!(pos >= 0 && pos < len_i64);
                        Some(pos as IdxSize)
                    } else {
                        None
                    }
                }
            })
        })
        .collect();

    Ok(out)
}

/// Convert arbitrary integer Series into IdxCa, using `target_len` as logical length.
///
/// - All OOB indices are mapped to null in `convert_*`.
/// - We track null counts before and after:
///   - if `null_on_oob == true`, extra nulls are expected and we just return.
///   - if `null_on_oob == false` and new nulls appear, we raise OutOfBounds.
pub fn convert_to_unsigned_index(
    s: &Series,
    target_len: usize,
    null_on_oob: bool,
) -> PolarsResult<IdxCa> {
    let dtype = s.dtype();
    polars_ensure!(
        dtype.is_integer(),
        InvalidOperation: "expected integers as index"
    );

    assert!(
        (target_len as u128) <= (IdxSize::MAX as u128),
        "internal error: target_len does not fit in IdxSize"
    );

    let in_nulls = s.null_count();

    // Normalize to IdxCa with all OOB indices already mapped to nulls.
    let idx: IdxCa = match dtype {
        // ----- SIGNED -----
        DataType::Int64 => {
            let ca = s.i64().unwrap();
            convert_signed(ca, target_len)?
        },
        DataType::Int32 => {
            let ca = s.i32().unwrap();
            convert_signed(ca, target_len)?
        },
        #[cfg(feature = "dtype-i16")]
        DataType::Int16 => {
            let ca = s.i16().unwrap();