reifydb-engine 0.4.12

Query execution and processing engine for ReifyDB
Documentation
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use std::collections::HashMap;

use reifydb_core::value::column::{columns::Columns, headers::ColumnHeaders};
use reifydb_rql::expression::Expression;
use reifydb_runtime::hash::Hash128;
use reifydb_transaction::transaction::Transaction;
use reifydb_type::{
	fragment::Fragment,
	value::{Value, row_number::RowNumber},
};
use tracing::instrument;

use super::common::{
	JoinContext, compute_join_hash, eval_join_condition, keys_equal_by_index, load_and_merge_all,
	resolve_column_names,
};
use crate::{
	Result,
	expression::{
		compile::{CompiledExpr, compile_expression},
		context::CompileContext,
	},
	vm::volcano::query::{QueryContext, QueryNode},
};

pub(crate) struct EquiKeyPair {
	pub left_col_name: String,
	pub right_col_name: String,
}

pub(crate) struct EquiJoinAnalysis {
	pub equi_keys: Vec<EquiKeyPair>,
	pub residual: Vec<Expression>,
}

/// Analyze join `ON` expressions to separate equi-join key pairs from residual
/// predicates.  An equi-key pair is `Equal(Column(name), AccessSource(name))`
/// (in either order).  Any `Or` anywhere causes an immediate abort — all
/// expressions are returned as residual.
pub(crate) fn extract_equi_keys(on: &[Expression]) -> EquiJoinAnalysis {
	let mut leaves = Vec::new();
	for expr in on {
		if contains_or(expr) {
			return EquiJoinAnalysis {
				equi_keys: vec![],
				residual: on.to_vec(),
			};
		}
		flatten_and(expr, &mut leaves);
	}

	let mut equi_keys = Vec::new();
	let mut residual = Vec::new();

	for leaf in leaves {
		match try_extract_equi_pair(&leaf) {
			Some(pair) => equi_keys.push(pair),
			None => residual.push(leaf),
		}
	}

	EquiJoinAnalysis {
		equi_keys,
		residual,
	}
}

/// Recursively check whether an expression tree contains `Or`.
fn contains_or(expr: &Expression) -> bool {
	match expr {
		Expression::Or(_) => true,
		Expression::And(and) => contains_or(&and.left) || contains_or(&and.right),
		_ => false,
	}
}

/// Flatten a tree of `And` nodes into a flat list of leaf expressions.
fn flatten_and(expr: &Expression, out: &mut Vec<Expression>) {
	match expr {
		Expression::And(and) => {
			flatten_and(&and.left, out);
			flatten_and(&and.right, out);
		}
		other => out.push(other.clone()),
	}
}

/// Try to extract an equi-join key pair from an `Equal` expression.
/// Matches `Equal(Column(..), AccessSource(..))` in either order.
fn try_extract_equi_pair(expr: &Expression) -> Option<EquiKeyPair> {
	if let Expression::Equal(eq) = expr {
		// Column == AccessSource
		if let (Expression::Column(col), Expression::AccessSource(acc)) = (eq.left.as_ref(), eq.right.as_ref())
		{
			return Some(EquiKeyPair {
				left_col_name: col.0.name.text().to_string(),
				right_col_name: acc.column.name.text().to_string(),
			});
		}
		// AccessSource == Column  (swapped)
		if let (Expression::AccessSource(acc), Expression::Column(col)) = (eq.left.as_ref(), eq.right.as_ref())
		{
			return Some(EquiKeyPair {
				left_col_name: col.0.name.text().to_string(),
				right_col_name: acc.column.name.text().to_string(),
			});
		}
	}
	None
}

#[derive(Clone, Copy, PartialEq)]
enum HashJoinMode {
	Inner,
	Left,
}

struct HashJoinState {
	build_columns: Columns,
	hash_table: HashMap<Hash128, Vec<usize>>,
	resolved_names: Vec<String>,
	right_width: usize,
	right_key_indices: Vec<usize>,
	left_key_indices: Vec<usize>,

	// Probe cursor
	probe_batch: Option<Columns>,
	probe_row_idx: usize,
	current_matches: Vec<usize>,
	current_match_idx: usize,
	current_row_matched: bool,
	probe_exhausted: bool,

	// Compiled residual predicates
	compiled_residual: Vec<CompiledExpr>,

	// Reusable scratch buffer for hashing
	hash_buf: Vec<u8>,
}

pub(crate) struct HashJoinNode {
	left: Box<dyn QueryNode>,
	right: Box<dyn QueryNode>,

	left_key_names: Vec<String>,
	right_key_names: Vec<String>,
	residual: Vec<Expression>,
	alias: Option<Fragment>,
	mode: HashJoinMode,

	headers: Option<ColumnHeaders>,
	context: JoinContext,

	state: Option<HashJoinState>,
}

impl HashJoinNode {
	pub(crate) fn new_inner(
		left: Box<dyn QueryNode>,
		right: Box<dyn QueryNode>,
		analysis: EquiJoinAnalysis,
		alias: Option<Fragment>,
	) -> Self {
		let (left_keys, right_keys) = split_key_names(&analysis.equi_keys);
		Self {
			left,
			right,
			left_key_names: left_keys,
			right_key_names: right_keys,
			residual: analysis.residual,
			alias,
			mode: HashJoinMode::Inner,
			headers: None,
			context: JoinContext::new(),
			state: None,
		}
	}

	pub(crate) fn new_left(
		left: Box<dyn QueryNode>,
		right: Box<dyn QueryNode>,
		analysis: EquiJoinAnalysis,
		alias: Option<Fragment>,
	) -> Self {
		let (left_keys, right_keys) = split_key_names(&analysis.equi_keys);
		Self {
			left,
			right,
			left_key_names: left_keys,
			right_key_names: right_keys,
			residual: analysis.residual,
			alias,
			mode: HashJoinMode::Left,
			headers: None,
			context: JoinContext::new(),
			state: None,
		}
	}

	/// Build phase: materialize the right side into a hash table.
	fn build<'a>(&mut self, rx: &mut Transaction<'a>, ctx: &mut QueryContext) -> Result<()> {
		let build_columns = load_and_merge_all(&mut self.right, rx, ctx)?;
		let right_width = build_columns.len();

		// Pre-resolve right key name → column index (empty when build side has no columns)
		let right_key_indices: Vec<usize> = if build_columns.is_empty() {
			Vec::new()
		} else {
			self.right_key_names
				.iter()
				.map(|name| {
					build_columns
						.iter()
						.position(|c| c.name().text() == name)
						.unwrap_or_else(|| panic!("right key column '{}' not found", name))
				})
				.collect()
		};

		// Build hash table using index-based hashing
		let mut hash_table: HashMap<Hash128, Vec<usize>> = HashMap::new();
		let mut hash_buf = Vec::with_capacity(256);
		let row_count = build_columns.row_count();
		for j in 0..row_count {
			if let Some(h) = compute_join_hash(&build_columns, &right_key_indices, j, &mut hash_buf) {
				hash_table.entry(h).or_default().push(j);
			}
		}

		// Compile residual predicates
		let compile_ctx = CompileContext {
			functions: &ctx.services.functions,
			symbols: &ctx.symbols,
		};
		let compiled_residual: Vec<CompiledExpr> = self
			.residual
			.iter()
			.map(|e| compile_expression(&compile_ctx, e).expect("compile residual"))
			.collect();

		self.state = Some(HashJoinState {
			build_columns,
			hash_table,
			resolved_names: Vec::new(),
			right_width,
			right_key_indices,
			left_key_indices: Vec::new(), // resolved on first probe batch
			probe_batch: None,
			probe_row_idx: 0,
			current_matches: Vec::new(),
			current_match_idx: 0,
			current_row_matched: false,
			probe_exhausted: false,
			compiled_residual,
			hash_buf,
		});

		Ok(())
	}
}

fn split_key_names(pairs: &[EquiKeyPair]) -> (Vec<String>, Vec<String>) {
	let left: Vec<String> = pairs.iter().map(|p| p.left_col_name.clone()).collect();
	let right: Vec<String> = pairs.iter().map(|p| p.right_col_name.clone()).collect();
	(left, right)
}

/// Compute hash → lookup bucket → filter by key equality for a single probe row.
fn compute_matches_for_probe_row(
	hash_table: &HashMap<Hash128, Vec<usize>>,
	build_columns: &Columns,
	probe: &Columns,
	probe_row_idx: usize,
	left_key_indices: &[usize],
	right_key_indices: &[usize],
	buf: &mut Vec<u8>,
) -> Vec<usize> {
	match compute_join_hash(probe, left_key_indices, probe_row_idx, buf) {
		Some(h) => hash_table
			.get(&h)
			.map(|indices| {
				indices.iter()
					.copied()
					.filter(|&build_idx| {
						keys_equal_by_index(
							probe,
							probe_row_idx,
							left_key_indices,
							build_columns,
							build_idx,
							right_key_indices,
						)
					})
					.collect()
			})
			.unwrap_or_default(),
		None => Vec::new(),
	}
}

impl QueryNode for HashJoinNode {
	#[instrument(level = "trace", skip_all, name = "volcano::join::hash::initialize")]
	fn initialize<'a>(&mut self, rx: &mut Transaction<'a>, ctx: &QueryContext) -> Result<()> {
		self.context.set(ctx);
		self.left.initialize(rx, ctx)?;
		self.right.initialize(rx, ctx)?;
		Ok(())
	}

	#[instrument(level = "trace", skip_all, name = "volcano::join::hash::next")]
	fn next<'a>(&mut self, rx: &mut Transaction<'a>, ctx: &mut QueryContext) -> Result<Option<Columns>> {
		debug_assert!(self.context.is_initialized(), "HashJoinNode::next() called before initialize()");

		// Build phase (first call)
		if self.state.is_none() {
			self.build(rx, ctx)?;
		}

		let batch_size = ctx.batch_size as usize;
		let stored_ctx = self.context.get().clone();

		// We need to work around the borrow checker: take state out, work with it, put it back.
		let mut state = self.state.take().unwrap();

		if state.probe_exhausted && state.probe_batch.is_none() {
			if self.headers.is_some() {
				self.state = Some(state);
				return Ok(None);
			}
			if state.resolved_names.is_empty() {
				let empty_left = Columns::empty();
				let resolved =
					resolve_column_names(&empty_left, &state.build_columns, &self.alias, None);
				state.resolved_names = resolved.qualified_names;
			}
			let names_refs: Vec<&str> = state.resolved_names.iter().map(|s| s.as_str()).collect();
			let empty: Vec<Vec<Value>> = Vec::new();
			let columns = Columns::from_rows(&names_refs, &empty);
			self.headers = Some(ColumnHeaders::from_columns(&columns));
			self.state = Some(state);
			return Ok(Some(columns));
		}

		let mut result_rows: Vec<Vec<Value>> = Vec::new();
		let mut result_row_numbers: Vec<RowNumber> = Vec::new();

		// Resolve column names and left key indices lazily on first probe batch
		let resolve_names_and_indices = |state: &mut HashJoinState,
		                                 probe: &Columns,
		                                 left_key_names: &[String]| {
			if state.resolved_names.is_empty() {
				let resolved = resolve_column_names(probe, &state.build_columns, &self.alias, None);
				state.resolved_names = resolved.qualified_names;
			}
			if state.left_key_indices.is_empty() {
				state.left_key_indices = left_key_names
					.iter()
					.map(|name| {
						probe.iter().position(|c| c.name().text() == name).unwrap_or_else(
							|| panic!("left key column '{}' not found", name),
						)
					})
					.collect();
			}
		};

		while result_rows.len() < batch_size {
			// Ensure we have a probe batch
			if state.probe_batch.is_none() {
				if state.probe_exhausted {
					break;
				}
				match self.left.next(rx, ctx)? {
					Some(batch) => {
						resolve_names_and_indices(&mut state, &batch, &self.left_key_names);
						state.probe_batch = Some(batch);
						state.probe_row_idx = 0;
						// Compute matches for first row
						let probe = state.probe_batch.as_ref().unwrap();
						if probe.row_count() == 0 {
							state.probe_batch = None;
							continue;
						}
						state.current_matches = compute_matches_for_probe_row(
							&state.hash_table,
							&state.build_columns,
							probe,
							0,
							&state.left_key_indices,
							&state.right_key_indices,
							&mut state.hash_buf,
						);
						state.current_match_idx = 0;
						state.current_row_matched = false;
					}
					None => {
						state.probe_exhausted = true;
						break;
					}
				}
			}

			let probe = state.probe_batch.as_ref().unwrap();
			let probe_row_count = probe.row_count();

			// Check if current probe row's matches are exhausted
			if state.current_match_idx >= state.current_matches.len() {
				// Emit unmatched left row for left joins
				if self.mode == HashJoinMode::Left && !state.current_row_matched {
					let left_row = probe.get_row(state.probe_row_idx);
					let mut combined = left_row;
					combined.extend(vec![Value::none(); state.right_width]);
					result_rows.push(combined);
					if !probe.row_numbers.is_empty() {
						result_row_numbers.push(probe.row_numbers[state.probe_row_idx]);
					}
				}

				// Advance to next probe row
				state.probe_row_idx += 1;
				if state.probe_row_idx >= probe_row_count {
					state.probe_batch = None;
					continue;
				}

				// Compute matches for new probe row
				state.current_matches = compute_matches_for_probe_row(
					&state.hash_table,
					&state.build_columns,
					probe,
					state.probe_row_idx,
					&state.left_key_indices,
					&state.right_key_indices,
					&mut state.hash_buf,
				);
				state.current_match_idx = 0;
				state.current_row_matched = false;
				continue;
			}

			// Emit a match
			let build_idx = state.current_matches[state.current_match_idx];
			state.current_match_idx += 1;

			let left_row = probe.get_row(state.probe_row_idx);
			let right_row = state.build_columns.get_row(build_idx);

			// Evaluate residual predicates
			if !state.compiled_residual.is_empty()
				&& !eval_join_condition(
					&state.compiled_residual,
					probe,
					&state.build_columns,
					&left_row,
					&right_row,
					&self.alias,
					&stored_ctx,
				) {
				continue;
			}

			state.current_row_matched = true;
			let mut combined = left_row;
			combined.extend(right_row);
			result_rows.push(combined);
			if !probe.row_numbers.is_empty() {
				result_row_numbers.push(probe.row_numbers[state.probe_row_idx]);
			}
		}

		self.state = Some(state);

		if result_rows.is_empty() {
			if self.headers.is_some() {
				return Ok(None);
			}
			if let Some(ref mut state) = self.state {
				if state.resolved_names.is_empty() {
					let empty_left = Columns::empty();
					let resolved = resolve_column_names(
						&empty_left,
						&state.build_columns,
						&self.alias,
						None,
					);
					state.resolved_names = resolved.qualified_names;
				}
				let names_refs: Vec<&str> = state.resolved_names.iter().map(|s| s.as_str()).collect();
				let columns = Columns::from_rows(&names_refs, &result_rows);
				self.headers = Some(ColumnHeaders::from_columns(&columns));
				return Ok(Some(columns));
			}
			return Ok(None);
		}

		let state = self.state.as_ref().unwrap();
		let names_refs: Vec<&str> = state.resolved_names.iter().map(|s| s.as_str()).collect();
		let columns = if result_row_numbers.is_empty() {
			Columns::from_rows(&names_refs, &result_rows)
		} else {
			Columns::from_rows_with_row_numbers(&names_refs, &result_rows, result_row_numbers)
		};

		self.headers = Some(ColumnHeaders::from_columns(&columns));
		Ok(Some(columns))
	}

	fn headers(&self) -> Option<ColumnHeaders> {
		self.headers.clone()
	}
}