solidb 1.0.1

A lightweight, high-performance structured database server written in Rust.
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// A single Common Table Expression (CTE)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CteClause {
    /// CTE name (e.g., "temp" in "WITH temp AS (...)")
    pub name: String,
    /// Optional column names: WITH temp(col1, col2) AS (...)
    pub columns: Vec<String>,
    /// Whether this is a recursive CTE
    pub recursive: bool,
    /// The CTE body query
    pub query: Box<Query>,
}

/// WITH clause containing one or more CTEs
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WithClause {
    pub ctes: Vec<CteClause>,
}

/// Set operation combining two query blocks: `a UNION b`, `a INTERSECT c`, ...
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SetOperator {
    /// UNION - concatenates and removes duplicates
    Union,
    /// UNION ALL - concatenates keeping duplicates
    UnionAll,
    /// INTERSECT - rows present in both sides, duplicates removed
    Intersect,
    /// EXCEPT - rows of the left side not present in the right side, duplicates removed
    Except,
}

/// One operand on the right-hand side of a set operation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SetOperationClause {
    pub op: SetOperator,
    pub query: Box<Query>,
}

impl SetOperator {
    /// True for the `ALL` variants, which keep duplicate rows
    pub fn is_all(&self) -> bool {
        matches!(self, SetOperator::UnionAll)
    }
}

/// AST node for a complete SDBQL query
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Query {
    /// Optional WITH clause for CTEs (Common Table Expressions)
    pub with_clause: Option<WithClause>,
    /// LET clauses for variable bindings (executed first, before any FOR)
    pub let_clauses: Vec<LetClause>,
    /// Multiple FOR clauses for JOINs (nested loops)
    pub for_clauses: Vec<ForClause>,
    /// JOIN clauses for cross-collection queries
    pub join_clauses: Vec<JoinClause>,
    /// Multiple FILTER clauses (can reference any FOR variable)
    pub filter_clauses: Vec<FilterClause>,
    pub sort_clause: Option<SortClause>,
    pub limit_clause: Option<LimitClause>,
    /// RETURN clause is optional - queries with only mutations (INSERT/UPDATE/REMOVE) don't need it
    pub return_clause: Option<ReturnClause>,
    /// Optional CREATE STREAM clause (wraps the query definition)
    pub create_stream_clause: Option<CreateStreamClause>,
    /// Optional CREATE MATERIALIZED VIEW clause
    pub create_materialized_view_clause: Option<CreateMaterializedViewClause>,
    /// Optional REFRESH MATERIALIZED VIEW clause
    pub refresh_materialized_view_clause: Option<RefreshMaterializedViewClause>,
    /// Optional WINDOW clause for stream processing
    pub window_clause: Option<WindowClause>,

    /// Ordered body clauses (FOR, LET, FILTER) preserving declaration order
    /// This enables correlated subqueries where LET can reference outer FOR variables
    pub body_clauses: Vec<BodyClause>,

    /// Set operations applied after this query block: `q1 UNION q2 INTERSECT q3`
    /// is parsed as `q1` with `set_operations = [UNION q2, INTERSECT q3]`.
    #[serde(default)]
    pub set_operations: Vec<SetOperationClause>,
}

impl Query {
    /// True if executing this query writes data (INSERT/UPDATE/UPSERT/REMOVE
    /// clauses, stream or materialized-view DDL). Used to decide whether a
    /// principal needs Write rather than Read permission.
    pub fn has_mutations(&self) -> bool {
        self.body_clauses.iter().any(|clause| {
            matches!(
                clause,
                BodyClause::Insert(_)
                    | BodyClause::Update(_)
                    | BodyClause::Upsert(_)
                    | BodyClause::Remove(_)
            )
        }) || self.create_stream_clause.is_some()
            || self.create_materialized_view_clause.is_some()
            || self.refresh_materialized_view_clause.is_some()
            || self
                .set_operations
                .iter()
                .any(|op| op.query.has_mutations())
            || self
                .with_clause
                .as_ref()
                .is_some_and(|with| with.ctes.iter().any(|cte| cte.query.has_mutations()))
    }
}

/// A clause that can appear in the query body (preserves order for correlated subqueries)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BodyClause {
    For(ForClause),
    Let(LetClause),
    Filter(FilterClause),
    Insert(InsertClause),
    Update(UpdateClause),
    Upsert(UpsertClause),
    Remove(RemoveClause),
    Join(JoinClause),
    GraphTraversal(GraphTraversalClause),
    ShortestPath(ShortestPathClause),
    Collect(CollectClause),
    Window(WindowClause),
    /// Scored filter (`SEARCH expr`); numeric scores are stored as `__search_score`.
    Search(FilterClause),
}

/// Edge direction for graph traversals
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EdgeDirection {
    /// Follow edges where start_vertex == _from
    Outbound,
    /// Follow edges where start_vertex == _to
    Inbound,
    /// Follow edges in both directions
    Any,
}

/// FOR vertex[, edge] IN [depth..depth] OUTBOUND|INBOUND|ANY start_vertex edge_collection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GraphTraversalClause {
    /// Variable for the visited vertices
    pub vertex_var: String,
    /// Optional variable for the edges (can be omitted)
    pub edge_var: Option<String>,
    /// Direction of traversal
    pub direction: EdgeDirection,
    /// Starting vertex (expression like "users/alice" or @start)
    pub start_vertex: Expression,
    /// Edge collection to traverse
    pub edge_collection: String,
    /// Minimum traversal depth (default 1)
    pub min_depth: usize,
    /// Maximum traversal depth (default 1)
    pub max_depth: usize,
    /// Optional path variable `FOR v, e, p`
    #[serde(default)]
    pub path_var: Option<String>,
    /// Stop expanding when this expression is true
    #[serde(default)]
    pub prune: Option<Expression>,
}

/// FOR vertex[, edge] IN SHORTEST_PATH start_vertex TO end_vertex OUTBOUND|INBOUND|ANY edge_collection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ShortestPathClause {
    /// Variable for the vertices in the path
    pub vertex_var: String,
    /// Optional variable for the edges in the path
    pub edge_var: Option<String>,
    /// Starting vertex
    pub start_vertex: Expression,
    /// Target vertex
    pub end_vertex: Expression,
    /// Direction of traversal
    pub direction: EdgeDirection,
    /// Edge collection to traverse
    pub edge_collection: String,
    /// Optional numeric edge field used as Dijkstra weight
    #[serde(default)]
    pub weight: Option<String>,
    #[serde(default)]
    pub path_var: Option<String>,
    #[serde(default)]
    pub mode: PathFindMode,
    #[serde(default)]
    pub k: Option<usize>,
    #[serde(default)]
    pub min_len: Option<usize>,
    #[serde(default)]
    pub max_len: Option<usize>,
    #[serde(default)]
    pub limit: Option<usize>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub enum PathFindMode {
    #[default]
    Shortest,
    AllShortest,
    KShortest,
    KPaths,
}

/// CREATE STREAM name AS ...
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateStreamClause {
    pub name: String,
    pub if_not_exists: bool,
}

/// CREATE MATERIALIZED VIEW name [REFRESH "<interval>"] AS ...
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateMaterializedViewClause {
    pub name: String,
    pub if_not_exists: bool,
    /// The query definition
    pub query: Box<Query>,
    /// Optional automatic refresh interval (e.g. "30s", "5m", "1h", "2d", or a
    /// plain number of seconds). When set, a background worker re-runs the view
    /// query on that cadence. `None` = manual `REFRESH MATERIALIZED VIEW` only.
    #[serde(default)]
    pub refresh_schedule: Option<String>,
}

/// REFRESH MATERIALIZED VIEW name
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RefreshMaterializedViewClause {
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum WindowType {
    /// TUMBLING (SIZE "1m") - Fixed non-overlapping windows
    Tumbling,
    /// SLIDING (SIZE "1m") - Sliding windows (hopping)
    Sliding,
}

/// WINDOW TUMBLING (SIZE "1m")
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WindowClause {
    pub window_type: WindowType,
    /// Duration string (e.g., "1m", "30s", "1h")
    pub duration: String,
}

/// LET variable = expression (can be a subquery)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LetClause {
    pub variable: String,
    pub expression: Expression,
}

/// FOR variable IN collection/expression
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ForClause {
    pub variable: String,
    pub collection: String,
    /// Optional: iterate over a variable (e.g., FOR x IN someLetVar)
    pub source_variable: Option<String>,
    /// Optional: iterate over an expression (e.g., FOR i IN 1..5)
    pub source_expression: Option<Expression>,
    /// `SYSTEM_TIME AS OF` timestamp expression (epoch ms or RFC3339)
    #[serde(default)]
    pub system_time: Option<Expression>,
    /// Application valid-time filter (`valid_from` / `valid_to` fields)
    #[serde(default)]
    pub valid_time: Option<ValidTimeSpec>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ValidTimeSpec {
    AsOf(Expression),
    Range { from: Expression, to: Expression },
}

/// FILTER expression
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FilterClause {
    pub expression: Expression,
}

/// INSERT document INTO collection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InsertClause {
    pub document: Expression,
    pub collection: String,
}

/// UPDATE document WITH changes IN collection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpdateClause {
    /// The document or key to update (usually a variable like `doc` or `doc._key`)
    pub selector: Expression,
    /// The changes to apply (object expression)
    pub changes: Expression,
    /// The collection to update in
    pub collection: String,
}

/// UPSERT search INSERT insert UPDATE update IN collection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpsertClause {
    pub search: Expression,
    pub insert: Expression,
    pub update: Expression,
    pub collection: String,
    pub replace: bool,
}

/// REMOVE document IN collection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RemoveClause {
    /// The document or key to remove (usually a variable like `doc` or `doc._key`)
    pub selector: Expression,
    /// The collection to remove from
    pub collection: String,
}

/// JOIN type (INNER vs LEFT/RIGHT/FULL)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum JoinType {
    Inner,
    Left,
    Right,
    FullOuter,
    Asof,
}

/// As-of join time alignment
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AsofStrategy {
    Backward,
    Forward,
    Nearest,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AsofSpec {
    pub left_time: Expression,
    pub right_time: Expression,
    pub strategy: AsofStrategy,
    pub tolerance: Option<Expression>,
}

/// JOIN variable IN collection ON condition
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JoinClause {
    /// Type of join (INNER, LEFT, etc.)
    pub join_type: JoinType,
    /// Variable to bind joined documents to
    pub variable: String,
    /// Collection to join with
    pub collection: String,
    /// Join condition (e.g., user._key == orders.user_key)
    pub condition: Expression,
    #[serde(default)]
    pub asof: Option<AsofSpec>,
}

/// COLLECT var = expr [INTO group [KEEP var1, var2]] [WITH COUNT INTO count] [AGGREGATE ...]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CollectClause {
    /// Group variables: (variable_name, expression) pairs
    pub group_vars: Vec<(String, Expression)>,
    /// INTO variable (collects grouped documents into an array)
    pub into_var: Option<String>,
    /// Optional KEEP restriction on the variables stored in the INTO array.
    /// Empty = keep every variable currently in scope (default).
    #[serde(default)]
    pub keep_vars: Vec<String>,
    /// WITH COUNT INTO variable
    pub count_var: Option<String>,
    /// AGGREGATE expressions
    pub aggregates: Vec<AggregateExpr>,
}

/// Aggregate expression: var = FUNC(expr)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AggregateExpr {
    /// Variable to store the result
    pub variable: String,
    /// Aggregate function name (SUM, AVG, MIN, MAX, COUNT, LENGTH, etc.)
    pub function: String,
    /// Argument expression
    pub argument: Option<Expression>,
}

/// SORT expression [ASC|DESC]
/// Supports both field-based sorting (SORT doc.age) and function-based sorting (SORT BM25(doc.content, "query"))
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SortClause {
    pub fields: Vec<(Expression, bool)>, // (expression, ascending)
}

/// LIMIT [offset,] count -- or a standalone OFFSET, which has no count
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LimitClause {
    pub offset: Expression,
    /// Row count. `None` means "no upper bound" (`OFFSET n` without `LIMIT`):
    /// callers must not substitute a sentinel maximum, because the count is
    /// pushed down into storage scans and index lookups as an allocation hint.
    pub count: Option<Expression>,
}

/// RETURN [DISTINCT] expression
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReturnClause {
    pub expression: Expression,
    /// RETURN DISTINCT - remove duplicate result rows (first occurrence wins)
    #[serde(default)]
    pub distinct: bool,
}

/// Part of a template string (used in AST after parsing)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TemplateStringPart {
    /// Static text between interpolations
    Literal(String),
    /// Parsed expression inside ${...}
    Expression(Box<Expression>),
}

/// Expression types
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Expression {
    /// Variable reference (e.g., doc)
    Variable(String),

    /// Bind variable reference (e.g., @name) - for parameterized queries
    BindVariable(String),

    /// Field access (e.g., doc.name)
    FieldAccess(Box<Expression>, String),

    /// Optional field access (e.g., doc?.name) - returns null if base is null
    OptionalFieldAccess(Box<Expression>, String),

    /// Dynamic field access (e.g., doc[@fieldName] or doc["name"])
    DynamicFieldAccess(Box<Expression>, Box<Expression>),

    /// Array element access (e.g., arr[0], arr[i])
    ArrayAccess(Box<Expression>, Box<Expression>),

    /// Array spread access (e.g., arr[*].field extracts field from all elements)
    /// field_path is None for bare [*], Some("field.nested") for chained access
    ArraySpreadAccess(Box<Expression>, Option<String>),

    /// Literal value
    Literal(Value),

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

    /// Unary operation
    UnaryOp {
        op: UnaryOperator,
        operand: Box<Expression>,
    },

    /// Object construction
    Object(Vec<(String, Expression)>),

    /// Array construction
    Array(Vec<Expression>),

    /// Range expression (e.g., 1..5 produces [1, 2, 3, 4, 5])
    Range(Box<Expression>, Box<Expression>),

    /// Function call (e.g., DISTANCE(lat1, lon1, lat2, lon2))
    FunctionCall { name: String, args: Vec<Expression> },

    /// Subquery (FOR ... RETURN ...) wrapped in parentheses
    Subquery(Box<Query>),

    /// Ternary conditional (condition ? true_expr : false_expr)
    Ternary {
        condition: Box<Expression>,
        true_expr: Box<Expression>,
        false_expr: Box<Expression>,
    },

    /// CASE expression - SQL-style conditional
    /// Simple form: CASE expr WHEN val1 THEN res1 WHEN val2 THEN res2 ELSE default END
    /// Searched form: CASE WHEN cond1 THEN res1 WHEN cond2 THEN res2 ELSE default END
    Case {
        /// Optional operand for simple CASE (None for searched CASE)
        operand: Option<Box<Expression>>,
        /// List of (condition/value, result) pairs
        when_clauses: Vec<(Expression, Expression)>,
        /// Optional ELSE result
        else_clause: Option<Box<Expression>>,
    },

    /// Pipeline operation (value |> FUNC(args))
    /// Left value becomes first argument to right-side function call
    Pipeline {
        left: Box<Expression>,
        right: Box<Expression>,
    },

    /// Lambda expression (x -> expr) or ((a, b) -> expr)
    /// Used as arguments to higher-order functions like FILTER, MAP
    Lambda {
        params: Vec<String>,
        body: Box<Expression>,
    },

    /// Window function call with OVER clause
    /// Example: ROW_NUMBER() OVER (PARTITION BY doc.region ORDER BY doc.amount DESC)
    WindowFunctionCall {
        function: String,
        arguments: Vec<Expression>,
        over_clause: WindowSpec,
    },

    /// Template string with interpolated expressions: $"Hello ${name}!"
    /// Syntax: $"text ${expression} more text"
    TemplateString { parts: Vec<TemplateStringPart> },
}

/// Window specification (the OVER clause)
/// Example: OVER (PARTITION BY doc.region ORDER BY doc.date ASC)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WindowSpec {
    /// PARTITION BY expressions (optional) - groups rows into partitions
    pub partition_by: Vec<Expression>,
    /// ORDER BY within the window (optional) - defines row ordering within each partition
    /// Each tuple is (expression, ascending)
    pub order_by: Vec<(Expression, bool)>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BinaryOperator {
    // Comparison
    Equal,
    NotEqual,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual,
    /// Vector cosine distance, or three-way compare (−1/0/1).
    Spaceship,
    /// Semantic / trigram match (`a ~ b`). Unary `~` stays bitwise NOT.
    SemanticMatch,
    In,
    NotIn,

    // Logical
    And,
    Or,

    // Arithmetic
    Add,
    Subtract,
    Multiply,
    Divide,
    Modulus, // Adding modulo strictly if needed, but standard request is just ops. Adding LIKE/REGEX first.
    Exponent, // For POW operator ^ or ** if we support it as operator

    // String matching
    Like,
    NotLike,
    RegEx,
    NotRegEx,
    FuzzyEqual, // ~= (fuzzy string matching)

    // Bitwise
    BitwiseAnd,
    BitwiseOr,
    BitwiseXor,
    LeftShift,
    RightShift,

    // Null coalescing
    NullCoalesce,

    // Logical OR (||) - returns left if truthy, otherwise right
    LogicalOr,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum UnaryOperator {
    Not,
    Negate,
    BitwiseNot,
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_expression_literal() {
        let expr = Expression::Literal(json!(42));
        assert_eq!(expr, Expression::Literal(json!(42)));
    }

    #[test]
    fn test_expression_variable() {
        let expr = Expression::Variable("doc".to_string());
        if let Expression::Variable(name) = expr {
            assert_eq!(name, "doc");
        } else {
            panic!("Expected Variable");
        }
    }

    #[test]
    fn test_expression_field_access() {
        let expr = Expression::FieldAccess(
            Box::new(Expression::Variable("doc".to_string())),
            "name".to_string(),
        );

        if let Expression::FieldAccess(base, field) = expr {
            assert_eq!(*base, Expression::Variable("doc".to_string()));
            assert_eq!(field, "name");
        } else {
            panic!("Expected FieldAccess");
        }
    }

    #[test]
    fn test_expression_binary_op() {
        let expr = Expression::BinaryOp {
            left: Box::new(Expression::Variable("a".to_string())),
            op: BinaryOperator::Add,
            right: Box::new(Expression::Literal(json!(1))),
        };

        if let Expression::BinaryOp { left, op, right } = expr {
            assert_eq!(*left, Expression::Variable("a".to_string()));
            assert_eq!(op, BinaryOperator::Add);
            assert_eq!(*right, Expression::Literal(json!(1)));
        } else {
            panic!("Expected BinaryOp");
        }
    }

    #[test]
    fn test_for_clause() {
        let clause = ForClause {
            variable: "doc".to_string(),
            collection: "users".to_string(),
            source_variable: None,
            source_expression: None,
            system_time: None,
            valid_time: None,
        };

        assert_eq!(clause.variable, "doc");
        assert_eq!(clause.collection, "users");
    }

    #[test]
    fn test_filter_clause() {
        let clause = FilterClause {
            expression: Expression::Literal(json!(true)),
        };

        assert_eq!(clause.expression, Expression::Literal(json!(true)));
    }

    #[test]
    fn test_limit_clause() {
        let clause = LimitClause {
            offset: Expression::Literal(json!(0)),
            count: Some(Expression::Literal(json!(10))),
        };

        assert_eq!(clause.offset, Expression::Literal(json!(0)));
        assert_eq!(clause.count, Some(Expression::Literal(json!(10))));

        // A standalone OFFSET has no count at all
        let unbounded = LimitClause {
            offset: Expression::Literal(json!(5)),
            count: None,
        };
        assert!(unbounded.count.is_none());
    }

    #[test]
    fn test_sort_clause() {
        let clause = SortClause {
            fields: vec![(
                Expression::FieldAccess(
                    Box::new(Expression::Variable("doc".to_string())),
                    "age".to_string(),
                ),
                true,
            )],
        };

        assert_eq!(clause.fields.len(), 1);
        assert!(clause.fields[0].1); // ascending
    }

    #[test]
    fn test_let_clause() {
        let clause = LetClause {
            variable: "x".to_string(),
            expression: Expression::Literal(json!(42)),
        };

        assert_eq!(clause.variable, "x");
    }

    #[test]
    fn test_insert_clause() {
        let clause = InsertClause {
            document: Expression::Object(vec![]),
            collection: "users".to_string(),
        };

        assert_eq!(clause.collection, "users");
    }

    #[test]
    fn test_edge_direction() {
        assert_ne!(EdgeDirection::Inbound, EdgeDirection::Outbound);
        assert_ne!(EdgeDirection::Any, EdgeDirection::Inbound);
    }

    #[test]
    fn test_binary_operators() {
        assert_eq!(BinaryOperator::Equal.clone(), BinaryOperator::Equal);
        assert_ne!(BinaryOperator::Equal, BinaryOperator::NotEqual);
        assert_ne!(BinaryOperator::Add, BinaryOperator::Subtract);
    }

    #[test]
    fn test_unary_operators() {
        assert_eq!(UnaryOperator::Not.clone(), UnaryOperator::Not);
        assert_ne!(UnaryOperator::Not, UnaryOperator::Negate);
    }

    #[test]
    fn test_expression_clone() {
        let expr = Expression::Variable("test".to_string());
        let cloned = expr.clone();
        assert_eq!(expr, cloned);
    }

    #[test]
    fn test_query_default() {
        let query = Query {
            with_clause: None,
            let_clauses: vec![],
            for_clauses: vec![],
            join_clauses: vec![],
            filter_clauses: vec![],
            sort_clause: None,
            limit_clause: None,
            return_clause: None,
            create_stream_clause: None,
            create_materialized_view_clause: None,
            refresh_materialized_view_clause: None,
            window_clause: None,
            body_clauses: vec![],
            set_operations: vec![],
        };

        assert!(query.for_clauses.is_empty());
        assert!(query.return_clause.is_none());
    }

    #[test]
    fn test_collect_clause() {
        let clause = CollectClause {
            group_vars: vec![(
                "category".to_string(),
                Expression::FieldAccess(
                    Box::new(Expression::Variable("doc".to_string())),
                    "cat".to_string(),
                ),
            )],
            into_var: Some("items".to_string()),
            keep_vars: vec![],
            count_var: Some("cnt".to_string()),
            aggregates: vec![],
        };

        assert_eq!(clause.group_vars.len(), 1);
        assert_eq!(clause.into_var, Some("items".to_string()));
        assert_eq!(clause.count_var, Some("cnt".to_string()));
    }

    #[test]
    fn test_aggregate_expr() {
        let agg = AggregateExpr {
            variable: "total".to_string(),
            function: "SUM".to_string(),
            argument: Some(Expression::FieldAccess(
                Box::new(Expression::Variable("doc".to_string())),
                "price".to_string(),
            )),
        };

        assert_eq!(agg.variable, "total");
        assert_eq!(agg.function, "SUM");
        assert!(agg.argument.is_some());
    }

    #[test]
    fn test_expression_array() {
        let expr = Expression::Array(vec![
            Expression::Literal(json!(1)),
            Expression::Literal(json!(2)),
            Expression::Literal(json!(3)),
        ]);

        if let Expression::Array(items) = expr {
            assert_eq!(items.len(), 3);
        } else {
            panic!("Expected Array");
        }
    }

    #[test]
    fn test_expression_object() {
        let expr = Expression::Object(vec![
            ("name".to_string(), Expression::Literal(json!("test"))),
            ("value".to_string(), Expression::Literal(json!(42))),
        ]);

        if let Expression::Object(fields) = expr {
            assert_eq!(fields.len(), 2);
            assert_eq!(fields[0].0, "name");
        } else {
            panic!("Expected Object");
        }
    }

    #[test]
    fn test_expression_range() {
        let expr = Expression::Range(
            Box::new(Expression::Literal(json!(1))),
            Box::new(Expression::Literal(json!(5))),
        );

        if let Expression::Range(start, end) = expr {
            assert_eq!(*start, Expression::Literal(json!(1)));
            assert_eq!(*end, Expression::Literal(json!(5)));
        } else {
            panic!("Expected Range");
        }
    }

    #[test]
    fn test_expression_function_call() {
        let expr = Expression::FunctionCall {
            name: "LENGTH".to_string(),
            args: vec![Expression::Variable("arr".to_string())],
        };

        if let Expression::FunctionCall { name, args } = expr {
            assert_eq!(name, "LENGTH");
            assert_eq!(args.len(), 1);
        } else {
            panic!("Expected FunctionCall");
        }
    }

    #[test]
    fn test_expression_ternary() {
        let expr = Expression::Ternary {
            condition: Box::new(Expression::Variable("flag".to_string())),
            true_expr: Box::new(Expression::Literal(json!(1))),
            false_expr: Box::new(Expression::Literal(json!(0))),
        };

        if let Expression::Ternary {
            condition,
            true_expr,
            false_expr,
        } = expr
        {
            assert_eq!(*condition, Expression::Variable("flag".to_string()));
            assert_eq!(*true_expr, Expression::Literal(json!(1)));
            assert_eq!(*false_expr, Expression::Literal(json!(0)));
        } else {
            panic!("Expected Ternary");
        }
    }
}