reifydb-engine 0.10.1

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

use reifydb_core::interface::catalog::object::ObjectId;
use reifydb_value::{
	error::{Diagnostic, Error, IntoDiagnostic},
	fragment::Fragment,
	value::value_type::ValueType,
};

#[derive(Debug, thiserror::Error)]
pub enum EngineError {
	#[error("column `{column}` not found in `{table_name}`")]
	BulkInsertColumnNotFound {
		fragment: Fragment,
		table_name: String,
		column: String,
	},

	#[error("too many values: expected {expected} columns, got {actual}")]
	BulkInsertTooManyValues {
		fragment: Fragment,
		expected: usize,
		actual: usize,
	},

	#[error("Frame must have a __ROW__ID__ column for UPDATE operations")]
	MissingRowNumberColumn,

	#[error("assertion failed: {message}")]
	AssertionFailed {
		fragment: Fragment,
		message: String,
		expression: Option<String>,
	},

	#[error("Cannot insert none into non-optional column of type {column_type}")]
	NoneNotAllowed {
		fragment: Fragment,
		column_type: ValueType,
	},

	#[error("Unknown callable: {name}")]
	UnknownCallable {
		name: String,
		fragment: Fragment,
	},

	#[error("function `{name}` returned a value that cannot be cast to its declared return type {declared}")]
	ReturnTypeMismatch {
		name: String,
		declared: ValueType,
		fragment: Fragment,
		cause: Box<Diagnostic>,
	},

	#[error("Generator function '{name}' not found")]
	GeneratorNotFound {
		name: String,
		fragment: Fragment,
	},

	#[error(
		"cannot locate partitioned rows for {operation} on object {object}: query object carries no partition address"
	)]
	MissingPartitionAddress {
		object: ObjectId,
		operation: &'static str,
	},

	#[error("`{object}` declares `{column}` as its #time populator but has no such column")]
	TimePopulatorMissing {
		object: String,
		column: String,
	},

	#[error("`{object}.{column}` is the declared #time populator but holds {found} on this row")]
	TimePopulatorNotDateTime {
		object: String,
		column: String,
		found: String,
	},

	#[error("cannot update the tag of series `{series}`: the tag is part of the row key")]
	SeriesTagImmutable {
		series: String,
		fragment: Fragment,
	},

	#[error("cannot update the key column `{column}` of series `{series}`: the key is part of the row key")]
	SeriesKeyImmutable {
		series: String,
		column: String,
		fragment: Fragment,
	},

	#[error("series key column `{column}` cannot store {value}")]
	SeriesKeyOutOfRange {
		column: String,
		value: String,
		fragment: Fragment,
	},

	#[error("series key column `{column}` of series `{series}` changed by a pre-insert hook")]
	SeriesKeyChangedByHook {
		series: String,
		column: String,
	},

	#[error("series `{series}` is partitioned by its key `{column}`, so an insert must give the key")]
	SeriesKeyPartitionNeedsKey {
		series: String,
		column: String,
		fragment: Fragment,
	},
}

impl IntoDiagnostic for EngineError {
	fn into_diagnostic(self) -> Diagnostic {
		match self {
			EngineError::BulkInsertColumnNotFound {
				fragment,
				table_name,
				column,
			} => Diagnostic {
				code: "BI_001".to_string(),
				rql: None,
				message: format!("column `{}` not found in `{}`", column, table_name),
				column: None,
				fragment,
				label: Some("unknown column".to_string()),
				help: Some("check that the column name matches the shape".to_string()),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},
			EngineError::BulkInsertTooManyValues {
				fragment,
				expected,
				actual,
			} => Diagnostic {
				code: "BI_003".to_string(),
				rql: None,
				message: format!("too many values: expected {} columns, got {}", expected, actual),
				column: None,
				fragment,
				label: Some("value count mismatch".to_string()),
				help: Some("ensure the number of values matches the column count".to_string()),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},
			EngineError::MissingRowNumberColumn => Diagnostic {
				code: "ENG_003".to_string(),
				rql: None,
				message: "Frame must have a __ROW__ID__ column for UPDATE operations".to_string(),
				column: None,
				fragment: Fragment::None,
				label: Some("missing required column".to_string()),
				help: Some("Ensure the query includes the encoded ID in the result set".to_string()),
				notes: vec!["UPDATE operations require encoded identifiers to locate existing rows"
					.to_string()],
				cause: None,
				operator_chain: None,
			},
			EngineError::AssertionFailed {
				fragment,
				message,
				expression,
			} => {
				let base_msg = if !message.is_empty() {
					message.clone()
				} else if let Some(ref expr) = expression {
					format!("assertion failed: {}", expr)
				} else {
					"assertion failed".to_string()
				};
				let label = expression
					.as_ref()
					.map(|expr| format!("this expression is false: {}", expr))
					.or_else(|| Some("assertion failed".to_string()));
				Diagnostic {
					code: "ASSERT".to_string(),
					rql: None,
					message: base_msg,
					fragment,
					label,
					help: None,
					notes: vec![],
					column: None,
					cause: None,
					operator_chain: None,
				}
			}
			EngineError::NoneNotAllowed {
				fragment,
				column_type,
			} => Diagnostic {
				code: "CONSTRAINT_007".to_string(),
				rql: None,
				message: format!(
					"Cannot insert none into non-optional column of type {}. Declare the column as Option({}) to allow none values.",
					column_type, column_type
				),
				column: None,
				fragment,
				label: Some("constraint violation".to_string()),
				help: Some(format!(
					"The column type is {} which does not accept none. Use Option({}) if the column should be optional.",
					column_type, column_type
				)),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},
			EngineError::UnknownCallable {
				name,
				fragment,
			} => Diagnostic {
				code: "CALLABLE_001".to_string(),
				rql: None,
				message: format!("Unknown callable: {}", name),
				column: None,
				fragment,
				label: Some("unknown callable".to_string()),
				help: Some(
					"Check the name and available functions, procedures, and closures".to_string()
				),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},
			EngineError::ReturnTypeMismatch {
				name,
				declared,
				fragment,
				cause,
			} => Diagnostic {
				code: "CALLABLE_002".to_string(),
				rql: None,
				message: format!(
					"function `{}` returned a value that cannot be cast to its declared return type {}",
					name, declared
				),
				column: None,
				fragment,
				label: Some(format!("`{}` must return {}", name, declared)),
				help: Some(format!(
					"return a value that fits {} from `{}`, or change its declared return type",
					declared, name
				)),
				notes: vec![],
				cause: Some(cause),
				operator_chain: None,
			},
			EngineError::GeneratorNotFound {
				name,
				fragment,
			} => Diagnostic {
				code: "FUNCTION_010".to_string(),
				rql: None,
				message: format!("Generator function '{}' not found", name),
				column: None,
				fragment,
				label: Some("unknown generator function".to_string()),
				help: Some("Check the generator function name and ensure it is registered".to_string()),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},
			EngineError::MissingPartitionAddress {
				object,
				operation,
			} => Diagnostic {
				code: "PART_001".to_string(),
				rql: None,
				message: format!(
					"cannot locate partitioned rows for {} on object {}: query object carries no partition address",
					operation, object
				),
				column: None,
				fragment: Fragment::None,
				label: Some("missing partition address".to_string()),
				help: Some(
					"the query must carry the row's partition alongside its row number; rewrite the query so the partitioned source's columns flow through unmodified"
						.to_string(),
				),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},

			EngineError::TimePopulatorMissing {
				object,
				column,
			} => Diagnostic {
				code: "TIME_001".to_string(),
				rql: None,
				message: format!(
					"`{}` declares `{}` as its #time populator but has no such column",
					object, column
				),
				column: None,
				fragment: Fragment::None,
				label: Some("declared populator does not exist".to_string()),
				help: Some(
					"the populator must name one of the object's own columns; correct the `ts` key in the object's WITH clause"
						.to_string(),
				),
				notes: vec![
					"definition-time validation rejects a populator naming an absent column, so reaching this means the object was stored with a populator it does not have"
						.to_string(),
				],
				cause: None,
				operator_chain: None,
			},

			EngineError::TimePopulatorNotDateTime {
				object,
				column,
				found,
			} => Diagnostic {
				code: "TIME_002".to_string(),
				rql: None,
				message: format!(
					"`{}.{}` is the declared #time populator but holds {} on this row",
					object, column, found
				),
				column: None,
				fragment: Fragment::None,
				label: Some("populator is not a non-none DateTime".to_string()),
				help: Some(
					"every row of an event-time object must carry a non-none DateTime in its populator column"
						.to_string(),
				),
				notes: vec![
					"#time is substrate-owned and cannot fall back to the write clock; silently substituting arrival time would date a replayed row to now"
						.to_string(),
				],
				cause: None,
				operator_chain: None,
			},

			EngineError::SeriesTagImmutable {
				series,
				fragment,
			} => Diagnostic {
				code: "UPDATE_004".to_string(),
				rql: None,
				message: format!(
					"cannot update the tag of series `{}`: the tag is part of the row key",
					series
				),
				column: None,
				fragment,
				label: Some("`tag` cannot be updated".to_string()),
				help: Some("delete the row and insert it again with the new tag".to_string()),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},

			EngineError::SeriesKeyImmutable {
				series,
				column,
				fragment,
			} => Diagnostic {
				code: "UPDATE_005".to_string(),
				rql: None,
				message: format!(
					"cannot update the key column `{}` of series `{}`: the key is part of the row key",
					column, series
				),
				column: None,
				fragment,
				label: Some(format!("`{}` cannot be updated", column)),
				help: Some("delete the row and insert it again with the new key".to_string()),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},

			EngineError::SeriesKeyOutOfRange {
				column,
				value,
				fragment,
			} => Diagnostic {
				code: "SERIES_001".to_string(),
				rql: None,
				message: format!("series key column `{}` cannot store {}", column, value),
				column: None,
				fragment,
				label: Some("key out of range".to_string()),
				help: Some(format!("a series key must be between 0 and {}", u64::MAX)),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},

			EngineError::SeriesKeyChangedByHook {
				series,
				column,
			} => Diagnostic {
				code: "SERIES_002".to_string(),
				rql: None,
				message: format!(
					"series key column `{}` of series `{}` changed by a pre-insert hook",
					column, series
				),
				column: None,
				fragment: Fragment::None,
				label: Some("series key changed by hook".to_string()),
				help: Some(format!(
					"the key `{}` of series `{}` is part of the row key, which is built before pre-insert hooks run, so a hook must not change it",
					column, series
				)),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},

			EngineError::SeriesKeyPartitionNeedsKey {
				series,
				column,
				fragment,
			} => Diagnostic {
				code: "SERIES_003".to_string(),
				rql: None,
				message: format!(
					"series `{}` is partitioned by its key `{}`, so an insert must give the key",
					series, column
				),
				column: None,
				fragment,
				label: Some("key not given".to_string()),
				help: Some(format!(
					"an integer key is generated as the next key of its partition, and the partition of `{}` is made of the key itself; give `{}` on every row",
					series, column
				)),
				notes: vec![],
				cause: None,
				operator_chain: None,
			},
		}
	}
}

impl From<EngineError> for Error {
	fn from(err: EngineError) -> Self {
		Error(Box::new(err.into_diagnostic()))
	}
}