Skip to content

Commit

Permalink
bake data_time into archetypeview
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jan 15, 2024
1 parent 7760784 commit 3482fc3
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 92 deletions.
4 changes: 2 additions & 2 deletions crates/re_query/benches/query_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn query_and_visit_points(store: &DataStore, paths: &[EntityPath]) -> Vec<SavePo

// TODO(jleibs): Add Radius once we have support for it in field_types
for path in paths {
let (_, arch_view) = query_archetype::<Points2D>(store, &query, path).unwrap();
let arch_view = query_archetype::<Points2D>(store, &query, path).unwrap();
itertools::izip!(
arch_view.iter_required_component::<Position2D>().unwrap(),
arch_view.iter_optional_component::<Color>().unwrap()
Expand All @@ -299,7 +299,7 @@ fn query_and_visit_strings(store: &DataStore, paths: &[EntityPath]) -> Vec<SaveS
let mut strings = Vec::with_capacity(NUM_STRINGS as _);

for path in paths {
let (_, arch_view) = query_archetype::<Points2D>(store, &query, path).unwrap();
let arch_view = query_archetype::<Points2D>(store, &query, path).unwrap();
arch_view
.iter_optional_component::<Text>()
.unwrap()
Expand Down
28 changes: 23 additions & 5 deletions crates/re_query/src/archetype_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, marker::PhantomData};

use arrow2::array::{Array, PrimitiveArray};
use re_format::arrow;
use re_log_types::{DataCell, RowId};
use re_log_types::{DataCell, RowId, TimeInt};
use re_types_core::{
components::InstanceKey, Archetype, Component, ComponentName, DeserializationError,
DeserializationResult, Loggable, SerializationResult,
Expand Down Expand Up @@ -225,15 +225,23 @@ where
{
}

/// A view of an [`Archetype`] at a particular point in time returned by [`crate::get_component_with_instances`]
/// A view of an [`Archetype`] at a particular point in time returned by [`crate::get_component_with_instances`].
///
/// The required [`Component`]s of an [`ArchetypeView`] determines the length of an entity
/// batch. When iterating over individual components, they will be implicitly joined onto
/// the required [`Component`]s using [`InstanceKey`] values.
#[derive(Clone, Debug)]
pub struct ArchetypeView<A: Archetype> {
/// The _data_ time of the most recent component in the view (not necessarily the primary!).
///
/// `None` if timeless.
pub(crate) data_time: Option<TimeInt>,

/// The [`RowId`] of the primary component in the view.
pub(crate) primary_row_id: RowId,

pub(crate) components: BTreeMap<ComponentName, ComponentWithInstances>,

pub(crate) phantom: PhantomData<A>,
}

Expand All @@ -259,6 +267,14 @@ impl<A: Archetype> ArchetypeView<A> {
self.required_comp().len()
}

/// The _data_ time of the most recent component in the view (not necessarily the primary!).
///
/// `None` if timeless.
#[inline]
pub fn data_time(&self) -> Option<TimeInt> {
self.data_time
}

/// Returns the [`RowId`] associated with the _primary_ component that was used to drive this
/// entire query.
///
Expand Down Expand Up @@ -485,14 +501,16 @@ impl<A: Archetype> ArchetypeView<A> {
}
}

/// Helper function to produce an [`ArchetypeView`] from a collection of [`ComponentWithInstances`]
/// Helper function to produce an [`ArchetypeView`] from a collection of [`ComponentWithInstances`].
#[inline]
pub fn from_components(
row_id: RowId,
data_time: Option<TimeInt>,
primary_row_id: RowId,
components: impl IntoIterator<Item = ComponentWithInstances>,
) -> Self {
Self {
primary_row_id: row_id,
data_time,
primary_row_id,
components: components
.into_iter()
.map(|comp| (comp.name(), comp))
Expand Down
16 changes: 9 additions & 7 deletions crates/re_query/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn query_archetype<A: Archetype>(
store: &DataStore,
query: &LatestAtQuery,
ent_path: &EntityPath,
) -> crate::Result<(Option<TimeInt>, ArchetypeView<A>)> {
) -> crate::Result<ArchetypeView<A>> {
re_tracing::profile_function!();

let required_components: Vec<_> = A::required_components()
Expand Down Expand Up @@ -165,12 +165,14 @@ pub fn query_archetype<A: Archetype>(
)
});

let arch_view = ArchetypeView::from_components(
*row_id,
required_components.into_iter().chain(other_components),
);
// NOTE: Need to collect so we can compute `max_data_time`.
let cwis: Vec<_> = required_components
.into_iter()
.chain(other_components)
.collect();
let arch_view = ArchetypeView::from_components(max_data_time, *row_id, cwis);

Ok((max_data_time, arch_view))
Ok(arch_view)
}

/// Helper used to create an example store we can use for querying in doctests
Expand Down Expand Up @@ -266,7 +268,7 @@ fn simple_query_archetype() {
let ent_path = "point";
let query = LatestAtQuery::new(Timeline::new_sequence("frame_nr"), 123.into());

let (_, arch_view) = query_archetype::<MyPoints>(&store, &query, &ent_path.into()).unwrap();
let arch_view = query_archetype::<MyPoints>(&store, &query, &ent_path.into()).unwrap();

let expected_positions = [MyPoint::new(1.0, 2.0), MyPoint::new(3.0, 4.0)];
let expected_colors = [None, Some(MyColor::from(0xff000000))];
Expand Down
8 changes: 3 additions & 5 deletions crates/re_query/src/range.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use itertools::Itertools as _;
use re_data_store::{DataStore, LatestAtQuery, RangeQuery, TimeInt};
use re_data_store::{DataStore, LatestAtQuery, RangeQuery};
use re_log_types::EntityPath;
use re_types_core::{Archetype, ComponentName};

Expand All @@ -26,7 +26,7 @@ pub fn range_archetype<'a, A: Archetype + 'a, const N: usize>(
store: &'a DataStore,
query: &RangeQuery,
ent_path: &'a EntityPath,
) -> impl Iterator<Item = (Option<TimeInt>, ArchetypeView<A>)> + 'a {
) -> impl Iterator<Item = ArchetypeView<A>> + 'a {
re_tracing::profile_function!();

// TODO(jleibs) this shim is super gross
Expand Down Expand Up @@ -135,9 +135,7 @@ pub fn range_archetype<'a, A: Archetype + 'a, const N: usize>(
.filter_map(|cwi| cwi.map(|(_, cwi)| cwi))
.collect();

let arch_view = ArchetypeView::from_components(row_id, components);

(data_time, arch_view)
ArchetypeView::from_components(data_time, row_id, components)
})
})
}
2 changes: 1 addition & 1 deletion crates/re_query/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn query_archetype_with_history<'a, A: Archetype + 'a, const N: usize>(
time: &'a TimeInt,
history: &ExtraQueryHistory,
ent_path: &'a EntityPath,
) -> crate::Result<impl Iterator<Item = (Option<TimeInt>, ArchetypeView<A>)> + 'a> {
) -> crate::Result<impl Iterator<Item = ArchetypeView<A>> + 'a> {
let visible_history = match timeline.typ() {
re_log_types::TimeType::Time => history.nanos,
re_log_types::TimeType::Sequence => history.sequences,
Expand Down
15 changes: 5 additions & 10 deletions crates/re_query/tests/archetype_query_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ fn simple_query() {
// Retrieve the view
let timeline_query = re_data_store::LatestAtQuery::new(timepoint[0].0, timepoint[0].1);

let (_, arch_view) =
query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();
let arch_view = query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();

// We expect this to generate the following `DataFrame`
// ┌──────────┬───────────┬────────────┐
Expand Down Expand Up @@ -106,8 +105,7 @@ fn timeless_query() {
// Retrieve the view
let timeline_query = re_data_store::LatestAtQuery::new(timepoint[0].0, timepoint[0].1);

let (_, arch_view) =
query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();
let arch_view = query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();

// We expect this to generate the following `DataFrame`
// ┌──────────┬───────────┬────────────┐
Expand Down Expand Up @@ -169,8 +167,7 @@ fn no_instance_join_query() {
// Retrieve the view
let timeline_query = re_data_store::LatestAtQuery::new(timepoint[0].0, timepoint[0].1);

let (_, arch_view) =
query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();
let arch_view = query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();

// We expect this to generate the following `DataFrame`
// ┌──────────┬───────────┬────────────┐
Expand Down Expand Up @@ -230,8 +227,7 @@ fn missing_column_join_query() {
// Retrieve the view
let timeline_query = re_data_store::LatestAtQuery::new(timepoint[0].0, timepoint[0].1);

let (_, arch_view) =
query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();
let arch_view = query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();

// We expect this to generate the following `DataFrame`
//
Expand Down Expand Up @@ -300,8 +296,7 @@ fn splatted_query() {
// Retrieve the view
let timeline_query = re_data_store::LatestAtQuery::new(timepoint[0].0, timepoint[0].1);

let (_, arch_view) =
query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();
let arch_view = query_archetype::<Points2D>(&store, &timeline_query, &ent_path.into()).unwrap();

// We expect this to generate the following `DataFrame`
// ┌──────────┬───────────┬────────────┐
Expand Down
Loading

0 comments on commit 3482fc3

Please sign in to comment.