grafeo_core/execution/operators/
mod.rs1pub mod accumulator;
23mod aggregate;
24mod apply;
25mod distinct;
26mod expand;
27mod factorized_aggregate;
28mod factorized_expand;
29mod factorized_filter;
30mod filter;
31mod horizontal_aggregate;
32mod join;
33mod leapfrog_join;
34mod limit;
35mod load_data;
36mod map_collect;
37mod merge;
38mod mutation;
39mod parameter_scan;
40mod project;
41pub mod push;
42mod scan;
43mod scan_vector;
44mod set_ops;
45mod shortest_path;
46pub mod single_row;
47mod sort;
48mod union;
49mod unwind;
50pub mod value_utils;
51mod variable_length_expand;
52mod vector_join;
53
54pub use accumulator::{AggregateExpr, AggregateFunction, HashableValue};
55pub use aggregate::{HashAggregateOperator, SimpleAggregateOperator};
56pub use apply::ApplyOperator;
57pub use distinct::DistinctOperator;
58pub use expand::ExpandOperator;
59pub use factorized_aggregate::{
60 FactorizedAggregate, FactorizedAggregateOperator, FactorizedOperator,
61};
62pub use factorized_expand::{
63 ExpandStep, FactorizedExpandChain, FactorizedExpandOperator, FactorizedResult,
64 LazyFactorizedChainOperator,
65};
66pub use factorized_filter::{
67 AndPredicate, ColumnPredicate, CompareOp as FactorizedCompareOp, FactorizedFilterOperator,
68 FactorizedPredicate, OrPredicate, PropertyPredicate,
69};
70pub use filter::{
71 BinaryFilterOp, ExpressionPredicate, FilterExpression, FilterOperator, LazyValue,
72 ListPredicateKind, Predicate, SessionContext, UnaryFilterOp,
73};
74pub use horizontal_aggregate::{EntityKind, HorizontalAggregateOperator};
75pub use join::{
76 EqualityCondition, HashJoinOperator, HashKey, JoinCondition, JoinType, NestedLoopJoinOperator,
77};
78pub use leapfrog_join::LeapfrogJoinOperator;
79pub use limit::{LimitOperator, LimitSkipOperator, SkipOperator};
80pub use load_data::{LoadDataFormat, LoadDataOperator};
81pub use map_collect::MapCollectOperator;
82pub use merge::{MergeConfig, MergeOperator, MergeRelationshipConfig, MergeRelationshipOperator};
83pub use mutation::{
84 AddLabelOperator, ConstraintValidator, CreateEdgeOperator, CreateNodeOperator,
85 DeleteEdgeOperator, DeleteNodeOperator, PropertySource, RemoveLabelOperator,
86 SetPropertyOperator,
87};
88pub use parameter_scan::{ParameterScanOperator, ParameterState};
89pub use project::{ProjectExpr, ProjectOperator};
90pub use push::{
91 AggregatePushOperator, DistinctMaterializingOperator, DistinctPushOperator, FilterPushOperator,
92 LimitPushOperator, ProjectPushOperator, SkipLimitPushOperator, SkipPushOperator,
93 SortPushOperator,
94};
95#[cfg(feature = "spill")]
96pub use push::{SpillableAggregatePushOperator, SpillableSortPushOperator};
97pub use scan::ScanOperator;
98pub use scan_vector::VectorScanOperator;
99pub use set_ops::{ExceptOperator, IntersectOperator, OtherwiseOperator};
100pub use shortest_path::ShortestPathOperator;
101pub use single_row::{EmptyOperator, NodeListOperator, SingleRowOperator};
102pub use sort::{NullOrder, SortDirection, SortKey, SortOperator};
103pub use union::UnionOperator;
104pub use unwind::UnwindOperator;
105pub use variable_length_expand::{PathMode as ExecutionPathMode, VariableLengthExpandOperator};
106pub use vector_join::VectorJoinOperator;
107
108use std::sync::Arc;
109
110use grafeo_common::types::{EdgeId, NodeId, TransactionId};
111use thiserror::Error;
112
113use super::DataChunk;
114use super::chunk_state::ChunkState;
115use super::factorized_chunk::FactorizedChunk;
116
117pub trait WriteTracker: Send + Sync {
123 fn record_node_write(&self, transaction_id: TransactionId, node_id: NodeId);
125
126 fn record_edge_write(&self, transaction_id: TransactionId, edge_id: EdgeId);
128}
129
130pub type SharedWriteTracker = Arc<dyn WriteTracker>;
132
133pub type OperatorResult = Result<Option<DataChunk>, OperatorError>;
135
136pub trait FactorizedData: Send + Sync {
164 fn chunk_state(&self) -> &ChunkState;
166
167 fn logical_row_count(&self) -> usize;
169
170 fn physical_size(&self) -> usize;
172
173 fn is_factorized(&self) -> bool;
175
176 fn flatten(&self) -> DataChunk;
178
179 fn as_factorized(&self) -> Option<&FactorizedChunk>;
181
182 fn as_flat(&self) -> Option<&DataChunk>;
184}
185
186pub struct FlatDataWrapper {
190 chunk: DataChunk,
191 state: ChunkState,
192}
193
194impl FlatDataWrapper {
195 #[must_use]
197 pub fn new(chunk: DataChunk) -> Self {
198 let state = ChunkState::flat(chunk.row_count());
199 Self { chunk, state }
200 }
201
202 #[must_use]
204 pub fn into_inner(self) -> DataChunk {
205 self.chunk
206 }
207}
208
209impl FactorizedData for FlatDataWrapper {
210 fn chunk_state(&self) -> &ChunkState {
211 &self.state
212 }
213
214 fn logical_row_count(&self) -> usize {
215 self.chunk.row_count()
216 }
217
218 fn physical_size(&self) -> usize {
219 self.chunk.row_count() * self.chunk.column_count()
220 }
221
222 fn is_factorized(&self) -> bool {
223 false
224 }
225
226 fn flatten(&self) -> DataChunk {
227 self.chunk.clone()
228 }
229
230 fn as_factorized(&self) -> Option<&FactorizedChunk> {
231 None
232 }
233
234 fn as_flat(&self) -> Option<&DataChunk> {
235 Some(&self.chunk)
236 }
237}
238
239#[derive(Error, Debug, Clone)]
241pub enum OperatorError {
242 #[error("type mismatch: expected {expected}, found {found}")]
244 TypeMismatch {
245 expected: String,
247 found: String,
249 },
250 #[error("column not found: {0}")]
252 ColumnNotFound(String),
253 #[error("execution error: {0}")]
255 Execution(String),
256 #[error("constraint violation: {0}")]
258 ConstraintViolation(String),
259}
260
261pub trait Operator: Send + Sync {
266 fn next(&mut self) -> OperatorResult;
268
269 fn reset(&mut self);
271
272 fn name(&self) -> &'static str;
274}
275
276#[cfg(test)]
277mod tests {
278 use super::*;
279 use crate::execution::vector::ValueVector;
280 use grafeo_common::types::LogicalType;
281
282 fn create_test_chunk() -> DataChunk {
283 let mut col = ValueVector::with_type(LogicalType::Int64);
284 col.push_int64(1);
285 col.push_int64(2);
286 col.push_int64(3);
287 DataChunk::new(vec![col])
288 }
289
290 #[test]
291 fn test_flat_data_wrapper_new() {
292 let chunk = create_test_chunk();
293 let wrapper = FlatDataWrapper::new(chunk);
294
295 assert!(!wrapper.is_factorized());
296 assert_eq!(wrapper.logical_row_count(), 3);
297 }
298
299 #[test]
300 fn test_flat_data_wrapper_into_inner() {
301 let chunk = create_test_chunk();
302 let wrapper = FlatDataWrapper::new(chunk);
303
304 let inner = wrapper.into_inner();
305 assert_eq!(inner.row_count(), 3);
306 }
307
308 #[test]
309 fn test_flat_data_wrapper_chunk_state() {
310 let chunk = create_test_chunk();
311 let wrapper = FlatDataWrapper::new(chunk);
312
313 let state = wrapper.chunk_state();
314 assert!(state.is_flat());
315 assert_eq!(state.logical_row_count(), 3);
316 }
317
318 #[test]
319 fn test_flat_data_wrapper_physical_size() {
320 let mut col1 = ValueVector::with_type(LogicalType::Int64);
321 col1.push_int64(1);
322 col1.push_int64(2);
323
324 let mut col2 = ValueVector::with_type(LogicalType::String);
325 col2.push_string("a");
326 col2.push_string("b");
327
328 let chunk = DataChunk::new(vec![col1, col2]);
329 let wrapper = FlatDataWrapper::new(chunk);
330
331 assert_eq!(wrapper.physical_size(), 4);
333 }
334
335 #[test]
336 fn test_flat_data_wrapper_flatten() {
337 let chunk = create_test_chunk();
338 let wrapper = FlatDataWrapper::new(chunk);
339
340 let flattened = wrapper.flatten();
341 assert_eq!(flattened.row_count(), 3);
342 assert_eq!(flattened.column(0).unwrap().get_int64(0), Some(1));
343 }
344
345 #[test]
346 fn test_flat_data_wrapper_as_factorized() {
347 let chunk = create_test_chunk();
348 let wrapper = FlatDataWrapper::new(chunk);
349
350 assert!(wrapper.as_factorized().is_none());
351 }
352
353 #[test]
354 fn test_flat_data_wrapper_as_flat() {
355 let chunk = create_test_chunk();
356 let wrapper = FlatDataWrapper::new(chunk);
357
358 let flat = wrapper.as_flat();
359 assert!(flat.is_some());
360 assert_eq!(flat.unwrap().row_count(), 3);
361 }
362
363 #[test]
364 fn test_operator_error_type_mismatch() {
365 let err = OperatorError::TypeMismatch {
366 expected: "Int64".to_string(),
367 found: "String".to_string(),
368 };
369
370 let msg = format!("{err}");
371 assert!(msg.contains("type mismatch"));
372 assert!(msg.contains("Int64"));
373 assert!(msg.contains("String"));
374 }
375
376 #[test]
377 fn test_operator_error_column_not_found() {
378 let err = OperatorError::ColumnNotFound("missing_col".to_string());
379
380 let msg = format!("{err}");
381 assert!(msg.contains("column not found"));
382 assert!(msg.contains("missing_col"));
383 }
384
385 #[test]
386 fn test_operator_error_execution() {
387 let err = OperatorError::Execution("something went wrong".to_string());
388
389 let msg = format!("{err}");
390 assert!(msg.contains("execution error"));
391 assert!(msg.contains("something went wrong"));
392 }
393
394 #[test]
395 fn test_operator_error_debug() {
396 let err = OperatorError::TypeMismatch {
397 expected: "Int64".to_string(),
398 found: "String".to_string(),
399 };
400
401 let debug = format!("{err:?}");
402 assert!(debug.contains("TypeMismatch"));
403 }
404
405 #[test]
406 fn test_operator_error_clone() {
407 let err1 = OperatorError::ColumnNotFound("col".to_string());
408 let err2 = err1.clone();
409
410 assert_eq!(format!("{err1}"), format!("{err2}"));
411 }
412}