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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*!
Traits and implementations to handle joining or merging two `DataView`s.

Joining [DataView](../view/struct.DataView.html)s involves finding the rows in each `DataView` which
satisfy a specific join predicate (much like a `JOIN` in a SQL database). Merging refers to
combining fields of two `DataView` objects with the same number of rows into a single `DataView`.
*/
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::ops::Add;

use access::*;
use cons::*;
use error::*;
use field::Value;
use label::*;
use select::*;
use store::{AssocStorage, DataStore, IntoView, PushBackClonedFromValueIter};
use view::*;

/// A trait for applying a frame index offset `O`.
pub trait Offset<O> {
    /// Result after applying offset `O`.
    type Output;
}
impl<O, U> Offset<O> for U
where
    U: Add<O>,
{
    type Output = <U as Add<O>>::Output;
}

/// Trait for updating the frame index of a [FrameLookupCons](../view/type.FrameLookupCons.html)
/// by a specified [Offset](trait.Offset.html).
pub trait UpdateFrameIndexMarker<FrameIndexOffset> {
    /// `FrameLookupCons` after updating frame index by `FrameIndexOffset`.
    type Output;
}
impl<FrameIndexOffset> UpdateFrameIndexMarker<FrameIndexOffset> for Nil {
    type Output = Nil;
}
impl<RLabel, RFrameIndex, RFrameLabel, RTail, FrameIndexOffset>
    UpdateFrameIndexMarker<FrameIndexOffset>
    for FrameLookupCons<RLabel, RFrameIndex, RFrameLabel, RTail>
where
    RFrameIndex: Offset<FrameIndexOffset>,
    RTail: UpdateFrameIndexMarker<FrameIndexOffset>,
{
    type Output = FrameLookupCons<
        RLabel,
        <RFrameIndex as Offset<FrameIndexOffset>>::Output,
        RFrameLabel,
        <RTail as UpdateFrameIndexMarker<FrameIndexOffset>>::Output,
    >;
}

/// Trait for updating the frame index of a [ViewFrameCons](../view/type.ViewFrameCons.html)
/// by a specified [Offset](trait.Offset.html).
pub trait UpdateFrameIndex<FrameIndexOffset> {
    /// `ViewFrameCons` after updating frame index by `FrameIndexOffset`.
    type Output;

    /// Update `ViewFrameCons` with frame index offset.
    fn update_frame_label(self) -> Self::Output;
}
impl<FrameIndexOffset> UpdateFrameIndex<FrameIndexOffset> for Nil {
    type Output = Nil;

    fn update_frame_label(self) -> Nil {
        Nil
    }
}

impl<RFrameIndex, RFrameFields, RTail, FrameIndexOffset> UpdateFrameIndex<FrameIndexOffset>
    for ViewFrameCons<RFrameIndex, RFrameFields, RTail>
where
    RFrameIndex: Offset<FrameIndexOffset>,
    RFrameFields: AssocStorage,
    RTail: UpdateFrameIndex<FrameIndexOffset>,
{
    type Output = ViewFrameCons<
        <RFrameIndex as Offset<FrameIndexOffset>>::Output,
        RFrameFields,
        <RTail as UpdateFrameIndex<FrameIndexOffset>>::Output,
    >;

    fn update_frame_label(self) -> Self::Output {
        LVCons {
            head: Labeled::from(self.head.value),
            tail: self.tail.update_frame_label(),
        }
    }
}

/// Trait for merging another [DataView](../view/struct.DataView.html) with `self`. `RLabels` and
/// `RFrames` are the type parameters of the `DataView` being merged onto `self`.
pub trait Merge<RLabels, RFrames> {
    /// Resulting `Labels` type parameter of merged `DataView` object.
    type OutLabels;
    /// Resulting `Frames` type parameterof merged `DataView` object.
    type OutFrames;

    /// Merge this object with `right`, producing a new `DataView` with `OutLabels` and `OutFrames`
    /// type parameters.
    fn merge(
        &self,
        right: &DataView<RLabels, RFrames>,
    ) -> DataView<Self::OutLabels, Self::OutFrames>;
}
impl<LLabels, LFrames, RLabels, RFrames> Merge<RLabels, RFrames> for DataView<LLabels, LFrames>
where
    LFrames: Len,
    RLabels: UpdateFrameIndexMarker<<LFrames as Len>::Len>,
    LLabels: Append<<RLabels as UpdateFrameIndexMarker<<LFrames as Len>::Len>>::Output>,
    RFrames: Clone + UpdateFrameIndex<<LFrames as Len>::Len>,
    LFrames: Append<<RFrames as UpdateFrameIndex<<LFrames as Len>::Len>>::Output> + Clone,
{
    type OutLabels = <LLabels as Append<
        <RLabels as UpdateFrameIndexMarker<<LFrames as Len>::Len>>::Output,
    >>::Appended;
    type OutFrames =
        <LFrames as Append<<RFrames as UpdateFrameIndex<<LFrames as Len>::Len>>::Output>>::Appended;

    fn merge(
        &self,
        right: &DataView<RLabels, RFrames>,
    ) -> DataView<Self::OutLabels, Self::OutFrames> {
        let out_frames = self
            .frames
            .clone()
            .append(right.frames.clone().update_frame_label());

        DataView {
            _labels: PhantomData,
            frames: out_frames,
        }
    }
}

/// Marker struct describing a join. `LLabel` is the label of the left-hand side, `RLabel` is the
/// label of the right-hand side, and `Predicate` represents the type of join predicate (equal join,
/// greater-than join, less-than join, etc.).
pub struct Join<LLabel, RLabel, Predicate> {
    _marker: PhantomData<(LLabel, RLabel, Predicate)>,
}

/// A trait for describing the course of action in a sort-merge join. This trait differentiates
/// the actions that are taken during a sort-merge join based on the implementing type.
pub trait Predicate {
    /// Returns `true` if predicate is an 'equality' predicate (`==`, `<=`, `=>`)
    fn is_equality_pred() -> bool;
    /// Returns `true` if predicate is a 'greater than' predicate (`>=`, `>`)
    fn is_greater_than_pred() -> bool;
    /// Returns `true` if predciate is a 'less than' predicate (`<=`, `<`)
    fn is_less_than_pred() -> bool;
    /// Applies this predicate to the two values and returns the desired action.
    fn apply<T>(left: Value<&T>, right: Value<&T>) -> PredAction
    where
        T: PartialEq + Ord;
    /// Advances indices appropriately as required for this predicate type.
    fn advance(left_idx: &mut usize, right_idx: &mut usize, left_end: usize, right_end: usize);
}

/// Predicate for equality joins (left == right).
pub struct Equal;
impl Predicate for Equal {
    fn is_equality_pred() -> bool {
        true
    }
    fn is_greater_than_pred() -> bool {
        false
    }
    fn is_less_than_pred() -> bool {
        false
    }
    fn apply<T>(left: Value<&T>, right: Value<&T>) -> PredAction
    where
        T: PartialEq + Ord,
    {
        match left.cmp(&right) {
            Ordering::Less => PredAction::Advance {
                left: true,
                right: false,
            },
            Ordering::Equal => PredAction::Add,
            Ordering::Greater => PredAction::Advance {
                left: false,
                right: true,
            },
        }
    }
    fn advance(left_idx: &mut usize, right_idx: &mut usize, left_end: usize, right_end: usize) {
        *left_idx = left_end;
        *right_idx = right_end;
    }
}

/// Predicate for less-than joins (left < right).
pub struct LessThan;
impl Predicate for LessThan {
    fn is_equality_pred() -> bool {
        false
    }
    fn is_greater_than_pred() -> bool {
        false
    }
    fn is_less_than_pred() -> bool {
        true
    }
    fn apply<T>(left: Value<&T>, right: Value<&T>) -> PredAction
    where
        T: PartialEq + Ord,
    {
        match left.cmp(&right) {
            Ordering::Less => PredAction::Add,
            _ => PredAction::Advance {
                left: false,
                right: true,
            },
        }
    }
    fn advance(left_idx: &mut usize, _right_idx: &mut usize, _left_end: usize, _right_end: usize) {
        *left_idx += 1;
    }
}

/// Predicate for less-than-equal joins (left <= right).
pub struct LessThanEqual;
impl Predicate for LessThanEqual {
    fn is_equality_pred() -> bool {
        true
    }
    fn is_greater_than_pred() -> bool {
        false
    }
    fn is_less_than_pred() -> bool {
        true
    }
    fn apply<T>(left: Value<&T>, right: Value<&T>) -> PredAction
    where
        T: PartialEq + Ord,
    {
        match left.cmp(&right) {
            Ordering::Greater => PredAction::Advance {
                left: false,
                right: true,
            },
            _ => PredAction::Add,
        }
    }
    fn advance(left_idx: &mut usize, _right_idx: &mut usize, left_end: usize, _right_end: usize) {
        *left_idx = left_end;
    }
}

/// Predicate for greater-than joins (left > right).
pub struct GreaterThan;
impl Predicate for GreaterThan {
    fn is_equality_pred() -> bool {
        false
    }
    fn is_greater_than_pred() -> bool {
        true
    }
    fn is_less_than_pred() -> bool {
        false
    }
    fn apply<T>(left: Value<&T>, right: Value<&T>) -> PredAction
    where
        T: PartialEq + Ord,
    {
        match left.cmp(&right) {
            Ordering::Greater => PredAction::Add,
            _ => PredAction::Advance {
                left: true,
                right: false,
            },
        }
    }
    fn advance(_left_idx: &mut usize, right_idx: &mut usize, _left_end: usize, _right_end: usize) {
        *right_idx += 1;
    }
}

/// Predicate for greater-than-equal joins (left >= right).
pub struct GreaterThanEqual;
impl Predicate for GreaterThanEqual {
    fn is_equality_pred() -> bool {
        true
    }
    fn is_greater_than_pred() -> bool {
        true
    }
    fn is_less_than_pred() -> bool {
        false
    }
    fn apply<T>(left: Value<&T>, right: Value<&T>) -> PredAction
    where
        T: PartialEq + Ord,
    {
        match left.cmp(&right) {
            Ordering::Less => PredAction::Advance {
                left: true,
                right: false,
            },
            _ => PredAction::Add,
        }
    }
    fn advance(_left_idx: &mut usize, right_idx: &mut usize, _left_end: usize, right_end: usize) {
        *right_idx = right_end;
    }
}

/// The action to take in the sort-merge join algorithm as a result of the selected predicate.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PredAction {
    /// Predicate matches, add current indices to join result.
    Add,
    /// Advance indices (either left, right, or both).
    Advance {
        /// Advance left index.
        left: bool,
        /// Advance right index.
        right: bool,
    },
}

/// A trait for merging a [DataView](../view/struct.DataView.html) with the current object using
/// specified `Join`. `RLabels` and `RFrames` are the `Labels` and `Frames` type parameters for the
/// `DataView` to merge.
pub trait SortMergeJoin<RLabels, RFrames, Join> {
    /// Resultant data structure after join.
    type Output;

    /// Join this object with a `DataView`, using the join details specified with `Join`.
    fn join(&self, right: &DataView<RLabels, RFrames>) -> Self::Output;
}
impl<LLabels, LFrames, RLabels, RFrames, LLabel, RLabel, Pred>
    SortMergeJoin<RLabels, RFrames, Join<LLabel, RLabel, Pred>> for DataView<LLabels, LFrames>
where
    LFrames: JoinIntoStore<LLabels, DataStore<Nil>>,
    RFrames: JoinIntoStore<RLabels, <LFrames as JoinIntoStore<LLabels, DataStore<Nil>>>::Output>,
    <RFrames as JoinIntoStore<
        RLabels,
        <LFrames as JoinIntoStore<LLabels, DataStore<Nil>>>::Output,
    >>::Output: IntoView,
    Self: SelectFieldByLabel<LLabel>,
    <Self as SelectFieldByLabel<LLabel>>::Output: SortOrder,
    VFieldTypeOf<Self, LLabel>: Ord + PartialEq,
    DataView<RLabels, RFrames>: SelectFieldByLabel<RLabel>,
    <DataView<RLabels, RFrames> as SelectFieldByLabel<RLabel>>::Output: SortOrder,
    VFieldOf<DataView<RLabels, RFrames>, RLabel>: DataIndex<DType = VFieldTypeOf<Self, LLabel>>,
    Pred: Predicate,
{
    type Output = <<RFrames as JoinIntoStore<
        RLabels,
        <LFrames as JoinIntoStore<LLabels, DataStore<Nil>>>::Output,
    >>::Output as IntoView>::Output;

    fn join(&self, right: &DataView<RLabels, RFrames>) -> Self::Output {
        let left = self;
        //TODO: return empty dataview if left or right is empty

        let merge_indices =
            merge_indices::<Pred, _, _>(&left.field::<LLabel>(), &right.field::<RLabel>());

        let store = DataStore::<Nil>::empty();

        let store = left
            .frames
            .join_into_store(store, &merge_indices.0)
            .unwrap();
        let store = right
            .frames
            .join_into_store(store, &merge_indices.1)
            .unwrap();
        store.into_view()
    }
}

fn merge_indices<Pred, T, U>(left_key_data: &T, right_key_data: &U) -> (Vec<usize>, Vec<usize>)
where
    Pred: Predicate,
    T: DataIndex + SortOrder,
    U: DataIndex<DType = <T as DataIndex>::DType> + SortOrder,
    <T as DataIndex>::DType: PartialEq + Ord,
{
    let left_order = left_key_data.sort_order();
    let right_order = right_key_data.sort_order();

    debug_assert!(!left_order.is_empty() && !right_order.is_empty());
    // NOTE: actual_idx = perm[sorted_idx]
    // NOTE: value = key_data.get(actual_idx).unwrap();

    let lval = |sorted_idx| left_key_data.get_datum(left_order[sorted_idx]).unwrap();
    let rval = |sorted_idx| right_key_data.get_datum(right_order[sorted_idx]).unwrap();

    // we know left_order and right_order both are non-empty, so there is at least one value
    let (mut left_idx, mut right_idx) = (0, 0);
    let mut left_merge_indices = vec![];
    let mut right_merge_indices = vec![];
    while left_idx < left_order.len() && right_idx < right_order.len() {
        let left_val = lval(left_idx);
        let right_val = rval(right_idx);
        let pred_results = Pred::apply(left_val, right_val);
        match pred_results {
            PredAction::Add => {
                // figure out subsets
                let mut left_subset = vec![left_idx];
                let mut right_subset = vec![right_idx];
                let (mut left_idx_end, mut right_idx_end);
                if Pred::is_equality_pred() {
                    // for equality predicates, add all records with same value
                    left_idx_end = left_idx + 1;
                    while left_idx_end < left_order.len() && left_val == lval(left_idx_end) {
                        left_subset.push(left_idx_end);
                        left_idx_end += 1;
                    }
                    right_idx_end = right_idx + 1;
                    while right_idx_end < right_order.len() && right_val == rval(right_idx_end) {
                        right_subset.push(right_idx_end);
                        right_idx_end += 1;
                    }
                } else {
                    left_idx_end = left_idx + 1;
                    right_idx_end = right_idx + 1;
                }
                let (left_eq_end, right_eq_end) = (left_idx_end, right_idx_end);
                if Pred::is_greater_than_pred() {
                    // for greater-than predicates, we can add the rest of the left values
                    while left_idx_end < left_order.len() {
                        left_subset.push(left_idx_end);
                        left_idx_end += 1;
                    }
                }
                if Pred::is_less_than_pred() {
                    // for less-than predicates, we can add the rest of the right values
                    while right_idx_end < right_order.len() {
                        right_subset.push(right_idx_end);
                        right_idx_end += 1;
                    }
                }
                // add cross product of subsets to merge indices
                for lidx in &left_subset {
                    // NAs shouldn't match a predicate, only add if value exists
                    if lval(*lidx).exists() {
                        for ridx in &right_subset {
                            if rval(*ridx).exists() {
                                left_merge_indices.push(left_order[*lidx]);
                                right_merge_indices.push(right_order[*ridx]);
                            }
                        }
                    }
                }
                // advance as needed
                Pred::advance(&mut left_idx, &mut right_idx, left_eq_end, right_eq_end);
            }
            PredAction::Advance { left, right } => {
                if left {
                    left_idx += 1;
                }
                if right {
                    right_idx += 1;
                }
            }
        }
    }
    (left_merge_indices, right_merge_indices)
}

/// A trait for augmenting type `Store` (a [DataStore](../store/struct.DataStore.html)) with
/// fields from this [ViewFrameCons](../view/type.ViewFrameCons.html) as labeled by `Labels`.
pub trait JoinIntoStore<Labels, Store> {
    /// The output type after augmenting `Store`.
    type Output;

    /// Augments `store` with data from `self` (as specified with `Labels`), using the provided
    /// permutation indices.
    fn join_into_store(&self, store: Store, permutation: &[usize]) -> Result<Self::Output>;
}
impl<Frames, Store> JoinIntoStore<Nil, Store> for Frames {
    type Output = Store;
    fn join_into_store(&self, store: Store, _permutation: &[usize]) -> Result<Store> {
        Ok(store)
    }
}
impl<Label, FrameIndex, FrameLabel, Tail, Frames, Store>
    JoinIntoStore<FrameLookupCons<Label, FrameIndex, FrameLabel, Tail>, Store> for Frames
where
    Frames: LookupValuedElemByLabel<FrameIndex>,
    FrameByFrameIndexOf<Frames, FrameIndex>: SelectFieldByLabel<FrameLabel>,
    Store: PushBackClonedFromValueIter<
        Label,
        FieldTypeFromFrameDetailsOf<Frames, FrameIndex, FrameLabel>,
    >,
    Frames: JoinIntoStore<
        Tail,
        DataStore<
            <Store as PushBackClonedFromValueIter<
                Label,
                FieldTypeFromFrameDetailsOf<Frames, FrameIndex, FrameLabel>,
            >>::OutputFields,
        >,
    >,
{
    type Output = <Frames as JoinIntoStore<
        Tail,
        DataStore<
            <Store as PushBackClonedFromValueIter<
                Label,
                FieldTypeFromFrameDetailsOf<Frames, FrameIndex, FrameLabel>,
            >>::OutputFields,
        >,
    >>::Output;

    fn join_into_store(&self, store: Store, permutation: &[usize]) -> Result<Self::Output> {
        let store = store.push_back_cloned_from_value_iter(
            SelectFieldByLabel::<FrameLabel>::select_field(
                LookupValuedElemByLabel::<FrameIndex>::elem(self).value_ref(),
            )
            .permute(permutation)?,
        );
        let store = JoinIntoStore::<Tail, _>::join_into_store(self, store, permutation)?;
        Ok(store)
    }
}

#[cfg(feature = "test-utils")]
#[cfg(test)]
mod tests {
    use super::*;
    use field::FieldData;
    use test_utils::*;

    #[test]
    fn inner_equi_join() {
        let dv_emp = sample_emp_table().into_view();
        let dv_dept = sample_dept_table().into_view();
        println!("{}", dv_emp);
        println!("{}", dv_dept);

        let joined_dv =
            dv_emp.join::<Join<emp_table::DeptId, dept_table::DeptId, Equal>, _, _>(&dv_dept);
        println!("{}", joined_dv);
        assert_eq!(joined_dv.nrows(), 7);
        assert_eq!(joined_dv.nfields(), 5);
        assert_eq!(
            joined_dv.field::<emp_table::EmpId>().to_vec(),
            vec![0u64, 5, 6, 2, 8, 9, 10]
        );
        assert_eq!(
            joined_dv.field::<emp_table::DeptId>().to_vec(),
            vec![1u64, 1, 1, 2, 3, 4, 4]
        );
        assert_eq!(
            joined_dv.field::<emp_table::EmpName>().to_vec(),
            vec!["Sally", "Bob", "Cara", "Jamie", "Louis", "Louise", "Ann"]
        );
        assert_eq!(
            joined_dv.field::<dept_table::DeptName>().to_vec(),
            vec![
                "Marketing",
                "Marketing",
                "Marketing",
                "Sales",
                "Manufacturing",
                "R&D",
                "R&D"
            ]
        );
    }

    #[test]
    fn inner_equi_join_missing_dept_id() {
        // dept id missing from dept table, should remove the entire marketing department from join
        let dv_emp = sample_emp_table().into_view();
        let dv_dept = dept_table_from_field(
            FieldData::from_field_vec(vec![
                Value::Na,
                Value::Exists(2),
                Value::Exists(3),
                Value::Exists(4),
            ]),
            FieldData::from_field_vec(vec![
                Value::Exists("Marketing".into()),
                Value::Exists("Sales".into()),
                Value::Exists("Manufacturing".into()),
                Value::Exists("R&D".into()),
            ]),
        )
        .into_view();

        println!("{}", dv_emp);
        println!("{}", dv_dept);

        let joined_dv =
            dv_emp.join::<Join<emp_table::DeptId, dept_table::DeptId, Equal>, _, _>(&dv_dept);
        println!("{}", joined_dv);

        assert_eq!(joined_dv.nrows(), 4);
        assert_eq!(joined_dv.nfields(), 5);
        assert_eq!(
            joined_dv.field::<emp_table::EmpId>().to_vec(),
            vec![2u64, 8, 9, 10]
        );
        assert_eq!(
            joined_dv.field::<emp_table::DeptId>().to_vec(),
            vec![2u64, 3, 4, 4]
        );
        assert_eq!(
            joined_dv.field::<emp_table::EmpName>().to_vec(),
            vec!["Jamie", "Louis", "Louise", "Ann"]
        );
        assert_eq!(
            joined_dv.field::<dept_table::DeptName>().to_vec(),
            vec!["Sales", "Manufacturing", "R&D", "R&D"]
        );

        // dept id missing from emp table, should remove single employee from join
        let ds_emp: emp_table::Store = emp_table_from_field!(
            FieldData::from_field_vec(vec![
                Value::Exists(0),
                Value::Exists(2),
                Value::Exists(5),
                Value::Exists(6),
                Value::Exists(8),
                Value::Exists(9),
                Value::Exists(10),
            ]),
            FieldData::from_field_vec(vec![
                Value::Exists(1),
                Value::Exists(2),
                Value::Na, // Bob's department isn't specified
                Value::Exists(1),
                Value::Exists(3),
                Value::Exists(4),
                Value::Exists(4),
            ]),
            FieldData::from_field_vec(vec![
                Value::Exists("Sally".into()),
                Value::Exists("Jamie".into()),
                Value::Exists("Bob".into()),
                Value::Exists("Cara".into()),
                Value::Exists("Louis".into()),
                Value::Exists("Louise".into()),
                Value::Exists("Ann".into()),
            ])
        );
        let dv_emp = ds_emp.into_view();
        let dv_dept = sample_dept_table().into_view();
        println!("{}", dv_emp);
        println!("{}", dv_dept);
        let joined_dv =
            dv_emp.join::<Join<emp_table::DeptId, dept_table::DeptId, Equal>, _, _>(&dv_dept);
        println!("{}", joined_dv);
        assert_eq!(joined_dv.nrows(), 6);
        assert_eq!(joined_dv.nfields(), 5);
        assert_eq!(
            joined_dv.field::<emp_table::EmpId>().to_vec(),
            vec![0u64, 6, 2, 8, 9, 10]
        );
        assert_eq!(
            joined_dv.field::<emp_table::DeptId>().to_vec(),
            vec![1u64, 1, 2, 3, 4, 4]
        );
        assert_eq!(
            joined_dv.field::<emp_table::EmpName>().to_vec(),
            vec!["Sally", "Cara", "Jamie", "Louis", "Louise", "Ann"]
        );
        assert_eq!(
            joined_dv.field::<dept_table::DeptName>().to_vec(),
            vec![
                "Marketing",
                "Marketing",
                "Sales",
                "Manufacturing",
                "R&D",
                "R&D"
            ]
        );
    }

    #[test]
    fn filter_inner_equi_join() {
        // should have same results as first test in inner_equi_join_missing_dept_id
        let dv_emp = sample_emp_table().into_view();
        let mut dv_dept = sample_dept_table().into_view();
        println!("{}", dv_emp);
        println!("{}", dv_dept);

        dv_dept.filter::<dept_table::DeptId, _>(|val: Value<&u64>| val != valref![1u64]);
        println!("{}", dv_dept);
        let joined_dv =
            dv_emp.join::<Join<emp_table::DeptId, dept_table::DeptId, Equal>, _, _>(&dv_dept);
        println!("{}", joined_dv);
        assert_eq!(joined_dv.nrows(), 4);
        assert_eq!(joined_dv.nfields(), 5);
        assert_eq!(
            joined_dv.field::<emp_table::EmpId>().to_vec(),
            vec![2u64, 8, 9, 10]
        );
        assert_eq!(
            joined_dv.field::<emp_table::DeptId>().to_vec(),
            vec![2u64, 3, 4, 4]
        );
        assert_eq!(
            joined_dv.field::<emp_table::EmpName>().to_vec(),
            vec!["Jamie", "Louis", "Louise", "Ann"]
        );
        assert_eq!(
            joined_dv.field::<dept_table::DeptName>().to_vec(),
            vec!["Sales", "Manufacturing", "R&D", "R&D"]
        );
    }

    tablespace![
        @continue(typenum::Add1<::test_utils::dept_table::Table>)
        table dept_rename {
            RDeptId: u64
        }
    ];

    #[test]
    fn inner_nonequi_join() {
        // greater than
        let dv_emp = sample_emp_table().into_view();
        let dv_dept = dept_table(vec![1, 2], vec!["Marketing", "Sales"]).into_view();
        println!("{}", dv_emp);
        println!("{}", dv_dept);

        let dv_dept = dv_dept.relabel::<dept_table::DeptId, dept_rename::RDeptId>();
        // also test relabeling
        let joined_dv = dv_emp
            .join::<Join<emp_table::DeptId, dept_rename::RDeptId, GreaterThan>, _, _>(&dv_dept);
        println!("{}", joined_dv);
        assert_eq!(joined_dv.nrows(), 7);
        assert_eq!(joined_dv.nfields(), 5);
        for value in joined_dv.field::<emp_table::DeptId>().iter() {
            assert![*value.unwrap() >= 2];
        }

        // greater than equal
        let dv_emp = sample_emp_table().into_view();
        let dv_dept = dept_table(vec![2], vec!["Sales"]).into_view();
        let joined_dv = dv_emp
            .join::<Join<emp_table::DeptId, dept_table::DeptId, GreaterThanEqual>, _, _>(&dv_dept);
        println!("{}", joined_dv);
        assert_eq!(joined_dv.nrows(), 4);
        assert_eq!(joined_dv.nfields(), 5);
        for value in joined_dv.field::<emp_table::DeptId>().iter() {
            assert![*value.unwrap() >= 2];
        }

        // less than
        let dv_emp = sample_emp_table().into_view();
        let dv_dept = dept_table(vec![2], vec!["Sales"]).into_view();
        let joined_dv =
            dv_emp.join::<Join<emp_table::DeptId, dept_table::DeptId, LessThan>, _, _>(&dv_dept);
        println!("{}", joined_dv);
        assert_eq!(joined_dv.nrows(), 3);
        assert_eq!(joined_dv.nfields(), 5);
        for value in joined_dv.field::<emp_table::DeptId>().iter() {
            assert_eq![*value.unwrap(), 1];
        }

        // less than equal
        let dv_emp = sample_emp_table().into_view();
        let dv_dept = dept_table(vec![2], vec!["Sales"]).into_view();
        let joined_dv = dv_emp
            .join::<Join<emp_table::DeptId, dept_table::DeptId, LessThanEqual>, _, _>(&dv_dept);
        println!("{}", joined_dv);
        assert_eq!(joined_dv.nrows(), 4);
        assert_eq!(joined_dv.nfields(), 5);
        for value in joined_dv.field::<emp_table::DeptId>().iter() {
            assert![*value.unwrap() <= 2];
        }
    }
}