-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfece7a
commit 2566fe1
Showing
6 changed files
with
108 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,88 @@ | ||
use std::sync::Arc; | ||
|
||
use arrow_array::{ArrayRef, UInt32Array}; | ||
use arrow_buffer::ScalarBuffer; | ||
use arrow_schema::{DataType, Field}; | ||
use arrow_array::builder::{UInt16Builder, UInt32Builder}; | ||
use arrow_array::types::{UInt16Type, UInt32Type}; | ||
use arrow_array::{ArrayRef, RecordBatch}; | ||
use arrow_buffer::alloc::Allocation; | ||
use arrow_schema::{Field, Schema}; | ||
use geo_index::indices::Indices; | ||
use geo_index::rtree::{RTreeIndex, RTreeRef}; | ||
use geo_index::CoordType; | ||
use pyo3::prelude::*; | ||
use pyo3_arrow::buffer::PyArrowBuffer; | ||
use pyo3_arrow::PyChunkedArray; | ||
use pyo3_arrow::PyRecordBatch; | ||
|
||
use crate::util::slice_to_arrow; | ||
|
||
#[pyfunction] | ||
pub fn partitions(py: Python, index: PyArrowBuffer) -> PyResult<PyObject> { | ||
let buffer = index.into_inner(); | ||
let owner = Arc::new(buffer.clone()); | ||
let slice = buffer.as_slice(); | ||
let coord_type = CoordType::from_buffer(&slice).unwrap(); | ||
let result = match coord_type { | ||
let (indices, partition_ids) = match coord_type { | ||
CoordType::Float32 => { | ||
let tree = RTreeRef::<f32>::try_new(&slice).unwrap(); | ||
let node_size = tree.node_size(); | ||
match tree.indices() { | ||
Indices::U16(indices) => indices_to_chunked_array(indices, node_size), | ||
Indices::U32(indices) => indices_to_chunked_array_u32(indices, node_size), | ||
} | ||
let indices = indices_to_arrow(tree.indices(), tree.num_items(), owner); | ||
let partition_ids = partition_id_array(tree.num_items(), tree.node_size()); | ||
(indices, partition_ids) | ||
} | ||
CoordType::Float64 => { | ||
let tree = RTreeRef::<f64>::try_new(&slice).unwrap(); | ||
let node_size = tree.node_size(); | ||
match tree.indices() { | ||
Indices::U16(indices) => indices_to_chunked_array(indices, node_size), | ||
Indices::U32(indices) => indices_to_chunked_array_u32(indices, node_size), | ||
} | ||
let indices = indices_to_arrow(tree.indices(), tree.num_items(), owner); | ||
let partition_ids = partition_id_array(tree.num_items(), tree.node_size()); | ||
(indices, partition_ids) | ||
} | ||
_ => todo!("Only f32 and f64 implemented so far"), | ||
}; | ||
result.to_arro3(py) | ||
|
||
let fields = vec![ | ||
Field::new("indices", indices.data_type().clone(), false), | ||
Field::new("partition_id", partition_ids.data_type().clone(), false), | ||
]; | ||
let schema = Schema::new(fields); | ||
PyRecordBatch::new(RecordBatch::try_new(schema.into(), vec![indices, partition_ids]).unwrap()) | ||
.to_arro3(py) | ||
} | ||
|
||
fn indices_to_chunked_array(indices: &[u16], node_size: u16) -> PyChunkedArray { | ||
let array_chunks = indices | ||
.chunks(node_size as usize) | ||
.map(|chunk| { | ||
Arc::new(UInt32Array::new( | ||
ScalarBuffer::from(Vec::from_iter(chunk.iter().map(|x| *x as u32))), | ||
None, | ||
)) as ArrayRef | ||
}) | ||
.collect::<Vec<_>>(); | ||
PyChunkedArray::try_new( | ||
array_chunks, | ||
Arc::new(Field::new("indices", DataType::UInt32, false)), | ||
) | ||
.unwrap() | ||
fn indices_to_arrow(indices: Indices, num_items: u32, owner: Arc<dyn Allocation>) -> ArrayRef { | ||
match indices { | ||
Indices::U16(slice) => slice_to_arrow::<UInt16Type>(&slice[0..num_items as usize], owner), | ||
Indices::U32(slice) => slice_to_arrow::<UInt32Type>(&slice[0..num_items as usize], owner), | ||
} | ||
} | ||
|
||
fn indices_to_chunked_array_u32(indices: &[u32], node_size: u16) -> PyChunkedArray { | ||
let array_chunks = indices | ||
.chunks(node_size as usize) | ||
.map(|chunk| { | ||
Arc::new(UInt32Array::new(ScalarBuffer::from(chunk.to_vec()), None)) as ArrayRef | ||
}) | ||
.collect::<Vec<_>>(); | ||
PyChunkedArray::try_new( | ||
array_chunks, | ||
Arc::new(Field::new("indices", DataType::UInt32, false)), | ||
) | ||
.unwrap() | ||
fn partition_id_array(num_items: u32, node_size: u16) -> ArrayRef { | ||
let num_full_nodes = num_items / node_size as u32; | ||
let remainder = num_items % node_size as u32; | ||
|
||
// Check if the partition ids fit inside a u16 | ||
// We add 1 to cover the remainder | ||
if num_full_nodes + 1 < u16::MAX as _ { | ||
let mut output_array = UInt16Builder::with_capacity(num_items as _); | ||
|
||
let mut partition_id = 0; | ||
for _ in 0..num_full_nodes { | ||
output_array.append_value_n(partition_id, node_size as usize); | ||
partition_id += 1; | ||
} | ||
|
||
// The loop omits the last node | ||
output_array.append_value_n(partition_id, remainder as usize); | ||
|
||
Arc::new(output_array.finish()) | ||
} else { | ||
let mut output_array = UInt32Builder::with_capacity(num_items as _); | ||
|
||
let mut partition_id = 0; | ||
for _ in 0..num_full_nodes { | ||
output_array.append_value_n(partition_id, node_size as usize); | ||
partition_id += 1; | ||
} | ||
|
||
// The loop omits the last node | ||
output_array.append_value_n(partition_id, remainder as usize); | ||
|
||
Arc::new(output_array.finish()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::ptr::NonNull; | ||
use std::sync::Arc; | ||
|
||
use arrow_array::{ArrayRef, ArrowPrimitiveType, PrimitiveArray}; | ||
use arrow_buffer::alloc::Allocation; | ||
use arrow_buffer::{ArrowNativeType, Buffer, ScalarBuffer}; | ||
|
||
pub(crate) fn slice_to_arrow<T: ArrowPrimitiveType>( | ||
slice: &[T::Native], | ||
owner: Arc<dyn Allocation>, | ||
) -> ArrayRef { | ||
let ptr = NonNull::new(slice.as_ptr() as *mut _).unwrap(); | ||
let len = slice.len(); | ||
let bytes_len = len * T::Native::get_byte_width(); | ||
|
||
// Safety: | ||
// ptr is a non-null pointer owned by the RTree, which is passed in as the Allocation | ||
let buffer = unsafe { Buffer::from_custom_allocation(ptr, bytes_len, owner) }; | ||
Arc::new(PrimitiveArray::<T>::new( | ||
ScalarBuffer::new(buffer, 0, len), | ||
None, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters