re_dataframe_ui 0.31.3

Rich table widget over DataFusion.
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
use std::mem;
use std::sync::Arc;

use arrow::datatypes::{DataType, SchemaRef};
use crossbeam::channel::{Receiver, TryRecvError};
use datafusion::common::{DataFusionError, TableReference};
use datafusion::execution::SendableRecordBatchStream;
use datafusion::functions::expr_fn::concat;
use datafusion::logical_expr::{binary_expr, col as datafusion_col, lit};
use datafusion::prelude::{SessionContext, cast, encode};
use futures::{StreamExt as _, TryStreamExt as _};
use re_log::{error, warn};
use re_log_types::Timestamp;
use re_mutex::Mutex;
use re_quota_channel::send_crossbeam;
use re_sorbet::{BatchType, SorbetBatch, SorbetSchema};
use re_viewer_context::AsyncRuntimeHandle;

use crate::ColumnFilter;
use crate::table_blueprint::{EntryLinksSpec, SegmentLinksSpec, SortBy, TableBlueprint};
use crate::table_selection::TableSelectionState;

/// Make sure we escape column names correctly for datafusion.
///
/// Background: even when round-tripping column names from the very schema that datafusion returns,
/// it can happen that column names have the "wrong" case and must be escaped. See this issue:
/// <https://github.com/apache/datafusion/issues/15922>
///
/// This function is named such as to replace the datafusion's `col` function, so we do the right
/// thing even if we forget about it.
fn col(name: &str) -> datafusion::logical_expr::Expr {
    datafusion_col(format!("{name:?}"))
}

/// The subset of [`TableBlueprint`] that is actually handled by datafusion.
///
/// In general, there are aspects of a table blueprint that are handled by the UI in an immediate
/// mode fashion (e.g. is a column visible?), and other aspects that are handled by datafusion (e.g.
/// sorting). This struct is for the latter.
#[derive(Debug, Clone, PartialEq, Default)]
struct DataFusionQueryData {
    pub sort_by: Option<SortBy>,
    pub segment_links: Option<SegmentLinksSpec>,
    pub entry_links: Option<EntryLinksSpec>,
    pub prefilter: Option<datafusion::prelude::Expr>,
    pub column_filters: Vec<ColumnFilter>,
}

impl From<&TableBlueprint> for DataFusionQueryData {
    fn from(value: &TableBlueprint) -> Self {
        let TableBlueprint {
            sort_by,
            segment_links,
            entry_links,
            prefilter,
            column_filters,
        } = value;

        Self {
            sort_by: sort_by.clone(),
            segment_links: segment_links.clone(),
            entry_links: entry_links.clone(),
            prefilter: prefilter.clone(),
            column_filters: column_filters.clone(),
        }
    }
}

/// Result of the async datafusion query process.
#[derive(Debug, Clone)]
pub struct DataFusionQueryResult {
    /// The record batches to display.
    pub sorbet_batches: Vec<SorbetBatch>,

    /// The schema of the record batches.
    pub original_schema: SchemaRef,

    /// The migrated schema of the record batches (useful when the list of batches is empty).
    pub sorbet_schema: re_sorbet::SorbetSchema,

    pub finished: bool,
}

/// A table blueprint along with the context required to execute the corresponding datafusion query.
#[derive(Clone)]
struct DataFusionQuery {
    session_ctx: Arc<SessionContext>,
    table_ref: TableReference,

    query_data: DataFusionQueryData,
}

impl DataFusionQuery {
    fn new(
        session_ctx: Arc<SessionContext>,
        table_ref: TableReference,
        query_data: DataFusionQueryData,
    ) -> Self {
        Self {
            session_ctx,
            table_ref,
            query_data,
        }
    }

    async fn batch_stream(self) -> Result<SendableRecordBatchStream, DataFusionError> {
        let mut dataframe = self.session_ctx.table(self.table_ref).await?;

        let DataFusionQueryData {
            sort_by,
            segment_links,
            entry_links,
            prefilter,
            column_filters,
        } = &self.query_data;

        //
        // Segment links
        //

        // Important: the needs to happen first, in case we sort/filter/etc. based on that
        // particular column.
        if let Some(segment_links) = segment_links {
            //TODO(ab): we should get this from `re_uri::DatasetDataUri` instead of hardcoding
            let uri = format!(
                "{}/dataset/{}/data?segment_id=",
                segment_links.origin, segment_links.dataset_id
            );

            dataframe = dataframe.with_column(
                &segment_links.column_name,
                concat(vec![lit(uri), col(&segment_links.segment_id_column_name)]),
            )?;
        }

        //
        // Entry links
        //

        if let Some(entry_links) = entry_links {
            let uri = format!("{}/entry/", entry_links.origin);

            let column = concat(vec![
                lit(uri),
                encode(
                    cast(col(&entry_links.entry_id_column_name), DataType::Binary),
                    lit("hex"),
                ),
            ]);
            dataframe = dataframe.with_column(&entry_links.column_name, column)?;
        }

        //
        // Prefilter
        //

        if let Some(prefilter) = prefilter {
            dataframe = dataframe.filter(prefilter.clone())?;
        }

        //
        // Filters
        //

        let filter_exprs = column_filters
            .iter()
            .filter_map(|filter| {
                filter
                    .as_filter_expression()
                    .inspect_err(|err| {
                        // TODO(ab): error handling will need to be improved once we introduce non-
                        // UI means of setting up filters.
                        re_log::warn_once!("invalid filter: {err}");
                    })
                    .ok()
            })
            .collect();
        let filter_expr =
            balanced_binary_exprs(filter_exprs, datafusion::logical_expr::Operator::And);
        if let Some(filter_expr) = filter_expr {
            dataframe = dataframe.filter(filter_expr)?;
        }

        //
        // Sort
        //

        if let Some(sort_by) = sort_by {
            dataframe = dataframe.sort(vec![
                col(&sort_by.column_physical_name).sort(sort_by.direction.is_ascending(), true),
            ])?;
        }

        //
        // Execute the query
        //

        let stream = dataframe.execute_stream().await?;

        Ok(stream)
    }

    /// Execute the query to produce the data to display.
    ///
    /// Note: the future returned by this function must be `'static`, so it takes `self`. Use
    /// `clone()` as required.
    fn execute_streaming(self, runtime: &AsyncRuntimeHandle) -> Receiver<QueryEvent> {
        let (tx, rx) = crate::create_channel(1000);
        runtime.spawn_future(async move {
            if let Ok(stream) = self.batch_stream().await {
                let schema = stream.schema();

                let mut sorbet_stream = stream.and_then(|s| {
                    std::future::ready(
                        SorbetBatch::try_from_record_batch(&s, BatchType::Dataframe)
                            .map_err(|err| DataFusionError::External(err.into())),
                    )
                });

                let mut sent_schemas = false;
                let mut sent_error = false;

                while let Some(frame) = sorbet_stream.next().await {
                    match frame {
                        Ok(batch) => {
                            if !sent_schemas {
                                let sorbet_schema = batch.sorbet_schema().clone();
                                let original_schema = Arc::clone(&schema);
                                if send_crossbeam(
                                    &tx,
                                    QueryEvent::Schema {
                                        original_schema,
                                        sorbet_schema,
                                    },
                                )
                                .is_err()
                                {
                                    return; // Receiver dropped, stop streaming
                                }
                                sent_schemas = true;
                            }
                            if send_crossbeam(&tx, QueryEvent::Batch(batch)).is_err() {
                                return; // Receiver dropped, stop streaming
                            }
                        }
                        Err(err) => {
                            sent_error = true;
                            send_crossbeam(&tx, QueryEvent::Error(err)).ok();
                        }
                    }
                }

                // We got no results, try to derive the sorbet schema from the raw arrow schema
                if !sent_schemas && !sent_error {
                    let sorbet_schema = SorbetSchema::try_from_raw_arrow_schema(schema.clone());
                    match sorbet_schema {
                        Ok(sorbet_schema) => {
                            send_crossbeam(
                                &tx,
                                QueryEvent::Schema {
                                    original_schema: schema,
                                    sorbet_schema,
                                },
                            )
                            .ok();
                        }
                        Err(err) => {
                            send_crossbeam(
                                &tx,
                                QueryEvent::Error(DataFusionError::External(err.into())),
                            )
                            .ok();
                        }
                    }
                }
            }
        });
        rx
    }
}

/// A event produced during the streaming execution of a datafusion query.
///
/// It's guaranteed that the first event is either [`QueryEvent::Schema`] or [`QueryEvent::Error`].
#[derive(Debug)]
pub enum QueryEvent {
    Schema {
        original_schema: SchemaRef,
        sorbet_schema: re_sorbet::SorbetSchema,
    },
    Batch(SorbetBatch),
    Error(DataFusionError),
}

impl PartialEq for DataFusionQuery {
    fn eq(&self, other: &Self) -> bool {
        let Self {
            session_ctx,
            table_ref,
            query_data,
        } = self;

        Arc::ptr_eq(session_ctx, &other.session_ctx)
            && table_ref == &other.table_ref
            && query_data == &other.query_data
    }
}

/// Helper struct to manage the datafusion async query and the resulting `SorbetBatch`.
#[derive(Clone)]
pub struct DataFusionAdapter {
    id: egui::Id,

    /// The current table blueprint
    blueprint: TableBlueprint,

    /// The query used to produce the dataframe.
    query: DataFusionQuery,

    // Used to have something to display while the new dataframe is being queried.
    pub last_query_results: Option<Result<DataFusionQueryResult, Arc<DataFusionError>>>,

    // TODO(ab, lucasmerlin): this `Mutex` is only needed because of the `Clone` bound in egui
    // so we should clean that up if the bound is lifted.
    pub rx: Arc<Mutex<Receiver<QueryEvent>>>,

    pub results: Option<Result<DataFusionQueryResult, Arc<DataFusionError>>>,

    pub queried_at: Timestamp,
}

impl DataFusionAdapter {
    pub fn clear_state(egui_ctx: &egui::Context, id: egui::Id) {
        egui_ctx.data_mut(|data| {
            data.remove::<Self>(id);
        });
    }

    /// Retrieve the state from egui's memory or create a new one if it doesn't exist.
    pub fn get(
        runtime: &AsyncRuntimeHandle,
        ui: &egui::Ui,
        session_ctx: &Arc<SessionContext>,
        table_ref: TableReference,
        id: egui::Id,
        initial_blueprint: TableBlueprint,
    ) -> Self {
        let adapter = ui.data(|data| data.get_temp::<Self>(id));

        let mut adapter = adapter.unwrap_or_else(|| {
            let initial_query = DataFusionQueryData::from(&initial_blueprint);
            let query = DataFusionQuery::new(Arc::clone(session_ctx), table_ref, initial_query);

            let rx = query.clone().execute_streaming(runtime);

            let table_state = Self {
                id,
                blueprint: initial_blueprint,
                rx: Arc::new(Mutex::new(rx)),
                results: None,
                query,
                last_query_results: None,
                queried_at: Timestamp::now(),
            };

            ui.data_mut(|data| {
                data.insert_temp(id, table_state.clone());
            });

            table_state
        });

        {
            let rx = adapter.rx.lock();
            let mut changed = false;
            loop {
                match rx.try_recv() {
                    Ok(QueryEvent::Schema {
                        sorbet_schema,
                        original_schema,
                    }) => {
                        adapter.results = Some(Ok(DataFusionQueryResult {
                            original_schema,
                            sorbet_schema,
                            sorbet_batches: vec![],
                            finished: false,
                        }));
                        changed = true;
                    }
                    Ok(QueryEvent::Batch(batch)) => match &mut adapter.results {
                        Some(Ok(data)) => {
                            data.sorbet_batches.push(batch);
                            changed = true;

                            // We received some data, so stop showing any previous results.
                            adapter.last_query_results = None;
                        }
                        Some(Err(err)) => {
                            warn!("Received data after receiving an error: {err}");
                        }
                        None => {
                            error!("Received data before receiving schema");
                        }
                    },
                    Ok(QueryEvent::Error(err)) => {
                        error!("DataFusion query error: {err}");
                        adapter.results = Some(Err(Arc::new(err)));
                        changed = true;
                    }
                    Err(TryRecvError::Empty) => {
                        break;
                    }
                    Err(TryRecvError::Disconnected) => {
                        if let Some(Ok(data)) = &mut adapter.results {
                            data.finished = true;
                            changed = true;
                        }
                        break;
                    }
                }
            }

            if changed {
                ui.data_mut(|data| {
                    data.insert_temp(adapter.id, adapter.clone());
                });
            }
        }

        adapter
    }

    pub fn blueprint(&self) -> &TableBlueprint {
        &self.blueprint
    }

    /// Update the query and save the state to egui's memory.
    ///
    /// If the query has changed (e.g. because the ui mutated it), it is executed to produce a new
    /// dataframe.
    pub fn update_query(
        mut self,
        runtime: &AsyncRuntimeHandle,
        ui: &egui::Ui,
        new_blueprint: TableBlueprint,
    ) {
        self.blueprint = new_blueprint;

        // retrigger a new datafusion query if required.
        let new_query_data = DataFusionQueryData::from(&self.blueprint);
        if self.query.query_data != new_query_data {
            self.query.query_data = new_query_data;

            self.last_query_results = mem::take(&mut self.results);

            if let Some(Ok(results)) = &mut self.last_query_results {
                results.finished = true;
            }

            let rx = self.query.clone().execute_streaming(runtime);

            self.rx = Arc::new(Mutex::new(rx));

            TableSelectionState::clear(ui.ctx(), self.id);
        }

        ui.data_mut(|data| {
            data.insert_temp(self.id, self);
        });
    }
}

/// Creates a _balanced_ chain of binary expressions.
fn balanced_binary_exprs(
    mut exprs: Vec<datafusion::logical_expr::Expr>,
    op: datafusion::logical_expr::Operator,
) -> Option<datafusion::logical_expr::Expr> {
    while exprs.len() > 1 {
        let mut exprs_next = Vec::with_capacity(exprs.len() / 2 + 1);
        let mut exprs_prev = exprs.into_iter();

        while let Some(left) = exprs_prev.next() {
            if let Some(right) = exprs_prev.next() {
                exprs_next.push(binary_expr(left, op, right));
            } else {
                exprs_next.push(left);
            }
        }

        exprs = exprs_next;
    }

    exprs.into_iter().next()
}