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
//! Join functionality for OptimizedDataFrame
use std::collections::HashMap;
use super::core::OptimizedDataFrame;
use crate::column::{BooleanColumn, Column, Float64Column, Int64Column, StringColumn};
use crate::error::{Error, Result};
/// Enumeration representing join types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
/// Inner join (only rows that exist in both tables)
Inner,
/// Left join (all rows from the left table, and matching rows from the right table)
Left,
/// Right join (all rows from the right table, and matching rows from the left table)
Right,
/// Outer join (all rows from both tables)
Outer,
}
/// A typed join key.
///
/// Keys are compared as values instead of via their textual rendering. That
/// removes two `String` allocations per row (one per side) and gives the
/// expected comparison semantics:
///
/// * `NaN` never matches anything (such keys are simply not indexed),
/// * `-0.0` and `0.0` are the same key,
/// * numbers never collide with their string spelling.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum JoinKey<'a> {
Int(i64),
/// Bit pattern of a normalized, non-NaN float.
Float(u64),
Str(&'a str),
Bool(bool),
}
/// Build the join key of `row`, or `None` when the row has no usable key
/// (NULL, or a NaN float which must never match).
fn join_key_at(column: &Column, row: usize) -> Option<JoinKey<'_>> {
match column {
Column::Int64(col) => col.get(row).ok().flatten().map(JoinKey::Int),
Column::Float64(col) => col
.get(row)
.ok()
.flatten()
.filter(|value| !value.is_nan())
.map(|value| JoinKey::Float(normalized_float_bits(value))),
Column::String(col) => col.get(row).ok().flatten().map(JoinKey::Str),
Column::Boolean(col) => col.get(row).ok().flatten().map(JoinKey::Bool),
}
}
/// `-0.0 == 0.0` must also hash equal, so zero is normalized to `+0.0`.
fn normalized_float_bits(value: f64) -> u64 {
if value == 0.0 {
0.0_f64.to_bits()
} else {
value.to_bits()
}
}
/// Materialize `column` for the given row positions, where `None` means "this
/// output row has no counterpart on that side" and therefore yields NULL.
fn gather_optional(column: &Column, positions: &[Option<usize>]) -> Column {
match column {
Column::Int64(col) => {
let mut values = Vec::with_capacity(positions.len());
let mut nulls = Vec::with_capacity(positions.len());
for position in positions {
match position.and_then(|row| col.get(row).ok().flatten()) {
Some(value) => {
values.push(value);
nulls.push(false);
}
None => {
values.push(0);
nulls.push(true);
}
}
}
Column::Int64(Int64Column::with_nulls(values, nulls))
}
Column::Float64(col) => {
let mut values = Vec::with_capacity(positions.len());
let mut nulls = Vec::with_capacity(positions.len());
for position in positions {
match position.and_then(|row| col.get(row).ok().flatten()) {
Some(value) => {
values.push(value);
nulls.push(false);
}
None => {
values.push(0.0);
nulls.push(true);
}
}
}
Column::Float64(Float64Column::with_nulls(values, nulls))
}
Column::String(col) => {
let mut values = Vec::with_capacity(positions.len());
let mut nulls = Vec::with_capacity(positions.len());
for position in positions {
match position.and_then(|row| col.get(row).ok().flatten()) {
Some(value) => {
values.push(value.to_string());
nulls.push(false);
}
None => {
values.push(String::new());
nulls.push(true);
}
}
}
Column::String(StringColumn::with_nulls(values, nulls))
}
Column::Boolean(col) => {
let mut values = Vec::with_capacity(positions.len());
let mut nulls = Vec::with_capacity(positions.len());
for position in positions {
match position.and_then(|row| col.get(row).ok().flatten()) {
Some(value) => {
values.push(value);
nulls.push(false);
}
None => {
values.push(false);
nulls.push(true);
}
}
}
Column::Boolean(BooleanColumn::with_nulls(values, nulls))
}
}
}
/// Build the join key column of the result: the left key value when the output
/// row has a left counterpart, otherwise the key value of the right row.
/// A NULL key stays NULL.
fn gather_key_column(
left_col: &Column,
right_col: &Column,
pairs: &[(Option<usize>, Option<usize>)],
left_on: &str,
right_on: &str,
) -> Result<Column> {
match (left_col, right_col) {
(Column::Int64(left), Column::Int64(right)) => {
let mut values = Vec::with_capacity(pairs.len());
let mut nulls = Vec::with_capacity(pairs.len());
for &(left_idx, right_idx) in pairs {
let value = match (left_idx, right_idx) {
(Some(row), _) => left.get(row).ok().flatten(),
(None, Some(row)) => right.get(row).ok().flatten(),
(None, None) => None,
};
match value {
Some(value) => {
values.push(value);
nulls.push(false);
}
None => {
values.push(0);
nulls.push(true);
}
}
}
Ok(Column::Int64(Int64Column::with_nulls(values, nulls)))
}
(Column::Float64(left), Column::Float64(right)) => {
let mut values = Vec::with_capacity(pairs.len());
let mut nulls = Vec::with_capacity(pairs.len());
for &(left_idx, right_idx) in pairs {
let value = match (left_idx, right_idx) {
(Some(row), _) => left.get(row).ok().flatten(),
(None, Some(row)) => right.get(row).ok().flatten(),
(None, None) => None,
};
match value {
Some(value) => {
values.push(value);
nulls.push(false);
}
None => {
values.push(0.0);
nulls.push(true);
}
}
}
Ok(Column::Float64(Float64Column::with_nulls(values, nulls)))
}
(Column::String(left), Column::String(right)) => {
let mut values = Vec::with_capacity(pairs.len());
let mut nulls = Vec::with_capacity(pairs.len());
for &(left_idx, right_idx) in pairs {
let value = match (left_idx, right_idx) {
(Some(row), _) => left.get(row).ok().flatten().map(|v| v.to_string()),
(None, Some(row)) => right.get(row).ok().flatten().map(|v| v.to_string()),
(None, None) => None,
};
match value {
Some(value) => {
values.push(value);
nulls.push(false);
}
None => {
values.push(String::new());
nulls.push(true);
}
}
}
Ok(Column::String(StringColumn::with_nulls(values, nulls)))
}
(Column::Boolean(left), Column::Boolean(right)) => {
let mut values = Vec::with_capacity(pairs.len());
let mut nulls = Vec::with_capacity(pairs.len());
for &(left_idx, right_idx) in pairs {
let value = match (left_idx, right_idx) {
(Some(row), _) => left.get(row).ok().flatten(),
(None, Some(row)) => right.get(row).ok().flatten(),
(None, None) => None,
};
match value {
Some(value) => {
values.push(value);
nulls.push(false);
}
None => {
values.push(false);
nulls.push(true);
}
}
}
Ok(Column::Boolean(BooleanColumn::with_nulls(values, nulls)))
}
(left, right) => Err(Error::ColumnTypeMismatch {
name: format!("{} and {}", left_on, right_on),
expected: left.column_type(),
found: right.column_type(),
}),
}
}
impl OptimizedDataFrame {
/// Inner join
///
/// # Arguments
/// * `other` - Right DataFrame to join with
/// * `left_on` - Join key column from the left DataFrame
/// * `right_on` - Join key column from the right DataFrame
///
/// # Returns
/// * `Result<Self>` - Result DataFrame after join operation
pub fn inner_join(&self, other: &Self, left_on: &str, right_on: &str) -> Result<Self> {
self.join_impl(other, left_on, right_on, JoinType::Inner)
}
/// Left join
///
/// # Arguments
/// * `other` - Right DataFrame to join with
/// * `left_on` - Join key column from the left DataFrame
/// * `right_on` - Join key column from the right DataFrame
///
/// # Returns
/// * `Result<Self>` - Result DataFrame after join operation
pub fn left_join(&self, other: &Self, left_on: &str, right_on: &str) -> Result<Self> {
self.join_impl(other, left_on, right_on, JoinType::Left)
}
/// Right join
///
/// # Arguments
/// * `other` - Right DataFrame to join with
/// * `left_on` - Join key column from the left DataFrame
/// * `right_on` - Join key column from the right DataFrame
///
/// # Returns
/// * `Result<Self>` - Result DataFrame after join operation
pub fn right_join(&self, other: &Self, left_on: &str, right_on: &str) -> Result<Self> {
self.join_impl(other, left_on, right_on, JoinType::Right)
}
/// Outer join
///
/// # Arguments
/// * `other` - Right DataFrame to join with
/// * `left_on` - Join key column from the left DataFrame
/// * `right_on` - Join key column from the right DataFrame
///
/// # Returns
/// * `Result<Self>` - Result DataFrame after join operation
pub fn outer_join(&self, other: &Self, left_on: &str, right_on: &str) -> Result<Self> {
self.join_impl(other, left_on, right_on, JoinType::Outer)
}
/// Join implementation (internal method)
///
/// Semantics:
/// * NULL keys (and NaN float keys) never match anything, but the rows are
/// still kept by the join types that preserve unmatched rows - a left join
/// returns every left row, even the ones whose key is missing.
/// * Columns of the side a result row does not come from are filled with
/// NULL, never with `0`/`""`/`false`.
/// * The result schema does not depend on the number of matches: an empty
/// result has exactly the same columns as a non-empty one.
/// * Row order is left-major, and within one left row the matches follow the
/// right frame order; unmatched right rows are appended at the end.
fn join_impl(
&self,
other: &Self,
left_on: &str,
right_on: &str,
join_type: JoinType,
) -> Result<Self> {
// Get join key columns
let left_col_idx = self
.column_indices
.get(left_on)
.ok_or_else(|| Error::ColumnNotFound(left_on.to_string()))?;
let right_col_idx = other
.column_indices
.get(right_on)
.ok_or_else(|| Error::ColumnNotFound(right_on.to_string()))?;
let left_col = &self.columns[*left_col_idx];
let right_col = &other.columns[*right_col_idx];
// Verify that both columns have the same type
if left_col.column_type() != right_col.column_type() {
return Err(Error::ColumnTypeMismatch {
name: format!("{} and {}", left_on, right_on),
expected: left_col.column_type(),
found: right_col.column_type(),
});
}
let keep_unmatched_left = join_type == JoinType::Left || join_type == JoinType::Outer;
let keep_unmatched_right = join_type == JoinType::Right || join_type == JoinType::Outer;
let mut join_indices: Vec<(Option<usize>, Option<usize>)> = Vec::new();
if other.row_count <= self.row_count {
// Build the hash table on the smaller (right) side and probe with
// the left side, which yields left-major order directly.
let mut right_key_to_indices: HashMap<JoinKey, Vec<usize>> =
HashMap::with_capacity(other.row_count);
for row in 0..other.row_count {
if let Some(key) = join_key_at(right_col, row) {
right_key_to_indices.entry(key).or_default().push(row);
}
}
for row in 0..self.row_count {
let matches = match join_key_at(left_col, row) {
Some(key) => right_key_to_indices.get(&key),
None => None,
};
match matches {
Some(right_rows) => {
for &right_row in right_rows {
join_indices.push((Some(row), Some(right_row)));
}
}
None => {
if keep_unmatched_left {
join_indices.push((Some(row), None));
}
}
}
}
} else {
// The left side is smaller: build on the left, probe with the right
// and restore the left-major ordering afterwards.
let mut left_key_to_indices: HashMap<JoinKey, Vec<usize>> =
HashMap::with_capacity(self.row_count);
for row in 0..self.row_count {
if let Some(key) = join_key_at(left_col, row) {
left_key_to_indices.entry(key).or_default().push(row);
}
}
let mut left_matched = vec![false; self.row_count];
for right_row in 0..other.row_count {
let matches = match join_key_at(right_col, right_row) {
Some(key) => left_key_to_indices.get(&key),
None => None,
};
if let Some(left_rows) = matches {
for &left_row in left_rows {
left_matched[left_row] = true;
join_indices.push((Some(left_row), Some(right_row)));
}
}
}
if keep_unmatched_left {
for (left_row, matched) in left_matched.iter().enumerate() {
if !matched {
join_indices.push((Some(left_row), None));
}
}
}
join_indices.sort_by_key(|&(left, right)| {
(left.unwrap_or(usize::MAX), right.unwrap_or(usize::MAX))
});
}
// For right or outer join, add unmatched rows from the right side
if keep_unmatched_right {
let mut right_matched = vec![false; other.row_count];
for (_, right_idx) in &join_indices {
if let Some(idx) = right_idx {
right_matched[*idx] = true;
}
}
for (row, matched) in right_matched.iter().enumerate() {
if !matched {
join_indices.push((None, Some(row)));
}
}
}
// Materialize the result. The very same code path also produces the
// (correctly typed, correctly named) empty frame when nothing matched.
let left_positions: Vec<Option<usize>> =
join_indices.iter().map(|&(left, _)| left).collect();
let right_positions: Vec<Option<usize>> =
join_indices.iter().map(|&(_, right)| right).collect();
let mut result = Self::new();
// Add columns from the left side (excluding the join key)
for name in &self.column_names {
if name != left_on {
let col_idx = self.column_indices[name];
let joined_col = gather_optional(&self.columns[col_idx], &left_positions);
result.add_column(name.clone(), joined_col)?;
}
}
// Add the join key column once, taken from whichever side has the row
let joined_key_col =
gather_key_column(left_col, right_col, &join_indices, left_on, right_on)?;
result.add_column(left_on.to_string(), joined_key_col)?;
// Add columns from the right side (excluding the join key)
for name in &other.column_names {
if name != right_on {
let new_name = if result.column_indices.contains_key(name) {
format!("{}_right", name)
} else {
name.clone()
};
let col_idx = other.column_indices[name];
let joined_col = gather_optional(&other.columns[col_idx], &right_positions);
result.add_column(new_name, joined_col)?;
}
}
Ok(result)
}
}