linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! OperatorState: Mutable per-execution state for operator nodes.
//!
//! Created fresh for each query execution from an immutable
//! [`OperatorSpec`](super::spec::OperatorSpec).  Holds cursors,
//! buffers, hash tables, row iterators, and all other mutable data that
//! must NOT be shared across concurrent executions of the same cached plan.

use std::collections::HashSet;

use crate::executor::base::MemoryTracker;
use crate::storage::{EdgeCursor, IndexCursor, ShardSlice, VertexCursor};
use linkrs_core::types::expr::Expression;
use linkrs_core::Value;

use super::super::chunk::DataChunk;
use super::super::executor::SortDirection;
use super::source_operator::NeighborScanState;
use super::spec::SourceSpec;

// ── Source state ─────────────────────────────────────────────────────────────

/// Mutable execution state for source operators.
#[derive(Debug)]
pub enum SourceState {
    ScanVertices {
        current_index: usize,
        col_names: Vec<String>,
    },
    StorageScanVertices {
        partition_id: usize,
        partition_range: Option<std::ops::Range<i64>>,
        cursor: Option<Box<dyn VertexCursor>>,
        buffer: Vec<Vec<Value>>,
        current_index: usize,
        col_names: Vec<String>,
    },
    ScanEdges {
        current_index: usize,
        col_names: Vec<String>,
    },
    StorageScanEdges {
        partition_id: usize,
        shard_slice: Option<ShardSlice>,
        cursor: Option<Box<dyn EdgeCursor>>,
        buffer: Vec<Vec<Value>>,
        current_index: usize,
        col_names: Vec<String>,
    },
    GetVertices {
        position: usize,
    },
    GetEdges {
        cursor: Option<Box<dyn EdgeCursor>>,
    },
    GetNeighbors {
        state: NeighborScanState,
    },
    IndexScan {
        cursor: Option<Box<dyn IndexCursor<Row = Value>>>,
    },
    Argument,
    GetProp {
        entity_slot: usize,
        prop_names: Vec<String>,
    },
    Start {
        emitted: bool,
    },
}

impl SourceState {
    /// Create a fresh state for the given spec.
    pub fn from_spec(spec: &SourceSpec) -> Self {
        match spec {
            SourceSpec::ScanVertices { rows: _, col_names } => SourceState::ScanVertices {
                current_index: 0,
                col_names: col_names.clone(),
            },
            SourceSpec::StandaloneValues { col_names, .. } => SourceState::ScanVertices {
                current_index: 0,
                col_names: col_names.clone(),
            },
            SourceSpec::StorageScanVertices { col_names, .. } => SourceState::StorageScanVertices {
                partition_id: 0,
                partition_range: None,
                cursor: None,
                buffer: Vec::new(),
                current_index: 0,
                col_names: col_names.clone(),
            },
            SourceSpec::ScanEdges { rows: _, col_names } => SourceState::ScanEdges {
                current_index: 0,
                col_names: col_names.clone(),
            },
            SourceSpec::StorageScanEdges { col_names, .. } => SourceState::StorageScanEdges {
                partition_id: 0,
                shard_slice: None,
                cursor: None,
                buffer: Vec::new(),
                current_index: 0,
                col_names: col_names.clone(),
            },
            SourceSpec::GetVertices { .. } => SourceState::GetVertices { position: 0 },
            SourceSpec::GetEdges { .. } => SourceState::GetEdges { cursor: None },
            SourceSpec::GetNeighbors { .. } => SourceState::GetNeighbors {
                state: NeighborScanState::Init,
            },
            SourceSpec::IndexScan { .. } => SourceState::IndexScan { cursor: None },
            SourceSpec::Argument { .. } => SourceState::Argument,
            // CTE scans buffer rows in the operator kind itself; the arena
            // state only records scan identity for diagnostics.
            SourceSpec::CteScan { col_names, .. } => SourceState::ScanVertices {
                current_index: 0,
                col_names: col_names.clone(),
            },
            SourceSpec::GetProp {
                entity_slot,
                prop_names,
                ..
            } => SourceState::GetProp {
                entity_slot: *entity_slot,
                prop_names: prop_names.clone(),
            },
            SourceSpec::Start => SourceState::Start { emitted: false },
        }
    }
}

// ── Blocking state ───────────────────────────────────────────────────────────
pub use super::blocking::{
    AggregateState, DataCollectState, DistinctState, FinalAggregateState, GroupByState,
    MaterializeState, PartialAggregateState, RollUpApplyState, SortState, TopNState,
    WindowFunctionState, WindowState,
};

/// Mutable execution state for blocking operators.
#[derive(Debug)]
pub enum BlockingState {
    Sort {
        memory_tracker: MemoryTracker,
        state: Option<SortState>,
    },
    Aggregate {
        memory_tracker: MemoryTracker,
        state: Option<AggregateState>,
    },
    GroupBy {
        memory_tracker: MemoryTracker,
        state: Option<GroupByState>,
    },
    WindowFunction {
        memory_tracker: MemoryTracker,
        state: Option<WindowFunctionState>,
    },
    Window {
        memory_tracker: MemoryTracker,
        state: Option<WindowState>,
    },
    TopN {
        memory_tracker: MemoryTracker,
        state: Option<TopNState>,
    },
    Distinct {
        memory_tracker: MemoryTracker,
        state: Option<DistinctState>,
    },
    Materialize {
        memory_tracker: MemoryTracker,
        state: Option<MaterializeState>,
    },
    DataCollect {
        memory_tracker: MemoryTracker,
        state: Option<DataCollectState>,
    },
    RollUpApply {
        memory_tracker: MemoryTracker,
        state: Option<RollUpApplyState>,
    },
    PartialAggregate {
        memory_tracker: MemoryTracker,
        state: Option<PartialAggregateState>,
    },
    FinalAggregate {
        memory_tracker: MemoryTracker,
        state: Option<FinalAggregateState>,
    },
}

// ── Join state ───────────────────────────────────────────────────────────────

/// Mutable execution state for join operators.
#[derive(Debug)]
pub enum JoinState {
    HashJoin {
        build_side: super::join_operator::HashJoinBuildSide,
        left_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    HashLeftJoin {
        build_side: super::join_operator::HashJoinBuildSide,
        left_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    NestedLoopJoin {
        build_side_tuples: Vec<Vec<Value>>,
        left_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    InnerJoin {
        build_side_tuples: Vec<Vec<Value>>,
        left_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    LeftJoin {
        build_side_tuples: Vec<Vec<Value>>,
        left_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    RightJoin {
        build_side_tuples: Vec<Vec<Value>>,
        right_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    FullOuterJoin {
        left_rows: Vec<Vec<Value>>,
        right_rows: Vec<Vec<Value>>,
        matched_right_indices: HashSet<usize>,
        result_iter: Option<std::vec::IntoIter<Vec<Value>>>,
        phase: super::super::executor::FullOuterJoinPhase,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    CrossJoin {
        all_left_rows: Vec<Vec<Value>>,
        all_right_rows: Vec<Vec<Value>>,
        left_consumed: bool,
        right_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
    SemiJoin {
        right_rows: Vec<Vec<Value>>,
        right_consumed: bool,
        memory_tracker: MemoryTracker,
        right_col_names: Vec<String>,
    },
}

// ── Graph state ──────────────────────────────────────────────────────────────

/// Mutable execution state for graph traversal operators.
#[derive(Debug)]
pub enum GraphState {
    Expand,
    ExpandAll,
    Traverse {
        visited: HashSet<String>,
    },
    BiExpand,
    BiTraverse {
        visited: HashSet<String>,
    },
    ShortestPath,
    BFSShortest {
        frontier: Vec<Vec<Value>>,
        visited: HashSet<String>,
    },
    AllPaths {
        all_paths: Vec<Vec<Value>>,
        result_iter: Option<std::vec::IntoIter<Vec<Value>>>,
    },
    MultiShortestPath {
        all_paths: Vec<Vec<Value>>,
        result_iter: Option<std::vec::IntoIter<Vec<Value>>>,
    },
}

// ── Sink state ───────────────────────────────────────────────────────────────

/// Mutable execution state for sink (data modification) operators.
#[derive(Debug)]
pub enum SinkState {
    InsertVertices { rows_inserted: u64 },
    InsertEdges { rows_inserted: u64 },
    UpdateVertices { rows_updated: u64 },
    UpdateEdges { rows_updated: u64 },
    DeleteVertices { rows_deleted: u64 },
    DeleteEdges { rows_deleted: u64 },
    PipeDeleteVertices { rows_deleted: u64 },
    PipeDeleteEdges { rows_deleted: u64 },
}

// ── Exchange state ───────────────────────────────────────────────────────────

/// Mutable execution state for exchange (gather / merge / repartition) operators.
#[derive(Debug)]
pub enum ExchangeState {
    Concatenate {
        current_index: usize,
        col_names: Option<Vec<String>>,
    },
    MergeSort {
        sort_expressions: Vec<Expression>,
        sort_directions: Vec<SortDirection>,
        inputs: Vec<MergeInputState>,
        col_names: Option<Vec<String>>,
        limit: Option<usize>,
        emitted: usize,
    },
    /// Hash-based repartition: buffer input rows, rehash by key, route to
    /// output buckets.  Each bucket is pulled sequentially.
    RepartitionHash {
        /// Number of output buckets / partitions.
        num_partitions: usize,
        /// Buffered input rows for each partition bucket.
        /// Indexed by hash(keys) % num_partitions.
        buckets: Vec<Vec<Vec<Value>>>,
        /// Current bucket being drained.
        current_bucket: usize,
        /// Current row index within `current_bucket`.
        current_row: usize,
        /// Hash expressions (from spec, cloned for state).
        hash_expressions: Vec<Expression>,
        /// Column names of the data flowing through.
        col_names: Option<Vec<String>>,
    },
    /// Broadcast: replicates every input chunk across N output channels.
    Broadcast {
        /// Number of consumers / output channels.
        num_consumers: usize,
        /// Buffered chunks from upstream (all consumed before broadcast).
        buffered_chunks: Vec<DataChunk>,
        /// Current position within the broadcast output sequence.
        /// Cycles through consumers for each row/chunk.
        current_consumer: usize,
        /// Current chunk index being broadcast.
        chunk_index: usize,
        /// Next row index within the current chunk.
        row_index: usize,
    },
    /// Barrier: collect input-fragment completion signals, then pass through.
    Barrier {
        /// Whether the barrier has been passed.
        passed: bool,
    },
    /// Materialize: fully consume child output before producing it.
    Materialize {
        /// All rows collected from children.
        rows: Vec<Vec<Value>>,
        /// Current position in the materialized output.
        position: usize,
        /// Column names.
        col_names: Option<Vec<String>>,
    },
}

/// Internal merge cursor state for Exchange merge-sort.
#[derive(Debug)]
pub enum MergeInputState {
    Pending,
    Buffered { chunk: DataChunk, row_index: usize },
    Exhausted,
}

// ── Set state ────────────────────────────────────────────────────────────────

/// Mutable execution state for set operators.
#[derive(Debug)]
pub enum SetState {
    Union {
        seen_rows: HashSet<String>,
        left_consumed: bool,
        memory_tracker: MemoryTracker,
    },
    UnionAll {
        left_consumed: bool,
    },
    Intersect {
        left_rows: Vec<Vec<Value>>,
        right_rows: HashSet<String>,
        left_buffered: bool,
        right_buffered: bool,
        memory_tracker: MemoryTracker,
    },
    Except {
        exclude_rows: HashSet<String>,
        right_buffered: bool,
        memory_tracker: MemoryTracker,
    },
    Minus {
        exclude_rows: HashSet<String>,
        right_buffered: bool,
        memory_tracker: MemoryTracker,
    },
}

// ── Apply state ──────────────────────────────────────────────────────────────

/// Mutable execution state for apply operators.
#[derive(Debug)]
pub enum ApplyState {
    Apply,
    PatternApply {
        all_rows: Vec<Vec<Value>>,
        result_iter: Option<std::vec::IntoIter<Vec<Value>>>,
        memory_tracker: MemoryTracker,
    },
    RollUpApply,
}

// ── DDL state ────────────────────────────────────────────────────────────────

/// Mutable execution state for DDL operators (minimal — DDL has no cursor/accumulator).
#[derive(Debug)]
pub enum DdlState {
    SpaceManage,
    TagManage,
    EdgeManage,
    IndexManage,
    UserManage,
    ShowStats,
    ShowConfigs,
    ShowQueries,
    ShowSessions,
    ShowFunctions,
    ShowGraphs,
    ShowMacros,
    ShowAttachedDatabases,
    ShowExtensions,
    LoadFrom,
    InQueryCall,
    Analyze,
    Migrate,
    MigratePlan,
    MigrateRun,
    MigrateRollback,
    SequenceManage,
    MacroManage,
    TypeManage,
}
impl DdlState {
    pub fn from_spec(spec: &super::spec::DdlSpec) -> Self {
        match spec {
            super::spec::DdlSpec::SpaceManage { .. } => DdlState::SpaceManage,
            super::spec::DdlSpec::TagManage { .. } => DdlState::TagManage,
            super::spec::DdlSpec::EdgeManage { .. } => DdlState::EdgeManage,
            super::spec::DdlSpec::IndexManage { .. } => DdlState::IndexManage,
            super::spec::DdlSpec::DeleteIndex { .. } => DdlState::IndexManage,
            super::spec::DdlSpec::UserManage { .. } => DdlState::UserManage,
            super::spec::DdlSpec::ShowStats { .. } => DdlState::ShowStats,
            super::spec::DdlSpec::ShowConfigs { .. } => DdlState::ShowConfigs,
            super::spec::DdlSpec::ShowQueries { .. } => DdlState::ShowQueries,
            super::spec::DdlSpec::ShowSessions { .. } => DdlState::ShowSessions,
            super::spec::DdlSpec::ShowFunctions { .. } => DdlState::ShowFunctions,
            super::spec::DdlSpec::ShowGraphs { .. } => DdlState::ShowGraphs,
            super::spec::DdlSpec::ShowMacros { .. } => DdlState::ShowMacros,
            super::spec::DdlSpec::ShowAttachedDatabases { .. } => DdlState::ShowAttachedDatabases,
            super::spec::DdlSpec::ShowExtensions { .. } => DdlState::ShowExtensions,
            super::spec::DdlSpec::LoadFrom { .. } => DdlState::LoadFrom,
            super::spec::DdlSpec::InQueryCall { .. } => DdlState::InQueryCall,
            super::spec::DdlSpec::Analyze { .. } => DdlState::Analyze,
            super::spec::DdlSpec::Migrate { .. } => DdlState::Migrate,
            super::spec::DdlSpec::MigratePlan { .. } => DdlState::MigratePlan,
            super::spec::DdlSpec::MigrateRun { .. } => DdlState::MigrateRun,
            super::spec::DdlSpec::MigrateRollback { .. } => DdlState::MigrateRollback,
            super::spec::DdlSpec::SequenceManage { .. } => DdlState::SequenceManage,
            super::spec::DdlSpec::MacroManage { .. } => DdlState::MacroManage,
            super::spec::DdlSpec::TypeManage { .. } => DdlState::TypeManage,
        }
    }
}

// ── Fulltext state ───────────────────────────────────────────────────────────

/// Mutable execution state for fulltext operators.
#[derive(Debug)]
pub enum FulltextState {
    FulltextManage,
    FulltextSearch,
    FulltextLookup,
    MatchFulltext,
}

impl FulltextState {
    pub fn from_spec(spec: &super::spec::FulltextSpec) -> Self {
        match spec {
            super::spec::FulltextSpec::FulltextManage { .. } => FulltextState::FulltextManage,
            super::spec::FulltextSpec::FulltextSearch { .. } => FulltextState::FulltextSearch,
            super::spec::FulltextSpec::FulltextLookup { .. } => FulltextState::FulltextLookup,
            super::spec::FulltextSpec::MatchFulltext { .. } => FulltextState::MatchFulltext,
        }
    }
}

// ── Vector state ─────────────────────────────────────────────────────────────

/// Mutable execution state for vector search operators.
#[derive(Debug)]
pub enum VectorState {
    VectorManage,
    VectorSearch,
    VectorLookup,
    VectorMatch,
}

impl VectorState {
    pub fn from_spec(spec: &super::spec::VectorSpec) -> Self {
        match spec {
            super::spec::VectorSpec::VectorManage { .. } => VectorState::VectorManage,
            super::spec::VectorSpec::VectorSearch { .. } => VectorState::VectorSearch,
            super::spec::VectorSpec::VectorLookup { .. } => VectorState::VectorLookup,
            super::spec::VectorSpec::VectorMatch { .. } => VectorState::VectorMatch,
        }
    }
}

// ── RecursiveFragment state ─────────────────────────────────────────────

/// Mutable execution state for recursive fragment operators.
#[derive(Debug)]
pub enum RecursiveFragmentState {
    ShortestPath,
    MultiShortestPath,
    BFSShortest,
    AllPaths,
    Fixpoint,
}

impl RecursiveFragmentState {
    pub fn from_spec(spec: &super::spec::RecursiveFragmentSpec) -> Self {
        match spec {
            super::spec::RecursiveFragmentSpec::ShortestPath { .. } => {
                RecursiveFragmentState::ShortestPath
            }
            super::spec::RecursiveFragmentSpec::MultiShortestPath { .. } => {
                RecursiveFragmentState::MultiShortestPath
            }
            super::spec::RecursiveFragmentSpec::BFSShortest { .. } => {
                RecursiveFragmentState::BFSShortest
            }
            super::spec::RecursiveFragmentSpec::AllPaths { .. } => RecursiveFragmentState::AllPaths,
            super::spec::RecursiveFragmentSpec::Fixpoint { .. } => RecursiveFragmentState::Fixpoint,
        }
    }
}

// ── Txn state ────────────────────────────────────────────────────────────────

/// Mutable execution state for transaction operators.
#[derive(Debug)]
pub enum TxnState {
    BeginTransaction,
    Commit,
    Rollback,
    RollbackToSavepoint,
    Savepoint,
    ReleaseSavepoint,
}

impl TxnState {
    pub fn from_spec(spec: &super::spec::TxnSpec) -> Self {
        match spec {
            super::spec::TxnSpec::BeginTransaction => TxnState::BeginTransaction,
            super::spec::TxnSpec::Commit => TxnState::Commit,
            super::spec::TxnSpec::Rollback => TxnState::Rollback,
            super::spec::TxnSpec::RollbackToSavepoint { .. } => TxnState::RollbackToSavepoint,
            super::spec::TxnSpec::Savepoint { .. } => TxnState::Savepoint,
            super::spec::TxnSpec::ReleaseSavepoint { .. } => TxnState::ReleaseSavepoint,
        }
    }
}