Skip to content

Commit

Permalink
re_query is now re_types-free
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Oct 17, 2023
1 parent ff1b327 commit a89c86d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 51 deletions.
33 changes: 33 additions & 0 deletions crates/re_log_types/src/example_components.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
//! Example components to be used for tests and docs
use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize};
use re_types::Loggable as _;

// ----------------------------------------------------------------------------

pub struct MyPoints;

impl re_types::Archetype for MyPoints {
type Indicator = re_types::GenericIndicatorComponent<Self>;

fn name() -> re_types::ArchetypeName {
"test.MyPoints".into()
}

fn required_components() -> ::std::borrow::Cow<'static, [re_types::ComponentName]> {
vec![MyPoint::name()].into()
}

fn recommended_components() -> std::borrow::Cow<'static, [re_types_core::ComponentName]> {
vec![MyColor::name(), MyLabel::name()].into()
}
}

// ----------------------------------------------------------------------------

Expand All @@ -10,6 +31,12 @@ pub struct MyPoint {
pub y: f32,
}

impl MyPoint {
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}

use crate as re_log_types;

re_log_types::arrow2convert_component_shim!(MyPoint as "test.Point2D");
Expand All @@ -31,6 +58,12 @@ re_log_types::arrow2convert_component_shim!(MyPoint as "test.Point2D");
#[repr(transparent)]
pub struct MyColor(pub u32);

impl From<u32> for MyColor {
fn from(value: u32) -> Self {
Self(value)
}
}

re_log_types::arrow2convert_component_shim!(MyColor as "test.Color");

// ----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions crates/re_query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ re_arrow_store.workspace = true
re_data_store.workspace = true
re_format.workspace = true
re_log_types.workspace = true
# NOTE: We're on our way to make this crate re_types-free, which is why we import both.
re_types_core.workspace = true
re_types = { workspace = true, features = ["datagen"] }
re_log.workspace = true
re_tracing.workspace = true

Expand All @@ -51,6 +49,8 @@ polars-core = { workspace = true, optional = true, features = [


[dev-dependencies]
re_types = { workspace = true, features = ["datagen"] }

criterion = "0.5"
itertools = { workspace = true }
mimalloc.workspace = true
Expand Down
7 changes: 3 additions & 4 deletions crates/re_query/src/archetype_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::{collections::BTreeMap, marker::PhantomData};
use arrow2::array::{Array, PrimitiveArray};
use re_format::arrow;
use re_log_types::{DataCell, RowId};
use re_types::components::InstanceKey;
use re_types_core::{
Archetype, Component, ComponentName, DeserializationError, DeserializationResult, Loggable,
SerializationResult,
components::InstanceKey, Archetype, Component, ComponentName, DeserializationError,
DeserializationResult, Loggable, SerializationResult,
};

use crate::QueryError;
Expand Down Expand Up @@ -41,7 +40,7 @@ impl ComponentWithInstances {

/// Returns the array of [`InstanceKey`]s.
#[inline]
pub fn instance_keys(&self) -> Vec<re_types::components::InstanceKey> {
pub fn instance_keys(&self) -> Vec<InstanceKey> {
re_tracing::profile_function!();
self.instance_keys.to_native::<InstanceKey>()
}
Expand Down
9 changes: 4 additions & 5 deletions crates/re_query/src/dataframe_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use arrow2::{
};
use polars_core::prelude::*;
use re_arrow_store::ArrayExt;
use re_types::components::InstanceKey;
use re_types_core::{Archetype, Component, Loggable};
use re_types_core::{components::InstanceKey, Archetype, Component, Loggable};

use crate::{ArchetypeView, ComponentWithInstances, QueryError};

Expand Down Expand Up @@ -125,7 +124,7 @@ impl ComponentWithInstances {
let array1 = self.values.as_arrow_ref();

let series0 = Series::try_from((
re_types::components::InstanceKey::name().as_ref(),
InstanceKey::name().as_ref(),
array0.as_ref().clean_for_polars(),
))?;
let series1 = Series::try_from((C0::name().as_ref(), array1.as_ref().clean_for_polars()))?;
Expand All @@ -145,7 +144,7 @@ impl<A: Archetype> ArchetypeView<A> {
let array1 = C1::to_arrow_opt(self.iter_optional_component::<C1>()?)?;

let series0 = Series::try_from((
re_types::components::InstanceKey::name().as_ref(),
InstanceKey::name().as_ref(),
array0.as_ref().clean_for_polars(),
))?;
let series1 = Series::try_from((C1::name().as_ref(), array1.as_ref().clean_for_polars()))?;
Expand All @@ -165,7 +164,7 @@ impl<A: Archetype> ArchetypeView<A> {
let array2 = C2::to_arrow_opt(self.iter_optional_component::<C2>()?)?;

let series0 = Series::try_from((
re_types::components::InstanceKey::name().as_ref(),
InstanceKey::name().as_ref(),
array0.as_ref().clean_for_polars(),
))?;
let series1 = Series::try_from((C1::name().as_ref(), array1.as_ref().clean_for_polars()))?;
Expand Down
72 changes: 32 additions & 40 deletions crates/re_query/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use re_arrow_store::{DataStore, LatestAtQuery};
use re_log_types::{DataRow, EntityPath, RowId};
use re_types::components::InstanceKey;
use re_types_core::{Archetype, ComponentName, Loggable};
use re_types_core::{components::InstanceKey, Archetype, ComponentName, Loggable};

use crate::{ArchetypeView, ComponentWithInstances, QueryError};

Expand All @@ -11,9 +10,8 @@ use crate::{ArchetypeView, ComponentWithInstances, QueryError};
///
/// ```
/// # use re_arrow_store::LatestAtQuery;
/// # use re_types::components::Position2D;
/// # use re_log_types::Timeline;
/// # use re_types::Loggable as _;
/// # use re_log_types::{Timeline, example_components::{MyColor, MyPoint}};
/// # use re_types_core::Loggable as _;
/// # let store = re_query::__populate_example_store();
///
/// let ent_path = "point";
Expand All @@ -23,27 +21,27 @@ use crate::{ArchetypeView, ComponentWithInstances, QueryError};
/// &store,
/// &query,
/// &ent_path.into(),
/// Position2D::name(),
/// MyPoint::name(),
/// )
/// .unwrap();
///
/// # #[cfg(feature = "polars")]
/// let df = component.as_df::<Position2D>().unwrap();
/// let df = component.as_df::<MyPoint>().unwrap();
///
/// //println!("{df:?}");
/// ```
///
/// Outputs:
/// ```text
/// ┌──────────┬───────────┐
/// │ instancepoint2d
/// │ --- ┆ --- │
/// │ u64 ┆ struct[2] │
/// ╞══════════╪═══════════╡
/// │ 42 ┆ {1.0,2.0} │
/// ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
/// │ 96 ┆ {3.0,4.0} │
/// └──────────┴───────────┘
/// ┌─────────────┬───────────┐
/// │ InstanceKeyMyPoint
/// │ --- ┆ --- │
/// │ u64 ┆ struct[2] │
/// ╞═════════════╪═══════════╡
/// │ 42 ┆ {1.0,2.0} │
/// ├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
/// │ 96 ┆ {3.0,4.0} │
/// └─────────────┴───────────┘
/// ```
///
pub fn get_component_with_instances(
Expand Down Expand Up @@ -76,32 +74,30 @@ pub fn get_component_with_instances(
///
/// ```
/// # use re_arrow_store::LatestAtQuery;
/// # use re_log_types::Timeline;
/// # use re_log_types::{Timeline, example_components::{MyColor, MyPoint, MyPoints}};
/// # use re_types_core::Component;
/// # use re_types::components::{Position2D, Color};
/// # use re_types::archetypes::Points2D;
/// # let store = re_query::__populate_example_store();
///
/// let ent_path = "point";
/// let query = LatestAtQuery::new(Timeline::new_sequence("frame_nr"), 123.into());
///
/// let arch_view = re_query::query_archetype::<Points2D>(
/// let arch_view = re_query::query_archetype::<MyPoints>(
/// &store,
/// &query,
/// &ent_path.into(),
/// )
/// .unwrap();
///
/// # #[cfg(feature = "polars")]
/// let df = arch_view.as_df2::<Position2D, Color>().unwrap();
/// let df = arch_view.as_df2::<MyPoint, MyColor>().unwrap();
///
/// //println!("{df:?}");
/// ```
///
/// Outputs:
/// ```text
/// ┌────────────────────┬───────────────┬─────────────────┐
/// │ rerun.components.InstanceKey ┆ rerun.components.Point2D ┆ rerun.components.Color
/// │ InstanceKey ┆ MyPoint ┆ MyColor
/// │ --- ┆ --- ┆ --- │
/// │ u64 ┆ struct[2] ┆ u32 │
/// ╞════════════════════╪═══════════════╪═════════════════╡
Expand Down Expand Up @@ -161,15 +157,15 @@ pub fn query_archetype<A: Archetype>(
/// Helper used to create an example store we can use for querying in doctests
pub fn __populate_example_store() -> DataStore {
use re_log_types::build_frame_nr;
use re_types::components::{Color, Position2D};
use re_log_types::example_components::{MyColor, MyPoint};

let mut store = DataStore::new(InstanceKey::name(), Default::default());

let ent_path = "point";
let timepoint = [build_frame_nr(123.into())];

let instances = vec![InstanceKey(42), InstanceKey(96)];
let positions = vec![Position2D::new(1.0, 2.0), Position2D::new(3.0, 4.0)];
let positions = vec![MyPoint::new(1.0, 2.0), MyPoint::new(3.0, 4.0)];

let row = DataRow::from_cells2_sized(
RowId::random(),
Expand All @@ -182,7 +178,7 @@ pub fn __populate_example_store() -> DataStore {
store.insert_row(&row).unwrap();

let instances = vec![InstanceKey(96)];
let colors = vec![Color::from(0xff000000)];
let colors = vec![MyColor::from(0xff000000)];

let row = DataRow::from_cells2_sized(
RowId::random(),
Expand All @@ -201,27 +197,24 @@ pub fn __populate_example_store() -> DataStore {
#[test]
fn simple_get_component() {
use re_arrow_store::LatestAtQuery;
use re_log_types::example_components::MyPoint;
use re_log_types::Timeline;
use re_types::components::Position2D;

let store = __populate_example_store();

let ent_path = "point";
let query = LatestAtQuery::new(Timeline::new_sequence("frame_nr"), 123.into());

let (_, component) =
get_component_with_instances(&store, &query, &ent_path.into(), Position2D::name()).unwrap();
get_component_with_instances(&store, &query, &ent_path.into(), MyPoint::name()).unwrap();

#[cfg(feature = "polars")]
{
let df = component.as_df::<Position2D>().unwrap();
let df = component.as_df::<MyPoint>().unwrap();
eprintln!("{df:?}");

let instances = vec![Some(InstanceKey(42)), Some(InstanceKey(96))];
let positions = vec![
Some(Position2D::new(1.0, 2.0)),
Some(Position2D::new(3.0, 4.0)),
];
let positions = vec![Some(MyPoint::new(1.0, 2.0)), Some(MyPoint::new(3.0, 4.0))];

let expected = crate::dataframe_util::df_builder2(&instances, &positions).unwrap();

Expand All @@ -237,27 +230,26 @@ fn simple_get_component() {
#[test]
fn simple_query_archetype() {
use re_arrow_store::LatestAtQuery;
use re_log_types::example_components::{MyColor, MyPoint, MyPoints};
use re_log_types::Timeline;
use re_types::archetypes::Points2D;
use re_types::components::{Color, Position2D};

let store = __populate_example_store();

let ent_path = "point";
let query = LatestAtQuery::new(Timeline::new_sequence("frame_nr"), 123.into());

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

let expected_positions = [Position2D::new(1.0, 2.0), Position2D::new(3.0, 4.0)];
let expected_colors = [None, Some(Color::from_unmultiplied_rgba(255, 0, 0, 0))];
let expected_positions = [MyPoint::new(1.0, 2.0), MyPoint::new(3.0, 4.0)];
let expected_colors = [None, Some(MyColor::from(0xff000000))];

let view_positions: Vec<_> = arch_view
.iter_required_component::<Position2D>()
.iter_required_component::<MyPoint>()
.unwrap()
.collect();

let view_colors: Vec<_> = arch_view
.iter_optional_component::<Color>()
.iter_optional_component::<MyColor>()
.unwrap()
.collect();

Expand All @@ -266,7 +258,7 @@ fn simple_query_archetype() {

#[cfg(feature = "polars")]
{
let df = arch_view.as_df2::<Position2D, Color>().unwrap();
let df = arch_view.as_df2::<MyPoint, MyColor>().unwrap();
eprintln!("{df:?}");
}
}

0 comments on commit a89c86d

Please sign in to comment.