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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use std::collections::HashMap;
use std::sync::Arc;

use parking_lot::RwLock;

use crate::executor::expression::evaluator::traits::ExpressionContext;
use crate::executor::expression::evaluator::ExpressionEvaluator;
use crate::executor::streaming::chunk::DataChunk;
use crate::executor::streaming::context::ValueRowContext;
use crate::executor::streaming::executor::StreamingExecutor;
use crate::executor::streaming::operators::source_operator::OperatorConfig;
use crate::executor::streaming::runtime::ExecutionRuntime;
use crate::executor::streaming::slot::SlotLayout;
use crate::storage::{QueryStorage, StorageWriter};
use linkrs_core::error::QueryError;
use linkrs_core::types::expr::Expression;
use linkrs_core::types::storage_ids::VertexId;
use linkrs_core::vertex_edge_path::{Edge, Tag, Vertex};
use linkrs_core::Value;

mod copy;
mod delete;
mod insert;
mod update;

#[derive(Debug)]
pub enum SinkOperatorKind {
    CopyFrom {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        target: crate::executor::streaming::operators::spec::CopyTarget,
        file_paths: Vec<String>,
        by_column: bool,
        header: bool,
        delimiter: u8,
        batch_size: usize,
        rows_inserted: u64,
        summary_returned: bool,
    },
    CopyTo {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        target: crate::executor::streaming::operators::spec::CopyTarget,
        file_path: String,
        header: bool,
        delimiter: u8,
        rows_exported: u64,
        summary_returned: bool,
    },
    InsertVertices {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        vertex_properties: Vec<(String, Expression)>,
        tag: String,
        tag_property_names: Vec<String>,
        if_not_exists: bool,
        rows_inserted: u64,
        summary_returned: bool,
    },
    InsertEdges {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        src_col: String,
        dst_col: String,
        rank_col: String,
        edge_type: String,
        edge_properties: Vec<(String, Expression)>,
        if_not_exists: bool,
        rows_inserted: u64,
        summary_returned: bool,
    },
    UpdateVertices {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        tag_name: String,
        updates: Vec<(String, Expression)>,
        condition: Option<Expression>,
        is_upsert: bool,
        replace_properties: bool,
        rows_updated: u64,
        summary_returned: bool,
    },
    UpdateEdges {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        src_col: String,
        dst_col: String,
        rank_col: String,
        edge_type: String,
        updates: Vec<(String, Expression)>,
        condition: Option<Expression>,
        is_upsert: bool,
        replace_properties: bool,
        rows_updated: u64,
        summary_returned: bool,
    },
    DeleteVertices {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        tag: String,
        vertex_id_col: String,
        cascade: bool,
        rows_deleted: u64,
        summary_returned: bool,
    },
    DeleteEdges {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        src_col: String,
        dst_col: String,
        rank_col: String,
        edge_type: String,
        rows_deleted: u64,
        summary_returned: bool,
    },
    PipeDeleteVertices {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        vertex_id_col: String,
        cascade: bool,
        vertex_id_is_literal: bool,
        rows_deleted: u64,
        summary_returned: bool,
    },
    PipeDeleteEdges {
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        space_name: String,
        src_col: String,
        dst_col: String,
        rank_col: String,
        edge_type: String,
        rows_deleted: u64,
        summary_returned: bool,
    },
}

/// Sink operator.
///
/// Wraps [`SinkOperatorKind`] with the runtime context injected at `open()`.
/// Lifecycle state is owned exclusively by the executor; operators never
/// write it.
#[derive(Debug)]
pub struct SinkOperator {
    pub kind: SinkOperatorKind,
    pub runtime: Option<Arc<ExecutionRuntime>>,
    pub output_layout: Arc<SlotLayout>,
    pub config: OperatorConfig,
}

pub(super) fn make_modify_result(
    output_layout: Arc<SlotLayout>,
    op: &str,
    count: u64,
) -> DataChunk {
    let row = vec![Value::string(op), Value::BigInt(count as i64)];
    DataChunk::new_with_layout(vec![row], output_layout)
}

pub(super) fn eval_expr(
    expr: &Expression,
    context: &mut ValueRowContext,
) -> Result<Value, QueryError> {
    ExpressionEvaluator::evaluate(expr, context).map_err(|e| QueryError::execution(e.to_string()))
}

pub(super) fn eval_update_props(
    updates: &[(String, Expression)],
    replace_properties: bool,
    context: &mut ValueRowContext,
) -> Result<HashMap<Arc<str>, Value>, QueryError> {
    if !replace_properties {
        let mut props = HashMap::new();
        for (prop_name, expr) in updates.iter() {
            let val = eval_expr(expr, context)?;
            props.insert(Arc::from(prop_name.as_str()), val);
        }
        return Ok(props);
    }
    let mut props = HashMap::new();
    let mut saw_map = false;
    for (prop_name, expr) in updates.iter() {
        let val = eval_expr(expr, context)?;
        match val {
            Value::Map(entries) => {
                saw_map = true;
                for (key, item) in entries.iter() {
                    match key {
                        Value::String(name) => {
                            props.insert(Arc::from(name.as_str()), item.clone());
                        }
                        Value::FixedString(name) => {
                            props.insert(Arc::from(name.as_str()), item.clone());
                        }
                        _ => {
                            return Err(QueryError::execution(
                                "Map overwrite keys must be strings".to_string(),
                            ));
                        }
                    }
                }
            }
            other => {
                props.insert(Arc::from(prop_name.as_str()), other);
            }
        }
    }
    if !saw_map {
        return Err(QueryError::execution(
            "Map overwrite value must be a map".to_string(),
        ));
    }
    Ok(props)
}

/// Build a row context that resolves `$name` parameter references.
///
/// Sink operators evaluate shape-normalized DML expressions (`$__dml_N`
/// placeholders), so the context must carry the runtime parameter values —
/// otherwise parameter resolution fails with "Undefined parameter".
pub(super) fn row_context(
    row: Vec<Value>,
    layout: Arc<SlotLayout>,
    params: Option<Arc<HashMap<String, Value>>>,
) -> ValueRowContext {
    match params {
        Some(parameters) => ValueRowContext::with_parameters(row, layout, parameters),
        None => ValueRowContext::new(row, layout),
    }
}

/// Row predicate semantics for update conditions (`WHEN`/`WHERE`), matching
/// the filter operator: false/null/zero/empty reject the row.
pub(super) fn condition_matches(value: &Value) -> bool {
    match value {
        Value::Bool(b) => *b,
        Value::Null(_) => false,
        Value::Int(i) => *i != 0,
        Value::SmallInt(i) => *i != 0,
        Value::BigInt(i) => *i != 0,
        Value::Float(f) => *f != 0.0,
        Value::Double(f) => *f != 0.0,
        Value::String(s) => !s.is_empty(),
        _ => true,
    }
}

/// Resolve edge endpoints from row values.
///
/// When the row carries an `Edge` value (e.g. `MATCH ... DELETE EDGE e`), the
/// endpoints are taken from the edge itself; otherwise both values are
/// converted to vertex ids directly.
///
/// Whether a write-path error message indicates a transaction conflict
/// (write-write conflict, rollback-only transaction).
///
/// Storage errors are flattened to strings at the operator boundary; the
/// storage layer's typed `StorageErrorKind::Conflict` renders as "conflict"
/// / "Write-write conflict", which this classifier recognizes.
fn is_transaction_conflict_message(message: &str) -> bool {
    let lowered = message.to_ascii_lowercase();
    lowered.contains("conflict")
        || lowered.contains("rollback-only")
        || lowered.contains("rollback_only")
}

pub(super) fn resolve_edge_endpoints(
    src_val: &Value,
    dst_val: &Value,
) -> Option<(VertexId, VertexId)> {
    match (src_val, dst_val) {
        (Value::Edge(edge), _) => Some((edge.src, edge.dst)),
        (_, Value::Edge(edge)) => Some((edge.src, edge.dst)),
        (Value::EdgeHeader(header), _) => Some((header.src, header.dst)),
        (_, Value::EdgeHeader(header)) => Some((header.src, header.dst)),
        _ => {
            let src = VertexId::try_from(src_val).ok()?;
            let dst = VertexId::try_from(dst_val).ok()?;
            Some((src, dst))
        }
    }
}

/// Resolve the edge rank for a write row.
///
/// A materialized `rank` column wins; otherwise the rank carried by an edge
/// value in the row is used; otherwise the rank defaults to 0.
pub(super) fn resolve_edge_rank(
    context: &ValueRowContext,
    rank_col: &str,
    src_val: &Value,
    dst_val: &Value,
) -> Result<i64, QueryError> {
    if let Some(rank_val) = context.get_variable(rank_col) {
        if !matches!(rank_val, Value::Null(_)) {
            return match &rank_val {
                Value::SmallInt(i) => Ok(*i as i64),
                Value::Int(i) => Ok(*i as i64),
                Value::BigInt(i) => Ok(*i),
                _ => Err(QueryError::execution(format!(
                    "Invalid edge rank: {:?}",
                    rank_val
                ))),
            };
        }
    }
    match (src_val, dst_val) {
        (Value::Edge(edge), _) => Ok(edge.ranking),
        (_, Value::Edge(edge)) => Ok(edge.ranking),
        (Value::EdgeHeader(header), _) => Ok(header.ranking),
        (_, Value::EdgeHeader(header)) => Ok(header.ranking),
        _ => Ok(0),
    }
}

impl SinkOperator {
    pub fn from_spec(
        spec: &super::spec::SinkSpec,
        storage: Option<Arc<RwLock<dyn QueryStorage>>>,
        output_layout: Arc<SlotLayout>,
    ) -> Self {
        let kind = match spec {
            super::spec::SinkSpec::CopyFrom {
                space_name,
                target,
                file_paths,
                by_column,
                header,
                delimiter,
                batch_size,
            } => SinkOperatorKind::CopyFrom {
                storage,
                space_name: space_name.clone(),
                target: target.clone(),
                file_paths: file_paths.clone(),
                by_column: *by_column,
                header: *header,
                delimiter: *delimiter,
                batch_size: *batch_size,
                rows_inserted: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::CopyTo {
                space_name,
                target,
                file_path,
                header,
                delimiter,
            } => SinkOperatorKind::CopyTo {
                storage,
                space_name: space_name.clone(),
                target: target.clone(),
                file_path: file_path.clone(),
                header: *header,
                delimiter: *delimiter,
                rows_exported: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::InsertVertices {
                space_name,
                vertex_properties,
                tag,
                tag_property_names,
                if_not_exists,
            } => SinkOperatorKind::InsertVertices {
                storage,
                space_name: space_name.clone(),
                vertex_properties: vertex_properties.clone(),
                tag: tag.clone(),
                tag_property_names: tag_property_names.clone(),
                if_not_exists: *if_not_exists,
                rows_inserted: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::InsertEdges {
                space_name,
                src_col,
                dst_col,
                rank_col,
                edge_type,
                edge_properties,
                if_not_exists,
            } => SinkOperatorKind::InsertEdges {
                storage,
                space_name: space_name.clone(),
                src_col: src_col.clone(),
                dst_col: dst_col.clone(),
                rank_col: rank_col.clone(),
                edge_type: edge_type.clone(),
                edge_properties: edge_properties.clone(),
                if_not_exists: *if_not_exists,
                rows_inserted: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::UpdateVertices {
                space_name,
                tag_name,
                updates,
                condition,
                is_upsert,
                replace_properties,
            } => SinkOperatorKind::UpdateVertices {
                storage,
                space_name: space_name.clone(),
                tag_name: tag_name.clone(),
                updates: updates.clone(),
                condition: condition.clone(),
                is_upsert: *is_upsert,
                replace_properties: *replace_properties,
                rows_updated: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::UpdateEdges {
                space_name,
                src_col,
                dst_col,
                rank_col,
                edge_type,
                updates,
                condition,
                is_upsert,
                replace_properties,
            } => SinkOperatorKind::UpdateEdges {
                storage,
                space_name: space_name.clone(),
                src_col: src_col.clone(),
                dst_col: dst_col.clone(),
                rank_col: rank_col.clone(),
                edge_type: edge_type.clone(),
                updates: updates.clone(),
                condition: condition.clone(),
                is_upsert: *is_upsert,
                replace_properties: *replace_properties,
                rows_updated: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::DeleteVertices {
                space_name,
                tag,
                vertex_id_col,
                cascade,
            } => SinkOperatorKind::DeleteVertices {
                storage,
                space_name: space_name.clone(),
                tag: tag.clone(),
                vertex_id_col: vertex_id_col.clone(),
                cascade: *cascade,
                rows_deleted: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::DeleteEdges {
                space_name,
                src_col,
                dst_col,
                rank_col,
                edge_type,
            } => SinkOperatorKind::DeleteEdges {
                storage,
                space_name: space_name.clone(),
                src_col: src_col.clone(),
                dst_col: dst_col.clone(),
                rank_col: rank_col.clone(),
                edge_type: edge_type.clone(),
                rows_deleted: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::PipeDeleteVertices {
                space_name,
                vertex_id_col,
                cascade,
                vertex_id_is_literal,
            } => SinkOperatorKind::PipeDeleteVertices {
                storage,
                space_name: space_name.clone(),
                vertex_id_col: vertex_id_col.clone(),
                cascade: *cascade,
                vertex_id_is_literal: *vertex_id_is_literal,
                rows_deleted: 0,
                summary_returned: false,
            },
            super::spec::SinkSpec::PipeDeleteEdges {
                space_name,
                src_col,
                dst_col,
                rank_col,
                edge_type,
            } => SinkOperatorKind::PipeDeleteEdges {
                storage,
                space_name: space_name.clone(),
                src_col: src_col.clone(),
                dst_col: dst_col.clone(),
                rank_col: rank_col.clone(),
                edge_type: edge_type.clone(),
                rows_deleted: 0,
                summary_returned: false,
            },
        };
        Self::new(kind, output_layout)
    }

    pub fn new(kind: SinkOperatorKind, output_layout: Arc<SlotLayout>) -> Self {
        Self {
            kind,
            runtime: None,
            output_layout,
            config: OperatorConfig::default(),
        }
    }

    /// Inject the runtime and execution config (called once by the executor
    /// before this operator produces any data).
    pub fn inject_context(
        &mut self,
        runtime: Option<&Arc<ExecutionRuntime>>,
        config: OperatorConfig,
    ) {
        if let Some(rt) = runtime {
            self.runtime = Some(rt.clone());
        }
        self.config = config;
    }

    /// Check that the transaction scope allows writes.
    ///
    /// requires a transaction scope for DML operations.  Absent scope
    /// is rejected to prevent unbounded writes outside any transaction.
    fn check_write_permission(&self) -> Result<(), QueryError> {
        let rt = self.runtime.as_ref().ok_or_else(|| {
            QueryError::execution(
                "DML requires an execution runtime with transaction scope".to_string(),
            )
        })?;
        let scope = rt.transaction_scope().ok_or_else(|| {
            QueryError::execution(
                "DML requires a transaction scope — no transaction is active".to_string(),
            )
        })?;
        if !scope.allows_write() {
            return Err(QueryError::execution(
                "Write operation not allowed in current transaction scope".to_string(),
            ));
        }
        Ok(())
    }

    pub fn open(&mut self, input: &mut StreamingExecutor) -> Result<(), QueryError> {
        self.check_write_permission()?;
        match &mut self.kind {
            SinkOperatorKind::CopyFrom { .. }
            | SinkOperatorKind::CopyTo { .. }
            | SinkOperatorKind::InsertVertices { .. }
            | SinkOperatorKind::InsertEdges { .. }
            | SinkOperatorKind::UpdateVertices { .. }
            | SinkOperatorKind::UpdateEdges { .. }
            | SinkOperatorKind::DeleteVertices { .. }
            | SinkOperatorKind::DeleteEdges { .. }
            | SinkOperatorKind::PipeDeleteVertices { .. }
            | SinkOperatorKind::PipeDeleteEdges { .. } => {
                input.open()?;
                Ok(())
            }
        }
    }

    pub fn next(&mut self, input: &mut StreamingExecutor) -> Result<Option<DataChunk>, QueryError> {
        let result = self.next_inner(input);
        if let Err(error) = &result {
            // Write-conflict linkage: a conflict-classified failure (write-write
            // conflict, rollback-only transaction) cancels the remaining pipeline
            // stages of the same transaction instead of letting them compute on.
            if is_transaction_conflict_message(&error.to_string()) {
                if let Some(rt) = self.runtime.as_ref() {
                    rt.note_transaction_conflict();
                }
            }
        }
        result
    }

    fn next_inner(
        &mut self,
        input: &mut StreamingExecutor,
    ) -> Result<Option<DataChunk>, QueryError> {
        if matches!(&self.kind, SinkOperatorKind::CopyFrom { .. }) {
            return copy::handle_copy_from(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::CopyTo { .. }) {
            return copy::handle_copy_to(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::InsertVertices { .. }) {
            return insert::handle_insert_vertices(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::InsertEdges { .. }) {
            return insert::handle_insert_edges(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::UpdateVertices { .. }) {
            return update::handle_update_vertices(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::UpdateEdges { .. }) {
            return update::handle_update_edges(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::DeleteVertices { .. }) {
            return delete::handle_delete_vertices(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::DeleteEdges { .. }) {
            return delete::handle_delete_edges(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::PipeDeleteVertices { .. }) {
            return delete::handle_pipe_delete_vertices(self, input);
        }
        if matches!(&self.kind, SinkOperatorKind::PipeDeleteEdges { .. }) {
            return delete::handle_pipe_delete_edges(self, input);
        }
        unreachable!("sink_operator::next_inner called for an unknown kind")
    }

    pub fn stop(&mut self) -> Result<(), QueryError> {
        Ok(())
    }

    pub fn close(&mut self) -> Result<(), QueryError> {
        Ok(())
    }
}