Skip to main content

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    /// Match nodes where `content_ref` is not NULL (i.e. content proxy nodes).
83    ContentRefNotNull,
84    /// Match nodes with the exact `content_ref` URI.
85    ContentRefEq(String),
86}
87
88/// Ordered comparison operator for JSON property filters.
89#[derive(Clone, Copy, Debug, PartialEq, Eq)]
90pub enum ComparisonOp {
91    /// Greater than.
92    Gt,
93    /// Greater than or equal.
94    Gte,
95    /// Less than.
96    Lt,
97    /// Less than or equal.
98    Lte,
99}
100
101/// A typed scalar value used in query predicates.
102#[derive(Clone, Debug, PartialEq, Eq)]
103pub enum ScalarValue {
104    /// A UTF-8 text value.
105    Text(String),
106    /// A 64-bit signed integer.
107    Integer(i64),
108    /// A boolean value.
109    Bool(bool),
110}
111
112/// Direction for graph traversal steps and expansion slots.
113#[derive(Clone, Copy, Debug, PartialEq, Eq)]
114pub enum TraverseDirection {
115    /// Follow edges pointing toward the current node.
116    In,
117    /// Follow edges pointing away from the current node.
118    Out,
119}