Skip to main content

polars_ops/chunked_array/binary/
get.rs

1use polars_core::prelude::arity::broadcast_try_binary_elementwise;
2use polars_core::prelude::*;
3use polars_error::{PolarsResult, polars_bail};
4
5fn get_byte(bytes: Option<&[u8]>, idx: Option<i64>, null_on_oob: bool) -> PolarsResult<Option<u8>> {
6    let (Some(bytes), Some(idx)) = (bytes, idx) else {
7        return Ok(None);
8    };
9
10    let len = bytes.len() as i64;
11    let idx = if idx >= 0 { idx } else { len + idx };
12    if idx < 0 || idx >= len {
13        if null_on_oob {
14            Ok(None)
15        } else {
16            polars_bail!(ComputeError: "get index is out of bounds")
17        }
18    } else {
19        Ok(Some(bytes[idx as usize]))
20    }
21}
22
23pub fn bin_get(
24    ca: &BinaryChunked,
25    index: &Int64Chunked,
26    null_on_oob: bool,
27) -> PolarsResult<Column> {
28    let out: UInt8Chunked =
29        broadcast_try_binary_elementwise(ca, index, |b, idx| get_byte(b, idx, null_on_oob))?;
30    Ok(out.into_column())
31}