jetro-core 0.5.11

jetro-core: parser, compiler, and VM for the Jetro JSON query language
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! Legacy per-shape pipeline execution path.
//!
//! Executes pipeline plans that have not been promoted to the composed or
//! columnar paths. Still the hot path for many common shapes; kept separate
//! from `composed_exec` so migration to the composed substrate can proceed
//! incrementally without breaking existing correctness.

use std::sync::Arc;

use crate::{
    data::context::{Env, EvalError},
    data::value::Val,
    vm::VM,
};

use super::nested::PreparedPlan;
use super::row_source;
use super::sink_accumulator::SinkAccumulator;
use super::{
    apply_item_in_env, cmp_val_total, compute_strategies_with_kernels, eval_kernel_with_vm,
    is_truthy, BodyKernel, Pipeline, PipelineBody, Sink, Source, Stage, StageFlow, StageStrategy,
    TerminalMapCollector,
};

use crate::builtins::{replace_apply, slice_apply, split_apply, BuiltinMethod};
use crate::plan::demand::PullDemand;

/// Runs the pipeline against `root`, materialising barrier stages then streaming the rest.
pub(super) fn run(
    pipeline: &Pipeline,
    root: &Val,
    base_env: &Env,
    vm: &mut VM,
) -> Result<Val, EvalError> {
    let mut loop_env = base_env.clone();

    let recv = row_source::resolve(&pipeline.source, root);

    let source_demand = pipeline.source_demand().chain.pull;
    let mut pulled_inputs: usize = 0;
    let mut emitted_outputs: usize = 0;

    let mut sink_acc = SinkAccumulator::new(&pipeline.sink);
    let membership_target = match &pipeline.sink {
        Sink::Membership(spec) => Some(eval_membership_target(spec, vm, &loop_env)?),
        _ => None,
    };
    if let Sink::Membership(spec) = &pipeline.sink {
        if pipeline.stages.is_empty() && row_source::array_like_rows(&recv).is_none() {
            return Ok(apply_membership_scalar_sink(
                spec,
                membership_target
                    .as_ref()
                    .expect("membership target exists"),
                &recv,
            ));
        }
    }

    let needs_barrier = pipeline
        .stages
        .iter()
        .any(Stage::requires_legacy_materialization);
    if !needs_barrier {
        return run_streaming_rows_with_vm(pipeline, base_env, row_source::source_iter(&recv), vm);
    }

    let pre_iter: LegacyPreIter = {
        let mut buf: Vec<Val> = match source_demand {
            PullDemand::FirstInput(n) => row_source::materialize_source_prefix(&recv, n),
            _ => row_source::materialize_source(&recv),
        };
        let strategies = compute_strategies_with_kernels(
            &pipeline.stages,
            &pipeline.stage_kernels,
            &pipeline.sink,
        );
        for (stage_idx, stage) in pipeline.stages.iter().enumerate() {
            let kernel = pipeline
                .stage_kernels
                .get(stage_idx)
                .unwrap_or(&BodyKernel::Generic);
            let strategy = strategies
                .get(stage_idx)
                .copied()
                .unwrap_or(StageStrategy::Default);
            if let Stage::CompiledMap(plan) = stage {
                let prepared = PreparedPlan::new(plan);
                let mut out: Vec<Val> = Vec::with_capacity(buf.len());
                for v in buf.into_iter() {
                    out.push(prepared.run(v)?);
                }
                buf = out;
                continue;
            }

            if let Some(applied) =
                apply_adapter_materialized(stage, &mut buf, vm, &mut loop_env, kernel, strategy)
            {
                applied?;
                continue;
            }
            unreachable!("descriptor-backed stage was not handled by materialized adapter")
        }
        LegacyPreIter::Owned(buf.into_iter())
    };

    'outer: for item in pre_iter {
        if matches!(source_demand, PullDemand::FirstInput(n) if pulled_inputs >= n) {
            break 'outer;
        }
        pulled_inputs += 1;

        let sink_done = match &pipeline.sink {
            Sink::Predicate(_) => {
                observe_predicate_sink_item(pipeline, item, &mut sink_acc, vm, &mut loop_env)?
            }
            Sink::Membership(spec) => sink_acc.observe_membership(
                spec.op,
                &item,
                membership_target
                    .as_ref()
                    .expect("membership target exists"),
            ),
            Sink::ArgExtreme(_) => {
                observe_arg_extreme_sink_item(pipeline, item, &mut sink_acc, vm, &mut loop_env)?
            }
            Sink::Reducer(_) => {
                match observe_reducer_item(pipeline, item, &mut sink_acc, vm, &mut loop_env)? {
                    ReducerItemFlow::Observed => false,
                    ReducerItemFlow::Skipped => continue 'outer,
                }
            }
            _ => sink_acc.push(item),
        };
        if sink_done {
            break 'outer;
        }
        emitted_outputs += 1;
        if matches!(source_demand, PullDemand::UntilOutput(n) if emitted_outputs >= n) {
            break 'outer;
        }
    }

    // Keyed reducers wrap their output in a single-element array; unwrap it so
    // terminal collection returns the reducer object.
    let unwrap_single_collect_obj = pipeline
        .stages
        .last()
        .and_then(Stage::descriptor)
        .is_some_and(|desc| {
            desc.method
                .is_some_and(|method| method.spec().keyed_reducer.is_some())
        });
    sink_acc.finish_result(unwrap_single_collect_obj)
}

/// Streams a pipeline directly from a `simd-json` tape; returns `None` when any stage requires materialisation.
#[allow(dead_code)]
pub(super) fn run_tape_field_chain(
    body: &PipelineBody,
    tape: &crate::data::tape::TapeData,
    keys: &[Arc<str>],
    base_env: &Env,
) -> Option<Result<Val, EvalError>> {
    let mut vm = VM::new();
    run_tape_field_chain_with_vm(body, tape, keys, base_env, &mut vm)
}

/// Streams a pipeline directly from a `simd-json` tape using caller-owned VM state.
pub(super) fn run_tape_field_chain_with_vm(
    body: &PipelineBody,
    tape: &crate::data::tape::TapeData,
    keys: &[Arc<str>],
    base_env: &Env,
    vm: &mut VM,
) -> Option<Result<Val, EvalError>> {
    if body
        .stages
        .iter()
        .any(Stage::requires_legacy_materialization)
    {
        return None;
    }
    if !body.can_run_with_materialized_receiver() {
        return None;
    }
    let source = row_source::TapeRowSource::from_field_chain(tape, keys);
    if !source.is_array_provider() {
        return None;
    }
    let pipeline = body.clone().with_source(Source::Receiver(Val::Null));
    Some(run_streaming_rows_with_vm(
        &pipeline,
        base_env,
        source.iter_materialized(),
        vm,
    ))
}

#[cfg(test)]
fn run_streaming_rows<I>(pipeline: &Pipeline, base_env: &Env, iter: I) -> Result<Val, EvalError>
where
    I: IntoIterator<Item = Val>,
{
    let mut vm = VM::new();
    run_streaming_rows_with_vm(pipeline, base_env, iter, &mut vm)
}

fn run_streaming_rows_with_vm<I>(
    pipeline: &Pipeline,
    base_env: &Env,
    iter: I,
    vm: &mut VM,
) -> Result<Val, EvalError>
where
    I: IntoIterator<Item = Val>,
{
    let mut loop_env = base_env.clone();
    let source_demand = pipeline.source_demand().chain.pull;
    let late_projection = pipeline
        .can_apply_late_projection_from(0)
        .then(|| pipeline.late_projection.as_ref())
        .flatten()
        .filter(|_| pipeline.sink.supports_late_projection(source_demand));
    let stage_limit = late_projection
        .map(|projection| projection.prefix_len)
        .unwrap_or(pipeline.stages.len());
    let mut pulled_inputs: usize = 0;
    let mut emitted_outputs: usize = 0;
    let mut stage_taken: Vec<usize> = vec![0; pipeline.stages.len()];
    let mut stage_skipped: Vec<usize> = vec![0; pipeline.stages.len()];
    let mut sink_acc = SinkAccumulator::new(&pipeline.sink);
    let membership_target = match &pipeline.sink {
        Sink::Membership(spec) => Some(eval_membership_target(spec, vm, &loop_env)?),
        _ => None,
    };
    if source_demand.is_zero() {
        return sink_acc.finish_result(false);
    }
    let terminal_map_idx = if late_projection.is_none()
        && matches!(pipeline.sink, Sink::Collect)
        && pipeline
            .stages
            .last()
            .is_some_and(Stage::can_use_terminal_map_collector)
    {
        pipeline.stages.len().checked_sub(1)
    } else {
        None
    };
    let terminal_map_kernel = terminal_map_idx.map(|idx| {
        pipeline
            .stage_kernels
            .get(idx)
            .unwrap_or(&BodyKernel::Generic)
    });
    let mut terminal_map_collect = terminal_map_kernel.map(TerminalMapCollector::new);
    let prepared_nested: Vec<Option<PreparedPlan>> = pipeline
        .stages
        .iter()
        .map(|stage| match stage {
            Stage::CompiledMap(plan) => Some(PreparedPlan::new(plan)),
            _ => None,
        })
        .collect();

    'outer: for mut item in iter {
        if matches!(source_demand, PullDemand::FirstInput(n) if pulled_inputs >= n) {
            break 'outer;
        }
        if matches!(source_demand, PullDemand::NthInput(n) if pulled_inputs < n) {
            pulled_inputs += 1;
            continue 'outer;
        }
        pulled_inputs += 1;

        for (stage_idx, stage) in pipeline.stages[..stage_limit].iter().enumerate() {
            let kernel = pipeline
                .stage_kernels
                .get(stage_idx)
                .unwrap_or(&BodyKernel::Generic);
            match stage {
                Stage::CompiledMap(_) => {
                    item = prepared_nested[stage_idx]
                        .as_ref()
                        .expect("compiled map stages have prepared nested plans")
                        .run(item)?;
                }
                _ => match super::val_stage_flow::apply_adapter_streaming(
                    stage,
                    stage_idx,
                    item,
                    vm,
                    &mut loop_env,
                    kernel,
                    &mut stage_taken,
                    &mut stage_skipped,
                    terminal_map_idx,
                    &mut terminal_map_collect,
                )? {
                    StageFlow::Continue(next) => item = next,
                    StageFlow::SkipRow => continue 'outer,
                    StageFlow::Stop => break 'outer,
                    StageFlow::TerminalCollected => {
                        emitted_outputs += 1;
                        if matches!(source_demand, PullDemand::UntilOutput(n) if emitted_outputs >= n)
                        {
                            break 'outer;
                        }
                        continue 'outer;
                    }
                },
            }
        }

        if matches!(source_demand, PullDemand::NthInput(_)) && matches!(pipeline.sink, Sink::Nth(_))
        {
            if let Some(projection) = late_projection {
                return eval_late_projection(&projection.kernel, &item, vm);
            }
            return Ok(item);
        }

        if let Some(projection) = late_projection {
            item = eval_late_projection(&projection.kernel, &item, vm)?;
        }

        let sink_done = match &pipeline.sink {
            Sink::Predicate(_) => {
                observe_predicate_sink_item(pipeline, item, &mut sink_acc, vm, &mut loop_env)?
            }
            Sink::Membership(spec) => sink_acc.observe_membership(
                spec.op,
                &item,
                membership_target
                    .as_ref()
                    .expect("membership target exists"),
            ),
            Sink::ArgExtreme(_) => {
                observe_arg_extreme_sink_item(pipeline, item, &mut sink_acc, vm, &mut loop_env)?
            }
            Sink::Reducer(_) => {
                match observe_reducer_item(pipeline, item, &mut sink_acc, vm, &mut loop_env)? {
                    ReducerItemFlow::Observed => false,
                    ReducerItemFlow::Skipped => continue 'outer,
                }
            }
            _ => sink_acc.push(item),
        };
        if sink_done {
            break 'outer;
        }
        emitted_outputs += 1;
        if matches!(source_demand, PullDemand::UntilOutput(n) if emitted_outputs >= n) {
            break 'outer;
        }
    }

    if let Some(collector) = terminal_map_collect {
        return Ok(collector.finish());
    }
    sink_acc.finish_result(false)
}

fn eval_late_projection(
    projection: &BodyKernel,
    item: &Val,
    vm: &mut crate::vm::VM,
) -> Result<Val, EvalError> {
    eval_kernel_with_vm(projection, item, vm, |_, _| {
        Err(EvalError(
            "late projection requires a native body kernel".to_string(),
        ))
    })
}

// barrier stages always produce a Vec<Val>, so only the Owned variant is needed here
enum LegacyPreIter {
    Owned(std::vec::IntoIter<Val>),
}

// returns None for unrecognised stage types so the caller can unreachable!()
fn apply_adapter_materialized(
    stage: &Stage,
    buf: &mut Vec<Val>,
    vm: &mut crate::vm::VM,
    loop_env: &mut Env,
    kernel: &BodyKernel,
    strategy: StageStrategy,
) -> Option<Result<(), EvalError>> {
    // Trait dispatch for migrated barrier methods.
    if let Some(method) = stage.descriptor().and_then(|d| d.method) {
        let body = stage.body_program();
        let mut ctx = crate::builtins::builtin::BarrierCtx {
            vm,
            env: loop_env,
            kernel,
            stage,
            strategy,
        };
        use crate::builtins::{builtin::Builtin, defs, BuiltinMethod as M};
        let trait_result = match method {
            M::Reverse => <defs::Reverse as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::Sort => <defs::Sort as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::Window => <defs::Window as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::Chunk => <defs::Chunk as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::GroupBy => <defs::GroupBy as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::CountBy => <defs::CountBy as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::IndexBy => <defs::IndexBy as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::Filter | M::Find | M::FindAll => {
                <defs::Filter as Builtin>::apply_barrier(&mut ctx, buf, body)
            }
            M::Map => <defs::Map as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::FlatMap => <defs::FlatMap as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::Unique => <defs::Unique as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::UniqueBy => <defs::UniqueBy as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::TakeWhile => <defs::TakeWhile as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::DropWhile => <defs::DropWhile as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::Take => <defs::Take as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::Skip => <defs::Skip as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::FindIndex => <defs::FindIndex as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::IndicesWhere => <defs::IndicesWhere as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::MaxBy => <defs::MaxBy as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::MinBy => <defs::MinBy as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::TransformKeys => {
                <defs::TransformKeys as Builtin>::apply_barrier(&mut ctx, buf, body)
            }
            M::TransformValues => {
                <defs::TransformValues as Builtin>::apply_barrier(&mut ctx, buf, body)
            }
            M::FilterKeys => <defs::FilterKeys as Builtin>::apply_barrier(&mut ctx, buf, body),
            M::FilterValues => <defs::FilterValues as Builtin>::apply_barrier(&mut ctx, buf, body),
            _ => None,
        };
        if let Some(r) = trait_result {
            return Some(r);
        }
    }
    // Remaining barrier dispatch by Stage variant — all other variants are handled
    // above by Builtin::apply_barrier trait dispatch and never reach this point.
    match stage {
        Stage::Builtin(call) if call.method == BuiltinMethod::Compact => {
            buf.retain(|v| !matches!(v, Val::Null));
            Some(Ok(()))
        }
        Stage::Builtin(call) if call.method == BuiltinMethod::Remove => {
            if let crate::builtins::BuiltinArgs::Val(target) = &call.args {
                buf.retain(|v| !crate::util::vals_eq(v, target));
            }
            Some(Ok(()))
        }
        // Element-wise scalar (Slice, Replace, ReplaceAll, BuiltinCall::apply).
        Stage::Builtin(_) | Stage::IntRangeBuiltin { .. } | Stage::StringPairBuiltin { .. } => {
            let mut out: Vec<Val> = Vec::with_capacity(buf.len());
            for v in std::mem::take(buf) {
                out.push(apply_element_adapter(stage, v));
            }
            *buf = out;
            Some(Ok(()))
        }
        // Expanding scalar (Split).
        Stage::StringBuiltin { .. } => {
            let mut out: Vec<Val> = Vec::with_capacity(buf.len());
            for v in std::mem::take(buf) {
                apply_expanding_adapter(stage, &v, &mut out);
            }
            *buf = out;
            Some(Ok(()))
        }
        // Sorted-dedup barrier — pre-sorted dedup, optionally keyed.
        Stage::SortedDedup(opt_prog) => {
            match opt_prog {
                None => {
                    buf.sort_by(cmp_val_total);
                    buf.dedup_by(|a, b| crate::util::vals_eq(a, b));
                }
                Some(prog) => {
                    let mut keyed: Vec<(Val, Val)> = Vec::with_capacity(buf.len());
                    for v in buf.iter() {
                        let key = match eval_kernel_with_vm(kernel, v, vm, |item, vm| {
                            apply_item_in_env(vm, loop_env, item, prog)
                        }) {
                            Ok(key) => key,
                            Err(err) => return Some(Err(err)),
                        };
                        keyed.push((key, v.clone()));
                    }
                    keyed.sort_by(|a, b| cmp_val_total(&a.0, &b.0));
                    keyed.dedup_by(|a, b| crate::util::vals_eq(&a.0, &b.0));
                    *buf = keyed.into_iter().map(|(_, v)| v).collect();
                }
            }
            Some(Ok(()))
        }
        // All other variants handled above by trait dispatch — unreachable.
        _ => None,
    }
}

/// Applies an element-wise stage (`Slice`, string pair builtins, `Builtin`) to a single `Val` row.
pub(super) fn apply_element_adapter(stage: &Stage, v: Val) -> Val {
    match stage {
        Stage::IntRangeBuiltin {
            method: BuiltinMethod::Slice,
            start,
            end,
        } => slice_apply(v, *start, *end),
        Stage::StringPairBuiltin {
            method,
            first,
            second,
        } if matches!(*method, BuiltinMethod::Replace | BuiltinMethod::ReplaceAll) => {
            replace_apply(
                v.clone(),
                first,
                second,
                *method == BuiltinMethod::ReplaceAll,
            )
            .unwrap_or(v)
        }
        Stage::Builtin(call) => call.apply(&v).unwrap_or(v),
        _ => v,
    }
}

fn apply_expanding_adapter(stage: &Stage, v: &Val, out: &mut Vec<Val>) {
    if let Stage::StringBuiltin {
        method: BuiltinMethod::Split,
        value,
    } = stage
    {
        if let Some(Val::Arr(a)) = split_apply(v, value.as_ref()) {
            out.extend(Arc::try_unwrap(a).unwrap_or_else(|a| (*a).clone()));
        }
    }
}

impl Iterator for LegacyPreIter {
    type Item = Val;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Owned(iter) => iter.next(),
        }
    }
}

enum ReducerItemFlow {
    Observed,
    Skipped,
}

fn observe_reducer_item(
    pipeline: &Pipeline,
    item: Val,
    sink_acc: &mut SinkAccumulator<'_>,
    vm: &mut crate::vm::VM,
    loop_env: &mut Env,
) -> Result<ReducerItemFlow, EvalError> {
    let Sink::Reducer(spec) = &pipeline.sink else {
        sink_acc.push(item);
        return Ok(ReducerItemFlow::Observed);
    };

    if let Some(predicate) = &spec.predicate {
        let kernel_idx = spec.predicate_kernel_index().expect("predicate exists");
        let kernel = pipeline
            .sink_kernels
            .get(kernel_idx)
            .unwrap_or(&BodyKernel::Generic);
        let keep = eval_kernel_with_vm(kernel, &item, vm, |item, vm| {
            apply_item_in_env(vm, loop_env, item, predicate)
        })?;
        if !crate::util::is_truthy(&keep) {
            return Ok(ReducerItemFlow::Skipped);
        }
    }

    if let Some(project) = &spec.projection {
        let project_kernel_idx = spec.projection_kernel_index().expect("projection exists");
        let kernel = pipeline
            .sink_kernels
            .get(project_kernel_idx)
            .unwrap_or(&BodyKernel::Generic);
        let reducer_item = eval_kernel_with_vm(kernel, &item, vm, |item, vm| {
            apply_item_in_env(vm, loop_env, item, project)
        })?;
        sink_acc.push_projected_numeric(&reducer_item);
    } else {
        sink_acc.push(item);
    }

    Ok(ReducerItemFlow::Observed)
}

fn eval_membership_target(
    spec: &super::MembershipSinkSpec,
    vm: &mut crate::vm::VM,
    env: &Env,
) -> Result<Val, EvalError> {
    match &spec.target {
        super::MembershipSinkTarget::Literal(value) => Ok(value.clone()),
        super::MembershipSinkTarget::Program(program) => vm.exec_in_env(program, env),
    }
}

fn apply_membership_scalar_sink(spec: &super::MembershipSinkSpec, target: &Val, recv: &Val) -> Val {
    match spec.method {
        crate::builtins::BuiltinMethod::Includes => crate::builtins::includes_apply(recv, target),
        crate::builtins::BuiltinMethod::Index => {
            crate::builtins::index_value_apply(recv, target).unwrap_or(Val::Null)
        }
        crate::builtins::BuiltinMethod::IndicesOf => {
            crate::builtins::indices_of_apply(recv, target).unwrap_or(Val::Null)
        }
        _ => Val::Null,
    }
}

fn observe_predicate_sink_item(
    pipeline: &Pipeline,
    item: Val,
    sink_acc: &mut SinkAccumulator<'_>,
    vm: &mut crate::vm::VM,
    loop_env: &mut Env,
) -> Result<bool, EvalError> {
    let Sink::Predicate(spec) = &pipeline.sink else {
        return Ok(sink_acc.push(item));
    };

    let kernel_idx = spec.predicate_kernel_index();
    let kernel = pipeline
        .sink_kernels
        .get(kernel_idx)
        .unwrap_or(&BodyKernel::Generic);
    let predicate = eval_kernel_with_vm(kernel, &item, vm, |item, vm| {
        apply_item_in_env(vm, loop_env, item, &spec.predicate)
    })?;
    sink_acc.observe_predicate_item(spec.op, crate::util::is_truthy(&predicate), item)
}

fn observe_arg_extreme_sink_item(
    pipeline: &Pipeline,
    item: Val,
    sink_acc: &mut SinkAccumulator<'_>,
    vm: &mut crate::vm::VM,
    loop_env: &mut Env,
) -> Result<bool, EvalError> {
    let Sink::ArgExtreme(spec) = &pipeline.sink else {
        return Ok(sink_acc.push(item));
    };

    let kernel_idx = spec.key_kernel_index();
    let kernel = pipeline
        .sink_kernels
        .get(kernel_idx)
        .unwrap_or(&BodyKernel::Generic);
    let key = eval_kernel_with_vm(kernel, &item, vm, |item, vm| {
        apply_item_in_env(vm, loop_env, item, &spec.key)
    })?;
    sink_acc.observe_arg_extreme(spec.want_max, item, key);
    Ok(false)
}

/// Applies an object-lambda stage (`TransformKeys`, `TransformValues`, `FilterKeys`, `FilterValues`) to `recv`.
pub(crate) fn apply_lambda_obj(
    stage: &Stage,
    recv: &Val,
    vm: &mut crate::vm::VM,
    loop_env: &mut crate::data::context::Env,
    kernel: &BodyKernel,
    prog: &crate::vm::Program,
) -> Result<Val, EvalError> {
    let m = match recv.as_object() {
        Some(m) => m,
        None => return Ok(recv.clone()),
    };
    let mut out: indexmap::IndexMap<std::sync::Arc<str>, Val> =
        indexmap::IndexMap::with_capacity(m.len());
    for (k, v) in m.iter() {
        match stage {
            Stage::ExprBuiltin {
                method: BuiltinMethod::TransformKeys,
                ..
            } => {
                let k_val = Val::Str(k.clone());
                let new_k = eval_kernel_with_vm(kernel, &k_val, vm, |item, vm| {
                    apply_item_in_env(vm, loop_env, item, prog)
                })?;
                let new_k_arc = match new_k {
                    Val::Str(s) => s,
                    other => std::sync::Arc::from(crate::util::val_to_string(&other).as_str()),
                };
                out.insert(new_k_arc, v.clone());
            }
            Stage::ExprBuiltin {
                method: BuiltinMethod::TransformValues,
                ..
            } => {
                let new_v = eval_kernel_with_vm(kernel, v, vm, |item, vm| {
                    apply_item_in_env(vm, loop_env, item, prog)
                })?;
                out.insert(k.clone(), new_v);
            }
            Stage::ExprBuiltin {
                method: BuiltinMethod::FilterKeys,
                ..
            } => {
                let k_val = Val::Str(k.clone());
                if is_truthy(&eval_kernel_with_vm(kernel, &k_val, vm, |item, vm| {
                    apply_item_in_env(vm, loop_env, item, prog)
                })?) {
                    out.insert(k.clone(), v.clone());
                }
            }
            Stage::ExprBuiltin {
                method: BuiltinMethod::FilterValues,
                ..
            } => {
                if is_truthy(&eval_kernel_with_vm(kernel, v, vm, |item, vm| {
                    apply_item_in_env(vm, loop_env, item, prog)
                })?) {
                    out.insert(k.clone(), v.clone());
                }
            }
            _ => unreachable!("apply_lambda_obj called with non-Obj-lambda Stage"),
        }
    }
    Ok(Val::obj(out))
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::rc::Rc;
    use std::sync::Arc;

    use crate::data::context::Env;
    use crate::data::value::Val;
    use crate::parse::ast::BinOp;

    use super::super::{
        BodyKernel, MembershipSinkOp, MembershipSinkSpec, MembershipSinkTarget, PipelineBody,
        PredicateSinkOp, PredicateSinkSpec, Sink, Source,
    };

    struct CountingRows {
        next: i64,
        end: i64,
        reads: Rc<Cell<usize>>,
    }

    impl CountingRows {
        fn new(end: i64, reads: Rc<Cell<usize>>) -> Self {
            Self {
                next: 1,
                end,
                reads,
            }
        }
    }

    impl Iterator for CountingRows {
        type Item = Val;

        fn next(&mut self) -> Option<Self::Item> {
            if self.next > self.end {
                return None;
            }
            self.reads.set(self.reads.get() + 1);
            let value = self.next;
            self.next += 1;
            Some(Val::Int(value))
        }
    }

    fn empty_pipeline(sink: Sink, sink_kernels: Vec<BodyKernel>) -> super::Pipeline {
        PipelineBody {
            stages: Vec::new(),
            stage_exprs: Vec::new(),
            sink,
            stage_kernels: Vec::new(),
            sink_kernels,
        }
        .with_source(Source::Receiver(Val::Null))
    }

    #[test]
    fn materialized_streaming_stops_when_any_sink_matches() {
        let reads = Rc::new(Cell::new(0));
        let pipeline = empty_pipeline(
            Sink::Predicate(PredicateSinkSpec {
                op: PredicateSinkOp::Any,
                predicate: Arc::new(crate::vm::Program::new(Vec::new(), "")),
            }),
            vec![BodyKernel::CurrentCmpLit(BinOp::Gt, Val::Int(2))],
        );
        let env = Env::new(Val::Null);

        let out = super::run_streaming_rows(&pipeline, &env, CountingRows::new(8, reads.clone()))
            .unwrap();

        assert_eq!(out, Val::Bool(true));
        assert_eq!(reads.get(), 3);
    }

    #[test]
    fn materialized_streaming_stops_when_all_sink_fails() {
        let reads = Rc::new(Cell::new(0));
        let pipeline = empty_pipeline(
            Sink::Predicate(PredicateSinkSpec {
                op: PredicateSinkOp::All,
                predicate: Arc::new(crate::vm::Program::new(Vec::new(), "")),
            }),
            vec![BodyKernel::CurrentCmpLit(BinOp::Lt, Val::Int(3))],
        );
        let env = Env::new(Val::Null);

        let out = super::run_streaming_rows(&pipeline, &env, CountingRows::new(8, reads.clone()))
            .unwrap();

        assert_eq!(out, Val::Bool(false));
        assert_eq!(reads.get(), 3);
    }

    #[test]
    fn materialized_streaming_stops_when_includes_sink_matches() {
        let reads = Rc::new(Cell::new(0));
        let pipeline = empty_pipeline(
            Sink::Membership(MembershipSinkSpec {
                op: MembershipSinkOp::Includes,
                target: MembershipSinkTarget::Literal(Val::Int(3)),
                method: crate::builtins::BuiltinMethod::Includes,
            }),
            Vec::new(),
        );
        let env = Env::new(Val::Null);

        let out = super::run_streaming_rows(&pipeline, &env, CountingRows::new(8, reads.clone()))
            .unwrap();

        assert_eq!(out, Val::Bool(true));
        assert_eq!(reads.get(), 3);
    }

    #[test]
    fn materialized_streaming_stops_when_index_sink_matches() {
        let reads = Rc::new(Cell::new(0));
        let pipeline = empty_pipeline(
            Sink::Membership(MembershipSinkSpec {
                op: MembershipSinkOp::Index,
                target: MembershipSinkTarget::Literal(Val::Int(3)),
                method: crate::builtins::BuiltinMethod::Index,
            }),
            Vec::new(),
        );
        let env = Env::new(Val::Null);

        let out = super::run_streaming_rows(&pipeline, &env, CountingRows::new(8, reads.clone()))
            .unwrap();

        assert_eq!(out, Val::Int(2));
        assert_eq!(reads.get(), 3);
    }
}