re_dataframe_ui/
datafusion_table_widget.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
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
use std::sync::Arc;

use datafusion::catalog::TableReference;
use datafusion::prelude::SessionContext;
use egui::{Frame, Id, Margin, RichText};
use egui_table::{CellInfo, HeaderCellInfo};
use nohash_hasher::IntMap;

use re_log_types::{EntryId, TimelineName};
use re_sorbet::{ColumnDescriptorRef, SorbetSchema};
use re_ui::list_item::ItemButton;
use re_ui::UiExt as _;
use re_viewer_context::{AsyncRuntimeHandle, ViewerContext};

use crate::datafusion_adapter::DataFusionAdapter;
use crate::table_blueprint::{PartitionLinksSpec, SortBy, SortDirection, TableBlueprint};
use crate::table_utils::{
    apply_table_style_fixes, cell_ui, header_ui, ColumnConfig, TableConfig, CELL_MARGIN,
};
use crate::DisplayRecordBatch;

/// Keep track of the columns in a sorbet batch, indexed by id.
//TODO(ab): merge this into `TableConfig` when table config is no longer used elsewhere.
struct Columns<'a> {
    /// Column index and descriptor from id
    inner: IntMap<egui::Id, (usize, ColumnDescriptorRef<'a>)>,
}

impl<'a> Columns<'a> {
    fn from(sorbet_schema: &'a SorbetSchema) -> Self {
        let inner = sorbet_schema
            .columns
            .descriptors()
            .enumerate()
            .map(|(index, desc)| (egui::Id::new(&desc), (index, desc)))
            .collect::<IntMap<_, _>>();

        Self { inner }
    }
}

impl Columns<'_> {
    fn descriptors(&self) -> impl Iterator<Item = &ColumnDescriptorRef<'_>> {
        self.inner.values().map(|(_, desc)| desc)
    }

    fn index_from_id(&self, id: Option<egui::Id>) -> Option<usize> {
        id.and_then(|id| self.inner.get(&id).map(|(index, _)| *index))
    }

    fn descriptor_from_id(&self, id: Option<egui::Id>) -> Option<&ColumnDescriptorRef<'_>> {
        id.and_then(|id| self.inner.get(&id).map(|(_, desc)| desc))
    }
}

type ColumnRenamerFn<'a> = Option<Box<dyn Fn(&ColumnDescriptorRef<'_>) -> String + 'a>>;

pub struct DataFusionTableWidget<'a> {
    session_ctx: Arc<SessionContext>,
    table_ref: TableReference,

    /// If provided, add a title UI on top of the table.
    //TODO(ab): for now, this is the only way to have the column visibility/order menu
    title: Option<String>,

    /// If provided and if `title` is set, add a button next to the title.
    title_button: Option<Box<dyn ItemButton + 'a>>,

    /// Closure used to determine the display name of the column.
    ///
    /// Defaults to using [`ColumnDescriptorRef::name`].
    column_renamer: ColumnRenamerFn<'a>,

    /// The blueprint used the first time the table is queried.
    initial_blueprint: TableBlueprint,
}

impl<'a> DataFusionTableWidget<'a> {
    /// Clears all caches related to this session context and table reference.
    pub fn clear_state(
        egui_ctx: &egui::Context,
        session_ctx: &SessionContext,
        table_ref: impl Into<TableReference>,
    ) {
        let id = id_from_session_context_and_table(session_ctx, &table_ref.into());

        TableConfig::clear_state(egui_ctx, id);
        DataFusionAdapter::clear_state(egui_ctx, id);
    }

    pub fn new(session_ctx: Arc<SessionContext>, table_ref: impl Into<TableReference>) -> Self {
        Self {
            session_ctx,
            table_ref: table_ref.into(),

            title: None,
            title_button: None,
            column_renamer: None,
            initial_blueprint: Default::default(),
        }
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());

        self
    }

    pub fn title_button(mut self, button: impl ItemButton + 'a) -> Self {
        self.title_button = Some(Box::new(button));

        self
    }

    pub fn column_renamer(
        mut self,
        renamer: impl Fn(&ColumnDescriptorRef<'_>) -> String + 'a,
    ) -> Self {
        self.column_renamer = Some(Box::new(renamer));

        self
    }

    pub fn generate_partition_links(
        mut self,
        column_name: impl Into<String>,
        partition_id_column_name: impl Into<String>,
        origin: re_uri::Origin,
        dataset_id: EntryId,
    ) -> Self {
        self.initial_blueprint.partition_links = Some(PartitionLinksSpec {
            column_name: column_name.into(),
            partition_id_column_name: partition_id_column_name.into(),
            origin,
            dataset_id,
        });

        self
    }

    pub fn show(
        self,
        viewer_ctx: &ViewerContext<'_>,
        runtime: &AsyncRuntimeHandle,
        ui: &mut egui::Ui,
    ) {
        let Self {
            session_ctx,
            table_ref,
            title,
            title_button,
            column_renamer,
            initial_blueprint,
        } = self;

        if !session_ctx
            .table_exist(table_ref.clone())
            .unwrap_or_default()
        {
            // Let's not be too intrusive here, as this can often happen temporarily while the table
            // providers are being registered to the session context after refreshing.
            ui.label(format!(
                "Loading table… (table `{}` not found in session context)",
                &table_ref
            ));
            return;
        }

        let id = id_from_session_context_and_table(&session_ctx, &table_ref);

        let table_state = DataFusionAdapter::get(
            runtime,
            ui,
            &session_ctx,
            table_ref.clone(),
            id,
            initial_blueprint,
        );

        let requested_sorbet_batches = table_state.requested_sorbet_batches.lock();

        let sorbet_batches = match (
            requested_sorbet_batches.try_as_ref(),
            &table_state.last_sorbet_batches,
        ) {
            (Some(Ok(dataframe)), _) => dataframe,

            (Some(Err(err)), _) => {
                let error = format!("Could not load table: {err}");
                drop(requested_sorbet_batches);

                ui.horizontal(|ui| {
                    ui.error_label(error);

                    if ui.small_icon_button(&re_ui::icons::RESET).clicked() {
                        // This will trigger a fresh query on the next frame.
                        Self::clear_state(ui.ctx(), &session_ctx, table_ref);
                    }
                });
                return;
            }

            (None, Some(last_dataframe)) => {
                // The new dataframe is still processing, but we have the previous one to display for now.
                //TODO(ab): add a progress indicator
                last_dataframe
            }

            (None, None) => {
                // still processing, nothing yet to show
                ui.label("Loading table…");
                return;
            }
        };

        let sorbet_schema = {
            let Some(sorbet_batch) = sorbet_batches.first() else {
                ui.label(egui::RichText::new("This dataset is empty").italics());
                return;
            };

            sorbet_batch.sorbet_schema()
        };

        let num_rows = sorbet_batches
            .iter()
            .map(|record_batch| record_batch.num_rows() as u64)
            .sum();

        let columns = Columns::from(sorbet_schema);

        let display_record_batches = sorbet_batches
            .iter()
            .map(|sorbet_batch| {
                DisplayRecordBatch::try_new(
                    sorbet_batch
                        .all_columns()
                        .map(|(desc, array)| (desc, array.clone())),
                )
            })
            .collect::<Result<Vec<_>, _>>();

        let display_record_batches = match display_record_batches {
            Ok(display_record_batches) => display_record_batches,
            Err(err) => {
                //TODO(ab): better error handling?
                ui.error_label(err.to_string());
                return;
            }
        };

        let mut table_config = TableConfig::get_with_columns(
            ui.ctx(),
            id,
            columns.descriptors().map(|c| {
                let name = if let Some(renamer) = &column_renamer {
                    renamer(c)
                } else {
                    c.name().to_owned()
                };

                ColumnConfig::new(Id::new(c), name)
            }),
        );

        if let Some(title) = title {
            title_ui(ui, &mut table_config, &title, title_button);
        }

        apply_table_style_fixes(ui.style_mut());

        let mut new_blueprint = table_state.blueprint().clone();

        let mut table_delegate = DataFusionTableDelegate {
            ctx: viewer_ctx,
            display_record_batches: &display_record_batches,
            columns: &columns,
            column_renamer: &column_renamer,
            blueprint: table_state.blueprint(),
            new_blueprint: &mut new_blueprint,
            table_config,
        };

        egui_table::Table::new()
            .id_salt(id)
            .columns(
                table_delegate
                    .table_config
                    .visible_column_ids()
                    .map(|id| egui_table::Column::new(200.0).resizable(true).id(id))
                    .collect::<Vec<_>>(),
            )
            .headers(vec![egui_table::HeaderRow::new(
                re_ui::DesignTokens::table_header_height() + CELL_MARGIN.sum().y,
            )])
            .num_rows(num_rows)
            .show(ui, &mut table_delegate);

        table_delegate.table_config.store(ui.ctx());
        drop(requested_sorbet_batches);
        table_state.update_query(runtime, ui, new_blueprint);
    }
}

fn id_from_session_context_and_table(
    session_ctx: &SessionContext,
    table_ref: &TableReference,
) -> Id {
    egui::Id::new((session_ctx.session_id(), table_ref))
}

fn title_ui<'a>(
    ui: &mut egui::Ui,
    table_config: &mut TableConfig,
    title: &str,
    title_button: Option<Box<dyn ItemButton + 'a>>,
) {
    Frame::new()
        .inner_margin(Margin {
            top: 16,
            bottom: 12,
            left: 16,
            right: 16,
        })
        .show(ui, |ui| {
            egui::Sides::new().show(
                ui,
                |ui| {
                    ui.heading(RichText::new(title).strong());
                    if let Some(title_button) = title_button {
                        title_button.ui(ui);
                    }
                },
                |ui| {
                    table_config.button_ui(ui);
                },
            );
        });
}

struct DataFusionTableDelegate<'a> {
    ctx: &'a ViewerContext<'a>,
    display_record_batches: &'a Vec<DisplayRecordBatch>,
    columns: &'a Columns<'a>,
    column_renamer: &'a ColumnRenamerFn<'a>,
    blueprint: &'a TableBlueprint,
    new_blueprint: &'a mut TableBlueprint,
    table_config: TableConfig,
}

impl egui_table::TableDelegate for DataFusionTableDelegate<'_> {
    fn header_cell_ui(&mut self, ui: &mut egui::Ui, cell: &HeaderCellInfo) {
        ui.set_truncate_style();

        let id = self.table_config.visible_column_ids().nth(cell.group_index);

        if let Some(desc) = self.columns.descriptor_from_id(id) {
            let column_name = desc.name();
            let name = if let Some(renamer) = self.column_renamer {
                renamer(desc)
            } else {
                desc.name().to_owned()
            };

            let current_sort_direction = self.blueprint.sort_by.as_ref().and_then(|sort_by| {
                (sort_by.column.as_str() == column_name).then_some(&sort_by.direction)
            });

            header_ui(ui, |ui| {
                egui::Sides::new().show(
                    ui,
                    |ui| {
                        ui.label(egui::RichText::new(name).strong().monospace());

                        if let Some(dir_icon) = current_sort_direction.map(SortDirection::icon) {
                            ui.add_space(-5.0);
                            ui.small_icon(
                                dir_icon,
                                Some(
                                    re_ui::design_tokens()
                                        .color(re_ui::ColorToken::blue(re_ui::Scale::S450)),
                                ),
                            );
                        }
                    },
                    |ui| {
                        egui::menu::menu_custom_button(
                            ui,
                            ui.small_icon_button_widget(&re_ui::icons::MORE),
                            |ui| {
                                for sort_direction in SortDirection::iter() {
                                    let already_sorted =
                                        Some(&sort_direction) == current_sort_direction;

                                    if ui
                                        .add_enabled_ui(!already_sorted, |ui| {
                                            sort_direction.menu_button(ui)
                                        })
                                        .inner
                                        .clicked()
                                    {
                                        self.new_blueprint.sort_by = Some(SortBy {
                                            column: column_name.to_owned(),
                                            direction: sort_direction,
                                        });
                                        ui.close_menu();
                                    }
                                }
                            },
                        );
                    },
                );
            });
        }
    }

    fn cell_ui(&mut self, ui: &mut egui::Ui, cell: &CellInfo) {
        cell_ui(ui, |ui| {
            // find record batch
            let mut row_index = cell.row_nr as usize;

            ui.set_truncate_style();

            let id = self.table_config.visible_column_ids().nth(cell.col_nr);

            if let Some(col_idx) = self.columns.index_from_id(id) {
                //TODO(ab): make an utility for that
                for display_record_batch in self.display_record_batches {
                    let row_count = display_record_batch.num_rows();
                    if row_index < row_count {
                        // this is the one
                        let column = &display_record_batch.columns()[col_idx];

                        // TODO(#9029): it is _very_ unfortunate that we must provide a fake timeline, but
                        // avoiding doing so needs significant refactoring work.
                        column.data_ui(
                            self.ctx,
                            ui,
                            &re_viewer_context::external::re_chunk_store::LatestAtQuery::latest(
                                TimelineName::new("unknown"),
                            ),
                            row_index,
                            None,
                        );

                        break;
                    } else {
                        row_index -= row_count;
                    }
                }
            }
        });
    }

    fn default_row_height(&self) -> f32 {
        re_ui::DesignTokens::table_line_height() + CELL_MARGIN.sum().y
    }
}