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
use crate::VeloxxError;
use crate::{dataframe::DataFrame, series::Series, types::Value};
use indexmap::IndexMap;
use rayon::iter::IntoParallelIterator;
use rayon::prelude::*;
#[derive(PartialEq)]
/// Defines the type of join to be performed between two DataFrames.
pub enum JoinType {
/// Returns only the rows that have matching values in both DataFrames.
Inner,
/// Returns all rows from the left DataFrame, and the matching rows from the right DataFrame.
Left,
/// Returns all rows from the right DataFrame, and the matching rows from the left DataFrame.
Right,
/// Returns all rows when there is a match in either left or right DataFrame.
Outer,
}
impl DataFrame {
/// Performs a join operation with another `DataFrame`.
///
/// This method combines two DataFrames based on a common column (`on_column`) and a specified
/// `JoinType`. It creates a new DataFrame containing columns from both original DataFrames.
///
/// # Arguments
///
/// * `other` - The other `DataFrame` to join with.
/// * `on_column` - The name of the column to join on. This column must exist in both DataFrames
/// and have comparable data types.
/// * `join_type` - The type of join to perform (`Inner`, `Left`, `Right`, or `Outer`).
///
/// # Returns
///
/// A `Result` which is `Ok(DataFrame)` containing the joined `DataFrame`,
/// or `Err(VeloxxError::ColumnNotFound)` if the `on_column` is not found in either DataFrame,
/// or `Err(VeloxxError::InvalidOperation)` if there are issues during the join process (e.g., incompatible types).
///
/// # Examples
///
/// ## Setup for Examples
///
/// ```rust
/// use veloxx::dataframe::DataFrame;
/// use veloxx::series::Series;
/// use indexmap::IndexMap;
/// use veloxx::types::Value;
///
/// // Left DataFrame
/// let mut left_cols = IndexMap::new();
/// left_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(1), Some(2), Some(3)]));
/// left_cols.insert("name".to_string(), Series::new_string("name", vec![Some("Alice".to_string()), Some("Bob".to_string()), Some("Charlie".to_string())]));
/// let left_df = DataFrame::new(left_cols).unwrap();
///
/// // Right DataFrame
/// let mut right_cols = IndexMap::new();
/// right_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(2), Some(3), Some(4)]));
/// right_cols.insert("city".to_string(), Series::new_string("city", vec![Some("London".to_string()), Some("Paris".to_string()), Some("Rome".to_string())]));
/// let right_df = DataFrame::new(right_cols).unwrap();
/// ```
///
/// ## Inner Join
///
/// Combines rows where `id` matches in both DataFrames.
///
/// ```rust
/// # use veloxx::dataframe::DataFrame;
/// # use veloxx::series::Series;
/// # use indexmap::IndexMap;
/// # use veloxx::types::Value;
/// # use veloxx::dataframe::join::JoinType;
/// # let mut left_cols = IndexMap::new();
/// # left_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(1), Some(2), Some(3)]));
/// # left_cols.insert("name".to_string(), Series::new_string("name", vec![Some("Alice".to_string()), Some("Bob".to_string()), Some("Charlie".to_string())]));
/// # let left_df = DataFrame::new(left_cols).unwrap();
/// # let mut right_cols = IndexMap::new();
/// # right_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(2), Some(3), Some(4)]));
/// # right_cols.insert("city".to_string(), Series::new_string("city", vec![Some("London".to_string()), Some("Paris".to_string()), Some("Rome".to_string())]));
/// # let right_df = DataFrame::new(right_cols).unwrap();
///
/// let inner_joined_df = left_df.join(&right_df, "id", JoinType::Inner).unwrap();
/// // Expected rows: (id=2, name=Bob, city=London), (id=3, name=Charlie, city=Paris)
/// assert_eq!(inner_joined_df.row_count(), 2);
/// assert!(inner_joined_df.get_column("name").unwrap().get_value(0) == Some(Value::String("Bob".to_string())) || inner_joined_df.get_column("name").unwrap().get_value(0) == Some(Value::String("Charlie".to_string())));
/// ```
///
/// ## Left Join
///
/// Returns all rows from `left_df`, and matching rows from `right_df`. Unmatched `right_df` columns will be null.
///
/// ```rust
/// # use veloxx::dataframe::DataFrame;
/// # use veloxx::series::Series;
/// # use indexmap::IndexMap;
/// # use veloxx::types::Value;
/// # use veloxx::dataframe::join::JoinType;
/// # let mut left_cols = IndexMap::new();
/// # left_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(1), Some(2), Some(3)]));
/// # left_cols.insert("name".to_string(), Series::new_string("name", vec![Some("Alice".to_string()), Some("Bob".to_string()), Some("Charlie".to_string())]));
/// # let left_df = DataFrame::new(left_cols).unwrap();
/// # let mut right_cols = IndexMap::new();
/// # right_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(2), Some(3), Some(4)]));
/// # right_cols.insert("city".to_string(), Series::new_string("city", vec![Some("London".to_string()), Some("Paris".to_string()), Some("Rome".to_string())]));
/// # let right_df = DataFrame::new(right_cols).unwrap();
///
/// let left_joined_df = left_df.join(&right_df, "id", JoinType::Left).unwrap();
/// // Expected rows: (id=1, name=Alice, city=null), (id=2, name=Bob, city=London), (id=3, name=Charlie, city=Paris)
/// assert_eq!(left_joined_df.row_count(), 3);
/// assert_eq!(left_joined_df.get_column("city").unwrap().get_value(0), None);
/// ```
///
/// ## Right Join
///
/// Returns all rows from `right_df`, and matching rows from `left_df`. Unmatched `left_df` columns will be null.
///
/// ```rust
/// # use veloxx::dataframe::DataFrame;
/// # use veloxx::series::Series;
/// # use indexmap::IndexMap;
/// # use veloxx::types::Value;
/// # use veloxx::dataframe::join::JoinType;
/// # let mut left_cols = IndexMap::new();
/// # left_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(1), Some(2), Some(3)]));
/// # left_cols.insert("name".to_string(), Series::new_string("name", vec![Some("Alice".to_string()), Some("Bob".to_string()), Some("Charlie".to_string())]));
/// # let left_df = DataFrame::new(left_cols).unwrap();
/// # let mut right_cols = IndexMap::new();
/// # right_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(2), Some(3), Some(4)]));
/// # right_cols.insert("city".to_string(), Series::new_string("city", vec![Some("London".to_string()), Some("Paris".to_string()), Some("Rome".to_string())]));
/// # let right_df = DataFrame::new(right_cols).unwrap();
///
/// let right_joined_df = left_df.join(&right_df, "id", JoinType::Right).unwrap();
/// // Expected rows: (id=2, name=Bob, city=London), (id=3, name=Charlie, city=Paris), (id=4, name=null, city=Rome)
/// assert_eq!(right_joined_df.row_count(), 3);
/// assert_eq!(right_joined_df.get_column("name").unwrap().get_value(2), None);
/// ```
///
/// ## Outer Join
///
/// Returns all rows from both DataFrames.
///
/// ```rust
/// # use veloxx::dataframe::DataFrame;
/// # use veloxx::series::Series;
/// # use indexmap::IndexMap;
/// # use veloxx::types::Value;
/// # use veloxx::dataframe::join::JoinType;
/// # let mut left_cols = IndexMap::new();
/// # left_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(1), Some(2), Some(3)]));
/// # left_cols.insert("name".to_string(), Series::new_string("name", vec![Some("Alice".to_string()), Some("Bob".to_string()), Some("Charlie".to_string())]));
/// # let left_df = DataFrame::new(left_cols).unwrap();
/// # let mut right_cols = IndexMap::new();
/// # right_cols.insert("id".to_string(), Series::new_i32("id", vec![Some(2), Some(3), Some(4)]));
/// # right_cols.insert("city".to_string(), Series::new_string("city", vec![Some("London".to_string()), Some("Paris".to_string()), Some("Rome".to_string())]));
/// # let right_df = DataFrame::new(right_cols).unwrap();
///
/// let outer_joined_df = left_df.join(&right_df, "id", JoinType::Outer).unwrap();
/// // Expected rows: 1, 2, 3, 4
/// assert_eq!(outer_joined_df.row_count(), 4);
/// ```
#[allow(clippy::type_complexity)]
pub fn join(
&self,
other: &DataFrame,
on_column: &str,
join_type: JoinType,
) -> Result<Self, VeloxxError> {
let mut new_columns: IndexMap<String, Series> = IndexMap::new();
let self_col_names: Vec<String> =
self.column_names().iter().map(|s| (*s).clone()).collect();
let other_col_names: Vec<String> =
other.column_names().iter().map(|s| (*s).clone()).collect();
// Check if join column exists in both DataFrames
if !self_col_names.contains(&on_column.to_string()) {
return Err(VeloxxError::ColumnNotFound(format!(
"Join column '{on_column}' not found in left DataFrame."
)));
}
if !other_col_names.contains(&on_column.to_string()) {
return Err(VeloxxError::ColumnNotFound(format!(
"Join column '{on_column}' not found in right DataFrame."
)));
}
// Determine all unique column names and their types
let all_column_names: Vec<String> = {
let mut temp_names = Vec::new();
for col_name in self_col_names.iter() {
temp_names.push(col_name.clone());
}
for col_name in other_col_names.iter() {
if !temp_names.contains(col_name) {
temp_names.push(col_name.clone());
}
}
temp_names
};
let mut column_types: IndexMap<String, crate::types::DataType> = IndexMap::new();
for col_name in self_col_names.iter() {
column_types.insert(
col_name.clone(),
self.get_column(col_name).unwrap().data_type(),
);
}
for col_name in other_col_names.iter() {
if !column_types.contains_key(col_name) {
column_types.insert(
col_name.clone(),
other.get_column(col_name).unwrap().data_type(),
);
}
}
// Initialize new Series data vectors
let mut series_data: indexmap::IndexMap<String, Vec<Option<Value>>> =
indexmap::IndexMap::new();
for col_name in all_column_names.iter() {
series_data.insert(col_name.clone(), Vec::new());
}
match join_type {
JoinType::Inner => {
let other_on_series = other.get_column(on_column).unwrap();
let other_join_map: indexmap::IndexMap<Value, Vec<usize>> = (0..other.row_count())
.into_par_iter()
.filter_map(|i| other_on_series.get_value(i).map(|val| (val, i)))
.fold(
indexmap::IndexMap::new,
|mut map: indexmap::IndexMap<Value, Vec<usize>>, (val, i)| {
map.entry(val).or_default().push(i);
map
},
)
.reduce(indexmap::IndexMap::new, |mut acc, map| {
for (key, value) in map {
acc.entry(key).or_default().extend(value);
}
acc
});
let self_on_series = self.get_column(on_column).unwrap();
let results: Vec<Vec<(String, Option<Value>)>> = (0..self.row_count())
.into_par_iter()
.filter_map(|i| {
if let Some(self_join_val) = self_on_series.get_value(i) {
if let Some(other_indices) = other_join_map.get(&self_join_val) {
let self_col_names_cloned = self_col_names.clone();
let all_column_names_cloned = all_column_names.clone();
Some(
other_indices
.par_iter()
.flat_map(move |&other_idx| {
let mut row_values = Vec::new();
for col_name in all_column_names_cloned.iter() {
let value = if self_col_names_cloned
.contains(col_name)
{
self.get_column(col_name).unwrap().get_value(i)
} else {
other
.get_column(col_name)
.unwrap()
.get_value(other_idx)
};
row_values.push((col_name.clone(), value));
}
vec![row_values]
})
.collect::<Vec<_>>(),
)
} else {
None
}
} else {
None
}
})
.flatten()
.collect();
for row_values in results {
for (col_name, value) in row_values {
series_data.get_mut(&col_name).unwrap().push(value);
}
}
}
JoinType::Left => {
let other_on_series = other.get_column(on_column).unwrap();
let other_join_map: indexmap::IndexMap<Value, Vec<usize>> = (0..other.row_count())
.into_par_iter()
.filter_map(|i| other_on_series.get_value(i).map(|val| (val, i)))
.fold(
indexmap::IndexMap::new,
|mut map: indexmap::IndexMap<Value, Vec<usize>>, (val, i)| {
map.entry(val).or_default().push(i);
map
},
)
.reduce(indexmap::IndexMap::new, |mut acc, map| {
for (key, value) in map {
acc.entry(key).or_default().extend(value);
}
acc
});
let self_on_series = self.get_column(on_column).unwrap();
let collected_rows: Vec<Vec<(String, Option<Value>)>> = (0..self.row_count())
.into_par_iter()
.flat_map(|i| {
if let Some(self_join_val) = self_on_series.get_value(i) {
if let Some(other_indices) = other_join_map.get(&self_join_val) {
let self_col_names_cloned = self_col_names.clone();
let all_column_names_cloned = all_column_names.clone();
let _other_col_names_cloned = other_col_names.clone();
other_indices
.par_iter()
.map(move |&other_idx| {
let mut row_values = Vec::new();
for col_name in all_column_names_cloned.iter() {
let value = if self_col_names_cloned.contains(col_name)
{
self.get_column(col_name).unwrap().get_value(i)
} else {
other
.get_column(col_name)
.unwrap()
.get_value(other_idx)
};
row_values.push((col_name.clone(), value));
}
row_values
})
.collect::<Vec<_>>()
} else {
let all_column_names_cloned = all_column_names.clone();
let self_col_names_cloned = self_col_names.clone();
let mut row_values = Vec::new();
for col_name in all_column_names_cloned.iter() {
let value = if self_col_names_cloned.contains(col_name) {
self.get_column(col_name).unwrap().get_value(i)
} else {
None
};
row_values.push((col_name.clone(), value));
}
vec![row_values]
}
} else {
let all_column_names_cloned = all_column_names.clone();
let self_col_names_cloned = self_col_names.clone();
let mut row_values = Vec::new();
for col_name in all_column_names_cloned.iter() {
let value = if self_col_names_cloned.contains(col_name) {
self.get_column(col_name).unwrap().get_value(i)
} else {
None
};
row_values.push((col_name.clone(), value));
}
vec![row_values]
}
})
.collect();
for row_values in collected_rows {
for (col_name, value) in row_values {
series_data.get_mut(&col_name).unwrap().push(value);
}
}
}
JoinType::Right => {
let self_on_series = self.get_column(on_column).unwrap();
let self_join_map: indexmap::IndexMap<Value, Vec<usize>> = (0..self.row_count())
.into_par_iter()
.filter_map(|i| self_on_series.get_value(i).map(|val| (val, i)))
.fold(
indexmap::IndexMap::new,
|mut map: indexmap::IndexMap<Value, Vec<usize>>, (val, i)| {
map.entry(val).or_default().push(i);
map
},
)
.reduce(indexmap::IndexMap::new, |mut acc, map| {
for (key, value) in map {
acc.entry(key).or_default().extend(value);
}
acc
});
let other_on_series = other.get_column(on_column).unwrap();
let collected_rows: Vec<Vec<(String, Option<Value>)>> = (0..other.row_count())
.into_par_iter()
.flat_map(|i| {
if let Some(other_join_val) = other_on_series.get_value(i) {
if let Some(self_indices) = self_join_map.get(&other_join_val) {
let other_col_names_cloned = other_col_names.clone();
let all_column_names_cloned = all_column_names.clone();
let _self_col_names_cloned = self_col_names.clone();
self_indices
.par_iter()
.map(move |&self_idx| {
let mut row_values = Vec::new();
for col_name in all_column_names_cloned.iter() {
let value = if other_col_names_cloned.contains(col_name)
{
other.get_column(col_name).unwrap().get_value(i)
} else {
self.get_column(col_name)
.unwrap()
.get_value(self_idx)
};
row_values.push((col_name.clone(), value));
}
row_values
})
.collect::<Vec<_>>()
} else {
let all_column_names_cloned = all_column_names.clone();
let other_col_names_cloned = other_col_names.clone();
let mut row_values = Vec::new();
for col_name in all_column_names_cloned.iter() {
let value = if other_col_names_cloned.contains(col_name) {
other.get_column(col_name).unwrap().get_value(i)
} else {
None
};
row_values.push((col_name.clone(), value));
}
vec![row_values]
}
} else {
let all_column_names_cloned = all_column_names.clone();
let other_col_names_cloned = other_col_names.clone();
let mut row_values = Vec::new();
for col_name in all_column_names_cloned.iter() {
let value = if other_col_names_cloned.contains(col_name) {
other.get_column(col_name).unwrap().get_value(i)
} else {
None
};
row_values.push((col_name.clone(), value));
}
vec![row_values]
}
})
.collect();
for row_values in collected_rows {
for (col_name, value) in row_values {
series_data.get_mut(&col_name).unwrap().push(value);
}
}
}
JoinType::Outer => {
// Full Outer Join implementation
// Strategy:
// 1. Perform Left Outer Join logic
// 2. Track which keys from Right DataFrame were matched
// 3. Append rows from Right DataFrame that were NOT matched
let other_on_series = other.get_column(on_column).unwrap();
// Build map of Right DataFrame keys -> indices
let other_join_map: indexmap::IndexMap<Value, Vec<usize>> = (0..other.row_count())
.into_par_iter()
.filter_map(|i| other_on_series.get_value(i).map(|val| (val, i)))
.fold(
indexmap::IndexMap::new,
|mut map: indexmap::IndexMap<Value, Vec<usize>>, (val, i)| {
map.entry(val).or_default().push(i);
map
},
)
.reduce(indexmap::IndexMap::new, |mut acc, map| {
for (key, value) in map {
acc.entry(key).or_default().extend(value);
}
acc
});
// Keep track of matched right indices to handle the "Right Anti" part later
// Using a thread-safe structure or collecting matched indices
// Since we are doing parallel processing, collecting all matched indices might be expensive?
// Alternative: Use a DashSet or concurrent bitset?
// Or just collect locally and merge.
// We'll process Left Join part and collect matched right indices
let self_on_series = self.get_column(on_column).unwrap();
// Process Left side (Left Outer Join)
let collected_results: Vec<(Vec<Vec<(String, Option<Value>)>>, Vec<usize>)> = (0
..self.row_count())
.into_par_iter()
.map(|i| {
let mut matched_indices = Vec::new();
let mut rows = Vec::new();
if let Some(self_join_val) = self_on_series.get_value(i) {
if let Some(other_indices) = other_join_map.get(&self_join_val) {
matched_indices.extend(other_indices.iter().cloned());
for &other_idx in other_indices {
let mut row_values = Vec::new();
for col_name in &all_column_names {
let value = if self_col_names.contains(col_name) {
self.get_column(col_name).unwrap().get_value(i)
} else {
other.get_column(col_name).unwrap().get_value(other_idx)
};
row_values.push((col_name.clone(), value));
}
rows.push(row_values);
}
} else {
// No match in right, emit Left row with nulls
let mut row_values = Vec::new();
for col_name in &all_column_names {
let value = if self_col_names.contains(col_name) {
self.get_column(col_name).unwrap().get_value(i)
} else {
None
};
row_values.push((col_name.clone(), value));
}
rows.push(row_values);
}
} else {
// Null key in left, emit Left row with nulls (assuming null != null for join usually, standard SQL behavior)
// If we want null=null matching, we'd handle it above. Here we assume strict equality.
let mut row_values = Vec::new();
for col_name in &all_column_names {
let value = if self_col_names.contains(col_name) {
self.get_column(col_name).unwrap().get_value(i)
} else {
None
};
row_values.push((col_name.clone(), value));
}
rows.push(row_values);
}
(rows, matched_indices)
})
.collect::<Vec<_>>();
// Sequential post-processing
let mut collected_rows = Vec::new();
let mut matched_right_set = std::collections::HashSet::new();
for (rows, indices) in collected_results {
collected_rows.extend(rows);
matched_right_set.extend(indices);
}
// Append Right rows that were not matched
for i in 0..other.row_count() {
if !matched_right_set.contains(&i) {
let mut row_values = Vec::new();
for col_name in &all_column_names {
let value = if other_col_names.contains(col_name) {
other.get_column(col_name).unwrap().get_value(i)
} else {
None // Not in Right (so it's a Left column), set to Null
};
row_values.push((col_name.clone(), value));
}
// Add to collected_rows? No, we can directly push to series_data or append to collected_rows
// Pushing to series_data is non-trivial because collected_rows is waiting to be pushed.
// Better to push everything to series_data at once or append here.
// collected_rows is consumed below.
// We can't append to collected_rows easily because it's Vec of Vec.
// Actually we can.
// But wait, `collected_rows` is immutable from the unzip. We need to make it mutable or chain.
// Let's just process this loop into series_data directly?
// No, consistent processing is better.
// Let's add these to a separate vector and process both.
}
}
// Actually, let's gather right-only rows separately
let right_only_rows: Vec<Vec<(String, Option<Value>)>> = (0..other.row_count())
.into_par_iter()
.filter(|i| !matched_right_set.contains(i))
.map(|i| {
let mut row_values = Vec::new();
for col_name in &all_column_names {
let value = if other_col_names.contains(col_name) {
other.get_column(col_name).unwrap().get_value(i)
} else {
None
};
row_values.push((col_name.clone(), value));
}
row_values
})
.collect();
// Populate series_data
for row_values in collected_rows {
for (col_name, value) in row_values {
series_data.get_mut(&col_name).unwrap().push(value);
}
}
for row_values in right_only_rows {
for (col_name, value) in row_values {
series_data.get_mut(&col_name).unwrap().push(value);
}
}
}
}
// Create new Series objects
for (col_name, data_vec) in series_data {
let col_data_type = column_types.get(&col_name).unwrap();
let new_series = match col_data_type {
crate::types::DataType::I32 => Series::new_i32(
&col_name,
data_vec
.into_iter()
.map(|x| {
x.and_then(|v| {
if let Value::I32(val) = v {
Some(val)
} else {
None
}
})
})
.collect(),
),
crate::types::DataType::F64 => Series::new_f64(
&col_name,
data_vec
.into_iter()
.map(|x| {
x.and_then(|v| {
if let Value::F64(val) = v {
Some(val)
} else {
None
}
})
})
.collect(),
),
crate::types::DataType::Bool => Series::new_bool(
&col_name,
data_vec
.into_iter()
.map(|x| {
x.and_then(|v| {
if let Value::Bool(val) = v {
Some(val)
} else {
None
}
})
})
.collect(),
),
crate::types::DataType::String => Series::new_string(
&col_name,
data_vec
.into_iter()
.map(|x| {
x.and_then(|v| {
if let Value::String(val) = v {
Some(val)
} else {
None
}
})
})
.collect(),
),
crate::types::DataType::DateTime => Series::new_datetime(
&col_name,
data_vec
.into_iter()
.map(|x| {
x.and_then(|v| {
if let Value::DateTime(val) = v {
Some(val)
} else {
None
}
})
})
.collect(),
),
};
new_columns.insert(col_name, new_series);
}
Ok(DataFrame::new(new_columns))
}
}