Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new Chunk iteration APIs #8553

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
475 changes: 468 additions & 7 deletions crates/store/re_chunk/src/iter.rs

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion crates/store/re_chunk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub use self::builder::{ChunkBuilder, TimeColumnBuilder};
pub use self::chunk::{Chunk, ChunkComponents, ChunkError, ChunkResult, TimeColumn};
pub use self::helpers::{ChunkShared, UnitChunkShared};
pub use self::id::{ChunkId, RowId};
pub use self::iter::{ChunkComponentIter, ChunkComponentIterItem, ChunkIndicesIter};
pub use self::iter::{
ChunkComponentIter, ChunkComponentIterItem, ChunkComponentSlicer, ChunkIndicesIter,
};
pub use self::latest_at::LatestAtQuery;
pub use self::range::{RangeQuery, RangeQueryOptions};
pub use self::transport::TransportChunk;
Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_grpc_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ async fn stream_catalog_async(
)))?;

let recording_uri_arrays: Vec<Box<dyn Arrow2Array>> = chunk
.iter_string(&"id".into())
.iter_slices::<String>("id".into())
.map(|id| {
let rec_id = &id[0]; // each component batch is of length 1 i.e. single 'id' value

Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_query/examples/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> anyhow::Result<()> {
.flat_map(|chunk| {
izip!(
chunk.iter_component_indices(&query.timeline(), &MyColor::name()),
chunk.iter_primitive::<u32>(&MyColor::name()),
chunk.iter_slices::<u32>(MyColor::name()),
)
});

Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_query/tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ fn query_and_compare(
.flat_map(|chunk| {
itertools::izip!(
chunk.iter_component_indices(&query.timeline(), &MyColor::name()),
chunk.iter_primitive::<u32>(&MyColor::name()),
chunk.iter_slices::<u32>(MyColor::name()),
)
})
.collect_vec();
Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_types_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub mod external {
///
#[macro_export]
macro_rules! static_assert_struct_has_fields {
($strct:ty, $($field:ident: $field_typ:ty),+) => {
($strct:ty, $($field:ident: $field_typ:ty),+ $(,)?) => {
const _: fn(&$strct) = |s: &$strct| {
$(let _: &$field_typ = &s.$field;)+
};
Expand Down
37 changes: 33 additions & 4 deletions crates/viewer/re_view/src/results_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use re_chunk_store::{Chunk, LatestAtQuery, RangeQuery, UnitChunkShared};
use re_log_types::external::arrow2::bitmap::Bitmap as Arrow2Bitmap;
use re_log_types::hash::Hash64;
use re_query::{LatestAtResults, RangeResults};
use re_types::ArchetypeFieldName;
use re_types_core::ComponentName;
use re_viewer_context::{DataResult, QueryContext, ViewContext};

Expand Down Expand Up @@ -239,9 +240,8 @@ pub trait RangeResultsExt {
/// Returns a zero-copy iterator over all the results for the given `(timeline, component)` pair.
///
/// Call one of the following methods on the returned [`HybridResultsChunkIter`]:
/// * [`HybridResultsChunkIter::primitive`]
/// * [`HybridResultsChunkIter::primitive_array`]
/// * [`HybridResultsChunkIter::string`]
/// * [`HybridResultsChunkIter::slice`]
/// * [`HybridResultsChunkIter::slice_from_struct_field`]
fn iter_as(
&self,
timeline: Timeline,
Expand Down Expand Up @@ -408,7 +408,7 @@ impl RangeResultsExt for HybridResults<'_> {
// ---

use re_chunk::{ChunkComponentIterItem, RowId, TimeInt, Timeline};
use re_chunk_store::external::{re_chunk, re_chunk::external::arrow2};
use re_chunk_store::external::re_chunk;

/// The iterator type backing [`HybridResults::iter_as`].
pub struct HybridResultsChunkIter<'a> {
Expand Down Expand Up @@ -523,4 +523,33 @@ impl<'a> HybridResultsChunkIter<'a> {
)
})
}

/// Iterate as indexed, sliced, deserialized component batches.
///
/// See [`Chunk::iter_slices`] for more information.
pub fn slice<S: 'a + re_chunk::ChunkComponentSlicer>(
&'a self,
) -> impl Iterator<Item = ((TimeInt, RowId), S::Item<'a>)> + 'a {
self.chunks.iter().flat_map(|chunk| {
itertools::izip!(
chunk.iter_component_indices(&self.timeline, &self.component_name),
chunk.iter_slices::<S>(self.component_name)
)
})
}

/// Iterate as indexed, sliced, deserialized component batches for a specific struct field.
///
/// See [`Chunk::iter_slices_from_struct_field`] for more information.
pub fn slice_from_struct_field<S: 'a + re_chunk::ChunkComponentSlicer>(
&'a self,
field_name: &'a ArchetypeFieldName,
teh-cmc marked this conversation as resolved.
Show resolved Hide resolved
) -> impl Iterator<Item = ((TimeInt, RowId), S::Item<'a>)> + 'a {
self.chunks.iter().flat_map(|chunk| {
itertools::izip!(
chunk.iter_component_indices(&self.timeline, &self.component_name),
chunk.iter_slices_from_struct_field::<S>(self.component_name, field_name)
)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
// ---

use re_chunk::{Chunk, ChunkComponentIterItem, ComponentName, RowId};
use re_chunk_store::external::{re_chunk, re_chunk::external::arrow2};
use re_chunk_store::external::re_chunk;

/// Iterate `chunks` as indexed deserialized batches.
///
Expand Down Expand Up @@ -239,3 +239,19 @@ pub fn iter_buffer<'a, T: arrow::datatypes::ArrowNativeType + arrow2::types::Nat
)
})
}

/// Iterate `chunks` as indexed primitives.
///
/// See [`Chunk::iter_slices`] for more information.
pub fn iter_slices<'a, T: 'a + re_chunk::ChunkComponentSlicer>(
chunks: &'a std::borrow::Cow<'a, [Chunk]>,
timeline: Timeline,
component_name: ComponentName,
) -> impl Iterator<Item = ((TimeInt, RowId), T::Item<'a>)> + 'a {
chunks.iter().flat_map(move |chunk| {
itertools::izip!(
chunk.iter_component_indices(&timeline, &component_name),
chunk.iter_slices::<T>(component_name)
)
})
}
Loading