-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathhash_join.rs
209 lines (193 loc) · 7.49 KB
/
hash_join.rs
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
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.
use std::marker::ConstParamTy;
use std::vec::Vec;
use ahash::{HashMap, HashMapExt, HashSet, HashSetExt};
use smallvec::SmallVec;
use super::*;
use crate::array::{ArrayBuilderImpl, ArrayImpl, DataChunk, DataChunkBuilder, RowRef};
use crate::types::{DataType, DataValue, Row};
/// The executor for hash join
pub struct HashJoinExecutor<const T: JoinType> {
pub left_keys: RecExpr,
pub right_keys: RecExpr,
pub left_types: Vec<DataType>,
pub right_types: Vec<DataType>,
}
/// Join types for generating join code during the compilation.
#[derive(Copy, Clone, Eq, PartialEq, ConstParamTy)]
pub enum JoinType {
Inner,
LeftOuter,
RightOuter,
FullOuter,
}
pub type JoinKeys = SmallVec<[DataValue; 2]>;
impl<const T: JoinType> HashJoinExecutor<T> {
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)]
pub async fn execute(self, left: BoxedExecutor, right: BoxedExecutor) {
// build
#[derive(Default, Debug)]
struct LeftKeyInfo {
rows: SmallVec<[Row; 1]>,
matched: bool,
}
let mut hash_map: HashMap<JoinKeys, LeftKeyInfo> = HashMap::new();
#[for_await]
for chunk in left {
let chunk = chunk?;
let keys_chunk = Evaluator::new(&self.left_keys).eval_list(&chunk)?;
for (row, keys) in chunk.rows().zip(keys_chunk.rows()) {
let keys = keys.values().collect();
hash_map.entry(keys).or_default().rows.push(row.to_owned());
}
tokio::task::consume_budget().await;
}
let data_types = self.left_types.iter().chain(self.right_types.iter());
let mut builder = DataChunkBuilder::new(data_types, PROCESSING_WINDOW_SIZE);
// probe
#[for_await]
for chunk in right {
let chunk = chunk?;
let keys_chunk = Evaluator::new(&self.right_keys).eval_list(&chunk)?;
for (right_row, keys) in chunk.rows().zip(keys_chunk.rows()) {
if let Some(left_rows) = hash_map.get_mut(&keys.values().collect::<JoinKeys>()) {
left_rows.matched = true;
for left_row in &left_rows.rows {
let values = left_row.iter().cloned().chain(right_row.values());
if let Some(chunk) = builder.push_row(values) {
yield chunk;
}
}
} else if T == JoinType::RightOuter || T == JoinType::FullOuter {
// append row: (NULL, right)
let values =
(self.left_types.iter().map(|_| DataValue::Null)).chain(right_row.values());
if let Some(chunk) = builder.push_row(values) {
yield chunk;
}
}
}
tokio::task::consume_budget().await;
}
// append rows for left outer join
if T == JoinType::LeftOuter || T == JoinType::FullOuter {
for (_, rows) in hash_map {
if rows.matched {
continue;
}
for row in rows.rows {
// append row: (left, NULL)
let values =
(row.into_iter()).chain(self.right_types.iter().map(|_| DataValue::Null));
if let Some(chunk) = builder.push_row(values) {
yield chunk;
}
}
tokio::task::consume_budget().await;
}
}
if let Some(chunk) = builder.take() {
yield chunk;
}
}
}
/// The executor for hash semi/anti join
pub struct HashSemiJoinExecutor {
pub left_keys: RecExpr,
pub right_keys: RecExpr,
pub anti: bool,
}
impl HashSemiJoinExecutor {
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)]
pub async fn execute(self, left: BoxedExecutor, right: BoxedExecutor) {
let mut key_set: HashSet<JoinKeys> = HashSet::new();
// build
#[for_await]
for chunk in right {
let chunk = chunk?;
let keys_chunk = Evaluator::new(&self.right_keys).eval_list(&chunk)?;
for row in keys_chunk.rows() {
key_set.insert(row.values().collect());
}
tokio::task::consume_budget().await;
}
// probe
#[for_await]
for chunk in left {
let chunk = chunk?;
let keys_chunk = Evaluator::new(&self.left_keys).eval_list(&chunk)?;
let exists = keys_chunk
.rows()
.map(|key| key_set.contains(&key.values().collect::<JoinKeys>()) ^ self.anti)
.collect::<Vec<bool>>();
yield chunk.filter(&exists);
}
}
}
/// The executor for hash semi/anti join
pub struct HashSemiJoinExecutor2 {
pub left_keys: RecExpr,
pub right_keys: RecExpr,
pub condition: RecExpr,
pub left_types: Vec<DataType>,
pub right_types: Vec<DataType>,
pub anti: bool,
}
impl HashSemiJoinExecutor2 {
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)]
pub async fn execute(self, left: BoxedExecutor, right: BoxedExecutor) {
let mut key_set: HashMap<JoinKeys, DataChunkBuilder> = HashMap::new();
// build
#[for_await]
for chunk in right {
let chunk = chunk?;
let keys_chunk = Evaluator::new(&self.right_keys).eval_list(&chunk)?;
for (key, row) in keys_chunk.rows().zip(chunk.rows()) {
let chunk = key_set
.entry(key.values().collect())
.or_insert_with(|| DataChunkBuilder::unbounded(&self.right_types))
.push_row(row.values());
assert!(chunk.is_none());
}
tokio::task::consume_budget().await;
}
let key_set = key_set
.into_iter()
.map(|(k, mut v)| (k, v.take().unwrap()))
.collect::<HashMap<JoinKeys, DataChunk>>();
// probe
#[for_await]
for chunk in left {
let chunk = chunk?;
let keys_chunk = Evaluator::new(&self.left_keys).eval_list(&chunk)?;
let mut exists = Vec::with_capacity(chunk.cardinality());
for (key, lrow) in keys_chunk.rows().zip(chunk.rows()) {
let b = if let Some(rchunk) = key_set.get(&key.values().collect::<JoinKeys>()) {
let lchunk = self.left_row_to_chunk(&lrow, rchunk.cardinality());
let join_chunk = lchunk.row_concat(rchunk.clone());
let ArrayImpl::Bool(a) = Evaluator::new(&self.condition).eval(&join_chunk)?
else {
panic!("join condition should return bool");
};
a.true_array().iter().any(|b| *b)
} else {
false
};
exists.push(b ^ self.anti);
}
yield chunk.filter(&exists);
}
}
/// Expand the left row to a chunk with given length.
fn left_row_to_chunk(&self, row: &RowRef<'_>, len: usize) -> DataChunk {
self.left_types
.iter()
.zip(row.values())
.map(|(ty, value)| {
let mut builder = ArrayBuilderImpl::with_capacity(len, ty);
builder.push_n(len, &value);
builder.finish()
})
.collect()
}
}