flowlog-build 0.3.4

Build-time FlowLog compiler for library mode.
Documentation
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Per-IDB output buffers + drain codegen.
//!
//! Pipeline: inspect → per-worker buffer → flush → drain (optional
//! ORDER BY / LIMIT) → sink.
//!
//! Buffers store `(data, time, diff)` triples. Batch mode hardcodes
//! `diff = 1` (DD uses `Present`, not `i32`). Sort operates on data only.

use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::Index;

use crate::codegen::CodeGen;
use crate::codegen::ty::tuple_type;
use crate::parser::{DataType, Relation};
use crate::profiler::{Profiler, with_profiler};

// =========================================================================
// Output struct
// =========================================================================

/// Per-IDB buffer machinery spliced into the generated `main()`.
#[derive(Default)]
pub(crate) struct InspectorCodegen {
    pub buf_declarations: Vec<TokenStream>, // before timely::execute
    pub buf_clones: Vec<TokenStream>,       // closure capture
    pub local_decls: Vec<TokenStream>,      // worker body, before dataflow
    pub inspect_stmts: Vec<TokenStream>,    // inside dataflow
    pub flush_stmts: Vec<TokenStream>,      // before barrier (all workers)
    pub size_cell_decls: Vec<TokenStream>,  // `.printsize` size cells, before execute
    pub size_cell_clones: Vec<TokenStream>, // size cell closure capture
}

// =========================================================================
// Orchestration
// =========================================================================

impl CodeGen {
    /// Walk IDB relations → fill [`InspectorCodegen`].
    pub(crate) fn collect_inspectors(
        &mut self,
        profiler: &mut Option<Profiler>,
    ) -> InspectorCodegen {
        let mut cg = InspectorCodegen::default();

        with_profiler(profiler, |p| p.update_inspect_block());

        for idb in self.program.idbs() {
            let var = self.find_global_ident(idb.fingerprint());
            let name = idb.name();
            let data_type = idb.data_type();

            if idb.printsize() {
                self.features.mark_as_collection();
                self.features.mark_timely_map();
                let cell_ident = Ident::new(&format!("size_{}", name), Span::call_site());
                cg.size_cell_decls.push(quote! {
                    let #cell_ident: std::sync::Arc<std::sync::Mutex<(Ts, i32)>> =
                        std::sync::Arc::new(std::sync::Mutex::new(<(Ts, i32)>::default()));
                });
                cg.size_cell_clones.push(quote! {
                    let #cell_ident = #cell_ident.clone();
                });
                cg.inspect_stmts.push(self.gen_size_inspector(
                    &var,
                    idb.raw_name(),
                    &cell_ident,
                    profiler,
                ));
            }

            if data_type
                .iter()
                .any(|dt| matches!(dt, DataType::Float32 | DataType::Float64))
            {
                self.features.mark_ordered_float();
            }

            if idb.output() {
                if data_type.contains(&DataType::String) {
                    self.features.mark_string_resolve_out();
                }

                self.features.mark_output_buffers();

                // Both file sinks build rows via `gen_row_bytes`: `itoa` for
                // integer columns and the incremental diff, `rayon` for the
                // parallel drain. Stderr uses neither. The scaffold gates the
                // deps on these marks.
                if !self.config.output_to_stdout() && !data_type.is_empty() {
                    if data_type.iter().any(DataType::is_integer) || self.config.is_incremental() {
                        self.features.mark_itoa();
                    }
                    if idb.uses_parallel_file_drain(self.config.output_to_stdout()) {
                        self.features.mark_parallel_output();
                    }
                }

                // Wiring (first arg) is the collection binding feeding the
                // sink; the label (second arg) is the human-facing name.
                if self.config.output_to_stdout() {
                    with_profiler(profiler, |p| {
                        p.inspect_content_terminal_operator(
                            var.to_string(),
                            idb.raw_name().to_string(),
                        );
                    });
                } else {
                    with_profiler(profiler, |p| {
                        p.inspect_content_file_operator(
                            var.to_string(),
                            idb.raw_name().to_string(),
                        );
                    });
                }

                let (buf_decl, buf_clone, buf_ident) = self.gen_buf_declaration(name, idb);
                cg.buf_declarations.push(buf_decl);
                cg.buf_clones.push(buf_clone);

                let (local_decl, inspect, flush) =
                    self.gen_write_inspector_mem(&var, &buf_ident, idb);
                cg.local_decls.push(local_decl);
                cg.inspect_stmts.push(inspect);
                cg.flush_stmts.push(flush);
            }
        }

        cg
    }
}

// =========================================================================
// Printsize
// =========================================================================

impl CodeGen {
    /// `.printsize` — consolidate into a single key, inspect the multiplicity.
    ///
    /// Datalog-batch: `.consolidate()` dedup.  Others: `threshold_i32()` first.
    fn gen_size_inspector(
        &self,
        var: &Ident,
        display: &str,
        cell_ident: &Ident,
        profiler: &mut Option<Profiler>,
    ) -> TokenStream {
        let maybe_probe = if self.config.is_incremental() {
            quote! { .probe_with(&mut probe) }
        } else {
            quote! {}
        };

        // Wiring (first arg) is the collection binding feeding the sink;
        // the label (second arg) is the human-facing name.
        with_profiler(profiler, |p| {
            p.inspect_size_operator(var.to_string(), display.to_string());
        });

        // The inspect fires once per epoch with `size` = the epoch's delta
        // (batch: single epoch → final count). Always overwrite — the cell
        // reports the most recent epoch's size-delta. Downstream consumers
        // surface it to stderr / file / typed API on their own terms.
        let dedup = if self.config.is_datalog_batch() {
            quote! {
                .consolidate()
                .inner
                .flat_map(move |(_, t, _)| std::iter::once(((), t.clone(), 1_i32)))
            }
        } else {
            quote! {
                .threshold(|_, w| if *w > 0 { 1i32 } else { 0 })
                .inner
                .flat_map(move |(_, t, d)| std::iter::once(((), t.clone(), d)))
            }
        };

        quote! {{
            let #cell_ident = #cell_ident.clone();
            #var.clone()
                #dedup
                .as_collection()
                .map(|_| ())
                .consolidate()
                .inspect(move |(_data, time, size)| {
                    *#cell_ident.lock().unwrap() = (time.clone(), *size);
                })
                #maybe_probe;
        }}
    }
}

// =========================================================================
// Buffer lifecycle
// =========================================================================

impl CodeGen {
    /// Shared buffer: `Arc<Mutex<Vec<Vec<T>>>>`.
    /// Worker 0 drains after barrier.
    fn gen_buf_declaration(&self, name: &str, idb: &Relation) -> (TokenStream, TokenStream, Ident) {
        let buf_ident = Ident::new(&format!("buf_{}", name), Span::call_site());
        let inner_ty = tuple_type(idb, self.features.string_intern());

        let declaration = quote! {
            let #buf_ident: Arc<Mutex<Vec<Vec<#inner_ty>>>> =
                Arc::new(Mutex::new(Vec::new()));
        };

        let clone_stmt = quote! {
            let #buf_ident = #buf_ident.clone();
        };

        (declaration, clone_stmt, buf_ident)
    }

    /// Local buffer: `Rc<RefCell<Vec<T>>>` — lock-free hot-path writes.
    ///
    /// Flushed into the shared buffer once at barrier via `std::mem::take`.
    /// Returns `(local_decl, inspect_stmt, flush_stmt)`.
    fn gen_write_inspector_mem(
        &self,
        var: &Ident,
        buf_ident: &Ident,
        idb: &Relation,
    ) -> (TokenStream, TokenStream, TokenStream) {
        let (maybe_consolidate, maybe_probe) = if self.config.is_incremental() {
            (
                quote! { .consolidate() },
                quote! { .probe_with(&mut probe) },
            )
        } else {
            (quote! {}, quote! {})
        };
        let local_ident = Ident::new(&format!("local_{}", idb.name()), Span::call_site());

        // The four cases below are independent: arity==0 picks the data
        // half (unit-typed key vs cloneable tuple), is_batch picks the
        // diff half (DD's `Present` is hardcoded to 1_i32 in batch mode).
        let (data_pat, data_expr) = if idb.arity() == 0 {
            (quote! { _data }, quote! { () })
        } else {
            (quote! { data }, quote! { data.clone() })
        };
        let (diff_pat, diff_expr) = if self.config.is_batch() {
            (quote! { _diff }, quote! { 1_i32 })
        } else {
            (quote! { diff }, quote! { *diff })
        };
        let inspect_pattern = quote! { (#data_pat, time, #diff_pat) };
        let push_stmt = quote! {
            #local_ident
                .borrow_mut()
                .push((#data_expr, time.clone(), #diff_expr));
        };

        let inner_ty = tuple_type(idb, self.features.string_intern());

        let local_decl = quote! {
            let #local_ident: Rc<RefCell<Vec<#inner_ty>>> =
                Rc::new(RefCell::new(Vec::new()));
        };

        let inspect_stmt = quote! {{
            let #local_ident = #local_ident.clone();
            #var
                #maybe_consolidate
                .inspect(move |#inspect_pattern| {
                    #push_stmt
                })
                #maybe_probe;
        }};

        let flush_stmt = quote! {
            #buf_ident.lock().unwrap().push(std::mem::take(&mut *#local_ident.borrow_mut()));
        };

        (local_decl, inspect_stmt, flush_stmt)
    }
}

// =========================================================================
// Drain & merge
// =========================================================================

/// Emit the per-IDB drain block: pull worker buffers, optionally sort/limit,
/// then iterate with `write_row` against the sink set up by `sink_preamble`.
///
/// `write_row` runs once per row with `row: &(tuple, Ts, i32)` in scope.
/// `sink_postamble` runs once after the last row, with the preamble's
/// bindings still in scope — file sinks use it to `flush()` explicitly
/// (relying on `BufWriter::Drop` would silently swallow flush errors).
/// The block evaluates to `()`; callers that need to capture a value (e.g.
/// library mode pushing into a typed `Vec`) should pre-declare the target
/// binding outside the block and have `write_row` mutate it.
pub fn gen_drain_block(
    buf_ident: &Ident,
    idb: &Relation,
    sink_preamble: TokenStream,
    write_row: TokenStream,
    sink_postamble: TokenStream,
    string_intern: bool,
) -> TokenStream {
    let order_by = idb.output_order_by();
    let limit = idb.output_limit();
    let elem_ty = tuple_type(idb, string_intern);

    match (order_by.as_ref(), limit) {
        (None, _) => quote! {{
            #sink_preamble
            for worker_buf in #buf_ident.lock().unwrap().drain(..) {
                for row in &worker_buf {
                    #write_row
                }
            }
            #sink_postamble
        }},

        (Some(spec), None) => {
            let cmp_body_sort = order_comparators(spec, string_intern);
            let cmp_body_merge = cmp_body_sort.clone();
            quote! {{
                let mut per_worker: Vec<Vec<#elem_ty>> =
                    std::mem::take(&mut *#buf_ident.lock().unwrap());
                for buf in per_worker.iter_mut() {
                    buf.sort_by(|a: &#elem_ty, b: &#elem_ty| {
                        #(#cmp_body_sort)*
                        std::cmp::Ordering::Equal
                    });
                }
                #sink_preamble
                ::flowlog_runtime::sort::k_way_merge(
                    per_worker,
                    |a: &#elem_ty, b: &#elem_ty| {
                        #(#cmp_body_merge)*
                        std::cmp::Ordering::Equal
                    },
                    |val| {
                        let row = &val;
                        #write_row
                    },
                );
                #sink_postamble
            }}
        }

        (Some(spec), Some(n)) => {
            let cmp_body = order_comparators(spec, string_intern);
            quote! {{
                let all: Vec<#elem_ty> = #buf_ident.lock().unwrap()
                    .drain(..).flatten().collect();
                let all = ::flowlog_runtime::sort::topk(all, #n, |a: &#elem_ty, b: &#elem_ty| {
                    #(#cmp_body)*
                    std::cmp::Ordering::Equal
                });
                #sink_preamble
                for row in &all {
                    #write_row
                }
                #sink_postamble
            }}
        }
    }
}

// =========================================================================
// Column + comparator helpers
// =========================================================================

/// Access column `col_idx` of a buffer row. `base` must evaluate to the
/// `(tuple, Ts, i32)` triple — produces `<base>.0.<col_idx>` and wraps with
/// `resolve_out()` for interned-string columns.
///
/// Output runs after fixpoint, so interned strings resolve through the flat
/// snapshot path (`resolve_out`) rather than the concurrent `DashMap`
/// (`resolve`) used while the dataflow is still interning.
pub fn field_accessor(
    col_idx: usize,
    data_type: &DataType,
    base: TokenStream,
    string_intern: bool,
) -> TokenStream {
    let idx = Index::from(col_idx);
    let inner = quote! { #base.0.#idx };
    if matches!(data_type, DataType::String) && string_intern {
        quote! { resolve_out(#inner) }
    } else {
        inner
    }
}

/// Comparator chain for ORDER BY — emits a sequence of statements suitable
/// for a `sort_by(|a, b| { ... std::cmp::Ordering::Equal })` closure body.
/// Compares by data columns only; time and diff are ignored.
pub(crate) fn order_comparators(
    spec: &[(usize, DataType, bool)],
    string_intern: bool,
) -> Vec<TokenStream> {
    spec.iter()
        .map(|(col_idx, data_type, ascending)| {
            let a_expr = field_accessor(*col_idx, data_type, quote! { a }, string_intern);
            let b_expr = field_accessor(*col_idx, data_type, quote! { b }, string_intern);
            let cmp_expr = if *ascending {
                quote! { #a_expr.cmp(&#b_expr) }
            } else {
                quote! { #b_expr.cmp(&#a_expr) }
            };
            quote! {
                let cmp = #cmp_expr;
                if cmp != std::cmp::Ordering::Equal { return cmp; }
            }
        })
        .collect()
}