re_redap_tests 0.31.1

Official test suite for the Rerun Data Protocol
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
use std::collections::BTreeMap;

use arrow::array::RecordBatch;
use futures::StreamExt as _;
use itertools::Itertools as _;
use re_log_types::{EntityPath, TimeType};
use re_protos::EntryName;
use re_protos::cloud::v1alpha1::ext::DatasetEntry;
use re_protos::cloud::v1alpha1::rerun_cloud_service_server::RerunCloudService;
use re_protos::cloud::v1alpha1::{
    CreateDatasetEntryRequest, DataSource, QueryTasksOnCompletionRequest, QueryTasksResponse,
    RegisterWithDatasetRequest, RegisterWithDatasetResponse, ext,
};
use re_protos::common::v1alpha1::TaskId;
use re_protos::common::v1alpha1::ext::IfDuplicateBehavior;
use re_protos::headers::RerunHeadersInjectorExt as _;

/// Test helper: parse a string into an `EntryName`, panicking on invalid names.
pub fn entry_name(name: &str) -> EntryName {
    EntryName::new(name).unwrap()
}
use re_types_core::AsComponents;
use tonic::async_trait;
use url::Url;

use crate::utils::rerun::{
    create_recording_with_static_components, multi_chunked_entities_recording,
};
use crate::{
    RecordBatchTestExt as _, TempPath, TuidPrefix, create_nasty_recording,
    create_recording_with_embeddings, create_recording_with_properties,
    create_recording_with_scalars, create_recording_with_text, create_simple_recording,
};

/// Extension trait for the most common test setup tasks.
#[async_trait]
pub trait RerunCloudServiceExt: RerunCloudService {
    async fn create_dataset_entry_with_name(&self, dataset_name: &str) -> DatasetEntry;

    async fn register_with_dataset_name_blocking(
        &self,
        dataset_name: &str,
        data_sources: Vec<re_protos::cloud::v1alpha1::DataSource>,
    );

    async fn register_with_dataset_name_blocking_with_behavior(
        &self,
        dataset_name: &str,
        data_sources: Vec<re_protos::cloud::v1alpha1::DataSource>,
        on_duplicate: IfDuplicateBehavior,
    );

    async fn register_table_with_name(&self, table_name: &str, path: &std::path::Path);

    async fn unregister_from_dataset_name(
        &self,
        dataset_name: &str,
        segments_to_drop: &[&str],
        layers_to_drop: &[&str],
    ) -> tonic::Result<RecordBatch>;
}

#[async_trait]
impl<T: RerunCloudService> RerunCloudServiceExt for T {
    async fn create_dataset_entry_with_name(&self, dataset_name: &str) -> DatasetEntry {
        self.create_dataset_entry(tonic::Request::new(CreateDatasetEntryRequest {
            name: Some(dataset_name.to_owned()),
            id: None,
        }))
        .await
        .expect("create_dataset_entry should succeed")
        .into_inner()
        .dataset
        .expect("some dataset field expected")
        .try_into()
        .expect("conversion to ext::DatasetEntry should succeed")
    }

    async fn register_with_dataset_name_blocking(
        &self,
        dataset_name: &str,
        data_sources: Vec<re_protos::cloud::v1alpha1::DataSource>,
    ) {
        self.register_with_dataset_name_blocking_with_behavior(
            dataset_name,
            data_sources,
            IfDuplicateBehavior::Error,
        )
        .await;
    }

    async fn register_with_dataset_name_blocking_with_behavior(
        &self,
        dataset_name: &str,
        data_sources: Vec<re_protos::cloud::v1alpha1::DataSource>,
        on_duplicate: IfDuplicateBehavior,
    ) {
        let request = tonic::Request::new(RegisterWithDatasetRequest {
            data_sources,
            on_duplicate: re_protos::common::v1alpha1::IfDuplicateBehavior::from(on_duplicate)
                as i32,
        })
        .with_entry_name(entry_name(dataset_name))
        .expect("Failed to create a request");

        register_with_dataset_blocking(self, request).await;
    }

    /// Helper to fire an [`UnregisterFromDatasetRequest`].
    ///
    /// `segments_to_drop` and `layers_to_drop` are combined using an *outer product*.
    /// Refer to [`UnregisterFromDatasetRequest`]'s to learn more about the semantics.
    ///
    /// [`UnregisterFromDatasetRequest`]: re_protos::cloud::v1alpha1::ext::UnregisterFromDatasetRequest
    async fn unregister_from_dataset_name(
        &self,
        dataset_name: &str,
        segments_to_drop: &[&str],
        layers_to_drop: &[&str],
    ) -> tonic::Result<RecordBatch> {
        let request = re_protos::cloud::v1alpha1::ext::UnregisterFromDatasetRequest {
            segments_to_drop: segments_to_drop
                .iter()
                .map(|id| (*id).to_owned().into())
                .collect(),
            layers_to_drop: layers_to_drop.iter().map(|s| (*s).to_owned()).collect(),
            force: false,
        };

        let request = tonic::Request::new(request.into())
            .with_entry_name(entry_name(dataset_name))
            .expect("Failed to create a request");

        use futures::TryStreamExt as _;
        let responses: Vec<_> = self
            .unregister_from_dataset(request)
            .await?
            .into_inner()
            .try_collect()
            .await
            .expect("could not collect responses");

        let batches: Vec<RecordBatch> = responses
            .into_iter()
            .map(|resp| {
                resp.data
                    .expect("missing data in response")
                    .try_into()
                    .expect("could not convert response data to record batch")
            })
            .collect_vec();

        Ok(arrow::compute::concat_batches(
            batches
                .first()
                .expect("there should be at least one batch")
                .schema_ref(),
            &batches,
        )
        .expect("could not concatenate batches"))
    }

    async fn register_table_with_name(&self, table_name: &str, path: &std::path::Path) {
        let table_url =
            Url::from_directory_path(path).expect("Unable to create URL from directory path");
        let provider_details = re_protos::cloud::v1alpha1::ext::ProviderDetails::LanceTable(
            re_protos::cloud::v1alpha1::ext::LanceTable { table_url },
        );
        let request = re_protos::cloud::v1alpha1::ext::RegisterTableRequest {
            name: entry_name(table_name),
            provider_details,
        };
        let request = tonic::Request::new(request.try_into().expect("Failed to convert request"));

        self.register_table(request)
            .await
            .expect("register table should succeed");
    }
}

// ---

/// Register data sources and wait for task completion, returning the task result batches.
pub async fn register_and_wait(
    service: &impl re_protos::cloud::v1alpha1::rerun_cloud_service_server::RerunCloudService,
    request: tonic::Request<RegisterWithDatasetRequest>,
) -> Vec<RecordBatch> {
    let resp: RecordBatch = service
        .register_with_dataset(request)
        .await
        .expect("register_with_dataset should succeed")
        .into_inner()
        .data
        .expect("data expected")
        .try_into()
        .expect("record batch expected");

    // extract task ids from the response record batch
    let task_ids: Vec<TaskId> = resp
        .column_by_name(RegisterWithDatasetResponse::FIELD_TASK_ID)
        .expect("task_id column expected")
        .as_any()
        .downcast_ref::<arrow::array::StringArray>()
        .expect("task_id column should be a string array")
        .iter()
        .flatten()
        .map(|s| TaskId { id: s.to_owned() })
        .unique() // dups are possible because of batching partitions per task
        .collect();

    // Early return if no tasks were created (e.g., all partitions were skipped)
    if task_ids.is_empty() {
        return vec![];
    }

    service
        .query_tasks_on_completion(tonic::Request::new(QueryTasksOnCompletionRequest {
            ids: task_ids,
            timeout: Some(prost_types::Duration {
                seconds: 20,
                nanos: 0,
            }),
        }))
        .await
        .expect("should get query results")
        .into_inner()
        .collect::<Vec<_>>()
        .await
        .into_iter()
        .map(|resp| {
            resp.expect("Failed to get task completion response")
                .data
                .expect("Expected response data")
                .try_into()
                .expect("Failed to decode response data")
        })
        .collect()
}

async fn register_with_dataset_blocking(
    service: &impl re_protos::cloud::v1alpha1::rerun_cloud_service_server::RerunCloudService,
    request: tonic::Request<RegisterWithDatasetRequest>,
) {
    let task_results = register_and_wait(service, request).await;

    // Verify all tasks completed successfully
    for batch in &task_results {
        let status_col = batch
            .column_by_name(QueryTasksResponse::FIELD_EXEC_STATUS)
            .expect("exec_status column expected")
            .as_any()
            .downcast_ref::<arrow::array::StringArray>()
            .expect("exec_status should be string array");

        for i in 0..batch.num_rows() {
            let status = status_col.value(i);
            assert_eq!(
                status, "success",
                "Expected task to succeed, got status: {status}"
            );
        }
    }
}

// ---

pub enum LayerType {
    /// See [`crate::utils::rerun::create_simple_recording`]
    Simple {
        entities: &'static [&'static str],
        start_time: i64,
        time_type: TimeType,
    },

    /// See [`crate::create_nasty_recording`]
    Nasty { entities: &'static [&'static str] },

    /// See [`crate::create_recording_with_properties`]
    Properties {
        properties: BTreeMap<String, Vec<Box<dyn AsComponents>>>,
    },

    /// See [`crate::create_recording_with_static_components`]
    StaticComponents {
        components: BTreeMap<EntityPath, Box<dyn AsComponents>>,
    },

    /// See [`crate::create_recording_with_scalars`].
    Scalars { n: usize },

    /// See [`crate::create_recording_with_text`].
    Text,

    /// See [`crate::create_recording_with_embeddings`].
    Embeddings {
        embeddings: u32,
        embeddings_per_row: u32,
    },

    /// See [`crate::create_simple_blueprint`]
    SimpleBlueprint,

    /// See [`crate::multi_chunked_entities_recording`]
    MultiChunkedEntities { entities: &'static [&'static str] },
}

impl LayerType {
    pub fn simple(entities: &'static [&'static str]) -> Self {
        Self::Simple {
            entities,
            start_time: 0,
            time_type: TimeType::Sequence,
        }
    }

    pub fn simple_with_time(
        entities: &'static [&'static str],
        start_time: i64,
        time_type: TimeType,
    ) -> Self {
        Self::Simple {
            entities,
            start_time,
            time_type,
        }
    }

    pub fn nasty(entities: &'static [&'static str]) -> Self {
        Self::Nasty { entities }
    }

    pub fn properties(
        properties: impl IntoIterator<Item = (String, Box<dyn AsComponents>)>,
    ) -> Self {
        Self::Properties {
            properties: properties.into_iter().map(|(k, v)| (k, vec![v])).collect(),
        }
    }

    pub fn static_components(
        components: impl IntoIterator<Item = (EntityPath, Box<dyn AsComponents>)>,
    ) -> Self {
        Self::StaticComponents {
            components: components.into_iter().collect(),
        }
    }

    pub fn scalars(n: usize) -> Self {
        Self::Scalars { n }
    }

    pub fn text() -> Self {
        Self::Text
    }

    pub fn embeddings(embeddings: u32, embeddings_per_row: u32) -> Self {
        Self::Embeddings {
            embeddings,
            embeddings_per_row,
        }
    }

    pub fn simple_blueprint() -> Self {
        Self::SimpleBlueprint
    }

    fn into_recording(self, tuid_prefix: TuidPrefix, segment_id: &str) -> anyhow::Result<TempPath> {
        match self {
            Self::Simple {
                entities,
                start_time,
                time_type,
            } => create_simple_recording(tuid_prefix, segment_id, entities, start_time, time_type),

            Self::Nasty { entities } => create_nasty_recording(tuid_prefix, segment_id, entities),

            Self::Properties { properties } => create_recording_with_properties(
                tuid_prefix,
                segment_id,
                // TODO(ab): avoid this annoying conversion
                properties
                    .iter()
                    .map(|(k, v)| (k.clone(), v.iter().map(|v| v.as_ref()).collect()))
                    .collect(),
            ),

            Self::StaticComponents { components } => {
                create_recording_with_static_components(tuid_prefix, segment_id, components)
            }

            Self::Scalars { n } => create_recording_with_scalars(tuid_prefix, segment_id, n),

            Self::Text => create_recording_with_text(tuid_prefix, segment_id),

            Self::Embeddings {
                embeddings,
                embeddings_per_row,
            } => create_recording_with_embeddings(
                tuid_prefix,
                segment_id,
                embeddings,
                embeddings_per_row,
            ),

            Self::SimpleBlueprint => crate::create_simple_blueprint(tuid_prefix, segment_id),

            Self::MultiChunkedEntities { entities } => {
                multi_chunked_entities_recording(tuid_prefix, segment_id, entities)
            }
        }
    }

    pub fn multi_chunked_entities(entities: &'static [&'static str]) -> Self {
        Self::MultiChunkedEntities { entities }
    }
}

pub struct LayerDefinition {
    pub segment_id: &'static str,
    pub layer_name: Option<&'static str>,
    pub layer_type: LayerType,
}

impl LayerDefinition {
    /// A simple layer with the provided entities
    pub fn simple(segment_id: &'static str, entities: &'static [&'static str]) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::simple(entities),
        }
    }

    pub fn simple_with_time(
        segment_id: &'static str,
        entities: &'static [&'static str],
        start_time: i64,
        time_type: TimeType,
    ) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::simple_with_time(entities, start_time, time_type),
        }
    }

    /// A layer with a nasty chunk representation for the provided entities.
    pub fn nasty(segment_id: &'static str, entities: &'static [&'static str]) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::nasty(entities),
        }
    }

    /// A layer with just the provided properties.
    pub fn properties(
        segment_id: &'static str,
        properties: impl IntoIterator<Item = (String, Box<dyn AsComponents>)>,
    ) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::properties(properties),
        }
    }

    pub fn static_components(
        segment_id: &'static str,
        components: impl IntoIterator<Item = (EntityPath, Box<dyn AsComponents>)>,
    ) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::static_components(components),
        }
    }

    /// A simple layer with a bunch of scalars, for testing B-Tree indexes.
    pub fn scalars(segment_id: &'static str) -> Self {
        Self {
            segment_id,
            layer_name: None,
            // TODO(cmc): we can always expose `n` later, if and when it's useful.
            layer_type: LayerType::scalars(10),
        }
    }

    /// A simple layer with a bunch of text, for testing FTS indexes.
    pub fn text(segment_id: &'static str) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::text(),
        }
    }

    /// A simple layer with a bunch of embeddings, for testing Vector indexes.
    pub fn embeddings(segment_id: &'static str, embeddings: u32, embeddings_per_row: u32) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::embeddings(embeddings, embeddings_per_row),
        }
    }

    pub fn simple_blueprint(segment_id: &'static str) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::simple_blueprint(),
        }
    }

    pub fn layer_name(mut self, layer_name: &'static str) -> Self {
        self.layer_name = Some(layer_name);
        self
    }

    pub fn multi_chunked_entities(
        segment_id: &'static str,
        entities: &'static [&'static str],
    ) -> Self {
        Self {
            segment_id,
            layer_name: None,
            layer_type: LayerType::multi_chunked_entities(entities),
        }
    }
}

/// Helper function to construct property tuples
pub fn prop(
    key: impl Into<String>,
    value: impl AsComponents + 'static,
) -> (String, Box<dyn AsComponents>) {
    (key.into(), Box::new(value) as Box<dyn AsComponents>)
}

/// Utility to simplify the creation of data sources to register with a dataset.
///
/// This utility holds the [`TempPath`] instances, so it should not be dropped until the end of
/// the test, lest the recording files are prematurely cleaned up.
pub struct DataSourcesDefinition {
    layers: Vec<(Option<String>, TempPath)>,
}

impl DataSourcesDefinition {
    /// Create layers with the provided definitions.
    ///
    /// The provided `tuid_prefix` is used for the first layer and then incremented.
    ///
    /// Note: we require an explicit prefix, otherwise using two `DataSourcesDefinition`s in the
    /// same test will cause a chunk id conflict, which is UB :true-story:
    pub fn new_with_tuid_prefix(
        tuid_prefix: TuidPrefix,
        layers: impl IntoIterator<Item = LayerDefinition>,
    ) -> Self {
        Self {
            layers: layers
                .into_iter()
                .enumerate()
                .map(|(tuid_prefix_increment, layer)| {
                    (
                        layer.layer_name.map(|s| s.to_owned()),
                        layer
                            .layer_type
                            .into_recording(
                                tuid_prefix.saturating_add(tuid_prefix_increment as _),
                                layer.segment_id,
                            )
                            .unwrap(),
                    )
                })
                .collect(),
        }
    }

    pub fn to_data_sources_ext(&self) -> Vec<ext::DataSource> {
        self.layers
            .iter()
            .map(|(layer_name, path)| ext::DataSource {
                storage_url: Url::from_file_path(path.as_path()).unwrap(),
                layer: layer_name
                    .clone()
                    .unwrap_or_else(|| ext::DataSource::DEFAULT_LAYER.to_owned()),
                is_prefix: false,
                kind: ext::DataSourceKind::Rrd,
            })
            .collect()
    }

    pub fn to_data_sources(&self) -> Vec<DataSource> {
        self.to_data_sources_ext()
            .into_iter()
            .map(Into::into)
            .collect()
    }
}

// ---

/// Concatenate record batches.
///
/// This function implicitly tests the following properties:
/// - There is always at least one record batch, even if it is empty.
/// - All record batches have the same schema.
pub fn concat_record_batches(record_batches: &[RecordBatch]) -> RecordBatch {
    arrow::compute::concat_batches(
        record_batches
            .first()
            .expect("at least one record batch must be passed")
            .schema_ref(),
        record_batches,
    )
    .expect("record batches should be concatenable")
    .auto_sort_rows()
    .expect("record batches should be sortable")
}