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
use polars_utils::flatten;
use super::*;
#[cfg(feature = "chunked_ids")]
unsafe fn apply_mapping(idx: Vec<IdxSize>, chunk_mapping: &[ChunkId]) -> Vec<ChunkId> {
idx.iter()
.map(|idx| *chunk_mapping.get_unchecked(*idx as usize))
.collect()
}
#[cfg(feature = "chunked_ids")]
unsafe fn apply_opt_mapping(
idx: Vec<Option<IdxSize>>,
chunk_mapping: &[ChunkId],
) -> Vec<Option<ChunkId>> {
idx.iter()
.map(|opt_idx| opt_idx.map(|idx| *chunk_mapping.get_unchecked(idx as usize)))
.collect()
}
#[cfg(feature = "chunked_ids")]
pub(super) fn finish_left_join_mappings(
result_idx_left: Vec<IdxSize>,
result_idx_right: Vec<Option<IdxSize>>,
chunk_mapping_left: Option<&[ChunkId]>,
chunk_mapping_right: Option<&[ChunkId]>,
) -> LeftJoinIds {
let left = match chunk_mapping_left {
None => JoinIds::Left(result_idx_left),
Some(mapping) => JoinIds::Right(unsafe { apply_mapping(result_idx_left, mapping) }),
};
let right = match chunk_mapping_right {
None => JoinOptIds::Left(result_idx_right),
Some(mapping) => JoinOptIds::Right(unsafe { apply_opt_mapping(result_idx_right, mapping) }),
};
(left, right)
}
#[cfg(not(feature = "chunked_ids"))]
pub(super) fn finish_left_join_mappings(
_result_idx_left: Vec<IdxSize>,
_result_idx_right: Vec<Option<IdxSize>>,
_chunk_mapping_left: Option<&[ChunkId]>,
_chunk_mapping_right: Option<&[ChunkId]>,
) -> LeftJoinIds {
(_result_idx_left, _result_idx_right)
}
pub(super) fn flatten_left_join_ids(result: Vec<LeftJoinIds>) -> LeftJoinIds {
#[cfg(feature = "chunked_ids")]
{
let left = if result[0].0.is_left() {
let lefts = result
.iter()
.map(|join_id| join_id.0.as_ref().left().unwrap())
.collect::<Vec<_>>();
let lefts = flatten(&lefts, None);
JoinIds::Left(lefts)
} else {
let lefts = result
.iter()
.map(|join_id| join_id.0.as_ref().right().unwrap())
.collect::<Vec<_>>();
let lefts = flatten(&lefts, None);
JoinIds::Right(lefts)
};
let right = if result[0].1.is_left() {
let rights = result
.iter()
.map(|join_id| join_id.1.as_ref().left().unwrap())
.collect::<Vec<_>>();
let rights = flatten(&rights, None);
JoinOptIds::Left(rights)
} else {
let rights = result
.iter()
.map(|join_id| join_id.1.as_ref().right().unwrap())
.collect::<Vec<_>>();
let rights = flatten(&rights, None);
JoinOptIds::Right(rights)
};
(left, right)
}
#[cfg(not(feature = "chunked_ids"))]
{
let lefts = result.iter().map(|join_id| &join_id.0).collect::<Vec<_>>();
let rights = result.iter().map(|join_id| &join_id.1).collect::<Vec<_>>();
let lefts = flatten(&lefts, None);
let rights = flatten(&rights, None);
(lefts, rights)
}
}
pub(super) fn hash_join_tuples_left<T, IntoSlice>(
probe: Vec<IntoSlice>,
build: Vec<IntoSlice>,
chunk_mapping_left: Option<&[ChunkId]>,
chunk_mapping_right: Option<&[ChunkId]>,
) -> LeftJoinIds
where
IntoSlice: AsRef<[T]> + Send + Sync,
T: Send + Hash + Eq + Sync + Copy + AsU64,
{
let hash_tbls = create_probe_table(build);
let offsets = probe_to_offsets(&probe);
let n_tables = hash_tbls.len() as u64;
debug_assert!(n_tables.is_power_of_two());
let result: Vec<LeftJoinIds> = POOL.install(move || {
probe
.into_par_iter()
.zip(offsets)
.map(move |(probe, offset)| {
let hash_tbls = &hash_tbls;
let probe = probe.as_ref();
let mut result_idx_left = Vec::with_capacity(probe.len());
let mut result_idx_right = Vec::with_capacity(probe.len());
probe.iter().enumerate().for_each(|(idx_a, k)| {
let idx_a = (idx_a + offset) as IdxSize;
let current_probe_table = unsafe {
get_hash_tbl_threaded_join_partitioned(k.as_u64(), hash_tbls, n_tables)
};
let value = current_probe_table.get(k);
match value {
Some(indexes_b) => {
result_idx_left.extend(std::iter::repeat(idx_a).take(indexes_b.len()));
result_idx_right.extend(indexes_b.iter().copied().map(Some))
}
None => {
result_idx_left.push(idx_a);
result_idx_right.push(None);
}
}
});
finish_left_join_mappings(
result_idx_left,
result_idx_right,
chunk_mapping_left,
chunk_mapping_right,
)
})
.collect()
});
flatten_left_join_ids(result)
}