Skip to content

Only override the arrow1 versions of the Loggable trait #8604

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

Merged
merged 8 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6538,7 +6538,6 @@ dependencies = [
"once_cell",
"parking_lot",
"rand",
"re_arrow2",
"re_entity_db",
"re_format",
"re_log",
Expand Down Expand Up @@ -6707,6 +6706,7 @@ version = "0.22.0-alpha.1+dev"
dependencies = [
"ahash",
"anyhow",
"arrow",
"bitflags 2.6.0",
"bytemuck",
"criterion",
Expand Down
25 changes: 4 additions & 21 deletions crates/store/re_chunk/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,6 @@ impl ChunkBuilder {
)
}

/// Add a row's worth of data using the given component data.
#[inline]
pub fn with_row_arrow2(
self,
row_id: RowId,
timepoint: impl Into<TimePoint>,
components: impl IntoIterator<Item = (ComponentDescriptor, Box<dyn Arrow2Array>)>,
) -> Self {
self.with_sparse_row(
row_id,
timepoint,
components
.into_iter()
.map(|(component_descr, array)| (component_descr, Some(array))),
)
}

/// Add a row's worth of data by destructuring an archetype into component columns.
#[inline]
pub fn with_archetype(
Expand All @@ -160,11 +143,11 @@ impl ChunkBuilder {
timepoint: impl Into<TimePoint>,
component_batch: &dyn ComponentBatch,
) -> Self {
self.with_row_arrow2(
self.with_row(
row_id,
timepoint,
component_batch
.to_arrow2()
.to_arrow()
.ok()
.map(|array| (component_batch.descriptor().into_owned(), array)),
)
Expand All @@ -178,12 +161,12 @@ impl ChunkBuilder {
timepoint: impl Into<TimePoint>,
component_batches: impl IntoIterator<Item = &'a dyn ComponentBatch>,
) -> Self {
self.with_row_arrow2(
self.with_row(
row_id,
timepoint,
component_batches.into_iter().filter_map(|component_batch| {
component_batch
.to_arrow2()
.to_arrow()
.ok()
.map(|array| (component_batch.descriptor().into_owned(), array))
}),
Expand Down
37 changes: 25 additions & 12 deletions crates/store/re_chunk/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ impl Chunk {
&self,
component_name: &ComponentName,
row_index: usize,
) -> Option<ChunkResult<ArrayRef>> {
self.component_batch_raw_arrow2(component_name, row_index)
.map(|res| res.map(|array| array.into()))
}

/// Returns the raw data for the specified component.
///
/// Returns an error if the row index is out of bounds.
#[inline]
fn component_batch_raw_arrow2(
&self,
component_name: &ComponentName,
row_index: usize,
) -> Option<ChunkResult<Box<dyn Arrow2Array>>> {
self.get_first_component(component_name)
.and_then(|list_array| {
Expand Down Expand Up @@ -50,7 +63,7 @@ impl Chunk {
Err(err) => return Some(Err(err)),
};

let data = C::from_arrow2(&*array);
let data = C::from_arrow(&*array);
Some(data.map_err(Into::into))
}

Expand All @@ -65,7 +78,7 @@ impl Chunk {
component_name: &ComponentName,
row_index: usize,
instance_index: usize,
) -> Option<ChunkResult<Box<dyn Arrow2Array>>> {
) -> Option<ChunkResult<ArrayRef>> {
let res = self.component_batch_raw(component_name, row_index)?;

let array = match res {
Expand All @@ -74,7 +87,7 @@ impl Chunk {
};

if array.len() > instance_index {
Some(Ok(array.sliced(instance_index, 1)))
Some(Ok(array.slice(instance_index, 1)))
} else {
Some(Err(crate::ChunkError::IndexOutOfBounds {
kind: "instance".to_owned(),
Expand All @@ -101,7 +114,7 @@ impl Chunk {
Err(err) => return Some(Err(err)),
};

match C::from_arrow2(&*array) {
match C::from_arrow(&*array) {
Ok(data) => data.into_iter().next().map(Ok), // NOTE: It's already sliced!
Err(err) => Some(Err(err.into())),
}
Expand All @@ -118,7 +131,7 @@ impl Chunk {
&self,
component_name: &ComponentName,
row_index: usize,
) -> Option<ChunkResult<Box<dyn Arrow2Array>>> {
) -> Option<ChunkResult<ArrayRef>> {
let res = self.component_batch_raw(component_name, row_index)?;

let array = match res {
Expand All @@ -127,7 +140,7 @@ impl Chunk {
};

if array.len() == 1 {
Some(Ok(array.sliced(0, 1)))
Some(Ok(array.slice(0, 1)))
} else {
Some(Err(crate::ChunkError::IndexOutOfBounds {
kind: "mono".to_owned(),
Expand All @@ -150,7 +163,7 @@ impl Chunk {
Err(err) => return Some(Err(err)),
};

match C::from_arrow2(&*array) {
match C::from_arrow(&*array) {
Ok(data) => data.into_iter().next().map(Ok), // NOTE: It's already sliced!
Err(err) => Some(Err(err.into())),
}
Expand Down Expand Up @@ -284,7 +297,7 @@ impl UnitChunkShared {
/// Returns an error if the data cannot be deserialized.
#[inline]
pub fn component_batch<C: Component>(&self) -> Option<ChunkResult<Vec<C>>> {
let data = C::from_arrow2(&*self.component_batch_raw_arrow2(&C::name())?);
let data = C::from_arrow(&*self.component_batch_raw(&C::name())?);
Some(data.map_err(Into::into))
}

Expand Down Expand Up @@ -322,11 +335,11 @@ impl UnitChunkShared {
let res = self.component_instance_raw(&C::name(), instance_index)?;

let array = match res {
Ok(array) => array,
Ok(array) => ArrayRef::from(array),
Err(err) => return Some(Err(err)),
};

match C::from_arrow2(&*array) {
match C::from_arrow(&*array) {
Ok(data) => data.into_iter().next().map(Ok), // NOTE: It's already sliced!
Err(err) => Some(Err(err.into())),
}
Expand Down Expand Up @@ -362,11 +375,11 @@ impl UnitChunkShared {
let res = self.component_mono_raw(&C::name())?;

let array = match res {
Ok(array) => array,
Ok(array) => ArrayRef::from(array),
Err(err) => return Some(Err(err)),
};

match C::from_arrow2(&*array) {
match C::from_arrow(&*array) {
Ok(data) => data.into_iter().next().map(Ok), // NOTE: It's already sliced!
Err(err) => Some(Err(err.into())),
}
Expand Down
4 changes: 2 additions & 2 deletions crates/store/re_chunk/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,8 @@ impl Chunk {
};
};

let values = list_array.values();
let values = match C::from_arrow2(&**values) {
let values = arrow::array::ArrayRef::from(list_array.values().clone());
let values = match C::from_arrow(&values) {
Ok(values) => values,
Err(err) => {
if cfg!(debug_assertions) {
Expand Down
8 changes: 4 additions & 4 deletions crates/store/re_chunk/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,12 +799,12 @@ mod tests {
let timepoint2 = [(Timeline::new_sequence("frame"), 2)];

let points32bit =
<MyPoint as re_types_core::LoggableBatch>::to_arrow2(&MyPoint::new(1.0, 1.0))?;
<MyPoint as re_types_core::LoggableBatch>::to_arrow(&MyPoint::new(1.0, 1.0))?;
let points64bit =
<MyPoint64 as re_types_core::LoggableBatch>::to_arrow2(&MyPoint64::new(1.0, 1.0))?;
<MyPoint64 as re_types_core::LoggableBatch>::to_arrow(&MyPoint64::new(1.0, 1.0))?;

let chunk1 = Chunk::builder(entity_path.into())
.with_row_arrow2(
.with_row(
row_id1,
timepoint1,
[
Expand All @@ -814,7 +814,7 @@ mod tests {
.build()?;

let chunk2 = Chunk::builder(entity_path.into())
.with_row_arrow2(
.with_row(
row_id2,
timepoint2,
[
Expand Down
3 changes: 2 additions & 1 deletion crates/store/re_format_arrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ fn parse_tuid(array: &dyn Array, index: usize) -> Option<Tuid> {
_ => (array.to_boxed(), index),
};

let tuids = Tuid::from_arrow2(array.as_ref()).ok()?;
let array = re_types_core::external::arrow::array::ArrayRef::from(array);
let tuids = Tuid::from_arrow(&array).ok()?;
tuids.get(index).copied()
}

Expand Down
20 changes: 10 additions & 10 deletions crates/store/re_log_types/src/path/entity_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,13 @@ re_types_core::macros::impl_into_cow!(EntityPath);

impl Loggable for EntityPath {
#[inline]
fn arrow2_datatype() -> arrow2::datatypes::DataType {
re_types_core::datatypes::Utf8::arrow2_datatype()
fn arrow_datatype() -> arrow::datatypes::DataType {
re_types_core::datatypes::Utf8::arrow_datatype()
}

fn to_arrow2_opt<'a>(
fn to_arrow_opt<'a>(
_data: impl IntoIterator<Item = Option<impl Into<std::borrow::Cow<'a, Self>>>>,
) -> re_types_core::SerializationResult<Box<dyn arrow2::array::Array>>
) -> re_types_core::SerializationResult<arrow::array::ArrayRef>
where
Self: 'a,
{
Expand All @@ -440,23 +440,23 @@ impl Loggable for EntityPath {
}

#[inline]
fn to_arrow2<'a>(
fn to_arrow<'a>(
data: impl IntoIterator<Item = impl Into<std::borrow::Cow<'a, Self>>>,
) -> re_types_core::SerializationResult<Box<dyn ::arrow2::array::Array>>
) -> re_types_core::SerializationResult<arrow::array::ArrayRef>
where
Self: 'a,
{
re_types_core::datatypes::Utf8::to_arrow2(
re_types_core::datatypes::Utf8::to_arrow(
data.into_iter()
.map(Into::into)
.map(|ent_path| re_types_core::datatypes::Utf8(ent_path.to_string().into())),
)
}

fn from_arrow2(
array: &dyn ::arrow2::array::Array,
fn from_arrow(
array: &dyn ::arrow::array::Array,
) -> re_types_core::DeserializationResult<Vec<Self>> {
Ok(re_types_core::datatypes::Utf8::from_arrow2(array)?
Ok(re_types_core::datatypes::Utf8::from_arrow(array)?
.into_iter()
.map(|utf8| Self::from(utf8.to_string()))
.collect())
Expand Down
13 changes: 6 additions & 7 deletions crates/store/re_types_core/benches/bench_tuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use criterion::{criterion_group, criterion_main, Criterion};

fn bench_arrow(c: &mut Criterion) {
use arrow2::array::Array;
use re_types_core::Loggable as _;

for elem_count in [1, 1000] {
Expand All @@ -14,9 +13,9 @@ fn bench_arrow(c: &mut Criterion) {

let tuids = vec![re_tuid::Tuid::new(); elem_count as usize];

group.bench_function("arrow2", |b| {
group.bench_function("arrow", |b| {
b.iter(|| {
let data: Box<dyn Array> = re_tuid::Tuid::to_arrow2(tuids.clone()).unwrap();
let data = re_tuid::Tuid::to_arrow(tuids.clone()).unwrap();
criterion::black_box(data)
});
});
Expand All @@ -26,12 +25,12 @@ fn bench_arrow(c: &mut Criterion) {
let mut group = c.benchmark_group(format!("arrow/deserialize/elem_count={elem_count}"));
group.throughput(criterion::Throughput::Elements(elem_count));

let data: Box<dyn Array> =
re_tuid::Tuid::to_arrow2(vec![re_tuid::Tuid::new(); elem_count as usize]).unwrap();
let data =
re_tuid::Tuid::to_arrow(vec![re_tuid::Tuid::new(); elem_count as usize]).unwrap();

group.bench_function("arrow2", |b| {
group.bench_function("arrow", |b| {
b.iter(|| {
let tuids = re_tuid::Tuid::from_arrow2(data.as_ref()).unwrap();
let tuids = re_tuid::Tuid::from_arrow(data.as_ref()).unwrap();
criterion::black_box(tuids)
});
});
Expand Down
18 changes: 0 additions & 18 deletions crates/store/re_types_core/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,6 @@ pub trait Archetype {
)
}

/// Given an iterator of Arrow arrays and their respective field metadata, deserializes them
/// into this archetype.
///
/// Arrow arrays that are unknown to this [`Archetype`] will simply be ignored and a warning
/// logged to stderr.
#[inline]
fn from_arrow2(
data: impl IntoIterator<Item = (arrow2::datatypes::Field, Box<dyn ::arrow2::array::Array>)>,
) -> DeserializationResult<Self>
where
Self: Sized,
{
Self::from_arrow2_components(
data.into_iter()
.map(|(field, array)| (field.name.into(), array)),
)
}

/// Given an iterator of Arrow arrays and their respective `ComponentNames`, deserializes them
/// into this archetype.
///
Expand Down
16 changes: 8 additions & 8 deletions crates/store/re_types_core/src/as_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,28 +312,28 @@ mod tests {
}

impl crate::Loggable for MyColor {
fn arrow2_datatype() -> arrow2::datatypes::DataType {
arrow2::datatypes::DataType::UInt32
fn arrow_datatype() -> arrow::datatypes::DataType {
arrow::datatypes::DataType::UInt32
}

fn to_arrow2_opt<'a>(
fn to_arrow_opt<'a>(
data: impl IntoIterator<Item = Option<impl Into<std::borrow::Cow<'a, Self>>>>,
) -> crate::SerializationResult<Box<dyn arrow2::array::Array>>
) -> crate::SerializationResult<arrow::array::ArrayRef>
where
Self: 'a,
{
use crate::datatypes::UInt32;
UInt32::to_arrow2_opt(
UInt32::to_arrow_opt(
data.into_iter()
.map(|opt| opt.map(Into::into).map(|c| UInt32(c.0))),
)
}

fn from_arrow2_opt(
data: &dyn arrow2::array::Array,
fn from_arrow_opt(
data: &dyn arrow::array::Array,
) -> crate::DeserializationResult<Vec<Option<Self>>> {
use crate::datatypes::UInt32;
Ok(UInt32::from_arrow2_opt(data)?
Ok(UInt32::from_arrow_opt(data)?
.into_iter()
.map(|opt| opt.map(|v| Self(v.0)))
.collect())
Expand Down
Loading
Loading