Skip to main content

solverforge_scoring/stream/joiner/
plan.rs

1//! Typed condition planning: concrete indexes and exact residual matching.
2//!
3//! Equality conjunctions normalize into one heterogeneous composite hash key,
4//! including equality components separated by predicates or range conditions.
5//! Mixed plans index equality and retain the authored residual predicates.
6//! Ordered and interval plans execute their concrete candidate indexes; arbitrary
7//! predicates explicitly scan. No closure inspection or dynamic condition list.
8
9use super::match_condition::AndJoiner;
10use super::{EqualJoiner, FilteringJoiner};
11use super::{GreaterThanJoiner, GreaterThanOrEqualJoiner, LessThanJoiner, LessThanOrEqualJoiner};
12use super::{Joiner, OverlappingJoiner};
13mod composite;
14mod conjunction;
15pub use composite::{
16    ComposePlans, CompositeEquality, EqualityKeys, EqualityKind, EqualityPlan, ResidualKind,
17};
18mod executable;
19mod hash_scan;
20mod mixed_equality;
21pub use mixed_equality::EqualityWithResidual;
22mod ordered_interval;
23pub use executable::{CompileCondition, ExecutablePlan, IndexedPlan};
24
25/// Deterministic execution strategy compiled from a condition's structure.
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub enum Strategy {
28    /// Typed hash indexes on both inputs plus exact equality residual.
29    EquiHash,
30    /// Typed ordered bounds plus exact strict/inclusive residual.
31    OrderedScan,
32    /// Interval candidate narrowing plus exact half-open predicate.
33    IntervalNarrow,
34    /// Explicit opposite-input scan with the exact predicate.
35    Scan,
36}
37
38impl Strategy {
39    /// Prefer the most selective usable structural index; retain all residuals.
40    /// Equality > ordered > interval > explicit scan.
41    #[inline]
42    pub const fn combine(self, other: Strategy) -> Strategy {
43        match (self, other) {
44            (Strategy::EquiHash, _) | (_, Strategy::EquiHash) => Strategy::EquiHash,
45            (Strategy::OrderedScan, _) | (_, Strategy::OrderedScan) => Strategy::OrderedScan,
46            (Strategy::IntervalNarrow, _) | (_, Strategy::IntervalNarrow) => {
47                Strategy::IntervalNarrow
48            }
49            (Strategy::Scan, Strategy::Scan) => Strategy::Scan,
50        }
51    }
52}
53
54/// A condition with a statically planned execution strategy.
55///
56/// `check` is the exact residual semantic check over one borrowed left row
57/// and one right entity — the same predicate `matches` evaluates, exposed
58/// so operators apply residuals without re-dispatching through `Joiner`.
59pub trait PlannedCondition<Row, C>: Send + Sync {
60    /// The deterministic strategy for this condition's structure.
61    const STRATEGY: Strategy;
62
63    /// Exact residual check: must agree with `Joiner::matches`.
64    fn check(&self, row: &Row, c: &C) -> bool;
65}
66
67/// Reads the planned strategy for one condition value.
68#[inline]
69pub fn plan_strategy<Row, C, P>(condition: &P) -> Strategy
70where
71    P: PlannedCondition<Row, C>,
72{
73    let _ = condition;
74    P::STRATEGY
75}
76
77impl<Row, C, Fa, Fb, T, Mode> PlannedCondition<Row, C> for EqualJoiner<Fa, Fb, T, Mode>
78where
79    T: PartialEq,
80    Fa: Fn(&Row) -> T + Send + Sync,
81    Fb: Fn(&C) -> T + Send + Sync,
82{
83    const STRATEGY: Strategy = Strategy::EquiHash;
84
85    #[inline]
86    fn check(&self, row: &Row, c: &C) -> bool {
87        self.matches(row, c)
88    }
89}
90
91impl<Row, C, Fa, Fb, T> PlannedCondition<Row, C> for LessThanJoiner<Fa, Fb, T>
92where
93    T: Ord,
94    Fa: Fn(&Row) -> T + Send + Sync,
95    Fb: Fn(&C) -> T + Send + Sync,
96{
97    const STRATEGY: Strategy = Strategy::OrderedScan;
98
99    #[inline]
100    fn check(&self, row: &Row, c: &C) -> bool {
101        self.matches(row, c)
102    }
103}
104
105impl<Row, C, Fa, Fb, T> PlannedCondition<Row, C> for LessThanOrEqualJoiner<Fa, Fb, T>
106where
107    T: Ord,
108    Fa: Fn(&Row) -> T + Send + Sync,
109    Fb: Fn(&C) -> T + Send + Sync,
110{
111    const STRATEGY: Strategy = Strategy::OrderedScan;
112
113    #[inline]
114    fn check(&self, row: &Row, c: &C) -> bool {
115        self.matches(row, c)
116    }
117}
118
119impl<Row, C, Fa, Fb, T> PlannedCondition<Row, C> for GreaterThanJoiner<Fa, Fb, T>
120where
121    T: Ord,
122    Fa: Fn(&Row) -> T + Send + Sync,
123    Fb: Fn(&C) -> T + Send + Sync,
124{
125    const STRATEGY: Strategy = Strategy::OrderedScan;
126
127    #[inline]
128    fn check(&self, row: &Row, c: &C) -> bool {
129        self.matches(row, c)
130    }
131}
132
133impl<Row, C, Fa, Fb, T> PlannedCondition<Row, C> for GreaterThanOrEqualJoiner<Fa, Fb, T>
134where
135    T: Ord,
136    Fa: Fn(&Row) -> T + Send + Sync,
137    Fb: Fn(&C) -> T + Send + Sync,
138{
139    const STRATEGY: Strategy = Strategy::OrderedScan;
140
141    #[inline]
142    fn check(&self, row: &Row, c: &C) -> bool {
143        self.matches(row, c)
144    }
145}
146
147impl<Row, C, Fsa, Fea, Fsb, Feb, T> PlannedCondition<Row, C>
148    for OverlappingJoiner<Fsa, Fea, Fsb, Feb, T>
149where
150    T: Ord,
151    Fsa: Fn(&Row) -> T + Send + Sync,
152    Fea: Fn(&Row) -> T + Send + Sync,
153    Fsb: Fn(&C) -> T + Send + Sync,
154    Feb: Fn(&C) -> T + Send + Sync,
155{
156    const STRATEGY: Strategy = Strategy::IntervalNarrow;
157
158    #[inline]
159    fn check(&self, row: &Row, c: &C) -> bool {
160        self.matches(row, c)
161    }
162}
163
164impl<Row, C, F> PlannedCondition<Row, C> for FilteringJoiner<F>
165where
166    F: Fn(&Row, &C) -> bool + Send + Sync,
167{
168    const STRATEGY: Strategy = Strategy::Scan;
169
170    #[inline]
171    fn check(&self, row: &Row, c: &C) -> bool {
172        self.matches(row, c)
173    }
174}
175
176impl<Row, C, J1, J2> PlannedCondition<Row, C> for AndJoiner<J1, J2>
177where
178    J1: PlannedCondition<Row, C> + Joiner<Row, C>,
179    J2: PlannedCondition<Row, C> + Joiner<Row, C>,
180{
181    const STRATEGY: Strategy = J1::STRATEGY.combine(J2::STRATEGY);
182
183    #[inline]
184    fn check(&self, row: &Row, c: &C) -> bool {
185        self.matches(row, c)
186    }
187}