Skip to content

feat(python): make Python binding for Roaring Bitmap #7695

Description

@wjones127

We often return a handle to a roaring bitmap as a Python set, and use a Python set to create a roaring bitmap. Examples:

let fragment_ids_ref: &Bound<'_, PySet> = fragment_ids.cast()?;
let fragment_bitmap = Some(
fragment_ids_ref
.into_iter()
.map(|id| id.extract::<u32>())
.collect::<PyResult<RoaringBitmap>>()?,
);

let fragment_ids = self.0.fragment_bitmap.as_ref().map_or_else(
|| PySet::empty(py).unwrap(),
|bitmap| {
let set = PySet::empty(py).unwrap();
for id in bitmap.iter() {
set.add(id).unwrap();
}
set
},
);

It might be nice to instead provide a Python type that is a wrapper around Arc<RoaringBitmap>. The bitmap can be passed directly from the Python layer without copies. For mutation, we can do copy on write, use Arc::make_mut().

Bonus points if it has efficient constructors from pyarrow arrays and ranges.

import pyarrow as pa
from lance.bitmap import bitmap

bitmap([1, 2, 4])
bitmap(range(1000))
bitmap(2 * pa.array(range(100)))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions