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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//
//! Cost-based filter, intersection, and fusion-signal ordering.
use uqa_operators::OperatorTree;
use super::{tree_map::map_operator_children, QueryOptimizer};
impl QueryOptimizer {
// ---------------------------------------------------------------
// 2. Filter pushdown into Intersect
// ---------------------------------------------------------------
pub(super) fn push_filters_down(&self, op: OperatorTree) -> OperatorTree {
if let OperatorTree::Filter {
field,
predicate,
source: Some(s),
} = op
{
if let OperatorTree::Intersect(operands) = *s {
let mut new_operands: Vec<OperatorTree> = Vec::with_capacity(operands.len());
let mut any_pushed = false;
for child in operands {
if Self::filter_applies_to(&field, &child) {
new_operands.push(OperatorTree::Filter {
field: field.clone(),
predicate: predicate.clone(),
source: Some(Box::new(child)),
});
any_pushed = true;
} else {
new_operands.push(child);
}
}
if any_pushed {
let recursed: Vec<OperatorTree> = new_operands
.into_iter()
.map(|o| self.push_filters_down(o))
.collect();
return self.recurse_children(OperatorTree::Intersect(recursed));
}
// No push happened; rebuild the original Filter.
return OperatorTree::Filter {
field,
predicate,
source: Some(Box::new(
self.recurse_children(OperatorTree::Intersect(new_operands)),
)),
};
}
// Source is something else; just recurse through it.
return OperatorTree::Filter {
field,
predicate,
source: Some(Box::new(self.recurse_children(*s))),
};
}
self.recurse_children(op)
}
// ---------------------------------------------------------------
// 8. Reorder Intersect by estimated cardinality
// ---------------------------------------------------------------
pub(super) fn reorder_intersect(&self, op: OperatorTree) -> OperatorTree {
if let OperatorTree::Intersect(operands) = op {
let mut children: Vec<OperatorTree> = operands
.into_iter()
.map(|c| self.recurse_children(c))
.collect();
// Rank intersect arms by algebraic operator cost
// (`CostModel.estimate`), not the
// cardinality estimator. The two diverge for `Filter`,
// `Score`, `Traverse`, `RegularPathQuery`, fusion / hybrid
// / cross-paradigm join nodes, and any operator with a
// dedicated formula in `cost_model`.
children.sort_by(|a, b| {
let ca = self.cost_model.estimate(a, &self.index_stats);
let cb = self.cost_model.estimate(b, &self.index_stats);
ca.total_cmp(&cb)
});
return OperatorTree::Intersect(children);
}
self.recurse_children(op)
}
// ---------------------------------------------------------------
// 9. Reorder fusion signals
// ---------------------------------------------------------------
pub(super) fn reorder_fusion_signals(&self, op: OperatorTree) -> OperatorTree {
match op {
OperatorTree::BayesianEvidenceFusion { signals, base_rate } => {
let mut signals: Vec<_> = signals
.into_iter()
.map(|signal| self.reorder_fusion_signals(signal))
.collect();
signals.sort_by(|left, right| {
self.graph_aware_signal_cost(left)
.total_cmp(&self.graph_aware_signal_cost(right))
});
OperatorTree::BayesianEvidenceFusion { signals, base_rate }
}
OperatorTree::RobustPositiveEvidencePool {
signals,
alpha,
gating,
weights,
logit_min,
logit_max,
adaptive_weights,
} => {
let mut indexed_signals: Vec<(usize, OperatorTree)> = signals
.into_iter()
.enumerate()
.map(|(index, signal)| (index, self.reorder_fusion_signals(signal)))
.collect();
indexed_signals.sort_by(|(_, left), (_, right)| {
let ca = self.graph_aware_signal_cost(left);
let cb = self.graph_aware_signal_cost(right);
ca.total_cmp(&cb)
});
let order: Vec<usize> = indexed_signals
.iter()
.map(|(original_index, _)| *original_index)
.collect();
let reordered_weights =
weights.map(|values| order.iter().map(|index| values[*index]).collect());
let reordered_logit_min =
logit_min.map(|values| order.iter().map(|index| values[*index]).collect());
let reordered_logit_max =
logit_max.map(|values| order.iter().map(|index| values[*index]).collect());
OperatorTree::RobustPositiveEvidencePool {
signals: indexed_signals
.into_iter()
.map(|(_, signal)| signal)
.collect(),
alpha,
gating,
weights: reordered_weights,
logit_min: reordered_logit_min,
logit_max: reordered_logit_max,
adaptive_weights,
}
}
OperatorTree::ProbBoolFusion { signals, mode } => {
let mut sigs: Vec<OperatorTree> = signals
.into_iter()
.map(|s| self.reorder_fusion_signals(s))
.collect();
sigs.sort_by(|a, b| {
let ca = self.graph_aware_signal_cost(a);
let cb = self.graph_aware_signal_cost(b);
ca.total_cmp(&cb)
});
OperatorTree::ProbBoolFusion {
signals: sigs,
mode,
}
}
other => self.recurse_fusion(other),
}
}
pub(super) fn graph_aware_signal_cost(&self, signal: &OperatorTree) -> f64 {
let base = self.estimator.estimate(signal, &self.index_stats);
if self.graph_stats.is_some()
&& matches!(
signal,
OperatorTree::Traverse { .. }
| OperatorTree::PatternMatch { .. }
| OperatorTree::RegularPathQuery { .. }
)
{
base * 0.5
} else {
base
}
}
pub(super) fn recurse_fusion(&self, op: OperatorTree) -> OperatorTree {
map_operator_children(op, |child| self.reorder_fusion_signals(child))
}
pub(super) fn filter_applies_to(field: &str, target: &OperatorTree) -> bool {
match target {
OperatorTree::Term {
field: term_field, ..
}
| OperatorTree::Phrase {
field: term_field, ..
} => match term_field {
Some(f) => f == field,
None => true,
},
OperatorTree::Filter {
field: filter_field,
..
} => filter_field == field,
OperatorTree::Intersect(ops) => ops.iter().any(|c| Self::filter_applies_to(field, c)),
_ => false,
}
}
}