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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
//! # Given a Relation representing a computaion on a sampled table, a table (representing the schema of original dataset) and a weight representing
//!
//! WARNING This is experimental and little tested yet.
// TODO Test and document this properly

use crate::{
    builder::{Ready, With},
    expr::{aggregate, identifier::Identifier, AggregateColumn, Expr},
    hierarchy::Hierarchy,
    relation::{Join, Map, Reduce, Relation, Set, Table, Values, Variant as _, Visitor},
    visitor::Acceptor,
    WithIterator,
};
use std::{error, fmt, result, sync::Arc, vec};

#[derive(Debug, Clone)]
pub enum Error {
    Other(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Other(err) => writeln!(f, "{}", err),
        }
    }
}

impl error::Error for Error {}

pub type Result<T> = result::Result<T, Error>;

#[derive(Clone, Debug)]
pub struct RelationWithWeight(Relation, f64);

/// Getters
impl RelationWithWeight {
    pub fn relation(&self) -> Relation {
        self.0.clone().filter_fields(|n| n != ROW_WEIGHT)
    }

    pub fn weight(&self) -> &f64 {
        &self.1
    }
}

// Visitors

/// A visitor to compute RelationWithWeight propagationing table weights
/// and ROW_WEIGHT.
#[derive(Clone, Debug)]
struct WeightRelationVisitor<F: Fn(&Table) -> RelationWithWeight> {
    weight_table: F,
}

pub const ROW_WEIGHT: &str = "_ROW_WEIGHT_";
pub const ONE_COUNT_ROW_WEIGHT: &str = "_ONE_COUNT_ROW_WEIGHT_";
pub const GREATER_THAN_ONE_COUNT_ROW_WEIGHT: &str = "_GREATER_THAN_ONE_COUNT_ROW_WEIGHT_";
pub const ACTUAL_GROUPS_COUNT: &str = "_ACTUAL_GROUPS_COUNT_";
pub const ESTIMATED_GROUPS_COUNT: &str = "_ESTIMATED_GROUPS_COUNT_";
pub const CORRECTION_FACTOR: &str = "_CORRECTION_FACTOR_";
pub const PROPAGATED_COLUMNS: usize = 1;

impl<F: Fn(&Table) -> RelationWithWeight> WeightRelationVisitor<F> {
    pub fn new(weight_table: F) -> Self {
        WeightRelationVisitor { weight_table }
    }
}

impl<'a, F: Fn(&Table) -> RelationWithWeight> Visitor<'a, RelationWithWeight>
    for WeightRelationVisitor<F>
{
    /// Apply the weight from weight_table and add ROW_WEIGHT field initialized to 1
    fn table(&self, table: &'a Table) -> RelationWithWeight {
        let weighted_table = (self.weight_table)(table);
        let new_rel = weighted_table.0.insert_field(0, ROW_WEIGHT, Expr::val(1));
        RelationWithWeight(new_rel, weighted_table.1)
    }

    /// Propagate the relation weight and ROW_WEIGHT.
    fn map(&self, map: &'a Map, input: RelationWithWeight) -> RelationWithWeight {
        let mew_relation: Relation = Relation::map()
            .with((ROW_WEIGHT, Expr::col(ROW_WEIGHT)))
            .with(map.clone())
            .input(input.0)
            .build();
        RelationWithWeight(mew_relation, input.1)
    }

    /// It constructs the needed relations to compute the CORRECTION_FACTOR from the ROW_WEIGHT
    /// to compensate for the lost of groups during sampling
    /// CORRECTION_FACTOR = ESTIMATED_GROUPS_COUNT / ACTUAL_GROUPS_COUNT
    /// ESTIMATED_GROUPS_COUNT = sqrt(w)*sum(ONE_COUNT_ROW_WEIGHT) + sum(GREATER_THAN_ONE_COUNT_ROW_WEIGHT)
    /// for more details see: https://dl.acm.org/doi/pdf/10.1145/276305.276343
    /// In the paper ONE_COUNT_ROW_WEIGHT is indicated as f1
    /// and GREATER_THAN_ONE_COUNT_ROW_WEIGHT is indicated as fj
    /// It applies the corrections to the aggregation functions
    /// The table weight of the output RelationWithWeight is 1.0.
    fn reduce(&self, reduce: &'a Reduce, input: RelationWithWeight) -> RelationWithWeight {
        // construct the new reduce from the visited reduce and the input relation.
        // with sum of ROW_WEIGHT
        let new_reduce: Relation = Relation::reduce()
            .with((ROW_WEIGHT, Expr::sum(Expr::col(ROW_WEIGHT))))
            .with(reduce.clone())
            .input(input.0.clone())
            .build();

        // compute the counts of line weight
        let counts_row_weight = &format!("_COUNTS{ROW_WEIGHT}")[..];
        let group_by_line_weight: Relation = Relation::reduce()
            .with((ROW_WEIGHT, Expr::first(Expr::col(ROW_WEIGHT))))
            .with((counts_row_weight, Expr::count(Expr::col(ROW_WEIGHT))))
            .group_by(Expr::col(ROW_WEIGHT))
            .input(new_reduce.clone())
            .build();

        let one_where_f1 = Expr::case(
            Expr::eq(Expr::col(ROW_WEIGHT), Expr::val(1)),
            Expr::val(1),
            Expr::val(0),
        );
        let one_where_fj = Expr::case(
            Expr::gt(Expr::col(ROW_WEIGHT), Expr::val(1)),
            Expr::col(counts_row_weight),
            Expr::val(0),
        );

        let tmp_map: Relation = Relation::map()
            .with((ROW_WEIGHT, Expr::col(ROW_WEIGHT)))
            .with((ONE_COUNT_ROW_WEIGHT, one_where_f1))
            .with((GREATER_THAN_ONE_COUNT_ROW_WEIGHT, one_where_fj))
            .input(group_by_line_weight)
            .build();

        // compute sum_f1: sum over number of distinct values which occur exactly 1 times
        // sum_fj: sum over number of distinct values which occur exactly j times
        let tmp_reduce: Relation = Relation::reduce()
            .with((
                format!("_SUM{ONE_COUNT_ROW_WEIGHT}"),
                Expr::sum(Expr::col(ONE_COUNT_ROW_WEIGHT)),
            ))
            .with((
                format!("_SUM{GREATER_THAN_ONE_COUNT_ROW_WEIGHT}"),
                Expr::sum(Expr::col(GREATER_THAN_ONE_COUNT_ROW_WEIGHT)),
            ))
            .input(tmp_map)
            .build();

        let estimated_groups = Expr::multiply(
            Expr::sqrt(Expr::val(input.1)),
            Expr::col(format!("_SUM{ONE_COUNT_ROW_WEIGHT}")),
        ) + Expr::col(format!("_SUM{GREATER_THAN_ONE_COUNT_ROW_WEIGHT}"));

        let estimated_and_actual_groups: Relation = Relation::map()
            .with((ESTIMATED_GROUPS_COUNT, estimated_groups))
            .with((
                ACTUAL_GROUPS_COUNT,
                Expr::col(format!("_SUM{ONE_COUNT_ROW_WEIGHT}"))
                    + Expr::col(format!("_SUM{GREATER_THAN_ONE_COUNT_ROW_WEIGHT}")),
            ))
            .input(tmp_reduce)
            .build();

        let correction_factor = Expr::divide(
            Expr::col(ESTIMATED_GROUPS_COUNT),
            Expr::col(ACTUAL_GROUPS_COUNT),
        );
        let correction_factor_map: Relation = Relation::map()
            .with((CORRECTION_FACTOR, correction_factor))
            .filter(Expr::and(
                Expr::gt_eq(Expr::col(ESTIMATED_GROUPS_COUNT), Expr::val(1.0)),
                Expr::gt_eq(Expr::col(ACTUAL_GROUPS_COUNT), Expr::val(1.0)),
            ))
            .limit(1)
            .input(estimated_and_actual_groups)
            .build();

        // join correction_factor_map with the new reduce.
        // correction_factor_map has 1 line result so the join type would be cross
        let reduce_with_correction: Relation = Relation::join()
            .left_names(
                new_reduce
                    .schema()
                    .iter()
                    .map(|f| f.name().to_string())
                    .collect(),
            )
            .right_names(vec![CORRECTION_FACTOR.to_string()])
            .cross()
            .left(new_reduce)
            .right(correction_factor_map)
            .build();

        // Extract field from the visited reduce
        let field_aggexpr_map: Vec<(&str, &AggregateColumn)> = reduce
            .schema()
            .fields()
            .iter()
            .map(|field| field.name())
            .zip(reduce.aggregate())
            .collect();

        // Apply corrections to aggregate function
        let new_exprs: Vec<(&str, Expr)> = field_aggexpr_map
            .into_iter()
            .map(|(name, agg)| match agg.aggregate() {
                aggregate::Aggregate::Count => (
                    name,
                    Expr::multiply(
                        Expr::col(CORRECTION_FACTOR),
                        Expr::multiply(Expr::val(input.1), Expr::col(name)),
                    ),
                ),
                aggregate::Aggregate::Sum => {
                    (name, Expr::multiply(Expr::val(input.1), Expr::col(name)))
                }
                aggregate::Aggregate::Mean => (
                    name,
                    Expr::divide(Expr::col(name), Expr::col(CORRECTION_FACTOR)),
                ),
                aggregate::Aggregate::First | aggregate::Aggregate::Last => (name, Expr::col(name)),
                // todo for aggregation function that we don't know how to correct yet such as MIN and MAX.
                _ => todo!(),
            })
            .collect();

        let new_map: Relation = Relation::map()
            .with((ROW_WEIGHT, Expr::col(ROW_WEIGHT)))
            .with_iter(new_exprs.into_iter())
            .input(reduce_with_correction)
            .build();

        RelationWithWeight(new_map, 1.0)
    }

    /// propagate table weight as table weight left * table weight right
    /// propagate ROW_WEIGHT as _LEFT_ROW_WEIGHT_ * _RIGHT_ROW_WEIGHT_
    fn join(
        &self,
        join: &'a Join,
        left: RelationWithWeight,
        right: RelationWithWeight,
    ) -> RelationWithWeight {
        let RelationWithWeight(left, left_weight) = &left;
        let RelationWithWeight(right, right_weight) = &right;

        let left_new_name = left.name().to_string();
        let right_new_name = right.name().to_string();
        let schema_names: Vec<String> =
            join.schema().iter().map(|f| f.name().to_string()).collect();

        let mut left_names = vec![format!("_LEFT{ROW_WEIGHT}")];
        left_names.extend(
            schema_names
                .iter()
                .take(join.left().schema().len())
                .cloned(),
        );

        let mut right_names = vec![format!("_RIGHT{ROW_WEIGHT}")];
        right_names.extend(
            schema_names
                .iter()
                .skip(join.left().schema().len())
                .cloned(),
        );

        // map old columns names (from the join) into new column names from the left and right
        let columns_mapping: Hierarchy<Identifier> = join
            .left()
            .schema()
            .iter()
            // skip 1 because the left (coming from the RelationWithWeight)
            // has the ROW_WEIGHT colum
            .zip(left.schema().iter().skip(PROPAGATED_COLUMNS))
            .map(|(o, n)| {
                (
                    vec![join.left().name().to_string(), o.name().to_string()],
                    Identifier::from(vec![left_new_name.clone(), n.name().to_string()]),
                )
            })
            .chain(
                join.right()
                    .schema()
                    .iter()
                    .zip(right.schema().iter().skip(PROPAGATED_COLUMNS))
                    .map(|(o, n)| {
                        (
                            vec![join.right().name().to_string(), o.name().to_string()],
                            Identifier::from(vec![right_new_name.clone(), n.name().to_string()]),
                        )
                    }),
            )
            .collect();

        let builder = Relation::join()
            .left_names(left_names)
            .right_names(right_names)
            .operator(join.operator().clone().rename(&columns_mapping))
            .left(left.clone())
            .right(right.clone());

        let join: Join = builder.build();

        let mut builder = Relation::map();
        builder = builder.with((
            ROW_WEIGHT,
            Expr::multiply(
                Expr::col(format!("_LEFT{ROW_WEIGHT}")),
                Expr::col(format!("_RIGHT{ROW_WEIGHT}")),
            ),
        ));
        builder = join.names().iter().fold(builder, |b, (p, n)| {
            if [ROW_WEIGHT].contains(&p[1].as_str()) {
                b
            } else {
                b.with((n, Expr::col(n)))
            }
        });
        let builder = builder.input(Arc::new(join.into()));

        let final_map: Relation = builder.build();

        RelationWithWeight(final_map, left_weight * right_weight)
    }

    fn set(
        &self,
        set: &'a Set,
        left: RelationWithWeight,
        right: RelationWithWeight,
    ) -> RelationWithWeight {
        todo!()
    }

    fn values(&self, values: &'a Values) -> RelationWithWeight {
        todo!()
    }
}

/// A visitor to samaple tables in a relation
struct TableSamplerVisitor<F: Fn(&Table) -> Relation> {
    table_sampler: F,
}

impl<F: Fn(&Table) -> Relation> TableSamplerVisitor<F> {
    pub fn new(table_sampler: F) -> Self {
        TableSamplerVisitor { table_sampler }
    }
}

impl<'a, F: Fn(&Table) -> Relation> Visitor<'a, Relation> for TableSamplerVisitor<F> {
    fn table(&self, table: &'a Table) -> Relation {
        (self.table_sampler)(table)
    }

    fn map(&self, map: &'a Map, input: Relation) -> Relation {
        Relation::map().with(map.clone()).input(input).build()
    }

    fn reduce(&self, reduce: &'a Reduce, input: Relation) -> Relation {
        Relation::reduce().with(reduce.clone()).input(input).build()
    }

    fn join(&self, join: &'a Join, left: Relation, right: Relation) -> Relation {
        let left_new_name = left.name().to_string();
        let right_new_name = right.name().to_string();

        // Preserve the schema names of the existing JOIN
        let schema_names: Vec<String> =
            join.schema().iter().map(|f| f.name().to_string()).collect();
        let left_names: Vec<String> = schema_names
            .iter()
            .take(join.left().schema().len())
            .cloned()
            .collect();
        let right_names: Vec<String> = schema_names
            .iter()
            .skip(join.left().schema().len())
            .cloned()
            .collect();

        let columns_mapping: Hierarchy<Identifier> = join
            .left()
            .schema()
            .iter()
            .zip(left.schema().iter())
            .map(|(o, n)| {
                (
                    vec![join.left().name().to_string(), o.name().to_string()],
                    Identifier::from(vec![left_new_name.clone(), n.name().to_string()]),
                )
            })
            .chain(
                join.right()
                    .schema()
                    .iter()
                    .zip(right.schema().iter())
                    .map(|(o, n)| {
                        (
                            vec![join.right().name().to_string(), o.name().to_string()],
                            Identifier::from(vec![right_new_name.clone(), n.name().to_string()]),
                        )
                    }),
            )
            .collect();

        // build the output relation
        Relation::join()
            .left_names(left_names)
            .right_names(right_names)
            .operator(join.operator().clone().rename(&columns_mapping))
            .left(left)
            .right(right)
            .build()
    }

    fn set(&self, set: &'a Set, left: Relation, right: Relation) -> Relation {
        todo!()
    }

    fn values(&self, values: &'a Values) -> Relation {
        Relation::Values(values.clone())
    }
}

// Visitor builders

/// Creates a WeightRelationVisitor that applies the same weight to all tables
fn uniform_adjustment_table_visitor(
    weight: f64,
) -> WeightRelationVisitor<impl Fn(&Table) -> RelationWithWeight> {
    WeightRelationVisitor::new(move |table: &Table| {
        RelationWithWeight(Relation::from(table.clone()), weight)
    })
}

/// Creates a WeightRelationVisitor that applies the different weights to tables according to the input
fn differenciated_adjustment_table_visitor<'a>(
    relations: &'a Hierarchy<Arc<Relation>>,
    tables_and_weights: Vec<(Vec<String>, f64)>,
) -> WeightRelationVisitor<impl Fn(&Table) -> RelationWithWeight + 'a> {
    WeightRelationVisitor::new(move |table: &Table| {
        match tables_and_weights.iter().find(|(tab, _)| {
            relations
                .get(tab)
                .map_or(false, |rel| rel.name() == table.name())
        }) {
            Some((_, weight)) => RelationWithWeight(Relation::from(table.clone()), *weight),
            None => RelationWithWeight(Relation::from(table.clone()), 1.0),
        }
    })
}

/// Creates a TableSamplerVisitor that applies poisson sampling with the same probability to all tables.
fn poisson_sampling_table_visitor(proba: f64) -> TableSamplerVisitor<impl Fn(&Table) -> Relation> {
    TableSamplerVisitor::new(move |table: &Table| {
        Relation::from(table.clone()).poisson_sampling(proba)
    })
}

/// Creates a TableSamplerVisitor that applies poisson sampling with different probability to tables as specified in the input
fn differenciated_poisson_sampling_table_visitor<'a>(
    relations: &'a Hierarchy<Arc<Relation>>,
    tables_and_sampling_probabilities: Vec<(Vec<String>, f64)>,
) -> TableSamplerVisitor<impl Fn(&Table) -> Relation + 'a> {
    TableSamplerVisitor::new(move |table: &Table| {
        match tables_and_sampling_probabilities.iter().find(|(tab, _)| {
            relations
                .get(tab)
                .map_or(false, |rel| rel.name() == table.name())
        }) {
            Some((_, proba)) => Relation::from(table.clone()).poisson_sampling(*proba),
            None => table.clone().into(),
        }
    })
}

/// Creates a TableSamplerVisitor that applies random samping with without replacement using the same rate for all tables.
fn sampling_without_replacements_table_visitor(
    rate: f64,
    rate_multiplier: f64,
) -> TableSamplerVisitor<impl Fn(&Table) -> Relation> {
    TableSamplerVisitor::new(move |table: &Table| {
        Relation::from(table.clone()).sampling_without_replacements(rate, rate_multiplier)
    })
}

impl Relation {
    pub fn uniform_adjustment<'a>(&'a self, weight: f64) -> RelationWithWeight {
        self.accept(uniform_adjustment_table_visitor(weight))
    }

    pub fn differenciated_adjustment<'a>(
        &'a self,
        relations: &'a Hierarchy<Arc<Relation>>,
        tables_and_weights: Vec<(Vec<String>, f64)>,
    ) -> RelationWithWeight {
        self.accept(differenciated_adjustment_table_visitor(
            relations,
            tables_and_weights,
        ))
    }

    pub fn uniform_poisson_sampling<'a>(&'a self, proba: f64) -> Relation {
        self.accept(poisson_sampling_table_visitor(proba))
    }

    pub fn differenciated_poisson_sampling<'a>(
        &'a self,
        relations: &'a Hierarchy<Arc<Relation>>,
        tables_and_sampling_probabilities: Vec<(Vec<String>, f64)>,
    ) -> Relation {
        self.accept(differenciated_poisson_sampling_table_visitor(
            relations,
            tables_and_sampling_probabilities,
        ))
    }

    pub fn uniform_sampling_without_replacements<'a>(
        &'a self,
        rate: f64,
        rate_multiplier: f64,
    ) -> Relation {
        self.accept(sampling_without_replacements_table_visitor(
            rate,
            rate_multiplier,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        ast,
        display::Dot,
        io::{postgresql, Database},
        namer,
        sql::parse,
    };

    use colored::Colorize;
    use itertools::Itertools;

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_uniform_poisson_sampling() {
        let mut database = postgresql::test_database();
        let proba = 0.1;
        let relations = database.relations();
        let relation = Relation::try_from(
            parse("SELECT * FROM order_table JOIN item_table ON id=order_id")
                .unwrap()
                .with(&relations),
        )
        .unwrap();
        namer::reset();
        let sampled = relation.uniform_poisson_sampling(proba);
        namer::reset();
        // build the resulting relation
        let item_table = relations
            .get(&["item_table".into()])
            .unwrap()
            .as_ref()
            .clone();

        let sampled_item_table = item_table.poisson_sampling(proba);

        let order_table = relations
            .get(&["order_table".into()])
            .unwrap()
            .as_ref()
            .clone();

        let sampled_order_table = order_table.poisson_sampling(proba);

        let relation_schema_names: Vec<&str> = relation.schema().iter().map(|f| f.name()).collect();
        let relation_name = relation.name();
        let left_names: Vec<String> = relation_schema_names
            .iter()
            .take(sampled_order_table.schema().len())
            .map(|s| s.to_string())
            .collect();
        let right_exprs: Vec<String> = relation_schema_names
            .iter()
            .skip(sampled_order_table.schema().len())
            .map(|s| s.to_string())
            .collect();

        let join: Relation = Relation::join()
            .left_names(left_names)
            .right_names(right_exprs)
            .left(sampled_order_table)
            .right(sampled_item_table)
            .on_eq("order_id", "id")
            .build();
        let exprs: Vec<(&str, Expr)> = join
            .schema()
            .iter()
            .map(|f| (f.name().clone(), Expr::col(f.name())))
            .collect();

        let final_map: Relation = Relation::map()
            .name(relation_name)
            .with_iter(exprs.into_iter())
            .input(join)
            .build();

        assert_eq!(final_map, sampled);

        relation.display_dot().unwrap();
        sampled.display_dot().unwrap();
        final_map.display_dot().unwrap();
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_differenciated_poisson_sampling() {
        let mut database = postgresql::test_database();
        let tables_and_proba: Vec<(Vec<String>, f64)> = vec![
            (vec!["order_table".to_string()], 0.1),
            (vec!["item_table".to_string()], 0.5),
        ];
        let relations = database.relations();
        let relation = Relation::try_from(
            parse("SELECT * FROM order_table JOIN item_table ON id=order_id")
                .unwrap()
                .with(&relations),
        )
        .unwrap();
        namer::reset();
        let sampled = relation.differenciated_poisson_sampling(&relations, tables_and_proba);
        // build the resulting relation
        namer::reset();
        let item_table = relations
            .get(&["item_table".into()])
            .unwrap()
            .as_ref()
            .clone();

        let sampled_item_table = item_table.poisson_sampling(0.5);

        let order_table = relations
            .get(&["order_table".into()])
            .unwrap()
            .as_ref()
            .clone();

        let sampled_order_table = order_table.poisson_sampling(0.1);

        let relation_schema_names: Vec<&str> = relation.schema().iter().map(|f| f.name()).collect();
        let relation_name = relation.name();
        let left_names: Vec<String> = relation_schema_names
            .iter()
            .take(sampled_order_table.schema().len())
            .map(|s| s.to_string())
            .collect();
        let right_exprs: Vec<String> = relation_schema_names
            .iter()
            .skip(sampled_order_table.schema().len())
            .map(|s| s.to_string())
            .collect();
        let join: Relation = Relation::join()
            .left_names(left_names)
            .right_names(right_exprs)
            .left(sampled_order_table)
            .right(sampled_item_table)
            .on_eq("order_id", "id")
            .build();
        let exprs: Vec<(&str, Expr)> = join
            .schema()
            .iter()
            .map(|f| (f.name().clone(), Expr::col(f.name())))
            .collect();

        let final_map: Relation = Relation::map()
            .name(relation_name)
            .with_iter(exprs.into_iter())
            .input(join)
            .build();

        assert_eq!(final_map, sampled);

        relation.display_dot().unwrap();
        sampled.display_dot().unwrap();
        final_map.display_dot().unwrap();
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_sampling_without_replacements() {
        let mut database = postgresql::test_database();
        let proba = 0.1;
        let relations = database.relations();
        namer::reset();
        let relation = Relation::try_from(
            parse("SELECT * FROM order_table JOIN item_table ON id=order_id")
                .unwrap()
                .with(&relations),
        )
        .unwrap();
        namer::reset();
        let sampled = relation.uniform_sampling_without_replacements(proba, 1.0);
        // build the resulting relation
        namer::reset();
        let item_table = relations
            .get(&["item_table".into()])
            .unwrap()
            .as_ref()
            .clone();

        let sampled_item_table = item_table.sampling_without_replacements(proba, 1.0);

        let order_table = relations
            .get(&["order_table".into()])
            .unwrap()
            .as_ref()
            .clone();

        let sampled_order_table = order_table.sampling_without_replacements(proba, 1.0);

        let relation_schema_names: Vec<&str> = relation.schema().iter().map(|f| f.name()).collect();
        let relation_name = relation.name();
        let left_names: Vec<String> = relation_schema_names
            .iter()
            .take(sampled_order_table.schema().len())
            .map(|s| s.to_string())
            .collect();
        let right_exprs: Vec<String> = relation_schema_names
            .iter()
            .skip(sampled_order_table.schema().len())
            .map(|s| s.to_string())
            .collect();

        let join: Relation = Relation::join()
            .left_names(left_names)
            .right_names(right_exprs)
            .left(sampled_order_table)
            .right(sampled_item_table)
            .on_eq("order_id", "id")
            .build();
        let exprs: Vec<(&str, Expr)> = join
            .schema()
            .iter()
            .map(|f| (f.name().clone(), Expr::col(f.name())))
            .collect();

        let final_map: Relation = Relation::map()
            .name(relation_name)
            .with_iter(exprs.into_iter())
            .input(join)
            .build();

        assert_eq!(final_map, sampled);

        relation.display_dot().unwrap();
        sampled.display_dot().unwrap();
        final_map.display_dot().unwrap();
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_table_with_weight() {
        let mut database = postgresql::test_database();
        let weight: f64 = 2.0;
        let relations = database.relations();
        let table = relations
            .get(&["item_table".into()])
            .unwrap()
            .as_ref()
            .clone();
        let uniform_weighted_relation = table.uniform_adjustment(weight);
        uniform_weighted_relation.relation().display_dot().unwrap();
        assert!(*uniform_weighted_relation.weight() == 2.0);

        let differenciated_weighted_relation = table
            .differenciated_adjustment(&relations, vec![(vec!["item_table".to_string()], weight)]);
        differenciated_weighted_relation
            .relation()
            .display_dot()
            .unwrap();
        assert!(*differenciated_weighted_relation.weight() == 2.0);

        assert_eq!(
            uniform_weighted_relation.relation(),
            differenciated_weighted_relation.relation()
        );

        let query_from_rel: &str =
            &ast::Query::from(&(uniform_weighted_relation.relation())).to_string();
        println!("\n{}", format!("{query_from_rel}").yellow());
        println!(
            "\n{}\n",
            database
                .query(query_from_rel)
                .unwrap()
                .iter()
                .map(ToString::to_string)
                .join("\n")
        );
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_map_with_weight() {
        let mut database = postgresql::test_database();
        let weight: f64 = 2.0;
        let relations = database.relations();
        let query = "SELECT * FROM item_table";
        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();

        let uniform_weighted_relation = relation.uniform_adjustment(weight);
        uniform_weighted_relation.relation().display_dot().unwrap();
        assert!(*uniform_weighted_relation.weight() == 2.0);

        let differenciated_weighted_relation = relation
            .differenciated_adjustment(&relations, vec![(vec!["item_table".to_string()], weight)]);
        differenciated_weighted_relation
            .relation()
            .display_dot()
            .unwrap();
        assert!(*differenciated_weighted_relation.weight() == 2.0);

        assert_eq!(
            uniform_weighted_relation.relation(),
            differenciated_weighted_relation.relation()
        );

        let query_from_rel: &str =
            &ast::Query::from(&(uniform_weighted_relation.relation())).to_string();
        println!("\n{}", format!("{query_from_rel}").yellow());
        println!(
            "\n{}\n",
            database
                .query(query_from_rel)
                .unwrap()
                .iter()
                .map(ToString::to_string)
                .join("\n")
        );
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_reduce_with_weight() {
        let mut database = postgresql::test_database();
        let weight: f64 = 2.0;
        let relations = database.relations();

        let query = "
        WITH tmp AS (
            SELECT
                id,
                AVG(income) AS avg_income
            FROM large_user_table
            GROUP BY id
        ) SELECT AVG(avg_income) FROM tmp";

        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();
        relation.display_dot().unwrap();
        let uniform_weighted_relation = relation.uniform_adjustment(weight);
        uniform_weighted_relation.relation().display_dot().unwrap();
        assert!(*uniform_weighted_relation.weight() == 1.0);
        let differenciated_weighted_relation = relation.differenciated_adjustment(
            &relations,
            vec![(vec!["large_user_table".to_string()], weight)],
        );
        differenciated_weighted_relation
            .relation()
            .display_dot()
            .unwrap();
        assert!(*differenciated_weighted_relation.weight() == 1.0);

        assert_eq!(
            uniform_weighted_relation.relation(),
            differenciated_weighted_relation.relation()
        );

        let query_from_rel: &str =
            &ast::Query::from(&(uniform_weighted_relation.relation())).to_string();
        println!("\n{}", format!("{query_from_rel}").yellow());
        println!(
            "\n{}\n",
            database
                .query(query_from_rel)
                .unwrap()
                .iter()
                .map(ToString::to_string)
                .join("\n")
        );
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_join_with_weight() {
        let mut database = postgresql::test_database();
        let weight: f64 = 2.0;
        let relations = database.relations();
        let relation = Relation::try_from(
            parse("SELECT * FROM order_table JOIN item_table ON id=order_id LIMIT 10")
                .unwrap()
                .with(&relations),
        )
        .unwrap();
        let uniform_weighted_relation = relation.uniform_adjustment(weight);
        uniform_weighted_relation.relation().display_dot().unwrap();
        assert!(*uniform_weighted_relation.weight() == 4.0);

        let differenciated_weighted_relation = relation.differenciated_adjustment(
            &relations,
            vec![
                (vec!["item_table".to_string()], weight),
                (vec!["order_table".to_string()], weight),
            ],
        );
        differenciated_weighted_relation
            .relation()
            .display_dot()
            .unwrap();
        assert!(*differenciated_weighted_relation.weight() == 4.0);

        assert_eq!(
            uniform_weighted_relation.relation(),
            differenciated_weighted_relation.relation()
        );

        let query_from_rel: &str =
            &ast::Query::from(&(uniform_weighted_relation.relation())).to_string();
        println!("\n{}", format!("{query_from_rel}").yellow());
        println!(
            "\n{}\n",
            database
                .query(query_from_rel)
                .unwrap()
                .iter()
                .map(ToString::to_string)
                .join("\n")
        );
    }

    // compute errors from results and from estimate
    fn errors(real: &Vec<f64>, estimate: &Vec<f64>) -> Vec<f64> {
        real.iter()
            .zip(estimate.iter())
            .map(|(source, sampled)| (source - sampled) / source)
            .collect()
    }

    // halper function that repeats the sampling multiple
    // times and it returns an average of the results.
    // it assumes that results are numeric single raw.
    fn collect_results_from_many_samples(
        relation_to_sample: &Relation,
        proba: f64,
        n_experiments: u32,
        use_poisson_sampling: bool,
    ) {
        let mut database = postgresql::test_database();

        print!("\nFROM WEIGHTED RELATION partial reslts:\n");
        let mut res_holder: Vec<Vec<f64>> = vec![];
        for _ in 0..n_experiments {
            let sampled_relation = if use_poisson_sampling {
                relation_to_sample.uniform_poisson_sampling(proba)
            } else {
                relation_to_sample.uniform_sampling_without_replacements(proba, 2.0)
            };
            let weighted_sampled_relation: RelationWithWeight =
                sampled_relation.uniform_adjustment(1.0 / proba);

            let weighted_filtered_sampled_relation =
                (weighted_sampled_relation.0).filter_fields(|n| n != ROW_WEIGHT);

            let query_weighted_relation: &str =
                &ast::Query::from(&weighted_filtered_sampled_relation).to_string();
            let res = database.query(query_weighted_relation).unwrap();
            assert!(res.len() == 1);
            let float_res: Vec<f64> = res[0]
                .iter()
                .filter_map(|f| f.to_string().parse::<f64>().ok())
                .collect();
            res_holder.extend([float_res].to_vec().into_iter());
        }
        let num_rows = res_holder.len();
        let num_cols = res_holder[0].len();

        let avg_res: Vec<f64> = (0..num_cols)
            .map(|col| {
                let sum: f64 = res_holder.iter().map(|row| row[col]).sum();
                sum / num_rows as f64
            })
            .collect();

        let query_unsampled_relation: &str = &ast::Query::from(relation_to_sample).to_string();
        let results_unsampled = database.query(query_unsampled_relation).unwrap();
        let source_res: Vec<f64> = results_unsampled[0]
            .iter()
            .filter_map(|f| f.to_string().parse::<f64>().ok())
            .collect();

        let errs: Vec<f64> = errors(&source_res, &avg_res);

        print!("\nFROM WEIGHTED RELATION:\n{:?}", avg_res);
        print!("\nFROM SOURCE RELATION:\n{:?}", source_res);
        print!("\nErrors:\n{:?}", errs);

        //for displaying purposes
        relation_to_sample.display_dot().unwrap();
        let sampled_relation = if use_poisson_sampling {
            relation_to_sample.uniform_poisson_sampling(proba)
        } else {
            // use a relatively safe rate_multiplier (2.0) for optimization purposes
            relation_to_sample.uniform_sampling_without_replacements(proba, 2.0)
        };

        let weighted_sampled_relation = sampled_relation.uniform_adjustment(1.0 / proba);
        weighted_sampled_relation.0.display_dot().unwrap();
        let query_weighted_relation: &str =
            &ast::Query::from(&(weighted_sampled_relation.0)).to_string();
        print!("Final weight: {}\n", weighted_sampled_relation.1);
        println!(
            "\nOriginal query \n{}",
            format!("{query_unsampled_relation}").yellow()
        );
        println!(
            "\nFinal readdressed query \n{}",
            format!("{query_weighted_relation}").yellow()
        );
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_adjustment_simple_reduce() {
        let mut database = postgresql::test_database();
        let relations: Hierarchy<Arc<Relation>> = database.relations();

        let query = "SELECT COUNT(order_id), SUM(price), AVG(price) FROM item_table";

        let weight: f64 = 5.0;
        let fraction: f64 = 1.0 / weight;
        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();

        collect_results_from_many_samples(&relation, fraction, 100, true)
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_adjustment_join_reduce() {
        let mut database = postgresql::test_database();
        let relations: Hierarchy<Arc<Relation>> = database.relations();

        let query = "SELECT COUNT(id), SUM(price), AVG(price) FROM order_table JOIN item_table ON id=order_id";

        let weight: f64 = 2.0;
        let fraction: f64 = 1.0 / weight;
        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();

        collect_results_from_many_samples(&relation, fraction, 100, true)
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_adjustment_reduce_reduce() {
        let mut database = postgresql::test_database();
        let relations: Hierarchy<Arc<Relation>> = database.relations();

        let query = "
        WITH tmp1 AS (
          SELECT
            id,
            AVG(income) AS avg_inc,
            SUM(income) AS sum_inc,
            COUNT(city) AS count_city
        FROM large_user_table GROUP BY id
        )
        SELECT
            COUNT(id),
            AVG(avg_inc) AS avg_avg_inc,
            SUM(avg_inc) AS sum_avg_inc,
            AVG(sum_inc) AS avg_sum_inc,
            SUM(sum_inc) AS sum_sum_inc,
            AVG(count_city) AS avg_count_city,
            SUM(count_city) AS sum_count_city
        FROM tmp1;";

        let weight: f64 = 6.0;
        let fraction: f64 = 1.0 / weight;
        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();

        collect_results_from_many_samples(&relation, fraction, 100, true)
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_adjustment_reduce_join_reduce() {
        let mut database = postgresql::test_database();
        let relations: Hierarchy<Arc<Relation>> = database.relations();

        // bug with USING (col)
        let query = "
        WITH
        tmp1 AS (select city, name, income from large_user_table),
        tmp2 AS (select city, SUM(age) AS sum_age from user_table GROUP BY city),
        tmp3 AS (SELECT name, income, sum_age FROM tmp1 JOIN tmp2 ON(tmp1.city=tmp2.city))
        SELECT COUNT(name), SUM(sum_age), AVG(income) FROM tmp3
        ";

        let weight = 4.0;
        let fraction: f64 = 1.0 / weight;
        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();
        collect_results_from_many_samples(&relation, fraction, 100, true)
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_adjustment_join_reduce_reduce() {
        let mut database = postgresql::test_database();
        let relations: Hierarchy<Arc<Relation>> = database.relations();

        // 2 reduce after the join
        let query = "
         WITH
         tmp1 AS (SELECT user_id FROM order_table),
         tmp2 AS (SELECT id, income, city FROM large_user_table),
         tmp3 AS (SELECT income, city FROM tmp1 JOIN tmp2 ON tmp1.user_id=tmp2.id),
         tmp4 AS (
            SELECT
                city,
                COUNT(income) AS count_income,
                SUM(income) AS sum_income,
                AVG(income) AS avg_income
            FROM tmp3
            GROUP BY city
        )
         SELECT COUNT(count_income), SUM(sum_income), AVG(avg_income), AVG(count_income), AVG(sum_income) FROM tmp4
         ";
        let weight = 4.0;
        let fraction: f64 = 1.0 / weight;
        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();
        collect_results_from_many_samples(&relation, fraction, 100, true)
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_adjustment_reduce_reduce_reduce() {
        let mut database = postgresql::test_database();
        let relations: Hierarchy<Arc<Relation>> = database.relations();

        let weight: f64 = 2.0;
        let fraction: f64 = 1.0 / weight;

        let query = "
        WITH tmp1 AS (
          SELECT id, city, AVG(income) AS avg_inc, SUM(income) AS sum_inc FROM large_user_table GROUP BY id, city
        ),
        tmp2 AS (
            SELECT
                id,
                COUNT(city) AS count_city,
                AVG(avg_inc) AS avg_avg_inc,
                SUM(avg_inc) AS sum_avg_inc,
                AVG(sum_inc) AS avg_sum_inc,
                SUM(sum_inc) AS sum_sum_inc
            FROM tmp1 GROUP BY id
          )
        SELECT
            COUNT(id) AS count_id,
            SUM(count_city) AS sum_count_city,
            AVG(count_city) AS avg_count_city,
            AVG(avg_avg_inc) AS avg_avg_avg_inc,
            AVG(sum_avg_inc) AS avg_sum_avg_inc,
            SUM(avg_sum_inc) AS sum_avg_sum_inc,
            SUM(sum_sum_inc) AS sum_sum_sum_inc
        FROM tmp2;";

        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();

        collect_results_from_many_samples(&relation, fraction, 100, true)
    }

    #[cfg(feature = "tested_sampling_adjustment")]
    #[test]
    fn test_adjustment_reduce_reduce_join_reduce() {
        let mut database = postgresql::test_database();
        let relations: Hierarchy<Arc<Relation>> = database.relations();

        let weight: f64 = 2.0;
        let fraction: f64 = 1.0 / weight;

        let query = "
        WITH
        tmp1 AS (
            SELECT
                id,
                COUNT(user_id) AS count_user_id,
                AVG(user_id) AS avg_user_id,
                SUM(user_id) AS sum_user_id
            FROM order_table
            GROUP BY id
        ),
        tmp2 AS (
            SELECT
                id,
                AVG(income) AS avg_income,
                SUM(income) AS sum_income
            FROM large_user_table
            GROUP BY id
        ),
        tmp3 AS (
            SELECT
                count_user_id,
                avg_user_id,
                avg_income,
                sum_user_id,
                sum_income
            FROM tmp1
            JOIN tmp2 ON (tmp1.id = tmp2.id)
        )
        SELECT
            COUNT(count_user_id),
            AVG(avg_user_id),
            AVG(avg_income),
            SUM(sum_user_id),
            AVG(sum_income),
            SUM(avg_income)
        FROM tmp3
        ";

        let relation = Relation::try_from(parse(query).unwrap().with(&relations)).unwrap();

        collect_results_from_many_samples(&relation, fraction, 100, true)
    }
}