densa-data 0.2.2

Live Arrow tables for densa: keyed upserts, sorted and filtered views and grouped aggregates kept up to date incrementally
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
//! Live tables: keys, upserts, deletes, compaction, and the errors of each.

use std::sync::Arc;

use arrow_array::cast::AsArray;
use arrow_array::types::{Int32Type, UInt8Type};
use arrow_array::{
    Array, ArrayRef, BinaryViewArray, Decimal128Array, DictionaryArray, Float64Array, Int64Array,
    ListArray, RecordBatch, StringArray, StringViewArray, TimestampMillisecondArray, UInt8Array,
};
use arrow_schema::{DataType, Field, Schema};
use densa_data::{
    Changes, CompareOp, DataError, Filter, LiveTable, Order, RowRef, Scalar, TableOptions, ViewSpec,
};

fn batch(columns: Vec<(&str, ArrayRef)>) -> RecordBatch {
    RecordBatch::try_from_iter(columns).unwrap()
}

fn prices(ids: Vec<i64>, values: Vec<f64>) -> RecordBatch {
    batch(vec![
        ("id", Arc::new(Int64Array::from(ids)) as ArrayRef),
        ("price", Arc::new(Float64Array::from(values)) as ArrayRef),
    ])
}

/// The price stored at `at`.
fn price(table: &LiveTable, at: RowRef) -> f64 {
    let column = table.batches()[at.batch].column(1);
    column
        .as_any()
        .downcast_ref::<Float64Array>()
        .unwrap()
        .value(at.row)
}

fn table() -> LiveTable {
    LiveTable::new(prices(vec![], vec![]).schema(), "id").unwrap()
}

mod keys {
    use super::*;

    #[test]
    fn the_key_must_be_an_existing_integer_or_string_column() {
        let schema = prices(vec![], vec![]).schema();
        assert_eq!(
            LiveTable::new(Arc::clone(&schema), "nope").unwrap_err(),
            DataError::UnknownColumn("nope".into())
        );
        assert_eq!(
            LiveTable::new(schema, "price").unwrap_err(),
            DataError::UnsupportedKey("price".into())
        );
    }

    #[test]
    fn upserting_a_new_key_inserts_and_a_known_key_updates() {
        let mut t = table();
        let changes: Changes = t.upsert(prices(vec![1, 2], vec![10.0, 20.0])).unwrap();
        assert_eq!((changes.inserted, changes.updated), (2, 0));
        let changes = t.upsert(prices(vec![2, 3], vec![21.0, 30.0])).unwrap();
        assert_eq!((changes.inserted, changes.updated), (1, 1));
        assert_eq!(t.len(), 3);
        assert_eq!(price(&t, t.find(&Scalar::Int(2)).unwrap()), 21.0);
    }

    #[test]
    fn the_last_row_for_a_key_in_one_batch_wins() {
        let mut t = table();
        let changes = t
            .upsert(prices(vec![7, 7, 7], vec![1.0, 2.0, 3.0]))
            .unwrap();
        assert_eq!(changes.inserted, 1);
        assert_eq!(t.len(), 1);
        assert_eq!(price(&t, t.find(&Scalar::Int(7)).unwrap()), 3.0);
    }

    #[test]
    fn string_keys_work_like_integer_keys() {
        let names = |v: Vec<&str>, p: Vec<f64>| {
            batch(vec![
                ("sym", Arc::new(StringArray::from(v)) as ArrayRef),
                ("price", Arc::new(Float64Array::from(p)) as ArrayRef),
            ])
        };
        let mut t = LiveTable::new(names(vec![], vec![]).schema(), "sym").unwrap();
        t.upsert(names(vec!["AAPL", "MSFT"], vec![1.0, 2.0]))
            .unwrap();
        t.upsert(names(vec!["AAPL"], vec![1.5])).unwrap();
        assert_eq!(t.len(), 2);
        let at = t.find(&Scalar::Str("AAPL".into())).unwrap();
        assert_eq!(price(&t, at), 1.5);
        assert_eq!(t.find(&Scalar::Str("NOPE".into())), None);
    }

    #[test]
    fn a_null_key_rejects_the_whole_batch() {
        let mut t = table();
        let ids: ArrayRef = Arc::new(Int64Array::from(vec![Some(1), None]));
        let values: ArrayRef = Arc::new(Float64Array::from(vec![1.0, 2.0]));
        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Int64, true),
            Field::new("price", DataType::Float64, false),
        ]));
        let bad = RecordBatch::try_new(schema, vec![ids, values]).unwrap();
        assert_eq!(t.upsert(bad), Err(DataError::NullKey { row: 1 }));
        assert!(t.is_empty());
    }

    #[test]
    fn columns_are_matched_by_name_too() {
        let mut t = table();
        let renamed = batch(vec![
            ("id", Arc::new(Int64Array::from(vec![1])) as ArrayRef),
            ("cost", Arc::new(Float64Array::from(vec![1.0])) as ArrayRef),
        ]);
        assert_eq!(
            t.upsert(renamed),
            Err(DataError::ColumnMismatch { column: 1 })
        );
    }

    #[test]
    fn a_batch_with_other_columns_is_rejected() {
        let mut t = table();
        let other = batch(vec![(
            "id",
            Arc::new(Int64Array::from(vec![1])) as ArrayRef,
        )]);
        assert_eq!(
            t.upsert(other),
            Err(DataError::ColumnCount {
                expected: 2,
                found: 1
            })
        );
        let retyped = batch(vec![
            ("id", Arc::new(Int64Array::from(vec![1])) as ArrayRef),
            ("price", Arc::new(StringArray::from(vec!["x"])) as ArrayRef),
        ]);
        assert_eq!(
            t.upsert(retyped),
            Err(DataError::ColumnMismatch { column: 1 })
        );
    }

    #[test]
    fn deleting_counts_only_keys_that_existed() {
        let mut t = table();
        t.upsert(prices(vec![1, 2, 3], vec![1.0, 2.0, 3.0]))
            .unwrap();
        assert_eq!(
            t.delete(&Int64Array::from(vec![2, 9, 2])).unwrap().deleted,
            1
        );
        assert_eq!(t.len(), 2);
        assert_eq!(t.find(&Scalar::Int(2)), None);
        assert_eq!(
            t.delete(&StringArray::from(vec!["1"])),
            Err(DataError::KeyType)
        );
    }
}

mod compaction {
    use super::*;

    #[test]
    fn nulls_in_a_column_the_first_schema_called_non_nullable_survive_compaction() {
        let strict = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Int64, false),
            Field::new("price", DataType::Float64, false),
        ]));
        let mut t = LiveTable::new(strict, "id").unwrap();
        t.upsert(prices(vec![1, 2], vec![1.0, 2.0])).unwrap();
        let loose = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Int64, false),
            Field::new("price", DataType::Float64, true),
        ]));
        let with_null = RecordBatch::try_new(
            loose,
            vec![
                Arc::new(Int64Array::from(vec![2])) as ArrayRef,
                Arc::new(Float64Array::from(vec![None])) as ArrayRef,
            ],
        )
        .unwrap();
        t.upsert(with_null).unwrap();
        t.compact().unwrap();
        let at = t.find(&Scalar::Int(2)).unwrap();
        assert!(t.batches()[at.batch].column(1).is_null(at.row));
        assert!(t.compaction_error().is_none());
    }

    #[test]
    fn without_automatic_compaction_superseded_rows_stay_until_compact() {
        let options = TableOptions::default().with_auto_compact(false);
        let mut t =
            LiveTable::with_options(prices(vec![], vec![]).schema(), "id", options).unwrap();
        for round in 0..1_000 {
            t.upsert(prices((0..10).collect(), vec![f64::from(round); 10]))
                .unwrap();
        }
        assert_eq!(t.generation(), 0);
        assert_eq!(t.batches().len(), 1_000);
        t.compact().unwrap();
        assert_eq!(t.batches().len(), 1);
    }

    #[test]
    fn compaction_keeps_every_live_row_and_drops_superseded_ones() {
        let mut t = table();
        for round in 0..50 {
            t.upsert(prices(vec![1, 2, 3], vec![round as f64; 3]))
                .unwrap();
        }
        t.delete(&Int64Array::from(vec![3])).unwrap();
        let generation = t.generation();
        t.compact().unwrap();
        assert!(t.generation() > generation);
        let rows: usize = t.batches().iter().map(RecordBatch::num_rows).sum();
        assert_eq!(rows, 2);
        assert_eq!(price(&t, t.find(&Scalar::Int(1)).unwrap()), 49.0);
    }

    #[test]
    fn compaction_writes_several_batches_for_many_rows() {
        let mut t = table();
        let ids: Vec<i64> = (0..70_000).collect();
        let values: Vec<f64> = ids.iter().map(|&i| i as f64).collect();
        t.upsert(prices(ids, values)).unwrap();
        t.upsert(prices(vec![69_999], vec![-1.0])).unwrap();
        t.compact().unwrap();
        assert!(t.batches().len() >= 2);
        assert_eq!(price(&t, t.find(&Scalar::Int(69_998)).unwrap()), 69_998.0);
        assert_eq!(price(&t, t.find(&Scalar::Int(69_999)).unwrap()), -1.0);
    }

    #[test]
    fn many_superseded_rows_are_compacted_automatically() {
        let mut t = table();
        for round in 0..2_000 {
            let ids: Vec<i64> = (0..10).collect();
            t.upsert(prices(ids, vec![f64::from(round); 10])).unwrap();
        }
        let stored: usize = t.batches().iter().map(RecordBatch::num_rows).sum();
        assert!(stored < 20_000 / 2, "{stored} rows kept for 10 keys");
    }

    /// 200 rows whose `name` is a u8-keyed dictionary of 200 distinct strings, starting at
    /// key `first`; strings or string views.
    fn names(first: i64, views: bool) -> RecordBatch {
        let text: Vec<String> = (first..first + 200).map(|i| format!("n{i:03}")).collect();
        let values: ArrayRef = if views {
            Arc::new(StringViewArray::from_iter_values(text))
        } else {
            Arc::new(StringArray::from(text))
        };
        let keys = UInt8Array::from((0..200u8).collect::<Vec<_>>());
        let names = DictionaryArray::<UInt8Type>::try_new(keys, values).unwrap();
        batch(vec![
            (
                "id",
                Arc::new(Int64Array::from((first..first + 200).collect::<Vec<_>>())) as ArrayRef,
            ),
            ("name", Arc::new(names) as ArrayRef),
        ])
    }

    /// Rows `ids` whose `name` is a u8-keyed dictionary of 200 strings `{prefix}000..199`,
    /// row `i` pointing at string `ids[i] % 200`.
    fn coded(ids: Vec<i64>, prefix: &str, views: bool) -> RecordBatch {
        let text: Vec<String> = (0..200).map(|i| format!("{prefix}{i:03}")).collect();
        let values: ArrayRef = if views {
            Arc::new(StringViewArray::from_iter_values(text))
        } else {
            Arc::new(StringArray::from(text))
        };
        let keys = UInt8Array::from(ids.iter().map(|id| (id % 200) as u8).collect::<Vec<_>>());
        let names = DictionaryArray::<UInt8Type>::try_new(keys, values).unwrap();
        batch(vec![
            ("id", Arc::new(Int64Array::from(ids)) as ArrayRef),
            ("name", Arc::new(names) as ArrayRef),
        ])
    }

    #[test]
    fn compacting_rows_that_alternate_between_dictionaries_writes_few_batches() {
        for views in [false, true] {
            let mut t = LiveTable::new(coded(vec![], "a", views).schema(), "id").unwrap();
            t.upsert(coded((0..1_000).collect(), "a", views)).unwrap();
            // Every odd row moves to another dictionary: live rows alternate between batches.
            t.upsert(coded(
                (0..1_000).filter(|i| i % 2 == 1).collect(),
                "b",
                views,
            ))
            .unwrap();
            for _ in 0..2 {
                t.compact().unwrap();
                // The live rows use 200 distinct names (even keys of one dictionary, odd of the
                // other), which a u8 key indexes: one batch, though the inputs hold 400.
                assert_eq!(t.batches().len(), 1);
                for id in [0, 1, 998, 999] {
                    let at = t.find(&Scalar::Int(id)).unwrap();
                    let column = t.batches()[at.batch].column(1);
                    let names = column.as_any_dictionary();
                    let key = names.normalized_keys()[at.row];
                    let values = names.values();
                    let name = match values.as_string_view_opt() {
                        Some(v) => v.value(key).to_owned(),
                        None => values.as_string::<i32>().value(key).to_owned(),
                    };
                    let prefix = if id % 2 == 1 { "b" } else { "a" };
                    assert_eq!(name, format!("{prefix}{:03}", id % 200));
                }
            }
        }
    }

    #[test]
    fn compaction_refuses_rather_than_writing_batches_of_a_few_rows() {
        // Arrow sizes a dictionary of binary views by all of its inputs' values: 400 here,
        // whichever rows are read, so no split helps. Below 128 rows compaction gives up.
        let coded_binary = |ids: Vec<i64>, prefix: u8| {
            let values = BinaryViewArray::from_iter_values((0..200u8).map(|i| [prefix, i]));
            let keys = UInt8Array::from(ids.iter().map(|id| (id % 200) as u8).collect::<Vec<_>>());
            let names = DictionaryArray::<UInt8Type>::try_new(keys, Arc::new(values)).unwrap();
            batch(vec![
                ("id", Arc::new(Int64Array::from(ids)) as ArrayRef),
                ("name", Arc::new(names) as ArrayRef),
            ])
        };
        let options = TableOptions::default().with_auto_compact(false);
        let schema = coded_binary(vec![], 0).schema();
        let mut t = LiveTable::with_options(schema, "id", options).unwrap();
        t.upsert(coded_binary((0..1_000).collect(), 0)).unwrap();
        t.upsert(coded_binary((0..1_000).filter(|i| i % 2 == 1).collect(), 1))
            .unwrap();
        assert!(matches!(t.compact(), Err(DataError::Arrow(_))));
        assert_eq!(
            (t.generation(), t.batches().len()),
            (0, 2),
            "nothing changed"
        );
    }

    #[test]
    fn compaction_splits_rows_a_narrow_dictionary_key_cannot_index_in_one_batch() {
        for views in [false, true] {
            let mut t = LiveTable::new(names(0, views).schema(), "id").unwrap();
            let sorted = t
                .add_view(ViewSpec::new().sort_by("name", Order::Descending))
                .unwrap();
            t.upsert(names(0, views)).unwrap();
            t.upsert(names(200, views)).unwrap();
            // 400 distinct names: no u8-keyed dictionary holds them all.
            t.compact().unwrap();
            assert_eq!(t.generation(), 1);
            assert!(t.batches().len() >= 2);
            let name = |at: RowRef| {
                let column = t.batches()[at.batch].column(1);
                let names = column.as_any_dictionary();
                let values = names.values();
                let key = names.normalized_keys()[at.row];
                match values.as_string_view_opt() {
                    Some(v) => v.value(key).to_owned(),
                    None => values.as_string::<i32>().value(key).to_owned(),
                }
            };
            assert_eq!(name(t.find(&Scalar::Int(399)).unwrap()), "n399");
            assert_eq!(name(t.find(&Scalar::Int(7)).unwrap()), "n007");
            let view = t.view(sorted).unwrap();
            assert_eq!(view.len(), 400);
            assert_eq!(name(view.row(0).unwrap()), "n399");
            assert_eq!(name(view.row(399).unwrap()), "n000");
        }
    }
}

mod views {
    use super::*;

    /// The prices a view shows, in order.
    fn shown(t: &LiveTable, id: densa_data::ViewId) -> Vec<f64> {
        let view = t.view(id).unwrap();
        (0..view.len())
            .map(|i| price(t, view.row(i).unwrap()))
            .collect()
    }

    #[test]
    fn a_rejected_upsert_leaves_views_as_they_were() {
        let mut t = table();
        t.upsert(prices(vec![1, 2], vec![2.0, 1.0])).unwrap();
        let id = t
            .add_view(ViewSpec::new().sort_by("price", Order::Ascending))
            .unwrap();
        let bad = RecordBatch::try_from_iter([
            (
                "id",
                Arc::new(Int64Array::from(vec![Some(3), None])) as ArrayRef,
            ),
            (
                "price",
                Arc::new(Float64Array::from(vec![0.5, 0.25])) as ArrayRef,
            ),
        ])
        .unwrap();
        assert_eq!(t.upsert(bad), Err(DataError::NullKey { row: 1 }));
        assert_eq!(shown(&t, id), [1.0, 2.0]);
    }

    #[test]
    fn a_deleted_key_s_id_is_reused_without_disturbing_views() {
        let mut t = table();
        t.upsert(prices(vec![1, 2, 3], vec![3.0, 1.0, 2.0]))
            .unwrap();
        let id = t
            .add_view(ViewSpec::new().sort_by("price", Order::Ascending))
            .unwrap();
        t.delete(&Int64Array::from(vec![2])).unwrap();
        t.upsert(prices(vec![4], vec![2.5])).unwrap();
        assert_eq!(shown(&t, id), [2.0, 2.5, 3.0]);
        let late = t
            .add_view(ViewSpec::new().sort_by("price", Order::Descending))
            .unwrap();
        assert_eq!(shown(&t, late), [3.0, 2.5, 2.0]);
    }

    #[test]
    fn view_ids_belong_to_their_table_and_its_clones() {
        let mut a = table();
        let id = a.add_view(ViewSpec::new()).unwrap();
        let clone = a.clone();
        assert!(clone.view(id).is_some(), "a clone copies its views");
        assert_ne!(clone.instance(), a.instance());
        let mut b = table();
        assert!(b.view(id).is_none());
        assert!(!b.remove_view(id));
    }

    #[test]
    fn views_name_existing_columns_of_supported_types_with_matching_literals() {
        let mut t = table();
        assert_eq!(
            t.add_view(ViewSpec::new().sort_by("nope", Order::Ascending)),
            Err(DataError::UnknownColumn("nope".into()))
        );
        let filter = Filter::compare("price", CompareOp::Eq, Scalar::Str("x".into()));
        assert_eq!(
            t.add_view(ViewSpec::new().filter(filter)),
            Err(DataError::LiteralMismatch("price".into()))
        );

        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![Some(vec![Some(1)])]);
        let with_list = batch(vec![
            ("id", Arc::new(Int64Array::from(vec![1])) as ArrayRef),
            ("tags", Arc::new(list) as ArrayRef),
        ]);
        let mut t = LiveTable::new(with_list.schema(), "id").unwrap();
        assert_eq!(
            t.add_view(ViewSpec::new().sort_by("tags", Order::Ascending)),
            Err(DataError::UnsupportedColumn("tags".into()))
        );
    }

    #[test]
    fn a_new_view_shows_the_rows_already_in_the_table() {
        let mut t = table();
        t.upsert(prices(vec![1, 2, 3], vec![30.0, 10.0, 20.0]))
            .unwrap();
        let id = t
            .add_view(ViewSpec::new().sort_by("price", Order::Descending))
            .unwrap();
        let view = t.view(id).unwrap();
        let shown: Vec<f64> = (0..view.len())
            .map(|i| price(&t, view.row(i).unwrap()))
            .collect();
        assert_eq!(shown, [30.0, 20.0, 10.0]);
        assert_eq!(view.row(3), None);
    }

    #[test]
    fn a_removed_view_is_gone() {
        let mut t = table();
        let id = t.add_view(ViewSpec::new()).unwrap();
        assert!(t.remove_view(id));
        assert!(t.view(id).is_none());
        assert!(!t.remove_view(id));
    }

    #[test]
    fn a_removed_views_id_never_names_the_view_added_after_it() {
        let mut t = table();
        t.upsert(prices(vec![1, 2], vec![30.0, 10.0])).unwrap();
        let old = t.add_view(ViewSpec::new()).unwrap();
        assert!(t.remove_view(old));
        let new = t
            .add_view(ViewSpec::new().sort_by("price", Order::Ascending))
            .unwrap();
        assert!(t.view(old).is_none());
        assert!(!t.remove_view(old));
        assert_eq!(t.view(new).unwrap().len(), 2);
    }

    #[test]
    fn decimals_timestamps_and_dictionary_strings_sort_and_filter() {
        let regions: DictionaryArray<Int32Type> =
            vec!["west", "east", "north"].into_iter().collect();
        let totals = Decimal128Array::from(vec![500, 150, 999])
            .with_precision_and_scale(10, 2)
            .unwrap();
        let rows = batch(vec![
            ("id", Arc::new(Int64Array::from(vec![1, 2, 3])) as ArrayRef),
            ("region", Arc::new(regions) as ArrayRef),
            ("total", Arc::new(totals) as ArrayRef),
            (
                "at",
                Arc::new(TimestampMillisecondArray::from(vec![30, 10, 20])) as ArrayRef,
            ),
        ]);
        let mut t = LiveTable::new(rows.schema(), "id").unwrap();
        t.upsert(rows).unwrap();
        let ids = |t: &LiveTable, spec: ViewSpec| -> Vec<i64> {
            let mut t2 = LiveTable::new(t.schema().clone(), "id").unwrap();
            for b in t.batches() {
                t2.upsert(b.clone()).unwrap();
            }
            let id = t2.add_view(spec).unwrap();
            let view = t2.view(id).unwrap();
            (0..view.len())
                .map(|i| {
                    let at = view.row(i).unwrap();
                    let column = t2.batches()[at.batch].column(0);
                    column
                        .as_any()
                        .downcast_ref::<Int64Array>()
                        .unwrap()
                        .value(at.row)
                })
                .collect()
        };
        assert_eq!(
            ids(&t, ViewSpec::new().sort_by("region", Order::Ascending)),
            [2, 3, 1]
        );
        assert_eq!(
            ids(&t, ViewSpec::new().sort_by("at", Order::Ascending)),
            [2, 3, 1]
        );
        // 5.00 and 9.99 are above 4.5; 1.50 is not.
        let above = Filter::compare("total", CompareOp::Gt, Scalar::Float(4.5));
        assert_eq!(
            ids(
                &t,
                ViewSpec::new()
                    .filter(above)
                    .sort_by("total", Order::Ascending)
            ),
            [1, 3]
        );
        let exact = Filter::compare("total", CompareOp::Eq, Scalar::Int(5));
        assert_eq!(ids(&t, ViewSpec::new().filter(exact)), [1]);
    }
}