fathomdb_query/ast.rs
1/// Abstract syntax tree representing a graph query.
2#[derive(Clone, Debug, PartialEq, Eq)]
3pub struct QueryAst {
4 /// Node kind used as the root of the query.
5 pub root_kind: String,
6 /// Ordered pipeline of search, traversal, and filter steps.
7 pub steps: Vec<QueryStep>,
8 /// Named expansion slots evaluated per root result in grouped queries.
9 pub expansions: Vec<ExpansionSlot>,
10 /// Optional hard cap on the number of result rows.
11 pub final_limit: Option<usize>,
12}
13
14/// A named expansion slot that traverses edges per root result.
15#[derive(Clone, Debug, PartialEq, Eq)]
16pub struct ExpansionSlot {
17 /// Slot name used to key the expansion results.
18 pub slot: String,
19 /// Direction to traverse edges.
20 pub direction: TraverseDirection,
21 /// Edge kind (label) to follow.
22 pub label: String,
23 /// Maximum traversal depth.
24 pub max_depth: usize,
25}
26
27/// A single step in the query pipeline.
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub enum QueryStep {
30 /// Nearest-neighbor search over vector embeddings.
31 VectorSearch {
32 /// The search query text (to be embedded by the caller).
33 query: String,
34 /// Maximum number of candidate rows from the vector index.
35 limit: usize,
36 },
37 /// Full-text search over indexed chunk content.
38 TextSearch {
39 /// The FTS match expression.
40 query: String,
41 /// Maximum number of candidate rows from the FTS index.
42 limit: usize,
43 },
44 /// Graph traversal following edges of the given label.
45 Traverse {
46 /// Direction to traverse.
47 direction: TraverseDirection,
48 /// Edge kind to follow.
49 label: String,
50 /// Maximum hops from each candidate.
51 max_depth: usize,
52 },
53 /// Row-level filter predicate.
54 Filter(Predicate),
55}
56
57/// A filter predicate applied to candidate nodes.
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub enum Predicate {
60 /// Match nodes with the exact logical ID.
61 LogicalIdEq(String),
62 /// Match nodes with the exact kind.
63 KindEq(String),
64 /// Equality check on a JSON property at the given path.
65 JsonPathEq {
66 /// JSON path expression (e.g. `$.status`).
67 path: String,
68 /// Value to compare against.
69 value: ScalarValue,
70 },
71 /// Ordered comparison on a JSON property at the given path.
72 JsonPathCompare {
73 /// JSON path expression.
74 path: String,
75 /// Comparison operator.
76 op: ComparisonOp,
77 /// Value to compare against.
78 value: ScalarValue,
79 },
80 /// Match nodes with the exact `source_ref`.
81 SourceRefEq(String),
82}
83
84/// Ordered comparison operator for JSON property filters.
85#[derive(Clone, Copy, Debug, PartialEq, Eq)]
86pub enum ComparisonOp {
87 /// Greater than.
88 Gt,
89 /// Greater than or equal.
90 Gte,
91 /// Less than.
92 Lt,
93 /// Less than or equal.
94 Lte,
95}
96
97/// A typed scalar value used in query predicates.
98#[derive(Clone, Debug, PartialEq, Eq)]
99pub enum ScalarValue {
100 /// A UTF-8 text value.
101 Text(String),
102 /// A 64-bit signed integer.
103 Integer(i64),
104 /// A boolean value.
105 Bool(bool),
106}
107
108/// Direction for graph traversal steps and expansion slots.
109#[derive(Clone, Copy, Debug, PartialEq, Eq)]
110pub enum TraverseDirection {
111 /// Follow edges pointing toward the current node.
112 In,
113 /// Follow edges pointing away from the current node.
114 Out,
115}