re_sorbet/
index_column_descriptor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use arrow::datatypes::{DataType as ArrowDatatype, Field as ArrowField};

use re_log_types::{Timeline, TimelineName};

use crate::MetadataExt as _;

#[derive(thiserror::Error, Debug)]
#[error("Unsupported time type: {datatype:?}")]
pub struct UnsupportedTimeType {
    pub datatype: ArrowDatatype,
}

/// Describes a time column, such as `log_time`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IndexColumnDescriptor {
    /// The timeline this column is associated with.
    timeline: Timeline,

    /// The Arrow datatype of the column.
    datatype: ArrowDatatype,

    /// Are the indices in this column sorted?
    ///
    /// `false` means either "unsorted" or "unknown".
    is_sorted: bool,
}

impl PartialOrd for IndexColumnDescriptor {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for IndexColumnDescriptor {
    #[inline]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let Self {
            timeline,
            datatype: _,
            is_sorted: _,
        } = self;
        timeline.cmp(&other.timeline)
    }
}

impl IndexColumnDescriptor {
    /// Used when returning a null column, i.e. when a lookup failed.
    #[inline]
    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),
            datatype: ArrowDatatype::Null,
            is_sorted: true,
        }
    }

    #[inline]
    pub fn from_timeline(timeline: Timeline, is_sorted: bool) -> Self {
        Self {
            timeline,
            datatype: timeline.datatype(),
            is_sorted,
        }
    }

    #[inline]
    pub fn timeline(&self) -> Timeline {
        self.timeline
    }

    #[inline]
    pub fn timeline_name(&self) -> TimelineName {
        *self.timeline.name()
    }

    #[inline]
    pub fn column_name(&self) -> &str {
        self.timeline.name()
    }

    #[inline]
    pub fn datatype(&self) -> &ArrowDatatype {
        &self.datatype
    }

    /// Are the indices in this column sorted?
    ///
    /// `false` means either "unsorted" or "unknown".
    #[inline]
    pub fn is_sorted(&self) -> bool {
        self.is_sorted
    }

    #[inline]
    pub fn to_arrow_field(&self) -> ArrowField {
        let Self {
            timeline,
            datatype,
            is_sorted,
        } = self;

        let nullable = true; // Time column must be nullable since static data doesn't have a time.

        let mut metadata = std::collections::HashMap::from([
            (
                "rerun.kind".to_owned(),
                crate::ColumnKind::Index.to_string(),
            ),
            ("rerun.index_name".to_owned(), timeline.name().to_string()),
        ]);
        if *is_sorted {
            metadata.insert("rerun.is_sorted".to_owned(), "true".to_owned());
        }

        ArrowField::new(timeline.name().to_string(), datatype.clone(), nullable)
            .with_metadata(metadata)
    }
}

impl From<Timeline> for IndexColumnDescriptor {
    fn from(timeline: Timeline) -> Self {
        Self {
            timeline,
            datatype: timeline.datatype(),
            is_sorted: false, // assume the worst
        }
    }
}

impl TryFrom<&ArrowField> for IndexColumnDescriptor {
    type Error = UnsupportedTimeType;

    fn try_from(field: &ArrowField) -> Result<Self, Self::Error> {
        let name = if let Some(name) = field.metadata().get("rerun.index_name") {
            name.to_owned()
        } else {
            re_log::warn_once!(
                "Timeline '{}' is missing 'rerun.index_name' metadata. Falling back on field/column name",
                field.name()
            );
            field.name().to_owned()
        };

        let datatype = field.data_type().clone();

        let Some(time_type) = re_log_types::TimeType::from_arrow_datatype(&datatype) else {
            return Err(UnsupportedTimeType { datatype });
        };

        let timeline = Timeline::new(name, time_type);

        Ok(Self {
            timeline,
            datatype,
            is_sorted: field.metadata().get_bool("rerun.is_sorted"),
        })
    }
}