uqa-graph 0.2.2

Graph store, RPQ, Cypher (lexer/parser/AST/compiler), graph algorithms
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Temporal filtering and traversal (Section 10, Paper 2).
//!
//! Edges may carry `valid_from` / `valid_to` properties (numeric
//! seconds-since-epoch by convention). [`TemporalFilter`] accepts an
//! edge whose validity interval covers a query timestamp or overlaps a
//! query range. [`TemporalTraverse`] is `Traverse` with the filter
//! applied to each edge before it is followed.

use std::collections::{BTreeMap, BTreeSet};

use uqa_core::{DocId, EdgeId, Payload, PostingEntry, PostingList, Value, VertexId};

use crate::operators::DEFAULT_GRAPH_SCORE;
use crate::pattern::GraphPattern;
use crate::posting_list::{GraphPayload, GraphPostingList};
use crate::store::{GraphStore, GraphStoreError, GraphStoreResult};

/// Time-aware edge filter. `Timestamp(t)` accepts an edge if
/// `valid_from <= t <= valid_to`; `Range(a, b)` accepts an edge whose
/// validity interval overlaps `[a, b]`. An edge with neither
/// `valid_from` nor `valid_to` is always accepted.
#[derive(Debug, Clone, Copy)]
pub enum TemporalFilter {
    /// Accept everything.
    Any,
    /// Accept edges valid at exactly this timestamp.
    Timestamp(f64),
    /// Accept edges whose validity interval overlaps the closed range.
    Range(f64, f64),
    /// Accept edges that are valid at `timestamp` and whose validity
    /// interval overlaps the closed range. Keeping the conjunction in
    /// the physical filter preserves an IR that supplies both bounds.
    TimestampAndRange(f64, f64, f64),
}

impl TemporalFilter {
    pub fn is_valid(&self, properties: &BTreeMap<String, Value>) -> GraphStoreResult<bool> {
        self.validate()?;
        let valid_from = numeric(properties.get("valid_from"), "valid_from")?;
        let valid_to = numeric(properties.get("valid_to"), "valid_to")?;
        if valid_from.is_none() && valid_to.is_none() {
            return Ok(true);
        }
        let vf = valid_from.unwrap_or(f64::NEG_INFINITY);
        let vt = valid_to.unwrap_or(f64::INFINITY);
        Ok(match *self {
            TemporalFilter::Any => true,
            TemporalFilter::Timestamp(t) => vf <= t && t <= vt,
            TemporalFilter::Range(start, end) => vf <= end && vt >= start,
            TemporalFilter::TimestampAndRange(t, start, end) => {
                vf <= t && t <= vt && vf <= end && vt >= start
            }
        })
    }

    fn validate(&self) -> GraphStoreResult<()> {
        let invalid = match *self {
            TemporalFilter::Any => false,
            TemporalFilter::Timestamp(timestamp) => !timestamp.is_finite(),
            TemporalFilter::Range(start, end) => {
                !start.is_finite() || !end.is_finite() || start > end
            }
            TemporalFilter::TimestampAndRange(timestamp, start, end) => {
                !timestamp.is_finite() || !start.is_finite() || !end.is_finite() || start > end
            }
        };
        if invalid {
            Err(GraphStoreError::InvalidMutation(format!(
                "invalid temporal filter {self:?}"
            )))
        } else {
            Ok(())
        }
    }
}

fn numeric(v: Option<&Value>, property: &str) -> GraphStoreResult<Option<f64>> {
    match v {
        None => Ok(None),
        Some(Value::Int(n)) if n.unsigned_abs() <= (1_u64 << 53) => Ok(Some(*n as f64)),
        Some(Value::Float(value)) if value.is_finite() => Ok(Some(*value)),
        Some(Value::Decimal(value)) => value
            .to_f64()
            .filter(|converted| converted.is_finite())
            .map(Some)
            .ok_or_else(|| {
                GraphStoreError::InvalidMutation(format!(
                    "temporal property {property:?} is not representable as finite f64"
                ))
            }),
        Some(Value::Int(value)) => Err(GraphStoreError::InvalidMutation(format!(
            "temporal property {property:?} integer {value} is not exactly representable as f64"
        ))),
        Some(value) => Err(GraphStoreError::InvalidMutation(format!(
            "temporal property {property:?} must be finite numeric, got {value:?}"
        ))),
    }
}

/// BFS traversal that applies a [`TemporalFilter`] to every candidate
/// edge before it is followed. Mirrors the structure of
/// [`crate::Traverse`] but with the filter step inlined.
pub struct TemporalTraverse<'a> {
    pub start_vertex: VertexId,
    pub graph: &'a str,
    pub label: Option<&'a str>,
    pub max_hops: u32,
    pub filter: TemporalFilter,
    pub score: f64,
}

impl<'a> TemporalTraverse<'a> {
    pub fn new(start: VertexId, graph: &'a str) -> Self {
        Self {
            start_vertex: start,
            graph,
            label: None,
            max_hops: 1,
            filter: TemporalFilter::Any,
            score: DEFAULT_GRAPH_SCORE,
        }
    }

    pub fn label(mut self, label: &'a str) -> Self {
        self.label = Some(label);
        self
    }

    pub fn max_hops(mut self, hops: u32) -> Self {
        self.max_hops = hops;
        self
    }

    pub fn filter(mut self, filter: TemporalFilter) -> Self {
        self.filter = filter;
        self
    }

    pub fn execute<G: GraphStore>(&self, store: &G) -> GraphStoreResult<GraphPostingList> {
        self.filter.validate()?;
        validate_score(self.score)?;
        store.require_vertex_in_graph(self.start_vertex, self.graph)?;
        let mut visited: BTreeSet<VertexId> = BTreeSet::new();
        let mut frontier: BTreeSet<VertexId> = BTreeSet::new();
        frontier.insert(self.start_vertex);
        let mut all_edges: BTreeSet<EdgeId> = BTreeSet::new();

        for _ in 0..self.max_hops {
            let mut next_frontier: BTreeSet<VertexId> = BTreeSet::new();
            for v in &frontier {
                for eid in store.out_edge_ids(*v, self.graph)? {
                    let edge = store.get_edge(eid).ok_or_else(|| {
                        GraphStoreError::CorruptGraph(format!(
                            "temporal traversal references missing edge {eid}"
                        ))
                    })?;
                    if let Some(want) = self.label {
                        if edge.label != want {
                            continue;
                        }
                    }
                    if !self.filter.is_valid(&edge.properties)? {
                        continue;
                    }
                    let neighbor = edge.target_id;
                    if !visited.contains(&neighbor) && !frontier.contains(&neighbor) {
                        next_frontier.insert(neighbor);
                    }
                    all_edges.insert(eid);
                }
            }
            visited.append(&mut frontier.clone());
            frontier = next_frontier;
            if frontier.is_empty() {
                break;
            }
        }
        visited.append(&mut frontier);

        let visited_vec: Vec<VertexId> = visited.iter().copied().collect();
        let edges_vec: Vec<EdgeId> = all_edges.iter().copied().collect();
        let mut entries: Vec<PostingEntry> = Vec::with_capacity(visited_vec.len());
        let mut graph_payloads: BTreeMap<DocId, GraphPayload> = BTreeMap::new();
        for vid in &visited_vec {
            entries.push(PostingEntry::new(*vid, Payload::with_score(self.score)));
            graph_payloads.insert(
                *vid,
                GraphPayload {
                    subgraph_vertices: visited_vec.clone(),
                    subgraph_edges: edges_vec.clone(),
                    graph_name: self.graph.to_string(),
                    score_override: Some(self.score),
                },
            );
        }
        GraphPostingList::try_from_parts(
            PostingList::from_sorted_unchecked(entries),
            graph_payloads,
        )
        .map_err(Into::into)
    }
}

/// Temporal-aware pattern matching (Section 10, Paper 2).
///
/// Same algorithm as the standard subgraph matcher but every edge
/// candidate is filtered through a [`TemporalFilter`] before it is
/// admitted into the assignment, so only temporally valid edges
/// participate in pattern matching.
pub struct TemporalPatternMatch<'a> {
    pub pattern: GraphPattern,
    pub graph: &'a str,
    pub temporal_filter: TemporalFilter,
    pub score: f64,
}

impl<'a> TemporalPatternMatch<'a> {
    pub fn new(pattern: GraphPattern, graph: &'a str) -> Self {
        Self {
            pattern,
            graph,
            temporal_filter: TemporalFilter::Any,
            score: DEFAULT_GRAPH_SCORE,
        }
    }

    pub fn filter(mut self, filter: TemporalFilter) -> Self {
        self.temporal_filter = filter;
        self
    }

    pub fn score(mut self, score: f64) -> Self {
        self.score = score;
        self
    }

    pub fn execute<G: GraphStore>(&self, store: &G) -> GraphStoreResult<GraphPostingList> {
        self.temporal_filter.validate()?;
        validate_score(self.score)?;
        let candidates = self.compute_candidates(store)?;

        // Group edges by both source and target variable so the
        // backtracking validator can quickly find every edge that
        // touches a newly-assigned variable.
        let mut var_edges: BTreeMap<String, Vec<usize>> = BTreeMap::new();
        for (i, ep) in self.pattern.edge_patterns.iter().enumerate() {
            var_edges.entry(ep.source_var.clone()).or_default().push(i);
            var_edges.entry(ep.target_var.clone()).or_default().push(i);
        }

        let mut unassigned: BTreeSet<String> = self
            .pattern
            .vertex_patterns
            .iter()
            .map(|vp| vp.variable.clone())
            .collect();
        let mut assignment: BTreeMap<String, VertexId> = BTreeMap::new();
        let mut assigned_values: BTreeSet<VertexId> = BTreeSet::new();
        let mut matches: Vec<BTreeMap<String, VertexId>> = Vec::new();

        self.backtrack(
            store,
            &candidates,
            &var_edges,
            &mut unassigned,
            &mut assignment,
            &mut assigned_values,
            &mut matches,
        )?;

        let mut entries: Vec<PostingEntry> = Vec::with_capacity(matches.len());
        let mut graph_payloads: BTreeMap<DocId, GraphPayload> = BTreeMap::new();
        for (i, assn) in matches.iter().enumerate() {
            let doc_id = u64::try_from(i)
                .ok()
                .and_then(|value| value.checked_add(1))
                .ok_or_else(|| {
                    GraphStoreError::IdExhausted(
                        "temporal match result id counter overflow".to_string(),
                    )
                })?;
            let mut fields: BTreeMap<String, Value> = BTreeMap::new();
            for (k, v) in assn {
                fields.insert(k.clone(), graph_id_value(*v));
            }
            entries.push(PostingEntry::new(
                doc_id,
                Payload {
                    score: self.score,
                    fields,
                    ..Default::default()
                },
            ));
            let match_vertices: Vec<VertexId> = assn.values().copied().collect();
            let match_edges = self.collect_match_edges(store, assn)?;
            graph_payloads.insert(
                doc_id,
                GraphPayload {
                    subgraph_vertices: match_vertices,
                    subgraph_edges: match_edges,
                    graph_name: self.graph.to_string(),
                    score_override: Some(self.score),
                },
            );
        }

        GraphPostingList::try_from_parts(
            PostingList::from_sorted_unchecked(entries),
            graph_payloads,
        )
        .map_err(Into::into)
    }

    fn compute_candidates<G: GraphStore>(
        &self,
        store: &G,
    ) -> GraphStoreResult<BTreeMap<String, Vec<VertexId>>> {
        let mut out: BTreeMap<String, Vec<VertexId>> = BTreeMap::new();
        let vids = store.vertex_ids_in_graph(self.graph)?;
        for vp in &self.pattern.vertex_patterns {
            let mut candidates = Vec::new();
            for vid in &vids {
                let vertex = store.get_vertex(*vid).ok_or_else(|| {
                    GraphStoreError::CorruptGraph(format!(
                        "graph {:?} references missing vertex {vid}",
                        self.graph
                    ))
                })?;
                if vp
                    .constraints
                    .iter()
                    .all(|constraint| constraint.matches(vertex))
                {
                    candidates.push(*vid);
                }
            }
            out.insert(vp.variable.clone(), candidates);
        }
        Ok(out)
    }

    #[expect(
        clippy::too_many_arguments,
        reason = "keeps graph scope inputs aligned"
    )]
    fn backtrack<G: GraphStore>(
        &self,
        store: &G,
        candidates: &BTreeMap<String, Vec<VertexId>>,
        var_edges: &BTreeMap<String, Vec<usize>>,
        unassigned: &mut BTreeSet<String>,
        assignment: &mut BTreeMap<String, VertexId>,
        assigned_values: &mut BTreeSet<VertexId>,
        matches: &mut Vec<BTreeMap<String, VertexId>>,
    ) -> GraphStoreResult<()> {
        if unassigned.is_empty() {
            matches.push(assignment.clone());
            return Ok(());
        }
        // Pick the variable with the fewest candidates first (the
        // minimum-remaining-values heuristic).
        let pick: String = unassigned
            .iter()
            .min_by_key(|v| candidates.get(*v).map_or(usize::MAX, Vec::len))
            .cloned()
            .ok_or_else(|| {
                GraphStoreError::CorruptGraph(
                    "temporal matcher has no variable to assign".to_string(),
                )
            })?;

        let cands: Vec<VertexId> = candidates.get(&pick).cloned().ok_or_else(|| {
            GraphStoreError::CorruptGraph(format!(
                "temporal matcher has no candidates entry for variable {pick:?}"
            ))
        })?;
        unassigned.remove(&pick);

        for vid in cands {
            if assigned_values.contains(&vid) {
                continue;
            }
            assignment.insert(pick.clone(), vid);
            assigned_values.insert(vid);

            if self.validate_edges_for(store, &pick, var_edges, assignment)? {
                self.backtrack(
                    store,
                    candidates,
                    var_edges,
                    unassigned,
                    assignment,
                    assigned_values,
                    matches,
                )?;
            }

            assignment.remove(&pick);
            assigned_values.remove(&vid);
        }

        unassigned.insert(pick);
        Ok(())
    }

    fn validate_edges_for<G: GraphStore>(
        &self,
        store: &G,
        var: &str,
        var_edges: &BTreeMap<String, Vec<usize>>,
        assignment: &BTreeMap<String, VertexId>,
    ) -> GraphStoreResult<bool> {
        let Some(edges) = var_edges.get(var) else {
            return Ok(true);
        };
        for &ei in edges {
            let ep = &self.pattern.edge_patterns[ei];
            let (Some(&src_id), Some(&tgt_id)) = (
                assignment.get(&ep.source_var),
                assignment.get(&ep.target_var),
            ) else {
                continue;
            };
            let mut found = false;
            for eid in store.out_edge_ids(src_id, self.graph)? {
                let edge = store.get_edge(eid).ok_or_else(|| {
                    GraphStoreError::CorruptGraph(format!(
                        "temporal matcher references missing edge {eid}"
                    ))
                })?;
                if edge.target_id != tgt_id {
                    continue;
                }
                if let Some(label) = &ep.label {
                    if edge.label != *label {
                        continue;
                    }
                }
                if !ep.constraints.iter().all(|c| c.matches(edge)) {
                    continue;
                }
                if !self.temporal_filter.is_valid(&edge.properties)? {
                    continue;
                }
                found = true;
                break;
            }
            if !found {
                return Ok(false);
            }
        }
        Ok(true)
    }

    fn collect_match_edges<G: GraphStore>(
        &self,
        store: &G,
        assignment: &BTreeMap<String, VertexId>,
    ) -> GraphStoreResult<Vec<EdgeId>> {
        let mut edge_ids: BTreeSet<EdgeId> = BTreeSet::new();
        for ep in &self.pattern.edge_patterns {
            let (Some(&src_id), Some(&tgt_id)) = (
                assignment.get(&ep.source_var),
                assignment.get(&ep.target_var),
            ) else {
                continue;
            };
            for eid in store.out_edge_ids(src_id, self.graph)? {
                let edge = store.get_edge(eid).ok_or_else(|| {
                    GraphStoreError::CorruptGraph(format!(
                        "temporal match result references missing edge {eid}"
                    ))
                })?;
                if edge.target_id == tgt_id
                    && (ep.label.as_deref().is_none_or(|l| edge.label == l))
                    && self.temporal_filter.is_valid(&edge.properties)?
                {
                    edge_ids.insert(eid);
                    break;
                }
            }
        }
        Ok(edge_ids.into_iter().collect())
    }
}

fn validate_score(score: f64) -> GraphStoreResult<()> {
    if score.is_finite() {
        Ok(())
    } else {
        Err(GraphStoreError::InvalidMutation(
            "temporal graph score must be finite".to_string(),
        ))
    }
}

fn graph_id_value(id: u64) -> Value {
    i64::try_from(id).map_or_else(|_| Value::Bytes(id.to_be_bytes().to_vec()), Value::Int)
}