dfir_lang/graph/ops/
cross_join_multiset.rs

1use quote::quote_spanned;
2use syn::parse_quote;
3
4use super::{OperatorCategory, OperatorConstraints, WriteContextArgs, RANGE_1};
5
6/// > 2 input streams of type S and T, 1 output stream of type (S, T)
7///
8/// Forms the multiset cross-join (Cartesian product) of the (possibly duplicated) items in the input streams, returning all
9/// tupled pairs regardless of duplicates.
10///
11/// ```dfir
12/// source_iter(vec!["happy", "happy", "sad"]) -> [0]my_join;
13/// source_iter(vec!["dog", "cat", "cat"]) -> [1]my_join;
14/// my_join = cross_join_multiset() -> sort() -> assert_eq([
15///     ("happy", "cat"),
16///     ("happy", "cat"),
17///     ("happy", "cat"),
18///     ("happy", "cat"),
19///     ("happy", "dog"),
20///     ("happy", "dog"),
21///     ("sad", "cat"),
22///     ("sad", "cat"),
23///     ("sad", "dog"), ]);
24/// ```
25pub const CROSS_JOIN_MULTISET: OperatorConstraints = OperatorConstraints {
26    name: "cross_join_multiset",
27    categories: &[OperatorCategory::MultiIn],
28    hard_range_inn: &(2..=2),
29    soft_range_inn: &(2..=2),
30    hard_range_out: RANGE_1,
31    soft_range_out: RANGE_1,
32    num_args: 0,
33    persistence_args: &(0..=2),
34    type_args: &(0..=1),
35    is_external_input: false,
36    has_singleton_output: false,
37    flo_type: None,
38    ports_inn: Some(|| super::PortListSpec::Fixed(parse_quote! { 0, 1 })),
39    ports_out: None,
40    input_delaytype_fn: |_| None,
41    write_fn: |wc @ &WriteContextArgs {
42                   op_span,
43                   ident,
44                   inputs,
45                   ..
46               },
47               diagnostics| {
48        let mut output = (super::join_multiset::JOIN_MULTISET.write_fn)(wc, diagnostics)?;
49
50        let lhs = &inputs[0];
51        let rhs = &inputs[1];
52        let write_iterator = output.write_iterator;
53        output.write_iterator = quote_spanned!(op_span=>
54            let #lhs = #lhs.map(|a| ((), a));
55            let #rhs = #rhs.map(|b| ((), b));
56            #write_iterator
57            let #ident = #ident.map(|((), (a, b))| (a, b));
58        );
59
60        Ok(output)
61    },
62};