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

Refactor TimeColumnDescriptor #8658

Merged
merged 5 commits into from
Jan 14, 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
59 changes: 43 additions & 16 deletions crates/store/re_chunk_store/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::{BTreeMap, BTreeSet};
use std::ops::Deref;
use std::ops::DerefMut;

use arrow::datatypes::DataType as ArrowDatatype;
use arrow2::{
array::ListArray as ArrowListArray,
datatypes::{DataType as Arrow2Datatype, Field as Arrow2Field},
Expand Down Expand Up @@ -45,7 +46,7 @@ impl ColumnDescriptor {
#[inline]
pub fn datatype(&self) -> Arrow2Datatype {
match self {
Self::Time(descr) => descr.datatype.clone(),
Self::Time(descr) => descr.datatype().into(),
Self::Component(descr) => descr.returned_datatype(),
}
}
Expand Down Expand Up @@ -79,10 +80,9 @@ impl ColumnDescriptor {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TimeColumnDescriptor {
/// The timeline this column is associated with.
pub timeline: Timeline,
timeline: Timeline,

/// The Arrow datatype of the column.
pub datatype: Arrow2Datatype,
is_null: bool,
}

impl PartialOrd for TimeColumnDescriptor {
Expand All @@ -97,17 +97,49 @@ impl Ord for TimeColumnDescriptor {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let Self {
timeline,
datatype: _,
is_null: _,
} = self;
timeline.cmp(&other.timeline)
}
}

impl TimeColumnDescriptor {
/// Used when returning a null column, i.e. when a lookup failed.
pub fn new_null(name: TimelineName) -> Self {
Self {
// TODO(cmc): I picked a sequence here because I have to pick something.
// It doesn't matter, only the name will remain in the Arrow schema anyhow.
timeline: Timeline::new_sequence(name),
is_null: true,
}
}

pub fn timeline(&self) -> Timeline {
self.timeline
}

pub fn name(&self) -> &TimelineName {
self.timeline.name()
}

pub fn typ(&self) -> re_log_types::TimeType {
self.timeline.typ()
}

#[inline]
pub fn datatype(&self) -> ArrowDatatype {
let Self { timeline, is_null } = self;
if *is_null {
ArrowDatatype::Null
} else {
timeline.typ().datatype()
}
}

fn metadata(&self) -> arrow2::datatypes::Metadata {
let Self {
timeline,
datatype: _,
is_null: _,
} = self;

std::iter::once(Some((
Expand All @@ -119,15 +151,10 @@ impl TimeColumnDescriptor {
}

#[inline]
// Time column must be nullable since static data doesn't have a time.
pub fn to_arrow_field(&self) -> Arrow2Field {
let Self { timeline, datatype } = self;
Arrow2Field::new(
timeline.name().to_string(),
datatype.clone(),
true, /* nullable */
)
.with_metadata(self.metadata())
let nullable = true; // Time column must be nullable since static data doesn't have a time.
Arrow2Field::new(self.name().to_string(), self.datatype().into(), nullable)
.with_metadata(self.metadata())
}
}

Expand Down Expand Up @@ -708,7 +735,7 @@ impl ChunkStore {
let timelines = self.all_timelines_sorted().into_iter().map(|timeline| {
ColumnDescriptor::Time(TimeColumnDescriptor {
timeline,
datatype: timeline.datatype().into(),
is_null: false,
})
});

Expand Down Expand Up @@ -782,7 +809,7 @@ impl ChunkStore {

TimeColumnDescriptor {
timeline,
datatype: timeline.datatype().into(),
is_null: false,
}
}

Expand Down
149 changes: 68 additions & 81 deletions crates/store/re_dataframe/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,82 +373,73 @@ impl<E: StorageEngineLike> QueryHandle<E> {
) -> Vec<(usize, ColumnDescriptor)> {
selection
.iter()
.map(|column| {
match column {
ColumnSelector::Time(selected_column) => {
let TimeColumnSelector {
timeline: selected_timeline,
} = selected_column;

view_contents
.iter()
.enumerate()
.filter_map(|(idx, view_column)| match view_column {
ColumnDescriptor::Time(view_descr) => Some((idx, view_descr)),
ColumnDescriptor::Component(_) => None,
})
.find(|(_idx, view_descr)| {
*view_descr.timeline.name() == *selected_timeline
})
.map_or_else(
|| {
(
usize::MAX,
ColumnDescriptor::Time(TimeColumnDescriptor {
// TODO(cmc): I picked a sequence here because I have to pick something.
// It doesn't matter, only the name will remain in the Arrow schema anyhow.
timeline: Timeline::new_sequence(*selected_timeline),
datatype: arrow2::datatypes::DataType::Null,
emilk marked this conversation as resolved.
Show resolved Hide resolved
}),
)
},
|(idx, view_descr)| {
(idx, ColumnDescriptor::Time(view_descr.clone()))
},
)
}
.map(|column| match column {
ColumnSelector::Time(selected_column) => {
let TimeColumnSelector {
timeline: selected_timeline,
} = selected_column;

view_contents
.iter()
.enumerate()
.filter_map(|(idx, view_column)| match view_column {
ColumnDescriptor::Time(view_descr) => Some((idx, view_descr)),
ColumnDescriptor::Component(_) => None,
})
.find(|(_idx, view_descr)| *view_descr.name() == *selected_timeline)
.map_or_else(
|| {
(
usize::MAX,
ColumnDescriptor::Time(TimeColumnDescriptor::new_null(
*selected_timeline,
)),
)
},
|(idx, view_descr)| (idx, ColumnDescriptor::Time(view_descr.clone())),
)
}

ColumnSelector::Component(selected_column) => {
let ComponentColumnSelector {
entity_path: selected_entity_path,
component_name: selected_component_name,
} = selected_column;

view_contents
.iter()
.enumerate()
.filter_map(|(idx, view_column)| match view_column {
ColumnDescriptor::Component(view_descr) => Some((idx, view_descr)),
ColumnDescriptor::Time(_) => None,
})
.find(|(_idx, view_descr)| {
view_descr.entity_path == *selected_entity_path
&& view_descr.component_name.matches(selected_component_name)
})
.map_or_else(
|| {
(
usize::MAX,
ColumnDescriptor::Component(ComponentColumnDescriptor {
entity_path: selected_entity_path.clone(),
archetype_name: None,
archetype_field_name: None,
component_name: ComponentName::from(
selected_component_name.clone(),
),
store_datatype: arrow2::datatypes::DataType::Null,
is_static: false,
is_indicator: false,
is_tombstone: false,
is_semantically_empty: false,
}),
)
},
|(idx, view_descr)| {
(idx, ColumnDescriptor::Component(view_descr.clone()))
},
)
}
ColumnSelector::Component(selected_column) => {
let ComponentColumnSelector {
entity_path: selected_entity_path,
component_name: selected_component_name,
} = selected_column;

view_contents
.iter()
.enumerate()
.filter_map(|(idx, view_column)| match view_column {
ColumnDescriptor::Component(view_descr) => Some((idx, view_descr)),
ColumnDescriptor::Time(_) => None,
})
.find(|(_idx, view_descr)| {
view_descr.entity_path == *selected_entity_path
&& view_descr.component_name.matches(selected_component_name)
})
.map_or_else(
|| {
(
usize::MAX,
ColumnDescriptor::Component(ComponentColumnDescriptor {
entity_path: selected_entity_path.clone(),
archetype_name: None,
archetype_field_name: None,
component_name: ComponentName::from(
selected_component_name.clone(),
),
store_datatype: arrow2::datatypes::DataType::Null,
is_static: false,
is_indicator: false,
is_tombstone: false,
is_semantically_empty: false,
}),
)
},
|(idx, view_descr)| {
(idx, ColumnDescriptor::Component(view_descr.clone()))
},
)
}
})
.collect_vec()
Expand Down Expand Up @@ -1248,14 +1239,10 @@ impl<E: StorageEngineLike> QueryHandle<E> {
.iter()
.map(|(view_idx, column)| match column {
ColumnDescriptor::Time(descr) => {
max_value_per_index.get(&descr.timeline).map_or_else(
max_value_per_index.get(&descr.timeline()).map_or_else(
|| arrow2::array::new_null_array(column.datatype(), 1),
|(_time, time_sliced)| {
descr
.timeline
.typ()
.make_arrow_array(time_sliced.clone())
.into()
descr.typ().make_arrow_array(time_sliced.clone()).into()
},
)
}
Expand Down
36 changes: 17 additions & 19 deletions crates/viewer/re_view_dataframe/src/dataframe_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl RowsDisplayData {
.iter()
.find_position(|desc| match desc {
ColumnDescriptor::Time(time_column_desc) => {
&time_column_desc.timeline == query_timeline
&time_column_desc.timeline() == query_timeline
}
ColumnDescriptor::Component(_) => false,
})
Expand Down Expand Up @@ -283,29 +283,27 @@ impl egui_table::TableDelegate for DataframeTableDelegate<'_> {
.unwrap_or_else(|| Timeline::new("", TimeType::Sequence));

// if this column can actually be hidden, then that's the corresponding action
let hide_action = match column {
ColumnDescriptor::Time(desc) => {
(desc.timeline != filtered_index).then(|| {
HideColumnAction::HideTimeColumn {
timeline_name: *desc.timeline.name(),
}
})
}

ColumnDescriptor::Component(desc) => {
Some(HideColumnAction::HideComponentColumn {
entity_path: desc.entity_path.clone(),
component_name: desc.component_name,
})
}
};
let hide_action =
match column {
ColumnDescriptor::Time(desc) => (desc.timeline() != filtered_index)
.then(|| HideColumnAction::HideTimeColumn {
timeline_name: *desc.name(),
}),

ColumnDescriptor::Component(desc) => {
Some(HideColumnAction::HideComponentColumn {
entity_path: desc.entity_path.clone(),
component_name: desc.component_name,
})
}
};

let header_ui = |ui: &mut egui::Ui| {
let text = egui::RichText::new(column.short_name()).strong();

let is_selected = match column {
ColumnDescriptor::Time(descr) => {
&descr.timeline == self.ctx.rec_cfg.time_ctrl.read().timeline()
&descr.timeline() == self.ctx.rec_cfg.time_ctrl.read().timeline()
}
ColumnDescriptor::Component(component_column_descriptor) => self
.ctx
Expand All @@ -323,7 +321,7 @@ impl egui_table::TableDelegate for DataframeTableDelegate<'_> {
self.ctx.command_sender.send_system(
re_viewer_context::SystemCommand::SetActiveTimeline {
rec_id: self.ctx.recording_id().clone(),
timeline: descr.timeline,
timeline: descr.timeline(),
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl DisplayColumn {
) -> Result<Self, DisplayRecordBatchError> {
match column_descriptor {
ColumnDescriptor::Time(desc) => {
let timeline = desc.timeline;
let timeline = desc.timeline();

let time_data = TimeColumn::read_array(column_data).map_err(|err| {
DisplayRecordBatchError::BadTimeColumn {
Expand Down
4 changes: 2 additions & 2 deletions crates/viewer/re_view_dataframe/src/view_query/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ impl Query {
.filter(|column| match column {
ColumnDescriptor::Time(desc) => {
// we always include the query timeline column because we need it for the dataframe ui
desc.timeline.name() == &query_timeline_name
|| selected_time_columns.contains(desc.timeline.name())
desc.name() == &query_timeline_name
|| selected_time_columns.contains(desc.name())
}

ColumnDescriptor::Component(desc) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/viewer/re_view_dataframe/src/view_query/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl Query {

// The query timeline is always active because it's necessary for the dataframe ui
// (for tooltips).
let is_query_timeline = time_column_descriptor.timeline.name() == timeline.name();
let is_query_timeline = time_column_descriptor.name() == timeline.name();
let is_enabled = !is_query_timeline;
let mut is_visible =
is_query_timeline || selected_columns.contains(&column_selector);
Expand Down
Loading
Loading