Skip to main content

solverforge_scoring/stream/
join_target.rs

1//! JoinTarget trait for single `.join()` dispatch on UniConstraintStream.
2//!
3//! Three impls cover all join patterns:
4//! 1. `EqualJoiner<KA, KA, K>` — self-join, returns `BiConstraintStream`
5//! 2. `(EB, EqualJoiner<KA, KB, K>)` — keyed cross-join, returns `Bi`
6//! 3. `(UniConstraintStream<...>, P)` — predicate cross-join, returns `Bi`
7
8use std::hash::Hash;
9
10use solverforge_core::score::Score;
11
12use super::bi_stream::BiConstraintStream;
13use super::collection_extract::CollectionExtract;
14use super::cross_bi_stream::{
15    Bi, BiPredicatePlan, BiUnaryPlan, ViewComparisonPlan, ViewOverlapPlan,
16};
17use super::filter::{UniBiFilter, UniFilter, UniLeftBiFilter, UniPairFilter};
18use super::joiner::plan::{CompileCondition, ExecutablePlan, IndexedPlan};
19use super::joiner::{
20    AndJoiner, EqualJoiner, GreaterThanJoiner, GreaterThanOrEqualJoiner, LessThanJoiner,
21    LessThanOrEqualJoiner, OverlappingJoiner, Symmetric,
22};
23use super::key_extract::EntityKeyAdapter;
24use super::relational::view_plan::{EntityKey, ViewEqualPlan};
25use super::relational::Leaf;
26use super::UniConstraintStream;
27
28/// Trait for single `.join()` dispatch.
29///
30/// `E` is the extractor type of the left stream.
31/// `F` is the filter type of the left stream.
32/// Implementors consume `self` and receive the left stream's extractor and filter,
33/// producing the appropriate cross-stream type.
34pub trait JoinTarget<S, A, E, F, Sc: Score> {
35    /// The resulting constraint stream type.
36    type Output;
37
38    /// Applies the join, consuming both the target and the left stream's components.
39    fn apply(self, extractor_a: E, filter_a: F) -> Self::Output;
40}
41
42/// Self-join: `.join(equal(|a: &A| a.key))` — pairs same-collection entities.
43impl<S, A, E, F, K, KA, Sc> JoinTarget<S, A, E, F, Sc> for EqualJoiner<KA, KA, K, Symmetric>
44where
45    S: Send + Sync + 'static,
46    A: Hash + PartialEq + Send + Sync + 'static,
47    E: CollectionExtract<S, Item = A>,
48    F: UniFilter<S, A>,
49    K: Eq + Hash + Clone + Send + Sync,
50    KA: Fn(&A) -> K + Send + Sync,
51    Sc: Score + 'static,
52{
53    type Output = BiConstraintStream<S, A, K, E, EntityKeyAdapter<KA>, UniBiFilter<F, A>, Sc>;
54
55    fn apply(self, extractor_a: E, filter_a: F) -> Self::Output {
56        let (key_fn, _) = self.into_keys();
57        let key_extractor = EntityKeyAdapter::new(key_fn);
58        let bi_filter = UniBiFilter::new(filter_a);
59        BiConstraintStream::new_self_join_with_filter(extractor_a, key_extractor, bi_filter)
60    }
61}
62
63/// Keyed cross-join: `.join((extractor_b, equal_bi(ka, kb)))` — pairs two collections by key.
64impl<S, A, B, E, F, EB, K, KA, KB, Mode, Sc> JoinTarget<S, A, E, F, Sc>
65    for (EB, EqualJoiner<KA, KB, K, Mode>)
66where
67    S: Send + Sync + 'static,
68    A: Send + Sync + 'static,
69    B: Send + Sync + 'static,
70    E: CollectionExtract<S, Item = A>,
71    F: UniFilter<S, A>,
72    EB: CollectionExtract<S, Item = B>,
73    K: Eq + Hash + Clone + Send + Sync,
74    KA: Fn(&A) -> K + Send + Sync,
75    KB: Fn(&B) -> K + Send + Sync,
76    Sc: Score + 'static,
77{
78    type Output = Bi<
79        S,
80        A,
81        B,
82        BiUnaryPlan<K, KA, KB>,
83        super::collection_extract::FilteredExtract<E, F>,
84        EB,
85        super::filter::TrueFilter,
86        Sc,
87    >;
88
89    fn apply(self, extractor_a: E, filter_a: F) -> Self::Output {
90        let (extractor_b, joiner) = self;
91        let (key_a, key_b) = joiner.into_keys();
92        Bi::from_condition(
93            super::collection_extract::FilteredExtract::new(extractor_a, filter_a),
94            extractor_b,
95            BiUnaryPlan::new(key_a, key_b),
96            super::filter::TrueFilter,
97        )
98    }
99}
100
101macro_rules! impl_comparison_join_target {
102    ($joiner:ident, $less:expr, $inclusive:expr) => {
103        impl<S, A, B, E, F, EB, K, KA, KB, Sc> JoinTarget<S, A, E, F, Sc>
104            for (EB, $joiner<KA, KB, K>)
105        where
106            S: Send + Sync + 'static,
107            A: Send + Sync + 'static,
108            B: Send + Sync + 'static,
109            E: CollectionExtract<S, Item = A>,
110            F: UniFilter<S, A>,
111            EB: CollectionExtract<S, Item = B>,
112            K: Ord + Clone + Send + Sync + 'static,
113            KA: Fn(&A) -> K + Send + Sync + 'static,
114            KB: Fn(&B) -> K + Send + Sync + 'static,
115            Sc: Score + 'static,
116        {
117            type Output = Bi<
118                S,
119                A,
120                B,
121                ViewComparisonPlan<K, KA, KB, $less, $inclusive>,
122                E,
123                EB,
124                UniLeftBiFilter<F, B>,
125                Sc,
126            >;
127
128            fn apply(self, extractor_a: E, filter_a: F) -> Self::Output {
129                let (extractor_b, joiner) = self;
130                let (key_a, key_b) = joiner.into_keys();
131                let bi_filter = UniLeftBiFilter::new(filter_a);
132                Bi::from_condition(
133                    extractor_a,
134                    extractor_b,
135                    ViewComparisonPlan::<K, KA, KB, $less, $inclusive>::new(key_a, key_b),
136                    bi_filter,
137                )
138            }
139        }
140    };
141}
142
143// Comparison conditions: all four directions, strict and inclusive. Authored
144// over entities, executed over leaf views by `ViewComparisonPlan`.
145impl_comparison_join_target!(LessThanJoiner, true, false);
146impl_comparison_join_target!(LessThanOrEqualJoiner, true, true);
147impl_comparison_join_target!(GreaterThanJoiner, false, false);
148impl_comparison_join_target!(GreaterThanOrEqualJoiner, false, true);
149
150/// Interval overlap over entity-authored bounds, executed over leaf views.
151impl<S, A, B, E, F, EB, K, SA, EA, SB, EBd, Sc> JoinTarget<S, A, E, F, Sc>
152    for (EB, OverlappingJoiner<SA, EA, SB, EBd, K>)
153where
154    S: Send + Sync + 'static,
155    A: Send + Sync + 'static,
156    B: Send + Sync + 'static,
157    E: CollectionExtract<S, Item = A>,
158    F: UniFilter<S, A>,
159    EB: CollectionExtract<S, Item = B>,
160    K: Ord + Clone + Send + Sync + 'static,
161    SA: Fn(&A) -> K + Send + Sync + 'static,
162    EA: Fn(&A) -> K + Send + Sync + 'static,
163    SB: Fn(&B) -> K + Send + Sync + 'static,
164    EBd: Fn(&B) -> K + Send + Sync + 'static,
165    Sc: Score + 'static,
166{
167    type Output =
168        Bi<S, A, B, ViewOverlapPlan<K, SA, EA, SB, EBd>, E, EB, UniLeftBiFilter<F, B>, Sc>;
169
170    fn apply(self, extractor_a: E, filter_a: F) -> Self::Output {
171        let (extractor_b, joiner) = self;
172        let (start_a, end_a, start_b, end_b) = joiner.into_bounds();
173        let bi_filter = UniLeftBiFilter::new(filter_a);
174        Bi::from_condition(
175            extractor_a,
176            extractor_b,
177            ViewOverlapPlan::new(start_a, end_a, start_b, end_b),
178            bi_filter,
179        )
180    }
181}
182
183/// Composed conjunction: each operand converts to its view plan, then the two
184/// compose into one concrete `AndJoiner` plan. No dynamic condition list.
185impl<S, A, B, E, F, EB, J1, J2, Sc> JoinTarget<S, A, E, F, Sc> for (EB, AndJoiner<J1, J2>)
186where
187    S: Send + Sync + 'static,
188    A: Send + Sync + 'static,
189    B: Send + Sync + 'static,
190    E: CollectionExtract<S, Item = A>,
191    F: UniFilter<S, A>,
192    EB: CollectionExtract<S, Item = B>,
193    J1: ToViewPlan<A, B> + 'static,
194    J2: ToViewPlan<A, B> + 'static,
195    AndJoiner<J1::ViewPlan, J2::ViewPlan>: CompileCondition<Plan: IndexedPlan> + 'static,
196    for<'a> <AndJoiner<J1::ViewPlan, J2::ViewPlan> as CompileCondition>::Plan:
197        ExecutablePlan<Leaf<'a, A>, Leaf<'a, B>>,
198    Sc: Score + 'static,
199{
200    type Output = Bi<
201        S,
202        A,
203        B,
204        <AndJoiner<J1::ViewPlan, J2::ViewPlan> as CompileCondition>::Plan,
205        E,
206        EB,
207        UniLeftBiFilter<F, B>,
208        Sc,
209    >;
210
211    fn apply(self, extractor_a: E, filter_a: F) -> Self::Output {
212        let (extractor_b, joiner) = self;
213        let (first, second) = joiner.into_parts();
214        let composed = AndJoiner::new(first.into_view_plan(), second.into_view_plan());
215        let bi_filter = UniLeftBiFilter::new(filter_a);
216        Bi::from_condition(extractor_a, extractor_b, composed.compile(), bi_filter)
217    }
218}
219
220/// Predicate cross-join: `.join((other_stream, |a, b| predicate))` — an
221/// explicit opposite-input scan under the exact predicate. The relationship
222/// compiles to `FilteringJoiner`, so no synthetic constant equality key is
223/// ever built for a non-indexable condition.
224impl<S, A, B, E, F, EB, FB, P, Sc> JoinTarget<S, A, E, F, Sc>
225    for (UniConstraintStream<S, B, EB, FB, Sc>, P)
226where
227    S: Send + Sync + 'static,
228    A: Send + Sync + 'static,
229    B: Send + Sync + 'static,
230    E: CollectionExtract<S, Item = A>,
231    F: UniFilter<S, A>,
232    EB: CollectionExtract<S, Item = B>,
233    FB: UniFilter<S, B>,
234    P: Fn(&A, &B) -> bool + Send + Sync + 'static,
235    Sc: Score + 'static,
236{
237    type Output = Bi<S, A, B, BiPredicatePlan<P>, E, EB, UniPairFilter<F, FB>, Sc>;
238
239    fn apply(self, extractor_a: E, filter_a: F) -> Self::Output {
240        let (other_stream, predicate) = self;
241        let (extractor_b, filter_b) = other_stream.into_parts();
242        let combined_filter = UniPairFilter::new(filter_a, filter_b);
243        Bi::from_condition(
244            extractor_a,
245            extractor_b,
246            BiPredicatePlan::new(predicate),
247            combined_filter,
248        )
249    }
250}
251
252/// Converts an entity-authored condition into the leaf-view plan the operator
253/// tree executes. Used by the conjunction impl to compose operands; the
254/// top-level equality, comparison, and overlap impls build their plans directly
255/// so the equality path keeps producing `BiUnaryPlan`.
256pub trait ToViewPlan<A, B> {
257    /// The leaf-view plan the condition converts to.
258    type ViewPlan;
259    /// Converts the condition into its leaf-view plan.
260    fn into_view_plan(self) -> Self::ViewPlan;
261}
262
263impl<A, B, K, KA, KB, Mode> ToViewPlan<A, B> for EqualJoiner<KA, KB, K, Mode>
264where
265    K: Eq + Hash + Clone,
266    KA: Fn(&A) -> K + Send + Sync,
267    KB: Fn(&B) -> K + Send + Sync,
268{
269    type ViewPlan = ViewEqualPlan<K, EntityKey<KA>, EntityKey<KB>>;
270    fn into_view_plan(self) -> Self::ViewPlan {
271        let (key_a, key_b) = self.into_keys();
272        ViewEqualPlan::new(EntityKey::new(key_a), EntityKey::new(key_b))
273    }
274}
275
276macro_rules! impl_to_view_plan_comparison {
277    ($joiner:ident, $less:expr, $inclusive:expr) => {
278        impl<A, B, K, KA, KB> ToViewPlan<A, B> for $joiner<KA, KB, K>
279        where
280            K: Ord + Clone,
281            KA: Fn(&A) -> K + Send + Sync,
282            KB: Fn(&B) -> K + Send + Sync,
283        {
284            type ViewPlan = ViewComparisonPlan<K, KA, KB, $less, $inclusive>;
285            fn into_view_plan(self) -> Self::ViewPlan {
286                let (key_a, key_b) = self.into_keys();
287                ViewComparisonPlan::new(key_a, key_b)
288            }
289        }
290    };
291}
292impl_to_view_plan_comparison!(LessThanJoiner, true, false);
293impl_to_view_plan_comparison!(LessThanOrEqualJoiner, true, true);
294impl_to_view_plan_comparison!(GreaterThanJoiner, false, false);
295impl_to_view_plan_comparison!(GreaterThanOrEqualJoiner, false, true);
296
297impl<A, B, K, SA, EA, SB, EBd> ToViewPlan<A, B> for OverlappingJoiner<SA, EA, SB, EBd, K>
298where
299    K: Ord + Clone,
300    SA: Fn(&A) -> K + Send + Sync,
301    EA: Fn(&A) -> K + Send + Sync,
302    SB: Fn(&B) -> K + Send + Sync,
303    EBd: Fn(&B) -> K + Send + Sync,
304{
305    type ViewPlan = ViewOverlapPlan<K, SA, EA, SB, EBd>;
306    fn into_view_plan(self) -> Self::ViewPlan {
307        let (start_a, end_a, start_b, end_b) = self.into_bounds();
308        ViewOverlapPlan::new(start_a, end_a, start_b, end_b)
309    }
310}
311
312impl<A, B, J1, J2> ToViewPlan<A, B> for AndJoiner<J1, J2>
313where
314    J1: ToViewPlan<A, B>,
315    J2: ToViewPlan<A, B>,
316{
317    type ViewPlan = AndJoiner<J1::ViewPlan, J2::ViewPlan>;
318    fn into_view_plan(self) -> Self::ViewPlan {
319        let (first, second) = self.into_parts();
320        AndJoiner::new(first.into_view_plan(), second.into_view_plan())
321    }
322}