reifydb-engine 0.9.3

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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use std::{slice, sync::Arc};

use reifydb_core::{
	error::{
		CoreError,
		diagnostic::{operation, query},
	},
	metrics::heap::HeapSize,
	value::column::{
		ColumnWithName,
		buffer::ColumnBuffer,
		columns::Columns,
		headers::ColumnHeaders,
		view::group_by::{GroupId, GroupKeyDict, GroupRows},
	},
};
use reifydb_evaluate::expression::{compile::compile_expression, context::CompileContext};
use reifydb_routine_abi::{Accumulator, context::FunctionContext, error::RoutineError, registry::Routines};
use reifydb_rql::{
	expression::{CallExpression, Expression, name::display_label},
	flow::aggregate::{rewrite_aggregate_calls, synthetic_aggregate_column_name},
};
use reifydb_transaction::transaction::Transaction;
use reifydb_value::{
	error,
	error::{FunctionErrorKind, TypeError},
	fragment::Fragment,
	reifydb_assertions,
	value::value_type::ValueType,
};
use tracing::instrument;

use crate::{
	Result,
	vm::volcano::query::{
		QueryContext, QueryNode, charge_query_memory_bytes, eval_context_from_query, is_scalar_type,
	},
};

struct AggregateSlot {
	column: String,
	column_fragment: Fragment,
	accumulator: Box<dyn Accumulator>,
}

impl AggregateSlot {
	fn update(&mut self, columns: &Columns, groups: &GroupRows) -> Result<()> {
		let column_ref = columns
			.column(&self.column)
			.ok_or_else(|| error!(query::column_not_found(self.column_fragment.clone())))?;
		let cwn = ColumnWithName::new(column_ref.name().clone(), column_ref.data().clone());
		self.accumulator.update(&Columns::new(vec![cwn]), groups)?;
		Ok(())
	}

	fn finalize(mut self, dict: &GroupKeyDict) -> Result<ColumnBuffer> {
		let (keys_out, mut data) = self.accumulator.finalize()?;
		align_column_data(dict, &keys_out, &mut data)?;
		Ok(data)
	}
}

enum Projection {
	Aggregate {
		alias: Fragment,
		slot: AggregateSlot,
	},
	Group {
		column: String,
		alias: Fragment,
	},
	Computed {
		alias: Fragment,
		expression: Expression,
		slots: Vec<AggregateSlot>,
	},
}

impl Projection {
	fn slots(&self) -> &[AggregateSlot] {
		match self {
			Projection::Aggregate {
				slot,
				..
			} => slice::from_ref(slot),
			Projection::Group {
				..
			} => &[],
			Projection::Computed {
				slots,
				..
			} => slots,
		}
	}

	fn slots_mut(&mut self) -> &mut [AggregateSlot] {
		match self {
			Projection::Aggregate {
				slot,
				..
			} => slice::from_mut(slot),
			Projection::Group {
				..
			} => &mut [],
			Projection::Computed {
				slots,
				..
			} => slots,
		}
	}
}

pub(crate) struct AggregateNode {
	input: Box<dyn QueryNode>,
	by: Vec<Expression>,
	map: Vec<Expression>,
	headers: Option<ColumnHeaders>,
	context: Option<Arc<QueryContext>>,
}

impl AggregateNode {
	pub fn new(
		input: Box<dyn QueryNode>,
		by: Vec<Expression>,
		map: Vec<Expression>,
		context: Arc<QueryContext>,
	) -> Self {
		Self {
			input,
			by,
			map,
			headers: None,
			context: Some(context),
		}
	}

	#[instrument(level = "trace", skip_all, name = "volcano::aggregate::accumulate")]
	fn accumulate<'a>(
		input: &mut Box<dyn QueryNode>,
		rx: &mut Transaction<'a>,
		ctx: &mut QueryContext,
		keys: &[&str],
		projections: &mut [Projection],
		dict: &mut GroupKeyDict,
		key_types: &mut Vec<Option<ValueType>>,
	) -> Result<()> {
		let mut charged = 0usize;
		while let Some(columns) = input.next(rx, ctx)? {
			if key_types.is_empty() {
				key_types.extend(keys
					.iter()
					.map(|key| columns.column(key).map(|c| c.data().get_type())));
				let aliases = projections.iter().filter_map(|projection| match projection {
					Projection::Group {
						alias,
						..
					} => Some(alias),
					Projection::Aggregate {
						..
					}
					| Projection::Computed {
						..
					} => None,
				});
				for (key_type, alias) in key_types.iter().zip(aliases) {
					if let Some(ty) = key_type
						&& !is_scalar_type(ty)
					{
						return Err(error!(operation::aggregate_group_by_unkeyable(
							alias.clone(),
							ty.clone()
						)));
					}
				}
			}
			let groups = columns.group_by_ids(keys, dict)?;

			for projection in projections.iter_mut() {
				for slot in projection.slots_mut() {
					slot.update(&columns, &groups)?;
				}
			}

			let state = dict.heap_size()
				+ projections
					.iter()
					.flat_map(Projection::slots)
					.map(|slot| slot.accumulator.heap_size())
					.sum::<usize>();
			charge_query_memory_bytes(&ctx.memory, &mut charged, state)?;
		}

		Ok(())
	}

	#[instrument(level = "trace", skip_all, name = "volcano::aggregate::finalize")]
	fn finalize(
		projections: Vec<Projection>,
		keys: &[&str],
		dict: &GroupKeyDict,
		key_types: &[Option<ValueType>],
		ctx: &QueryContext,
	) -> Result<Vec<ColumnWithName>> {
		let mut result_columns = Vec::new();

		for projection in projections {
			match projection {
				Projection::Group {
					alias,
					column,
					..
				} => {
					let col_idx = keys.iter().position(|k| k == &column).unwrap();

					let first_key_type = dict.values(GroupId(0)).map(|key| key[col_idx].get_type());
					let key_type = first_key_type
						.or_else(|| key_types.get(col_idx).cloned().flatten())
						.unwrap_or(ValueType::Boolean);
					let mut c = ColumnWithName {
						name: Fragment::internal(alias.fragment()),
						data: ColumnBuffer::none_typed(key_type, 0),
					};
					for (_, key) in dict.iter() {
						c.data_mut().push_value(key[col_idx].clone());
					}
					result_columns.push(c);
				}
				Projection::Aggregate {
					alias,
					slot,
				} => {
					result_columns.push(ColumnWithName {
						name: Fragment::internal(alias.fragment()),
						data: slot.finalize(dict)?,
					});
				}
				Projection::Computed {
					alias,
					expression,
					slots,
				} => {
					let slot_columns = slots
						.into_iter()
						.enumerate()
						.map(|(idx, slot)| {
							Ok(ColumnWithName::new(
								Fragment::internal(synthetic_aggregate_column_name(
									idx,
								)),
								slot.finalize(dict)?,
							))
						})
						.collect::<Result<Vec<_>>>()?;
					let compiled = compile_expression(
						&CompileContext {
							symbols: &ctx.symbols,
						},
						&expression,
					)?;
					let eval_ctx = eval_context_from_query(ctx)
						.with_eval(Columns::new(slot_columns), dict.len());
					result_columns.push(ColumnWithName {
						name: Fragment::internal(alias.fragment()),
						data: compiled.execute(&eval_ctx)?.data,
					});
				}
			}
		}

		Ok(result_columns)
	}
}

impl QueryNode for AggregateNode {
	#[instrument(level = "trace", skip_all, name = "volcano::aggregate::initialize")]
	fn initialize<'a>(&mut self, rx: &mut Transaction<'a>, ctx: &QueryContext) -> Result<()> {
		self.input.initialize(rx, ctx)?;

		Ok(())
	}

	#[instrument(level = "trace", skip_all, name = "volcano::aggregate::next")]
	fn next<'a>(&mut self, rx: &mut Transaction<'a>, ctx: &mut QueryContext) -> Result<Option<Columns>> {
		reifydb_assertions! {
			assert!(self.context.is_some(), "AggregateNode::next() called before initialize()");
		}
		let stored_ctx = self.context.as_ref().unwrap();

		if self.headers.is_some() {
			return Ok(None);
		}

		let (keys, mut projections) =
			parse_keys_and_aggregates(&self.by, &self.map, &stored_ctx.services.routines, stored_ctx)?;

		let mut dict = GroupKeyDict::new();
		let mut key_types = Vec::new();

		Self::accumulate(&mut self.input, rx, ctx, &keys, &mut projections, &mut dict, &mut key_types)?;

		let result_columns = Self::finalize(projections, &keys, &dict, &key_types, stored_ctx)?;

		let columns = Columns::new(result_columns);
		self.headers = Some(ColumnHeaders::from_columns(&columns));

		Ok(Some(columns))
	}

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

fn parse_keys_and_aggregates<'a>(
	by: &'a [Expression],
	project: &'a [Expression],
	routines: &'a Routines,
	ctx: &QueryContext,
) -> Result<(Vec<&'a str>, Vec<Projection>)> {
	let mut keys = Vec::new();
	let mut projections = Vec::new();

	for gb in by {
		match gb {
			Expression::Column(c) => {
				keys.push(c.0.name.text());
				projections.push(Projection::Group {
					column: c.0.name.text().to_string(),
					alias: c.0.name.clone(),
				})
			}
			Expression::AccessSource(access) => {
				keys.push(access.column.name.text());
				projections.push(Projection::Group {
					column: access.column.name.text().to_string(),
					alias: access.column.name.clone(),
				})
			}

			expr => {
				return Err(error!(operation::aggregate_group_by_not_column(
					expr.full_fragment_owned()
				)));
			}
		}
	}

	for p in project {
		let (actual_expr, alias) = match p {
			Expression::Alias(alias_expr) => (alias_expr.expression.as_ref(), alias_expr.alias.0.clone()),
			expr => (expr, display_label(expr)),
		};

		match actual_expr {
			Expression::Call(call) => {
				let slot = aggregate_slot(call, routines, ctx)?;
				projections.push(Projection::Aggregate {
					alias,
					slot,
				});
			}

			expr => {
				let mut expression = expr.clone();
				let mut calls = Vec::new();
				let rewritten = rewrite_aggregate_calls(
					&mut expression,
					&mut |e| match e {
						Expression::Call(call) => Some(call.clone()),
						_ => None,
					},
					&mut calls,
				);
				if !rewritten || calls.is_empty() {
					return Err(error!(operation::aggregate_map_without_aggregate(
						expr.full_fragment_owned()
					)));
				}
				let slots = calls
					.iter()
					.map(|call| aggregate_slot(call, routines, ctx))
					.collect::<Result<Vec<_>>>()?;
				projections.push(Projection::Computed {
					alias,
					expression,
					slots,
				});
			}
		}
	}
	Ok((keys, projections))
}

fn aggregate_slot(call: &CallExpression, routines: &Routines, ctx: &QueryContext) -> Result<AggregateSlot> {
	let func_name = call.func.0.text();
	let function = routines.get_aggregate_function(func_name).ok_or_else(|| RoutineError::FunctionNotFound {
		function: call.func.0.clone(),
	})?;

	let mut fn_ctx = FunctionContext {
		fragment: call.func.0.clone(),
		identity: ctx.identity,
		row_count: 0,
		runtime_context: &ctx.services.runtime_context,
	};

	let accumulator = function.accumulator(&mut fn_ctx).ok_or_else(|| RoutineError::FunctionExecutionFailed {
		function: call.func.0.clone(),
		reason: format!("Function {} is not an aggregate", func_name),
	})?;

	if call.args.len() > 1 {
		return Err(TypeError::Function {
			kind: FunctionErrorKind::TooManyArguments {
				max_args: 1,
				actual: call.args.len(),
			},
			message: format!(
				"aggregate function {} takes at most 1 argument, got {}",
				func_name,
				call.args.len()
			),
			fragment: call.func.0.clone(),
		}
		.into());
	}

	match call.args.first() {
		Some(Expression::Column(c)) => Ok(AggregateSlot {
			column: c.0.name.text().to_string(),
			column_fragment: c.0.name.clone(),
			accumulator,
		}),
		Some(Expression::AccessSource(access)) => Ok(AggregateSlot {
			column: access.column.name.text().to_string(),
			column_fragment: access.column.name.clone(),
			accumulator,
		}),
		None => Err(RoutineError::FunctionArityMismatch {
			function: call.func.0.clone(),
			expected: 1,
			actual: 0,
		}
		.into()),
		Some(arg) => {
			let actual_type = arg.infer_type().ok_or_else(|| RoutineError::FunctionExecutionFailed {
				function: call.func.0.clone(),
				reason: "aggregate function arguments must be column references".to_string(),
			})?;
			let expected = function.accepted_types().expected_at(0).to_vec();
			Err(RoutineError::FunctionInvalidArgumentType {
				function: call.func.0.clone(),
				argument_index: 0,
				expected,
				actual: actual_type,
			}
			.into())
		}
	}
}

fn align_column_data(dict: &GroupKeyDict, produced: &[GroupId], data: &mut ColumnBuffer) -> Result<()> {
	let mut position_of: Vec<Option<usize>> = vec![None; dict.len()];
	for (position, group) in produced.iter().enumerate() {
		if let Some(slot) = position_of.get_mut(group.index()) {
			*slot = Some(position);
		}
	}

	let reorder_indices: Vec<usize> = (0..dict.len())
		.map(|index| {
			position_of[index].ok_or_else(|| {
				CoreError::FrameError {
					message: format!(
						"Group key {:?} missing in aggregate output",
						dict.values(GroupId(index as u32))
					),
				}
				.into()
			})
		})
		.collect::<Result<Vec<_>>>()?;

	data.reorder(&reorder_indices);
	Ok(())
}