recoco-core 0.2.1

Recoco-core is the core library of Recoco; it's nearly identical to the main ReCoco crate, which is a simple wrapper around recoco-core and other sub-crates.
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
// ReCoco is a Rust-only fork of CocoIndex, by [CocoIndex](https://CocoIndex)
// Original code from CocoIndex is copyrighted by CocoIndex
// SPDX-FileCopyrightText: 2025-2026 CocoIndex (upstream)
// SPDX-FileContributor: CocoIndex Contributors
//
// All modifications from the upstream for ReCoco are copyrighted by Knitli Inc.
// SPDX-FileCopyrightText: 2026 Knitli Inc. (ReCoco)
// SPDX-FileContributor: Adam Poulemanos <adam@knit.li>
//
// Both the upstream CocoIndex code and the ReCoco modifications are licensed under the Apache-2.0 License.
// SPDX-License-Identifier: Apache-2.0

use crate::builder::exec_ctx::AnalyzedSetupState;
use crate::ops::{
    get_attachment_factory, get_function_factory, get_source_factory, get_target_factory,
};
use crate::prelude::*;

use super::plan::*;
use crate::lib_context::get_auth_registry;
use crate::{
    base::{schema::*, spec::*},
    ops::interface::*,
};
use futures::future::{BoxFuture, try_join3};
use futures::{FutureExt, future::try_join_all};
use std::time::Duration;
use utils::fingerprint::Fingerprinter;

const TIMEOUT_THRESHOLD: Duration = Duration::from_secs(1800);

#[derive(Debug)]
pub(super) enum ValueTypeBuilder {
    Basic(BasicValueType),
    Struct(StructSchemaBuilder),
    Table(TableSchemaBuilder),
}

impl TryFrom<&ValueType> for ValueTypeBuilder {
    type Error = Error;

    fn try_from(value_type: &ValueType) -> std::result::Result<Self, Self::Error> {
        match value_type {
            ValueType::Basic(basic_type) => Ok(ValueTypeBuilder::Basic(basic_type.clone())),
            ValueType::Struct(struct_type) => Ok(ValueTypeBuilder::Struct(struct_type.try_into()?)),
            ValueType::Table(table_type) => Ok(ValueTypeBuilder::Table(table_type.try_into()?)),
        }
    }
}

impl TryInto<ValueType> for &ValueTypeBuilder {
    type Error = Error;

    fn try_into(self) -> std::result::Result<ValueType, Self::Error> {
        match self {
            ValueTypeBuilder::Basic(basic_type) => Ok(ValueType::Basic(basic_type.clone())),
            ValueTypeBuilder::Struct(struct_type) => Ok(ValueType::Struct(struct_type.try_into()?)),
            ValueTypeBuilder::Table(table_type) => Ok(ValueType::Table(table_type.try_into()?)),
        }
    }
}

#[derive(Default, Debug)]
pub(super) struct StructSchemaBuilder {
    fields: Vec<FieldSchema<ValueTypeBuilder>>,
    field_name_idx: HashMap<FieldName, u32>,
    description: Option<Arc<str>>,
}

impl StructSchemaBuilder {
    fn add_field(&mut self, field: FieldSchema<ValueTypeBuilder>) -> Result<u32> {
        let field_idx = self.fields.len() as u32;
        match self.field_name_idx.entry(field.name.clone()) {
            std::collections::hash_map::Entry::Occupied(_) => {
                client_bail!("Field name already exists: {}", field.name);
            }
            std::collections::hash_map::Entry::Vacant(entry) => {
                entry.insert(field_idx);
            }
        }
        self.fields.push(field);
        Ok(field_idx)
    }

    pub fn find_field(&self, field_name: &'_ str) -> Option<(u32, &FieldSchema<ValueTypeBuilder>)> {
        self.field_name_idx
            .get(field_name)
            .map(|&field_idx| (field_idx, &self.fields[field_idx as usize]))
    }
}

impl TryFrom<&StructSchema> for StructSchemaBuilder {
    type Error = Error;

    fn try_from(schema: &StructSchema) -> std::result::Result<Self, Self::Error> {
        let mut result = StructSchemaBuilder {
            fields: Vec::with_capacity(schema.fields.len()),
            field_name_idx: HashMap::with_capacity(schema.fields.len()),
            description: schema.description.clone(),
        };
        for field in schema.fields.iter() {
            result.add_field(FieldSchema::<ValueTypeBuilder>::from_alternative(field)?)?;
        }
        Ok(result)
    }
}

impl TryInto<StructSchema> for &StructSchemaBuilder {
    type Error = Error;

    fn try_into(self) -> std::result::Result<StructSchema, Self::Error> {
        Ok(StructSchema {
            fields: Arc::new(
                self.fields
                    .iter()
                    .map(FieldSchema::<ValueType>::from_alternative)
                    .collect::<std::result::Result<Vec<_>, _>>()?,
            ),
            description: self.description.clone(),
        })
    }
}

#[derive(Debug)]
pub(super) struct TableSchemaBuilder {
    pub kind: TableKind,
    pub sub_scope: Arc<Mutex<DataScopeBuilder>>,
}

impl TryFrom<&TableSchema> for TableSchemaBuilder {
    type Error = Error;

    fn try_from(schema: &TableSchema) -> std::result::Result<Self, Self::Error> {
        Ok(Self {
            kind: schema.kind,
            sub_scope: Arc::new(Mutex::new(DataScopeBuilder {
                data: (&schema.row).try_into()?,
                added_fields_def_fp: Default::default(),
            })),
        })
    }
}

impl TryInto<TableSchema> for &TableSchemaBuilder {
    type Error = Error;

    fn try_into(self) -> std::result::Result<TableSchema, Self::Error> {
        let sub_scope = self.sub_scope.lock().unwrap();
        let row = (&sub_scope.data).try_into()?;
        Ok(TableSchema {
            kind: self.kind,
            row,
        })
    }
}

fn try_make_common_value_type(
    value_type1: &EnrichedValueType,
    value_type2: &EnrichedValueType,
) -> Result<EnrichedValueType> {
    let typ = match (&value_type1.typ, &value_type2.typ) {
        (ValueType::Basic(basic_type1), ValueType::Basic(basic_type2)) => {
            if basic_type1 != basic_type2 {
                api_bail!("Value types are not compatible: {basic_type1} vs {basic_type2}");
            }
            ValueType::Basic(basic_type1.clone())
        }
        (ValueType::Struct(struct_type1), ValueType::Struct(struct_type2)) => {
            let common_schema = try_merge_struct_schemas(struct_type1, struct_type2)?;
            ValueType::Struct(common_schema)
        }
        (ValueType::Table(table_type1), ValueType::Table(table_type2)) => {
            if table_type1.kind != table_type2.kind {
                api_bail!(
                    "Collection types are not compatible: {} vs {}",
                    table_type1,
                    table_type2
                );
            }
            let row = try_merge_struct_schemas(&table_type1.row, &table_type2.row)?;
            ValueType::Table(TableSchema {
                kind: table_type1.kind,
                row,
            })
        }
        (t1 @ (ValueType::Basic(_) | ValueType::Struct(_) | ValueType::Table(_)), t2) => {
            api_bail!("Unmatched types:\n  {t1}\n  {t2}\n",)
        }
    };
    let common_attrs: Vec<_> = value_type1
        .attrs
        .iter()
        .filter_map(|(k, v)| {
            if value_type2.attrs.get(k) == Some(v) {
                Some((k, v))
            } else {
                None
            }
        })
        .collect();
    let attrs = if common_attrs.len() == value_type1.attrs.len() {
        value_type1.attrs.clone()
    } else {
        Arc::new(
            common_attrs
                .into_iter()
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect(),
        )
    };

    Ok(EnrichedValueType {
        typ,
        nullable: value_type1.nullable || value_type2.nullable,
        attrs,
    })
}

fn try_merge_fields_schemas(
    schema1: &[FieldSchema],
    schema2: &[FieldSchema],
) -> Result<Vec<FieldSchema>> {
    if schema1.len() != schema2.len() {
        api_bail!(
            "Fields are not compatible as they have different fields count:\n  ({})\n  ({})\n",
            schema1
                .iter()
                .map(|f| f.to_string())
                .collect::<Vec<_>>()
                .join(", "),
            schema2
                .iter()
                .map(|f| f.to_string())
                .collect::<Vec<_>>()
                .join(", ")
        );
    }
    let mut result_fields = Vec::with_capacity(schema1.len());
    for (field1, field2) in schema1.iter().zip(schema2.iter()) {
        if field1.name != field2.name {
            api_bail!(
                "Structs are not compatible as they have incompatible field names `{}` vs `{}`",
                field1.name,
                field2.name
            );
        }
        result_fields.push(FieldSchema {
            name: field1.name.clone(),
            value_type: try_make_common_value_type(&field1.value_type, &field2.value_type)?,
            description: None,
        });
    }
    Ok(result_fields)
}

fn try_merge_struct_schemas(
    schema1: &StructSchema,
    schema2: &StructSchema,
) -> Result<StructSchema> {
    let fields = try_merge_fields_schemas(&schema1.fields, &schema2.fields)?;
    Ok(StructSchema {
        fields: Arc::new(fields),
        description: schema1
            .description
            .clone()
            .or_else(|| schema2.description.clone()),
    })
}

fn try_merge_collector_schemas(
    schema1: &CollectorSchema,
    schema2: &CollectorSchema,
) -> Result<CollectorSchema> {
    let schema1_fields = &schema1.fields;
    let schema2_fields = &schema2.fields;

    // Create a map from field name to index in schema1
    let field_map: HashMap<FieldName, usize> = schema1_fields
        .iter()
        .enumerate()
        .map(|(i, f)| (f.name.clone(), i))
        .collect();

    let mut output_fields = Vec::new();
    let mut next_field_id_1 = 0;
    let mut next_field_id_2 = 0;

    for (idx, field) in schema2_fields.iter().enumerate() {
        if let Some(&idx1) = field_map.get(&field.name) {
            if idx1 < next_field_id_1 {
                api_bail!(
                    "Common fields are expected to have consistent order across different `collect()` calls, but got different orders between fields '{}' and '{}'",
                    field.name,
                    schema1_fields[next_field_id_1 - 1].name
                );
            }
            // Add intervening fields from schema1
            for i in next_field_id_1..idx1 {
                output_fields.push(schema1_fields[i].clone());
            }
            // Add intervening fields from schema2
            for i in next_field_id_2..idx {
                output_fields.push(schema2_fields[i].clone());
            }
            // Merge the field
            let merged_type =
                try_make_common_value_type(&schema1_fields[idx1].value_type, &field.value_type)?;
            output_fields.push(FieldSchema {
                name: field.name.clone(),
                value_type: merged_type,
                description: None,
            });
            next_field_id_1 = idx1 + 1;
            next_field_id_2 = idx + 1;
            // Fields not in schema1 and not UUID are added at the end
        }
    }

    // Add remaining fields from schema1
    for i in next_field_id_1..schema1_fields.len() {
        output_fields.push(schema1_fields[i].clone());
    }

    // Add remaining fields from schema2
    for i in next_field_id_2..schema2_fields.len() {
        output_fields.push(schema2_fields[i].clone());
    }

    // Handle auto_uuid_field_idx
    let auto_uuid_field_idx = match (schema1.auto_uuid_field_idx, schema2.auto_uuid_field_idx) {
        (Some(idx1), Some(idx2)) => {
            let name1 = &schema1_fields[idx1].name;
            let name2 = &schema2_fields[idx2].name;
            if name1 == name2 {
                // Find the position of the auto_uuid field in the merged output
                output_fields.iter().position(|f| &f.name == name1)
            } else {
                api_bail!(
                    "Generated UUID fields must have the same name across different `collect()` calls, got different names: '{}' vs '{}'",
                    name1,
                    name2
                );
            }
        }
        (Some(_), None) | (None, Some(_)) => {
            api_bail!(
                "The generated UUID field, once present for one `collect()`, must be consistently present for other `collect()` calls for the same collector"
            );
        }
        (None, None) => None,
    };

    Ok(CollectorSchema {
        fields: output_fields,
        auto_uuid_field_idx,
    })
}

struct FieldDefFingerprintBuilder {
    source_op_names: HashSet<String>,
    fingerprinter: Fingerprinter,
}

impl FieldDefFingerprintBuilder {
    pub fn new() -> Self {
        Self {
            source_op_names: HashSet::new(),
            fingerprinter: Fingerprinter::default(),
        }
    }

    pub fn add(&mut self, key: Option<&str>, def_fp: FieldDefFingerprint) -> Result<()> {
        self.source_op_names.extend(def_fp.source_op_names);
        let mut fingerprinter = std::mem::take(&mut self.fingerprinter);
        if let Some(key) = key {
            fingerprinter = fingerprinter.with(key)?;
        }
        fingerprinter = fingerprinter.with(def_fp.fingerprint.as_slice())?;
        self.fingerprinter = fingerprinter;
        Ok(())
    }

    pub fn build(self) -> FieldDefFingerprint {
        FieldDefFingerprint {
            source_op_names: self.source_op_names,
            fingerprint: self.fingerprinter.into_fingerprint(),
        }
    }
}

#[derive(Debug)]
pub(super) struct CollectorBuilder {
    pub schema: Arc<CollectorSchema>,
    pub is_used: bool,
    pub def_fps: Vec<FieldDefFingerprint>,
}

impl CollectorBuilder {
    pub fn new(schema: Arc<CollectorSchema>, def_fp: FieldDefFingerprint) -> Self {
        Self {
            schema,
            is_used: false,
            def_fps: vec![def_fp],
        }
    }

    pub fn collect(&mut self, schema: &CollectorSchema, def_fp: FieldDefFingerprint) -> Result<()> {
        if self.is_used {
            api_bail!("Collector is already used");
        }
        let existing_schema = Arc::make_mut(&mut self.schema);
        *existing_schema = try_merge_collector_schemas(existing_schema, schema)?;
        self.def_fps.push(def_fp);
        Ok(())
    }

    pub fn use_collection(&mut self) -> Result<(Arc<CollectorSchema>, FieldDefFingerprint)> {
        self.is_used = true;

        self.def_fps
            .sort_by(|a, b| a.fingerprint.as_slice().cmp(b.fingerprint.as_slice()));
        let mut def_fp_builder = FieldDefFingerprintBuilder::new();
        for def_fp in self.def_fps.iter() {
            def_fp_builder.add(None, def_fp.clone())?;
        }
        Ok((self.schema.clone(), def_fp_builder.build()))
    }
}

#[derive(Debug)]
pub(super) struct DataScopeBuilder {
    pub data: StructSchemaBuilder,
    pub added_fields_def_fp: IndexMap<FieldName, FieldDefFingerprint>,
}

impl DataScopeBuilder {
    pub fn new() -> Self {
        Self {
            data: Default::default(),
            added_fields_def_fp: Default::default(),
        }
    }

    pub fn last_field(&self) -> Option<&FieldSchema<ValueTypeBuilder>> {
        self.data.fields.last()
    }

    pub fn add_field(
        &mut self,
        name: FieldName,
        value_type: &EnrichedValueType,
        def_fp: FieldDefFingerprint,
    ) -> Result<AnalyzedOpOutput> {
        let field_index = self.data.add_field(FieldSchema {
            name: name.clone(),
            value_type: EnrichedValueType::from_alternative(value_type)?,
            description: None,
        })?;
        self.added_fields_def_fp.insert(name, def_fp);
        Ok(AnalyzedOpOutput {
            field_idx: field_index,
        })
    }

    /// Must be called on an non-empty field path.
    pub fn analyze_field_path<'a>(
        &'a self,
        field_path: &'_ FieldPath,
        base_def_fp: FieldDefFingerprint,
    ) -> Result<(
        AnalyzedLocalFieldReference,
        &'a EnrichedValueType<ValueTypeBuilder>,
        FieldDefFingerprint,
    )> {
        let mut indices = Vec::with_capacity(field_path.len());
        let mut struct_schema = &self.data;
        let mut def_fp = base_def_fp;

        if field_path.is_empty() {
            client_bail!("Field path is empty");
        }

        let mut i = 0;
        let value_type = loop {
            let field_name = &field_path[i];
            let (field_idx, field) = struct_schema.find_field(field_name).ok_or_else(|| {
                api_error!("Field {} not found", field_path[0..(i + 1)].join("."))
            })?;
            if let Some(added_def_fp) = self.added_fields_def_fp.get(field_name) {
                def_fp = added_def_fp.clone();
            } else {
                def_fp.fingerprint = Fingerprinter::default()
                    .with(&("field", &def_fp.fingerprint, field_name))?
                    .into_fingerprint();
            };
            indices.push(field_idx);
            if i + 1 >= field_path.len() {
                break &field.value_type;
            }
            i += 1;

            struct_schema = match &field.value_type.typ {
                ValueTypeBuilder::Struct(struct_type) => struct_type,
                _ => {
                    api_bail!("Field {} is not a struct", field_path[0..(i + 1)].join("."));
                }
            };
        };
        Ok((
            AnalyzedLocalFieldReference {
                fields_idx: indices,
            },
            value_type,
            def_fp,
        ))
    }
}

pub(super) struct AnalyzerContext {
    pub lib_ctx: Arc<LibContext>,
    pub flow_ctx: Arc<FlowInstanceContext>,
}

#[derive(Debug, Default)]
pub(super) struct OpScopeStates {
    pub op_output_types: HashMap<FieldName, EnrichedValueType>,
    pub collectors: IndexMap<FieldName, CollectorBuilder>,
    pub sub_scopes: HashMap<String, Arc<OpScopeSchema>>,
}

impl OpScopeStates {
    pub fn add_collector(
        &mut self,
        collector_name: FieldName,
        schema: CollectorSchema,
        def_fp: FieldDefFingerprint,
    ) -> Result<AnalyzedLocalCollectorReference> {
        let existing_len = self.collectors.len();
        let idx = match self.collectors.entry(collector_name) {
            indexmap::map::Entry::Occupied(mut entry) => {
                entry.get_mut().collect(&schema, def_fp)?;
                entry.index()
            }
            indexmap::map::Entry::Vacant(entry) => {
                entry.insert(CollectorBuilder::new(Arc::new(schema), def_fp));
                existing_len
            }
        };
        Ok(AnalyzedLocalCollectorReference {
            collector_idx: idx as u32,
        })
    }

    pub fn consume_collector(
        &mut self,
        collector_name: &FieldName,
    ) -> Result<(
        AnalyzedLocalCollectorReference,
        Arc<CollectorSchema>,
        FieldDefFingerprint,
    )> {
        let (collector_idx, _, collector) = self
            .collectors
            .get_full_mut(collector_name)
            .ok_or_else(|| api_error!("Collector not found: {}", collector_name))?;
        let (schema, def_fp) = collector.use_collection()?;
        Ok((
            AnalyzedLocalCollectorReference {
                collector_idx: collector_idx as u32,
            },
            schema,
            def_fp,
        ))
    }

    fn build_op_scope_schema(&self) -> OpScopeSchema {
        OpScopeSchema {
            op_output_types: self
                .op_output_types
                .iter()
                .map(|(name, value_type)| (name.clone(), value_type.without_attrs()))
                .collect(),
            collectors: self
                .collectors
                .iter()
                .map(|(name, schema)| NamedSpec {
                    name: name.clone(),
                    spec: schema.schema.clone(),
                })
                .collect(),
            op_scopes: self.sub_scopes.clone(),
        }
    }
}

#[derive(Debug)]
pub struct OpScope {
    pub name: String,
    pub parent: Option<(Arc<OpScope>, spec::FieldPath)>,
    pub(super) data: Arc<Mutex<DataScopeBuilder>>,
    pub(super) states: Mutex<OpScopeStates>,
    pub(super) base_value_def_fp: FieldDefFingerprint,
}

struct Iter<'a>(Option<&'a OpScope>);

impl<'a> Iterator for Iter<'a> {
    type Item = &'a OpScope;

    fn next(&mut self) -> Option<Self::Item> {
        match self.0 {
            Some(scope) => {
                self.0 = scope.parent.as_ref().map(|(parent, _)| parent.as_ref());
                Some(scope)
            }
            None => None,
        }
    }
}

impl OpScope {
    pub(super) fn new(
        name: String,
        parent: Option<(Arc<OpScope>, spec::FieldPath)>,
        data: Arc<Mutex<DataScopeBuilder>>,
        base_value_def_fp: FieldDefFingerprint,
    ) -> Arc<Self> {
        Arc::new(Self {
            name,
            parent,
            data,
            states: Mutex::default(),
            base_value_def_fp,
        })
    }

    fn add_op_output(
        &self,
        name: FieldName,
        value_type: EnrichedValueType,
        def_fp: FieldDefFingerprint,
    ) -> Result<AnalyzedOpOutput> {
        let op_output = self
            .data
            .lock()
            .unwrap()
            .add_field(name.clone(), &value_type, def_fp)?;
        self.states
            .lock()
            .unwrap()
            .op_output_types
            .insert(name, value_type);
        Ok(op_output)
    }

    pub fn ancestors(&self) -> impl Iterator<Item = &OpScope> {
        Iter(Some(self))
    }

    pub fn is_op_scope_descendant(&self, other: &Self) -> bool {
        if self == other {
            return true;
        }
        match &self.parent {
            Some((parent, _)) => parent.is_op_scope_descendant(other),
            None => false,
        }
    }

    pub(super) fn new_foreach_op_scope(
        self: &Arc<Self>,
        scope_name: String,
        field_path: &FieldPath,
    ) -> Result<(AnalyzedLocalFieldReference, Arc<Self>)> {
        let (local_field_ref, sub_data_scope, def_fp) = {
            let data_scope = self.data.lock().unwrap();
            let (local_field_ref, value_type, def_fp) =
                data_scope.analyze_field_path(field_path, self.base_value_def_fp.clone())?;
            let sub_data_scope = match &value_type.typ {
                ValueTypeBuilder::Table(table_type) => table_type.sub_scope.clone(),
                _ => api_bail!("ForEach only works on collection, field {field_path} is not"),
            };
            (local_field_ref, sub_data_scope, def_fp)
        };
        let sub_op_scope = OpScope::new(
            scope_name,
            Some((self.clone(), field_path.clone())),
            sub_data_scope,
            def_fp,
        );
        Ok((local_field_ref, sub_op_scope))
    }
}

impl std::fmt::Display for OpScope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some((scope, field_path)) = &self.parent {
            write!(f, "{} [{} AS {}]", scope, field_path, self.name)?;
        } else {
            write!(f, "[{}]", self.name)?;
        }
        Ok(())
    }
}

impl PartialEq for OpScope {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(self, other)
    }
}
impl Eq for OpScope {}

fn find_scope<'a>(scope_name: &ScopeName, op_scope: &'a OpScope) -> Result<(u32, &'a OpScope)> {
    let (up_level, scope) = op_scope
        .ancestors()
        .enumerate()
        .find(|(_, s)| &s.name == scope_name)
        .ok_or_else(|| api_error!("Scope not found: {}", scope_name))?;
    Ok((up_level as u32, scope))
}

fn analyze_struct_mapping(
    mapping: &StructMapping,
    op_scope: &OpScope,
) -> Result<(AnalyzedStructMapping, Vec<FieldSchema>, FieldDefFingerprint)> {
    let mut field_mappings = Vec::with_capacity(mapping.fields.len());
    let mut field_schemas = Vec::with_capacity(mapping.fields.len());

    let mut fields_def_fps = Vec::with_capacity(mapping.fields.len());
    for field in mapping.fields.iter() {
        let (field_mapping, value_type, field_def_fp) =
            analyze_value_mapping(&field.spec, op_scope)?;
        field_mappings.push(field_mapping);
        field_schemas.push(FieldSchema {
            name: field.name.clone(),
            value_type,
            description: None,
        });
        fields_def_fps.push((field.name.as_str(), field_def_fp));
    }
    fields_def_fps.sort_by_key(|(name, _)| *name);
    let mut def_fp_builder = FieldDefFingerprintBuilder::new();
    for (name, def_fp) in fields_def_fps {
        def_fp_builder.add(Some(name), def_fp)?;
    }
    Ok((
        AnalyzedStructMapping {
            fields: field_mappings,
        },
        field_schemas,
        def_fp_builder.build(),
    ))
}

fn analyze_value_mapping(
    value_mapping: &ValueMapping,
    op_scope: &OpScope,
) -> Result<(AnalyzedValueMapping, EnrichedValueType, FieldDefFingerprint)> {
    let result = match value_mapping {
        ValueMapping::Constant(v) => {
            let value = value::Value::from_json(v.value.clone(), &v.schema.typ)?;
            let value_mapping = AnalyzedValueMapping::Constant { value };
            let def_fp = FieldDefFingerprint {
                source_op_names: HashSet::new(),
                fingerprint: Fingerprinter::default()
                    .with(&("constant", &v.value, &v.schema.without_attrs()))?
                    .into_fingerprint(),
            };
            (value_mapping, v.schema.clone(), def_fp)
        }

        ValueMapping::Field(v) => {
            let (scope_up_level, op_scope) = match &v.scope {
                Some(scope_name) => find_scope(scope_name, op_scope)?,
                None => (0, op_scope),
            };
            let data_scope = op_scope.data.lock().unwrap();
            let (local_field_ref, value_type, def_fp) =
                data_scope.analyze_field_path(&v.field_path, op_scope.base_value_def_fp.clone())?;
            let schema = EnrichedValueType::from_alternative(value_type)?;
            let value_mapping = AnalyzedValueMapping::Field(AnalyzedFieldReference {
                local: local_field_ref,
                scope_up_level,
            });
            (value_mapping, schema, def_fp)
        }
    };
    Ok(result)
}

fn analyze_input_fields(
    arg_bindings: &[OpArgBinding],
    op_scope: &OpScope,
) -> Result<(Vec<OpArgSchema>, FieldDefFingerprint)> {
    let mut op_arg_schemas = Vec::with_capacity(arg_bindings.len());
    let mut def_fp_builder = FieldDefFingerprintBuilder::new();
    for arg_binding in arg_bindings.iter() {
        let (analyzed_value, value_type, def_fp) =
            analyze_value_mapping(&arg_binding.value, op_scope)?;
        let op_arg_schema = OpArgSchema {
            name: arg_binding.arg_name.clone(),
            value_type,
            analyzed_value: analyzed_value.clone(),
        };
        def_fp_builder.add(arg_binding.arg_name.0.as_deref(), def_fp)?;
        op_arg_schemas.push(op_arg_schema);
    }
    Ok((op_arg_schemas, def_fp_builder.build()))
}

fn add_collector(
    scope_name: &ScopeName,
    collector_name: FieldName,
    schema: CollectorSchema,
    op_scope: &OpScope,
    def_fp: FieldDefFingerprint,
) -> Result<AnalyzedCollectorReference> {
    let (scope_up_level, scope) = find_scope(scope_name, op_scope)?;
    let local_ref = scope
        .states
        .lock()
        .unwrap()
        .add_collector(collector_name, schema, def_fp)?;
    Ok(AnalyzedCollectorReference {
        local: local_ref,
        scope_up_level,
    })
}

struct ExportDataFieldsInfo {
    local_collector_ref: AnalyzedLocalCollectorReference,
    primary_key_def: AnalyzedPrimaryKeyDef,
    primary_key_schema: Box<[FieldSchema]>,
    value_fields_idx: Vec<u32>,
    value_stable: bool,
    output_value_fingerprinter: Fingerprinter,
    def_fp: FieldDefFingerprint,
}

impl AnalyzerContext {
    pub(super) async fn analyze_import_op(
        &self,
        op_scope: &Arc<OpScope>,
        import_op: NamedSpec<ImportOpSpec>,
    ) -> Result<impl Future<Output = Result<AnalyzedImportOp>> + Send + use<>> {
        let source_factory = get_source_factory(&import_op.spec.source.kind)?;
        let (output_type, executor) = source_factory
            .build(
                &import_op.name,
                serde_json::Value::Object(import_op.spec.source.spec),
                self.flow_ctx.clone(),
            )
            .await?;

        let op_name = import_op.name;
        let primary_key_schema = Box::from(output_type.typ.key_schema());
        let def_fp = FieldDefFingerprint {
            source_op_names: HashSet::from([op_name.clone()]),
            fingerprint: Fingerprinter::default()
                .with(&("import", &op_name))?
                .into_fingerprint(),
        };
        let output = op_scope.add_op_output(op_name.clone(), output_type, def_fp)?;

        let concur_control_options = import_op
            .spec
            .execution_options
            .get_concur_control_options();
        let global_concurrency_controller = self.lib_ctx.global_concurrency_controller.clone();
        let result_fut = async move {
            trace!("Start building executor for source op `{op_name}`");
            let executor = executor
                .await
                .with_context(|| format!("Preparing for source op: {op_name}"))?;
            trace!("Finished building executor for source op `{op_name}`");
            Ok(AnalyzedImportOp {
                executor,
                output,
                primary_key_schema,
                name: op_name,
                refresh_options: import_op.spec.refresh_options,
                concurrency_controller: concur_control::CombinedConcurrencyController::new(
                    &concur_control_options,
                    global_concurrency_controller,
                ),
            })
        };
        Ok(result_fut)
    }

    pub(super) async fn analyze_reactive_op(
        &self,
        op_scope: &Arc<OpScope>,
        reactive_op: &NamedSpec<ReactiveOpSpec>,
    ) -> Result<BoxFuture<'static, Result<AnalyzedReactiveOp>>> {
        let reactive_op_clone = reactive_op.clone();
        let reactive_op_name = reactive_op.name.clone();
        let result_fut = match reactive_op_clone.spec {
            ReactiveOpSpec::Transform(op) => {
                let (input_field_schemas, input_def_fp) =
                    analyze_input_fields(&op.inputs, op_scope).with_context(|| {
                        format!("Preparing inputs for transform op: {}", reactive_op_name)
                    })?;
                let spec = serde_json::Value::Object(op.op.spec.clone());

                let fn_executor = get_function_factory(&op.op.kind)?;
                let input_value_mappings = input_field_schemas
                    .iter()
                    .map(|field| field.analyzed_value.clone())
                    .collect();
                let build_output = fn_executor
                    .build(spec, input_field_schemas, self.flow_ctx.clone())
                    .await?;
                let output_type = build_output.output_type.typ.clone();
                let logic_fingerprinter = Fingerprinter::default()
                    .with(&op.op)?
                    .with(&build_output.output_type.without_attrs())?
                    .with(&build_output.behavior_version)?;

                let def_fp = FieldDefFingerprint {
                    source_op_names: input_def_fp.source_op_names,
                    fingerprint: Fingerprinter::default()
                        .with(&(
                            "transform",
                            &op.op,
                            &input_def_fp.fingerprint,
                            &build_output.behavior_version,
                        ))?
                        .into_fingerprint(),
                };
                let output = op_scope.add_op_output(
                    reactive_op_name.clone(),
                    build_output.output_type,
                    def_fp,
                )?;
                let op_name = reactive_op_name.clone();
                let op_kind = op.op.kind.clone();

                let execution_options_timeout = op.execution_options.timeout;

                let behavior_version = build_output.behavior_version;
                async move {
                            trace!("Start building executor for transform op `{op_name}`");
                            let executor = build_output.executor.await.with_context(|| {
                                format!("Preparing for transform op: {op_name}")
                            })?;
                            let enable_cache = executor.enable_cache();
                            let timeout = executor.timeout()
                                .or(execution_options_timeout)
                                .or(Some(TIMEOUT_THRESHOLD));
                            trace!("Finished building executor for transform op `{op_name}`, enable cache: {enable_cache}, behavior version: {behavior_version:?}");
                            let function_exec_info = AnalyzedFunctionExecInfo {
                                enable_cache,
                                timeout,
                                behavior_version,
                                fingerprinter: logic_fingerprinter,
                                output_type
                            };
                            if function_exec_info.enable_cache
                                && function_exec_info.behavior_version.is_none()
                            {
                                api_bail!(
                                    "When caching is enabled, behavior version must be specified for transform op: {op_name}"
                                );
                            }
                            Ok(AnalyzedReactiveOp::Transform(AnalyzedTransformOp {
                                name: op_name,
                                op_kind,
                                inputs: input_value_mappings,
                                function_exec_info,
                                executor,
                                output,
                            }))
                }
                .boxed()
            }

            ReactiveOpSpec::ForEach(foreach_op) => {
                let (local_field_ref, sub_op_scope) = op_scope.new_foreach_op_scope(
                    foreach_op.op_scope.name.clone(),
                    &foreach_op.field_path,
                )?;
                let analyzed_op_scope_fut = {
                    let analyzed_op_scope_fut = self
                        .analyze_op_scope(&sub_op_scope, &foreach_op.op_scope.ops)
                        .boxed_local()
                        .await?;
                    let sub_op_scope_schema =
                        sub_op_scope.states.lock().unwrap().build_op_scope_schema();
                    op_scope
                        .states
                        .lock()
                        .unwrap()
                        .sub_scopes
                        .insert(reactive_op_name.clone(), Arc::new(sub_op_scope_schema));
                    analyzed_op_scope_fut
                };
                let op_name = reactive_op_name.clone();

                let concur_control_options =
                    foreach_op.execution_options.get_concur_control_options();
                async move {
                    Ok(AnalyzedReactiveOp::ForEach(AnalyzedForEachOp {
                        local_field_ref,
                        op_scope: analyzed_op_scope_fut
                            .await
                            .with_context(|| format!("Preparing for foreach op: {op_name}"))?,
                        name: op_name,
                        concurrency_controller: concur_control::ConcurrencyController::new(
                            &concur_control_options,
                        ),
                    }))
                }
                .boxed()
            }

            ReactiveOpSpec::Collect(op) => {
                let (struct_mapping, fields_schema, mut def_fp) =
                    analyze_struct_mapping(&op.input, op_scope)?;
                let has_auto_uuid_field = op.auto_uuid_field.is_some();
                def_fp.fingerprint = Fingerprinter::default()
                    .with(&(
                        "collect",
                        &def_fp.fingerprint,
                        &fields_schema,
                        &has_auto_uuid_field,
                    ))?
                    .into_fingerprint();
                let fingerprinter = Fingerprinter::default().with(&fields_schema)?;

                let input_field_names: Vec<FieldName> =
                    fields_schema.iter().map(|f| f.name.clone()).collect();
                let collector_ref = add_collector(
                    &op.scope_name,
                    op.collector_name.clone(),
                    CollectorSchema::from_fields(fields_schema, op.auto_uuid_field.clone()),
                    op_scope,
                    def_fp,
                )?;
                let op_scope = op_scope.clone();
                async move {
                    // Get the merged collector schema after adding
                    let collector_schema: Arc<CollectorSchema> = {
                        let scope = find_scope(&op.scope_name, &op_scope)?.1;
                        let states = scope.states.lock().unwrap();
                        let collector = states.collectors.get(&op.collector_name).unwrap();
                        collector.schema.clone()
                    };

                    // Pre-compute field index mappings for efficient evaluation
                    let field_name_to_index: HashMap<&FieldName, usize> = input_field_names
                        .iter()
                        .enumerate()
                        .map(|(i, n)| (n, i))
                        .collect();
                    let field_index_mapping = collector_schema
                        .fields
                        .iter()
                        .map(|field| field_name_to_index.get(&field.name).copied())
                        .collect::<Vec<Option<usize>>>();

                    let collect_op = AnalyzedReactiveOp::Collect(AnalyzedCollectOp {
                        name: reactive_op_name,
                        has_auto_uuid_field,
                        input: struct_mapping,
                        input_field_names,
                        collector_schema,
                        collector_ref,
                        field_index_mapping,
                        fingerprinter,
                    });
                    Ok(collect_op)
                }
                .boxed()
            }
        };
        Ok(result_fut)
    }

    #[allow(clippy::too_many_arguments)]
    async fn analyze_export_op_group(
        &self,
        target_kind: &str,
        op_scope: &Arc<OpScope>,
        flow_inst: &FlowInstanceSpec,
        export_op_group: &AnalyzedExportTargetOpGroup,
        declarations: Vec<serde_json::Value>,
        targets_analyzed_ss: &mut [Option<exec_ctx::AnalyzedTargetSetupState>],
        declarations_analyzed_ss: &mut Vec<exec_ctx::AnalyzedTargetSetupState>,
    ) -> Result<Vec<impl Future<Output = Result<AnalyzedExportOp>> + Send + use<>>> {
        let mut collection_specs = Vec::<interface::ExportDataCollectionSpec>::new();
        let mut data_fields_infos = Vec::<ExportDataFieldsInfo>::new();
        for idx in export_op_group.op_idx.iter() {
            let export_op = &flow_inst.export_ops[*idx];
            let (local_collector_ref, collector_schema, def_fp) =
                op_scope
                    .states
                    .lock()
                    .unwrap()
                    .consume_collector(&export_op.spec.collector_name)?;
            let (value_fields_schema, data_collection_info) =
                match &export_op.spec.index_options.primary_key_fields {
                    Some(fields) => {
                        let pk_fields_idx = fields
                            .iter()
                            .map(|f| {
                                collector_schema
                                    .fields
                                    .iter()
                                    .position(|field| &field.name == f)
                                    .ok_or_else(|| client_error!("field not found: {}", f))
                            })
                            .collect::<Result<Vec<_>>>()?;

                        let primary_key_schema = pk_fields_idx
                            .iter()
                            .map(|idx| collector_schema.fields[*idx].without_attrs())
                            .collect::<Box<[_]>>();
                        let mut value_fields_schema: Vec<FieldSchema> = vec![];
                        let mut value_fields_idx = vec![];
                        for (idx, field) in collector_schema.fields.iter().enumerate() {
                            if !pk_fields_idx.contains(&idx) {
                                value_fields_schema.push(field.without_attrs());
                                value_fields_idx.push(idx as u32);
                            }
                        }
                        let value_stable = collector_schema
                            .auto_uuid_field_idx
                            .as_ref()
                            .map(|uuid_idx| pk_fields_idx.contains(uuid_idx))
                            .unwrap_or(false);
                        let output_value_fingerprinter =
                            Fingerprinter::default().with(&value_fields_schema)?;
                        (
                            value_fields_schema,
                            ExportDataFieldsInfo {
                                local_collector_ref,
                                primary_key_def: AnalyzedPrimaryKeyDef::Fields(pk_fields_idx),
                                primary_key_schema,
                                value_fields_idx,
                                value_stable,
                                output_value_fingerprinter,
                                def_fp,
                            },
                        )
                    }
                    None => {
                        // TODO: Support auto-generate primary key
                        api_bail!("Primary key fields must be specified")
                    }
                };
            collection_specs.push(interface::ExportDataCollectionSpec {
                name: export_op.name.clone(),
                spec: serde_json::Value::Object(export_op.spec.target.spec.clone()),
                key_fields_schema: data_collection_info.primary_key_schema.clone(),
                value_fields_schema,
                index_options: export_op.spec.index_options.clone(),
            });
            data_fields_infos.push(data_collection_info);
        }
        let (data_collections_output, declarations_output) = export_op_group
            .target_factory
            .clone()
            .build(collection_specs, declarations, self.flow_ctx.clone())
            .await?;
        let analyzed_export_ops = export_op_group
            .op_idx
            .iter()
            .zip(data_collections_output.into_iter())
            .zip(data_fields_infos.into_iter())
            .map(|((idx, data_coll_output), data_fields_info)| {
                let export_op = &flow_inst.export_ops[*idx];
                let op_name = export_op.name.clone();
                let export_target_factory = export_op_group.target_factory.clone();

                let attachments = export_op
                    .spec
                    .attachments
                    .iter()
                    .map(|attachment| {
                        let attachment_factory = get_attachment_factory(&attachment.kind)?;
                        let attachment_state = attachment_factory.get_state(
                            &op_name,
                            &export_op.spec.target.spec,
                            serde_json::Value::Object(attachment.spec.clone()),
                        )?;
                        Ok((
                            interface::AttachmentSetupKey(
                                attachment.kind.clone(),
                                attachment_state.setup_key,
                            ),
                            attachment_state.setup_state,
                        ))
                    })
                    .collect::<Result<IndexMap<_, _>>>()?;

                let export_op_ss = exec_ctx::AnalyzedTargetSetupState {
                    target_kind: target_kind.to_string(),
                    setup_key: data_coll_output.setup_key,
                    desired_setup_state: data_coll_output.desired_setup_state,
                    setup_by_user: export_op.spec.setup_by_user,
                    key_type: Some(
                        data_fields_info
                            .primary_key_schema
                            .iter()
                            .map(|field| field.value_type.typ.clone())
                            .collect::<Box<[_]>>(),
                    ),
                    attachments,
                };
                targets_analyzed_ss[*idx] = Some(export_op_ss);

                let def_fp = FieldDefFingerprint {
                    source_op_names: data_fields_info.def_fp.source_op_names,
                    fingerprint: Fingerprinter::default()
                        .with("export")?
                        .with(&data_fields_info.def_fp.fingerprint)?
                        .with(&export_op.spec.target)?
                        .into_fingerprint(),
                };
                Ok(async move {
                    trace!("Start building executor for export op `{op_name}`");
                    let export_context = data_coll_output
                        .export_context
                        .await
                        .with_context(|| format!("Preparing for export op: {op_name}"))?;
                    trace!("Finished building executor for export op `{op_name}`");
                    Ok(AnalyzedExportOp {
                        name: op_name,
                        input: data_fields_info.local_collector_ref,
                        export_target_factory,
                        export_context,
                        primary_key_def: data_fields_info.primary_key_def,
                        primary_key_schema: data_fields_info.primary_key_schema,
                        value_fields: data_fields_info.value_fields_idx,
                        value_stable: data_fields_info.value_stable,
                        output_value_fingerprinter: data_fields_info.output_value_fingerprinter,
                        def_fp,
                    })
                })
            })
            .collect::<Result<Vec<_>>>()?;
        for (setup_key, desired_setup_state) in declarations_output {
            let decl_ss = exec_ctx::AnalyzedTargetSetupState {
                target_kind: target_kind.to_string(),
                setup_key,
                desired_setup_state,
                setup_by_user: false,
                key_type: None,
                attachments: IndexMap::new(),
            };
            declarations_analyzed_ss.push(decl_ss);
        }
        Ok(analyzed_export_ops)
    }

    async fn analyze_op_scope(
        &self,
        op_scope: &Arc<OpScope>,
        reactive_ops: &[NamedSpec<ReactiveOpSpec>],
    ) -> Result<impl Future<Output = Result<AnalyzedOpScope>> + Send + use<>> {
        let mut op_futs = Vec::with_capacity(reactive_ops.len());
        for reactive_op in reactive_ops.iter() {
            op_futs.push(self.analyze_reactive_op(op_scope, reactive_op).await?);
        }
        let collector_len = op_scope.states.lock().unwrap().collectors.len();
        let scope_qualifier = self.build_scope_qualifier(op_scope);
        let result_fut = async move {
            Ok(AnalyzedOpScope {
                reactive_ops: try_join_all(op_futs).await?,
                collector_len,
                scope_qualifier,
            })
        };
        Ok(result_fut)
    }

    fn build_scope_qualifier(&self, op_scope: &Arc<OpScope>) -> String {
        let mut scope_names = Vec::new();
        let mut current_scope = op_scope.as_ref();

        // Walk up the parent chain to collect scope names
        while let Some((parent, _)) = &current_scope.parent {
            scope_names.push(current_scope.name.as_str());
            current_scope = parent.as_ref();
        }

        // Reverse to get the correct order (root to leaf)
        scope_names.reverse();

        // Build the qualifier string
        let mut result = String::new();
        for name in scope_names {
            result.push_str(name);
            result.push('.');
        }
        result
    }
}

pub fn build_flow_instance_context(flow_inst_name: &str) -> Arc<FlowInstanceContext> {
    Arc::new(FlowInstanceContext {
        flow_instance_name: flow_inst_name.to_string(),
        auth_registry: get_auth_registry().clone(),
    })
}

fn build_flow_schema(root_op_scope: &OpScope) -> Result<FlowSchema> {
    let schema = (&root_op_scope.data.lock().unwrap().data).try_into()?;
    let root_op_scope_schema = root_op_scope.states.lock().unwrap().build_op_scope_schema();
    Ok(FlowSchema {
        schema,
        root_op_scope: root_op_scope_schema,
    })
}

pub async fn analyze_flow(
    flow_inst: &FlowInstanceSpec,
    flow_ctx: Arc<FlowInstanceContext>,
) -> Result<(
    FlowSchema,
    AnalyzedSetupState,
    impl Future<Output = Result<ExecutionPlan>> + Send + use<>,
)> {
    let analyzer_ctx = AnalyzerContext {
        lib_ctx: get_lib_context().await?,
        flow_ctx,
    };
    let root_data_scope = Arc::new(Mutex::new(DataScopeBuilder::new()));
    let root_op_scope = OpScope::new(
        ROOT_SCOPE_NAME.to_string(),
        None,
        root_data_scope,
        FieldDefFingerprint::default(),
    );
    let mut import_ops_futs = Vec::with_capacity(flow_inst.import_ops.len());
    for import_op in flow_inst.import_ops.iter() {
        import_ops_futs.push(
            analyzer_ctx
                .analyze_import_op(&root_op_scope, import_op.clone())
                .await
                .with_context(|| format!("Preparing for import op: {}", import_op.name))?,
        );
    }
    let op_scope_fut = analyzer_ctx
        .analyze_op_scope(&root_op_scope, &flow_inst.reactive_ops)
        .await?;

    #[derive(Default)]
    struct TargetOpGroup {
        export_op_ids: Vec<usize>,
        declarations: Vec<serde_json::Value>,
    }
    let mut target_op_group = IndexMap::<String, TargetOpGroup>::new();
    for (idx, export_op) in flow_inst.export_ops.iter().enumerate() {
        target_op_group
            .entry(export_op.spec.target.kind.clone())
            .or_default()
            .export_op_ids
            .push(idx);
    }
    for declaration in flow_inst.declarations.iter() {
        target_op_group
            .entry(declaration.kind.clone())
            .or_default()
            .declarations
            .push(serde_json::Value::Object(declaration.spec.clone()));
    }

    let mut export_ops_futs = vec![];
    let mut analyzed_target_op_groups = vec![];

    let mut targets_analyzed_ss = Vec::with_capacity(flow_inst.export_ops.len());
    targets_analyzed_ss.resize_with(flow_inst.export_ops.len(), || None);

    let mut declarations_analyzed_ss = Vec::with_capacity(flow_inst.declarations.len());

    for (target_kind, op_ids) in target_op_group.into_iter() {
        let target_factory = get_target_factory(&target_kind)?;
        let analyzed_target_op_group = AnalyzedExportTargetOpGroup {
            target_factory,
            target_kind: target_kind.clone(),
            op_idx: op_ids.export_op_ids,
        };
        export_ops_futs.extend(
            analyzer_ctx
                .analyze_export_op_group(
                    target_kind.as_str(),
                    &root_op_scope,
                    flow_inst,
                    &analyzed_target_op_group,
                    op_ids.declarations,
                    &mut targets_analyzed_ss,
                    &mut declarations_analyzed_ss,
                )
                .await
                .with_context(|| format!("Analyzing export ops for target `{target_kind}`"))?,
        );
        analyzed_target_op_groups.push(analyzed_target_op_group);
    }

    let flow_schema = build_flow_schema(&root_op_scope)?;
    let analyzed_ss = exec_ctx::AnalyzedSetupState {
        targets: targets_analyzed_ss
            .into_iter()
            .enumerate()
            .map(|(idx, v)| v.ok_or_else(|| internal_error!("target op `{}` not found", idx)))
            .collect::<Result<Vec<_>>>()?,
        declarations: declarations_analyzed_ss,
    };

    let legacy_fingerprint_v1 = Fingerprinter::default()
        .with(&flow_inst)?
        .with(&flow_schema.schema)?
        .into_fingerprint();

    fn append_reactive_op_scope(
        mut fingerprinter: Fingerprinter,
        reactive_ops: &[NamedSpec<ReactiveOpSpec>],
    ) -> Result<Fingerprinter> {
        fingerprinter = fingerprinter.with(&reactive_ops.len())?;
        for reactive_op in reactive_ops.iter() {
            fingerprinter = fingerprinter.with(&reactive_op.name)?;
            match &reactive_op.spec {
                ReactiveOpSpec::Transform(_) => {}
                ReactiveOpSpec::ForEach(foreach_op) => {
                    fingerprinter = fingerprinter.with(&foreach_op.field_path)?;
                    fingerprinter =
                        append_reactive_op_scope(fingerprinter, &foreach_op.op_scope.ops)?;
                }
                ReactiveOpSpec::Collect(collect_op) => {
                    fingerprinter = fingerprinter.with(collect_op)?;
                }
            }
        }
        Ok(fingerprinter)
    }
    let current_fingerprinter =
        append_reactive_op_scope(Fingerprinter::default(), &flow_inst.reactive_ops)?
            .with(&flow_inst.export_ops)?
            .with(&flow_inst.declarations)?
            .with(&flow_schema.schema)?;
    let plan_fut = async move {
        let (import_ops, op_scope, export_ops) = try_join3(
            try_join_all(import_ops_futs),
            op_scope_fut,
            try_join_all(export_ops_futs),
        )
        .await?;

        fn append_function_behavior(
            mut fingerprinter: Fingerprinter,
            reactive_ops: &[AnalyzedReactiveOp],
        ) -> Result<Fingerprinter> {
            for reactive_op in reactive_ops.iter() {
                match reactive_op {
                    AnalyzedReactiveOp::Transform(transform_op) => {
                        fingerprinter = fingerprinter.with(&transform_op.name)?.with(
                            &transform_op
                                .function_exec_info
                                .fingerprinter
                                .clone()
                                .into_fingerprint(),
                        )?;
                    }
                    AnalyzedReactiveOp::ForEach(foreach_op) => {
                        fingerprinter = append_function_behavior(
                            fingerprinter,
                            &foreach_op.op_scope.reactive_ops,
                        )?;
                    }
                    _ => {}
                }
            }
            Ok(fingerprinter)
        }
        let legacy_fingerprint_v2 =
            append_function_behavior(current_fingerprinter, &op_scope.reactive_ops)?
                .into_fingerprint();
        Ok(ExecutionPlan {
            legacy_fingerprint: vec![legacy_fingerprint_v1, legacy_fingerprint_v2],
            import_ops,
            op_scope,
            export_ops,
            export_op_groups: analyzed_target_op_groups,
        })
    };

    Ok((flow_schema, analyzed_ss, plan_fut))
}

pub async fn analyze_transient_flow<'a>(
    flow_inst: &TransientFlowSpec,
    flow_ctx: Arc<FlowInstanceContext>,
) -> Result<(
    EnrichedValueType,
    FlowSchema,
    impl Future<Output = Result<TransientExecutionPlan>> + Send + 'a,
)> {
    let mut root_data_scope = DataScopeBuilder::new();
    let analyzer_ctx = AnalyzerContext {
        lib_ctx: get_lib_context().await?,
        flow_ctx,
    };
    let mut input_fields = vec![];
    for field in flow_inst.input_fields.iter() {
        let analyzed_field = root_data_scope.add_field(
            field.name.clone(),
            &field.value_type,
            FieldDefFingerprint::default(),
        )?;
        input_fields.push(analyzed_field);
    }
    let root_op_scope = OpScope::new(
        ROOT_SCOPE_NAME.to_string(),
        None,
        Arc::new(Mutex::new(root_data_scope)),
        FieldDefFingerprint::default(),
    );
    let op_scope_fut = analyzer_ctx
        .analyze_op_scope(&root_op_scope, &flow_inst.reactive_ops)
        .await?;
    let (output_value, output_type, _) =
        analyze_value_mapping(&flow_inst.output_value, &root_op_scope)?;
    let data_schema = build_flow_schema(&root_op_scope)?;
    let plan_fut = async move {
        let op_scope = op_scope_fut.await?;
        Ok(TransientExecutionPlan {
            input_fields,
            op_scope,
            output_value,
        })
    };
    Ok((output_type, data_schema, plan_fut))
}