Skip to main content

uqa_sql/retrieval/
ir.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bound retrieval expressions contain values and configuration; execution models are constructed by the executor.
8
9use uqa_core::{
10    retrieval::{Direction, ExternalPriorMode, GatingSpec, MultiStageCutoff, TemporalFilterIR},
11    Predicate,
12};
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum TextScoringMode {
16    BM25,
17    BayesianBM25,
18}
19
20#[derive(Clone, Debug)]
21pub enum AttentionSpec {
22    Single {
23        alpha: f64,
24        normalized: bool,
25        base_rate: Option<f64>,
26    },
27    MultiHead {
28        n_heads: usize,
29        alpha: f64,
30        normalized: bool,
31    },
32}
33
34#[derive(Clone, Debug)]
35pub struct MultiStageEntry {
36    pub child: RetrievalExpr,
37    pub cutoff: MultiStageCutoff,
38}
39
40/// SQL-owned retrieval algebra. Physical scorers, fusers, closures, index choices and top-k strategies do not belong to this representation.
41#[derive(Clone, Debug)]
42pub enum RetrievalExpr {
43    Empty,
44    Term {
45        query: String,
46        field: Option<String>,
47        scoring: Option<TextScoringMode>,
48    },
49    /// Preserve the complete quoted input for one field-specific token-graph analysis.
50    Phrase {
51        query: String,
52        field: Option<String>,
53        scoring: Option<TextScoringMode>,
54    },
55    Filter {
56        field: String,
57        predicate: Predicate,
58        source: Option<Box<Self>>,
59    },
60    BayesianScore {
61        source: Box<Self>,
62        field: Option<String>,
63    },
64    BayesianMatchWithPrior {
65        field: String,
66        query: String,
67        prior_field: String,
68        mode: ExternalPriorMode,
69    },
70    Intersect(Vec<Self>),
71    Union(Vec<Self>),
72    Complement(Box<Self>),
73    Composed(Vec<Self>),
74    EncodeGraphPosting {
75        source: Box<Self>,
76    },
77    KNN {
78        query_vector: Vec<f32>,
79        k: usize,
80        field: String,
81    },
82    CalibratedVectorMatch {
83        query_vector: Vec<f32>,
84        k: usize,
85        field: String,
86        threshold: Option<f64>,
87    },
88    CosineProbability(Box<Self>),
89    BayesianEvidenceFusion {
90        signals: Vec<Self>,
91        base_rate: Option<f64>,
92    },
93    RobustPositiveEvidencePool {
94        signals: Vec<Self>,
95        alpha: f64,
96        gating: GatingSpec,
97        weights: Option<Vec<f64>>,
98        logit_min: Option<Vec<f64>>,
99        logit_max: Option<Vec<f64>>,
100        adaptive_weights: bool,
101    },
102    AttentionFusion {
103        signals: Vec<Self>,
104        options: AttentionSpec,
105        function_name: String,
106    },
107    LearnedFusion {
108        signals: Vec<Self>,
109        alpha: f64,
110    },
111    SparseThreshold {
112        source: Box<Self>,
113        threshold: f64,
114    },
115    Traverse {
116        start_vertex: u64,
117        graph: String,
118        label: Option<String>,
119        max_hops: usize,
120    },
121    GraphNeighbors {
122        vertex: u64,
123        graph: String,
124        label: Option<String>,
125        direction: Direction,
126    },
127    GraphEdges {
128        graph: String,
129        label: Option<String>,
130    },
131    RegularPathQuery {
132        rpq_source: String,
133        start_vertex: u64,
134        graph: String,
135    },
136    TemporalTraverse {
137        start_vertex: u64,
138        graph: String,
139        label: Option<String>,
140        max_hops: usize,
141        temporal_filter: Option<TemporalFilterIR>,
142    },
143    PageRank {
144        graph: String,
145    },
146    HITS {
147        graph: String,
148    },
149    BetweennessCentrality {
150        graph: String,
151    },
152    DeepPredict {
153        model: String,
154    },
155    MultiStage {
156        stages: Vec<MultiStageEntry>,
157    },
158    MultiFieldSearch {
159        fields: Vec<String>,
160        queries: Vec<String>,
161        weights: Option<Vec<f64>>,
162    },
163    TextSimilarityJoin {
164        left: Box<Self>,
165        right: Box<Self>,
166        threshold: f64,
167    },
168    VectorSimilarityJoin {
169        left: Box<Self>,
170        right: Box<Self>,
171        threshold: f64,
172    },
173    GraphJoin {
174        left: Box<Self>,
175        right: Box<Self>,
176        label: Option<String>,
177        graph: String,
178    },
179    HybridJoin {
180        left: Box<Self>,
181        right: Box<Self>,
182    },
183    CrossParadigmJoin {
184        left: Box<Self>,
185        right: Box<Self>,
186    },
187}