nopaldb 0.4.35

High-performance graph database with ACID transactions, MVCC time-travel, and Arrow analytics
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
// src/query/nql/parser/ast.rs
//
// Abstract Syntax Tree for NQL v0.2
//
// Major changes from v0.1:
// - Statement enum wrapper (Query/Sketch/Commit/Delete/Update/Add)
// - HAVING clause support
// - Extended Projection (*, all(), expressions)
// - SortOrder enum (Asc/Desc)
// - SKETCH/COMMIT for conceptual operations
// - DELETE/UPDATE/ADD statements

use crate::types::PropertyValue;
use std::collections::HashMap;

// ═══════════════════════════════════════════════════════════════
// TOP-LEVEL STATEMENT
// ═══════════════════════════════════════════════════════════════

/// Top-level NQL statement
///
/// NQL v0.2 supports multiple statement types:
/// - Query: Read operations (FIND ... FROM ...)
/// - Sketch: Conceptual operations (preview before execution)
/// - Commit: Execute a previously defined sketch
/// - Delete: Remove nodes/edges from the graph
/// - Update: Modify properties of nodes/edges
/// - Create: Add new nodes/edges to the graph
#[derive(Debug, Clone, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum Statement {
    /// Read query (FIND ... FROM ...)
    Query(Query),

    /// Sketch definition (SKETCH name = statement)
    Sketch(SketchStmt),

    /// Commit a sketch (COMMIT sketch_name)
    Commit(CommitStmt),

    /// Delete nodes/edges (DELETE pattern WHERE ...)
    Delete(DeleteStmt),

    /// Update properties (UPDATE pattern SET ... WHERE ...)
    Update(UpdateStmt),

    /// Add nodes/edges (Add pattern)
    Add(AddStmt),

    CreateIndex(CreateIndexStmt),
    DropIndex(DropIndexStmt),
    Explain(Box<Statement>),
    Profile(Box<Statement>),
}

#[derive(Debug, Clone, PartialEq)]
pub struct CreateIndexStmt {
    pub label: String,
    pub property: String,
    pub index_type: IndexType,
}

#[derive(Debug, Clone, PartialEq)]
pub enum IndexType {
    Hash,
    BTree,
    FullText,
    Taxonomy,
}

#[derive(Debug, Clone, PartialEq)]
pub struct DropIndexStmt {
    pub index_name: String,
}

// ═══════════════════════════════════════════════════════════════
// QUERY (READ) - Extended from v0.1
// ═══════════════════════════════════════════════════════════════

/// Query statement (read-only)
///
/// Syntax:
/// ```nql
/// [EXPORT arrow | parquet "file.parquet"]
/// FIND <projections>
/// FROM <pattern>
/// [WHERE <condition>]
/// [GROUP BY <expressions>]
/// [HAVING <condition>]
/// [ORDER BY <expression> [ASC|DESC], ...]
/// [LIMIT <n> [OFFSET <m>]]
/// [AT <timestamp>]
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Query {
    pub export: Option<ExportClause>,
    pub find: FindClause,
    pub from: FromClause,
    pub filter: Option<WhereClause>,
    pub init: Vec<String>,
    pub gather: Vec<String>,
    pub return_expr: Option<String>,    // F4-C: return "..." evaluated once per path
    pub group_by: Option<GroupByClause>,
    pub having: Option<HavingClause>,        // NEW in v0.2
    pub order_by: Option<OrderByClause>,
    pub limit: Option<LimitClause>,
    pub time_travel: Option<TimeTravelClause>,
}

// ═══════════════════════════════════════════════════════════════
// SKETCH/COMMIT (Conceptual Operations)
// ═══════════════════════════════════════════════════════════════

/// Sketch statement - define operation without executing
///
/// Syntax:
/// ```nql
/// SKETCH <name> = <statement>
/// ```
///
/// Example:
/// ```nql
/// sketch cleanup =
///   delete (u:User)
///   where u.last_login < timestamp("2020-01-01")
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct SketchStmt {
    pub name: String,
    pub operation: Box<Statement>,
    pub description: Option<String>,
}

/// Commit statement - execute a sketch
///
/// Syntax:
/// ```nql
/// COMMIT <sketch_name>
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct CommitStmt {
    pub sketch_name: String,
}

// ═══════════════════════════════════════════════════════════════
// WRITE STATEMENTS (NEW in v0.2)
// ═══════════════════════════════════════════════════════════════

/// Delete statement - remove nodes/edges
///
/// Syntax:
/// ```nql
/// DELETE <pattern>
/// [WHERE <condition>]
/// [LIMIT <n>]
/// ```
///
/// Example:
/// ```nql
/// delete (u:User)
/// where u.last_login < timestamp("2020-01-01")
/// limit 1000
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteStmt {
    pub pattern: Pattern,
    pub filter: Option<WhereClause>,
    pub limit: Option<LimitClause>,
}

/// Update statement - modify properties
///
/// Syntax:
/// ```nql
/// UPDATE <pattern>
/// SET <variable>.<property> = <value>, ...
/// [WHERE <condition>]
/// [LIMIT <n>]
/// ```
///
/// Example:
/// ```nql
/// update (u:User)
/// set u.verified = true, u.verified_at = now()
/// where u.email_confirmed = true
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct UpdateStmt {
    pub pattern: Pattern,
    pub assignments: Vec<Assignment>,
    pub filter: Option<WhereClause>,
    pub limit: Option<LimitClause>,
}

/// Assignment in UPDATE statement
#[derive(Debug, Clone, PartialEq)]
pub struct Assignment {
    pub variable: String,
    pub property: String,
    pub value: Expression,
}

/// Create statement - add nodes/edges
///
/// Syntax:
/// ```nql
/// CREATE <pattern>
/// ```
///
/// Example:
/// ```nql
/// create (p:Person {name: "Alice", age: 30})
/// create (a:Person)-[:KNOWS {since: 2020}]->(b:Person)
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct AddStmt {
    pub pattern: Pattern,
}

// ═══════════════════════════════════════════════════════════════
// CLAUSES
// ═══════════════════════════════════════════════════════════════

#[derive(Debug, Clone, PartialEq)]
pub struct ExportClause {
    pub format: ExportFormat,
    pub options: HashMap<String, PropertyValue>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ExportFormat {
    Arrow,
    Csv,
    Json,
    Parquet(String), // file path
}

/// FIND clause - specify what to return
///
/// Supports:
/// - Wildcard: `find *`
/// - All function: `find all(p)`
/// - Property access: `find p.name, p.age`
/// - Expressions with alias: `find count(*) as total`
#[derive(Debug, Clone, PartialEq)]
pub struct FindClause {
    pub distinct: bool,
    pub projections: Vec<Projection>,
}

/// Projection in FIND clause
///
/// v0.2 supports multiple projection types for flexibility:
/// - Wildcard (*): All properties of all variables
/// - All(var): All properties of specific variable
/// - Expression: Property access, function call, etc.
#[derive(Debug, Clone, PartialEq)]
pub enum Projection {
    /// Wildcard (*) - all properties of all variables
    ///
    /// Example: `find *`
    Wildcard,

    /// All(variable) - all properties of specific variable
    ///
    /// Example: `find all(p), all(r)`
    All(String),

    /// Expression with optional alias
    ///
    /// Examples:
    /// - `find p.name`
    /// - `find count(*) as total`
    /// - `find p.age + 1 as next_age`
    Expression {
        expr: Expression,
        alias: Option<String>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct FromClause {
    pub patterns: Vec<Pattern>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Pattern {
    pub elements: Vec<PatternElement>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum PatternElement {
    Node(NodePattern),
    Relationship(RelationshipPattern),
}

#[derive(Debug, Clone, PartialEq)]
pub struct NodePattern {
    pub variable: Option<String>,
    pub label: Option<String>,
    pub properties: HashMap<String, PropertyValue>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct RelationshipPattern {
    pub variable: Option<String>,
    pub rel_type: Option<String>,
    pub direction: Direction,
    pub quantifier: Option<Quantifier>,
    /// Filtros inline sobre propiedades de la arista, ej. -[r:TRANS {amount: 1000}]->
    pub properties: HashMap<String, PropertyValue>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Direction {
    Outgoing,      // ->
    Incoming,      // <-
    Bidirectional, // <->
}

#[derive(Debug, Clone, PartialEq)]
pub struct Quantifier {
    pub min: usize,
    pub max: Option<usize>, // None = unbounded
}

#[derive(Debug, Clone, PartialEq)]
pub struct VmAssignment {
    pub variable: String,
    pub expr: Expression,
}

/// WHERE clause - pre-aggregation filter
///
/// Applied BEFORE GROUP BY
#[derive(Debug, Clone, PartialEq)]
pub struct WhereClause {
    pub condition: Expression,
}

#[derive(Debug, Clone, PartialEq)]
pub struct GroupByClause {
    pub expressions: Vec<Expression>,
}

/// HAVING clause - post-aggregation filter (NEW in v0.2)
///
/// Applied AFTER GROUP BY
///
/// Requirements:
/// - Can only be used with GROUP BY
/// - Can reference:
///   - Aggregation functions (count, sum, avg, min, max)
///   - Columns in GROUP BY clause
///
/// Example:
/// ```nql
/// find p.city, count(*) as total
/// from (p:Person)
/// group by p.city
/// having count(*) > 1000
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct HavingClause {
    pub condition: Expression,
}

#[derive(Debug, Clone, PartialEq)]
pub struct OrderByClause {
    pub items: Vec<OrderByItem>,
}

/// ORDER BY item with explicit sort order (improved in v0.2)
///
/// v0.1 used `descending: bool`
/// v0.2 uses `order: SortOrder` for clarity
#[derive(Debug, Clone, PartialEq)]
pub struct OrderByItem {
    pub expression: Expression,
    pub order: SortOrder,
}

/// Sort order for ORDER BY
///
/// Syntax:
/// ```nql
/// order by p.age asc
/// order by p.age desc
/// order by p.age        -- default: Asc
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
pub enum SortOrder {
    #[default]
    Asc,  // Ascending (default)
    Desc, // Descending
}

#[derive(Debug, Clone, PartialEq)]
pub struct LimitClause {
    pub limit: usize,
    pub offset: Option<usize>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TimeTravelClause {
    pub timestamp: u64,
}

// ═══════════════════════════════════════════════════════════════
// EXPRESSIONS
// ═══════════════════════════════════════════════════════════════

#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
    // Literals
    Literal(PropertyValue),

    // Property access: node.property
    Property {
        variable: String,
        property: String,
    },

    // Binary operations
    BinaryOp {
        left: Box<Expression>,
        op: BinaryOperator,
        right: Box<Expression>,
    },

    // Unary operations
    UnaryOp {
        op: UnaryOperator,
        expr: Box<Expression>,
    },

    // Function call
    FunctionCall {
        name: String,
        args: Vec<Expression>,
    },

    // Wildcard * (used in function arguments like count(*))
    Wildcard,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BinaryOperator {
    // Comparison
    Eq,    // =
    NotEq, // !=
    Lt,    // <
    Gt,    // >
    LtEq,  // <=
    GtEq,  // >=

    // Logical
    And,
    Or,

    // Arithmetic
    Add, // +
    Sub, // -
    Mul, // *
    Div, // /
    Mod, // %
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum UnaryOperator {
    Not, // !
    Neg, // -
}

// ═══════════════════════════════════════════════════════════════
// HELPERS & IMPLEMENTATIONS
// ═══════════════════════════════════════════════════════════════

impl Statement {
    /// Returns true if statement is read-only
    pub fn is_read_only(&self) -> bool {
        matches!(self, Statement::Query(_) | Statement::Sketch(_) | Statement::Explain(_) | Statement::Profile(_))
    }

    /// Returns true if statement modifies the graph
    pub fn is_write(&self) -> bool {
        matches!(
            self,
            Statement::Delete(_) | Statement::Update(_) | Statement::Add(_) | Statement::Commit(_)
        )
    }

    /// Returns true if statement requires confirmation
    pub fn requires_confirmation(&self) -> bool {
        match self {
            Statement::Delete(del) => del.filter.is_none() && del.limit.is_none(),
            Statement::Update(upd) => upd.filter.is_none() && upd.limit.is_none(),
            Statement::Commit(_) => true,
            _ => false,
        }
    }
}

impl Query {
    /// Returns true if query is read-only (always true for Query)
    pub fn is_read_only(&self) -> bool {
        true
    }

    /// Returns true if query requires time travel
    pub fn requires_time_travel(&self) -> bool {
        self.time_travel.is_some()
    }

    /// Returns true if query exports results
    pub fn is_export(&self) -> bool {
        self.export.is_some()
    }

    /// Returns true if query has aggregations
    pub fn has_aggregations(&self) -> bool {
        self.group_by.is_some()
    }

    /// Returns true if query has HAVING clause
    pub fn has_having(&self) -> bool {
        self.having.is_some()
    }
}

impl SketchStmt {
    /// Create a new sketch
    pub fn new(name: String, operation: Statement) -> Self {
        Self {
            name,
            operation: Box::new(operation),
            description: None,
        }
    }

    /// Create a sketch with description
    pub fn with_description(name: String, operation: Statement, description: String) -> Self {
        Self {
            name,
            operation: Box::new(operation),
            description: Some(description),
        }
    }
}

impl DeleteStmt {
    /// Returns estimated danger level
    /// - High: No WHERE, no LIMIT (deletes everything)
    /// - Medium: No WHERE but has LIMIT
    /// - Low: Has WHERE
    pub fn danger_level(&self) -> DangerLevel {
        match (&self.filter, &self.limit) {
            (None, None) => DangerLevel::High,
            (None, Some(_)) => DangerLevel::Medium,
            (Some(_), _) => DangerLevel::Low,
        }
    }
}

impl UpdateStmt {
    /// Returns estimated danger level
    pub fn danger_level(&self) -> DangerLevel {
        match (&self.filter, &self.limit) {
            (None, None) => DangerLevel::High,
            (None, Some(_)) => DangerLevel::Medium,
            (Some(_), _) => DangerLevel::Low,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DangerLevel {
    Low,    // Has WHERE clause
    Medium, // No WHERE but has LIMIT
    High,   // No WHERE, no LIMIT - affects ALL nodes
}

impl Pattern {
    /// Extract all variable names from pattern
    pub fn variables(&self) -> Vec<String> {
        let mut vars = Vec::new();

        for element in &self.elements {
            match element {
                PatternElement::Node(node) => {
                    if let Some(var) = &node.variable {
                        vars.push(var.clone());
                    }
                }
                PatternElement::Relationship(rel) => {
                    if let Some(var) = &rel.variable {
                        vars.push(var.clone());
                    }
                }
            }
        }

        vars
    }

    /// Returns true if pattern has any nodes
    pub fn has_nodes(&self) -> bool {
        self.elements.iter().any(|e| matches!(e, PatternElement::Node(_)))
    }

    /// Returns true if pattern has any relationships
    pub fn has_relationships(&self) -> bool {
        self.elements.iter().any(|e| matches!(e, PatternElement::Relationship(_)))
    }
}

impl Projection {
    /// Create a wildcard projection
    pub fn wildcard() -> Self {
        Projection::Wildcard
    }

    /// Create an all(var) projection
    pub fn all(variable: impl Into<String>) -> Self {
        Projection::All(variable.into())
    }

    /// Create a property projection
    pub fn property(variable: impl Into<String>, property: impl Into<String>) -> Self {
        Projection::Expression {
            expr: Expression::property_access(&variable.into(), &property.into()),
            alias: None,
        }
    }

    /// Create a property projection with alias
    pub fn property_as(
        variable: impl Into<String>,
        property: impl Into<String>,
        alias: impl Into<String>,
    ) -> Self {
        Projection::Expression {
            expr: Expression::property_access(&variable.into(), &property.into()),
            alias: Some(alias.into()),
        }
    }
}

impl Expression {
    /// Create a property access expression
    pub fn property_access(variable: &str, property: &str) -> Self {
        Expression::Property {
            variable: variable.to_string(),
            property: property.to_string(),
        }
    }

    /// Create a literal expression
    pub fn literal(value: PropertyValue) -> Self {
        Expression::Literal(value)
    }

    /// Create an equals expression
    pub fn equals(left: Expression, right: Expression) -> Self {
        Expression::BinaryOp {
            left: Box::new(left),
            op: BinaryOperator::Eq,
            right: Box::new(right),
        }
    }

    /// Create a function call expression
    pub fn function(name: impl Into<String>, args: Vec<Expression>) -> Self {
        Expression::FunctionCall {
            name: name.into(),
            args,
        }
    }

    /// Create a wildcard expression
    pub fn wildcard() -> Self {
        Expression::Wildcard
    }

    /// Returns true if expression is a TRUE aggregation function (combines N rows → 1).
    ///
    /// Solo `count`, `sum`, `avg`, `min`, `max`. Las funciones algorítmicas
    /// (`degree`, `pagerank`, `community`, etc.) son per-node y se identifican
    /// con `is_algorithm()` — no son agregaciones.
    pub fn is_aggregation(&self) -> bool {
        matches!(
            self,
            Expression::FunctionCall { name, .. }
                if matches!(name.to_lowercase().as_str(),
                    "count" | "sum" | "avg" | "min" | "max")
        )
    }

    /// Returns true if expression is a per-node algorithm function.
    ///
    /// Estas funciones (degree, pagerank, betweenness, clustering, community,
    /// leiden, community_fast, shortestPath) computan un valor por nodo y se
    /// pre-calculan globalmente. Aunque comparten el path de pre-cómputo con
    /// agregaciones, semánticamente NO son agregaciones — son válidas en WHERE
    /// y en proyecciones non-aggregated.
    pub fn is_algorithm(&self) -> bool {
        matches!(
            self,
            Expression::FunctionCall { name, .. }
                if matches!(name.to_lowercase().as_str(),
                    "pagerank" | "betweenness" | "clustering" | "degree"
                    | "community" | "community_fast" | "leiden" | "shortestpath")
        )
    }
}



// ═══════════════════════════════════════════════════════════════
// DISPLAY IMPLEMENTATIONS (for debugging and error messages)
// ═══════════════════════════════════════════════════════════════

impl std::fmt::Display for Statement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Statement::Query(_) => write!(f, "Query"),
            Statement::Sketch(s) => write!(f, "Sketch({})", s.name),
            Statement::Commit(c) => write!(f, "Commit({})", c.sketch_name),
            Statement::Delete(_) => write!(f, "Delete"),
            Statement::Update(_) => write!(f, "Update"),
            Statement::Add(_) => write!(f, "Add"),
            Statement::CreateIndex(_) => write!(f, "CreateIndex"),
            Statement::DropIndex(_) => write!(f, "DropIndex"),
            Statement::Explain(_) => write!(f, "Explain"),
            Statement::Profile(_) => write!(f, "Profile"),
        }
    }
}

impl std::fmt::Display for DangerLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DangerLevel::Low => write!(f, "low"),
            DangerLevel::Medium => write!(f, "medium"),
            DangerLevel::High => write!(f, "HIGH"),
        }
    }
}

impl std::fmt::Display for SortOrder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SortOrder::Asc => write!(f, "ASC"),
            SortOrder::Desc => write!(f, "DESC"),
        }
    }
}