panproto-mig 0.16.0

Migration engine for panproto
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
//! Lift operations: applying compiled migrations to instances.
//!
//! `lift_wtype` and `lift_functor` delegate to the restrict
//! implementations in `panproto-inst`, passing the compiled
//! migration's precomputed tables.

use panproto_inst::{CompiledMigration, FInstance, WInstance};
use panproto_schema::Schema;

use crate::error::LiftError;

/// Apply a compiled migration to a W-type instance.
///
/// Delegates to [`panproto_inst::wtype_restrict`], which executes the
/// 5-step pipeline: anchor surviving, reachability BFS, ancestor
/// contraction, edge resolution, and fan reconstruction.
///
/// # Errors
///
/// Returns `LiftError::Restrict` if the underlying restrict operation
/// fails (e.g., edge resolution ambiguity, root pruned).
pub fn lift_wtype(
    compiled: &CompiledMigration,
    src_schema: &Schema,
    tgt_schema: &Schema,
    instance: &WInstance,
) -> Result<WInstance, LiftError> {
    let result = panproto_inst::wtype_restrict(instance, src_schema, tgt_schema, compiled)?;
    Ok(result)
}

/// Apply a compiled migration to a set-valued functor instance.
///
/// Delegates to [`panproto_inst::functor_restrict`], which performs
/// precomposition (`Delta_F`): for each table in the target, pull the
/// corresponding table from the source via the vertex remap.
///
/// # Errors
///
/// Returns `LiftError::Restrict` if the underlying restrict operation fails.
pub fn lift_functor(
    compiled: &CompiledMigration,
    instance: &FInstance,
) -> Result<FInstance, LiftError> {
    let result = panproto_inst::functor_restrict(instance, compiled)?;
    Ok(result)
}

/// Apply a compiled migration as a left Kan extension (`Sigma_F`) to a W-type instance.
///
/// Delegates to [`panproto_inst::wtype_extend`], which pushes nodes forward
/// along the migration morphism, remapping anchors and edges.
///
/// # Errors
///
/// Returns `LiftError::Restrict` if the underlying extend operation fails.
pub fn lift_wtype_sigma(
    compiled: &CompiledMigration,
    tgt_schema: &Schema,
    instance: &WInstance,
) -> Result<WInstance, LiftError> {
    let result = panproto_inst::wtype_extend(instance, tgt_schema, compiled)?;
    Ok(result)
}

/// Apply a compiled migration as a right Kan extension (`Pi_F`) to a W-type instance.
///
/// Delegates to [`panproto_inst::wtype_pi`], which computes the product
/// over fibers. Multi-element fibers produce subtree Cartesian products
/// bounded by `max_product_nodes`.
///
/// # Errors
///
/// Returns `LiftError::Restrict` if the underlying pi operation fails
/// (e.g., product size exceeded for multi-element fibers).
pub fn lift_wtype_pi(
    compiled: &CompiledMigration,
    tgt_schema: &Schema,
    instance: &WInstance,
    max_product_nodes: usize,
) -> Result<WInstance, LiftError> {
    let result = panproto_inst::wtype_pi(instance, tgt_schema, compiled, max_product_nodes)?;
    Ok(result)
}

/// Apply a compiled migration as a right Kan extension (`Pi_F`) to a functor instance.
///
/// Delegates to [`panproto_inst::functor_pi`], which computes Cartesian
/// products over fibers.
///
/// # Errors
///
/// Returns `LiftError::Restrict` if the underlying pi operation fails
/// (e.g., product size exceeded).
pub fn lift_functor_pi(
    compiled: &CompiledMigration,
    instance: &FInstance,
    max_product_size: usize,
) -> Result<FInstance, LiftError> {
    let result = panproto_inst::functor_pi(instance, compiled, max_product_size)?;
    Ok(result)
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use std::collections::{HashMap, HashSet};

    use super::*;
    use panproto_inst::value::FieldPresence;
    use panproto_inst::{Node, Value};
    use panproto_schema::{Edge, Vertex};

    fn test_schema(vertices: &[(&str, &str)], edges: &[Edge]) -> Schema {
        let mut vert_map = HashMap::new();
        let mut edge_map = HashMap::new();
        let mut outgoing: HashMap<panproto_gat::Name, smallvec::SmallVec<Edge, 4>> = HashMap::new();
        let mut incoming: HashMap<panproto_gat::Name, smallvec::SmallVec<Edge, 4>> = HashMap::new();
        let mut between: HashMap<
            (panproto_gat::Name, panproto_gat::Name),
            smallvec::SmallVec<Edge, 2>,
        > = HashMap::new();

        for (id, kind) in vertices {
            vert_map.insert(
                panproto_gat::Name::from(*id),
                Vertex {
                    id: panproto_gat::Name::from(*id),
                    kind: panproto_gat::Name::from(*kind),
                    nsid: None,
                },
            );
        }

        for edge in edges {
            edge_map.insert(edge.clone(), edge.kind.clone());
            outgoing
                .entry(edge.src.clone())
                .or_default()
                .push(edge.clone());
            incoming
                .entry(edge.tgt.clone())
                .or_default()
                .push(edge.clone());
            between
                .entry((edge.src.clone(), edge.tgt.clone()))
                .or_default()
                .push(edge.clone());
        }

        Schema {
            protocol: "test".into(),
            vertices: vert_map,
            edges: edge_map,
            hyper_edges: HashMap::new(),
            constraints: HashMap::new(),
            required: HashMap::new(),
            nsids: HashMap::new(),
            variants: HashMap::new(),
            orderings: HashMap::new(),
            recursion_points: HashMap::new(),
            spans: HashMap::new(),
            usage_modes: HashMap::new(),
            nominal: HashMap::new(),
            coercions: HashMap::new(),
            mergers: HashMap::new(),
            defaults: HashMap::new(),
            policies: HashMap::new(),
            outgoing,
            incoming,
            between,
        }
    }

    #[test]
    fn identity_migration_preserves_all_nodes() {
        // Test 1: identity migration preserves all nodes.
        let edge_text = Edge {
            src: "body".into(),
            tgt: "body.text".into(),
            kind: "prop".into(),
            name: Some("text".into()),
        };
        let edge_time = Edge {
            src: "body".into(),
            tgt: "body.createdAt".into(),
            kind: "prop".into(),
            name: Some("createdAt".into()),
        };

        let schema = test_schema(
            &[
                ("body", "object"),
                ("body.text", "string"),
                ("body.createdAt", "string"),
            ],
            &[edge_text.clone(), edge_time.clone()],
        );

        let mut nodes = HashMap::new();
        nodes.insert(0, Node::new(0, "body"));
        nodes.insert(
            1,
            Node::new(1, "body.text")
                .with_value(FieldPresence::Present(Value::Str("hello".into()))),
        );
        nodes.insert(
            2,
            Node::new(2, "body.createdAt")
                .with_value(FieldPresence::Present(Value::Str("2024-01-01".into()))),
        );

        let arcs = vec![(0, 1, edge_text.clone()), (0, 2, edge_time.clone())];
        let instance = WInstance::new(nodes, arcs, vec![], 0, panproto_gat::Name::from("body"));

        // Identity compiled migration
        let compiled = CompiledMigration {
            surviving_verts: HashSet::from([
                "body".into(),
                "body.text".into(),
                "body.createdAt".into(),
            ]),
            surviving_edges: HashSet::from([edge_text, edge_time]),
            vertex_remap: HashMap::new(),
            edge_remap: HashMap::new(),
            resolver: HashMap::new(),
            hyper_resolver: HashMap::new(),
            field_transforms: HashMap::new(),
            conditional_survival: HashMap::new(),
        };

        let result = lift_wtype(&compiled, &schema, &schema, &instance);
        assert!(result.is_ok(), "identity lift should succeed");
        let lifted = result.unwrap_or_else(|_| panic!("lift should succeed"));
        assert_eq!(
            lifted.node_count(),
            instance.node_count(),
            "identity migration should preserve all nodes"
        );
        assert_eq!(
            lifted.arc_count(),
            instance.arc_count(),
            "identity migration should preserve all arcs"
        );
    }

    #[test]
    fn projection_drops_vertices() {
        // Test 2: projection - drop vertices, verify surviving set.
        let edge_text = Edge {
            src: "body".into(),
            tgt: "body.text".into(),
            kind: "prop".into(),
            name: Some("text".into()),
        };
        let edge_time = Edge {
            src: "body".into(),
            tgt: "body.createdAt".into(),
            kind: "prop".into(),
            name: Some("createdAt".into()),
        };

        let schema = test_schema(
            &[("body", "object"), ("body.text", "string")],
            std::slice::from_ref(&edge_text),
        );

        let mut nodes = HashMap::new();
        nodes.insert(0, Node::new(0, "body"));
        nodes.insert(
            1,
            Node::new(1, "body.text")
                .with_value(FieldPresence::Present(Value::Str("hello".into()))),
        );
        nodes.insert(
            2,
            Node::new(2, "body.createdAt")
                .with_value(FieldPresence::Present(Value::Str("2024-01-01".into()))),
        );

        let arcs = vec![(0, 1, edge_text.clone()), (0, 2, edge_time)];
        let instance = WInstance::new(nodes, arcs, vec![], 0, panproto_gat::Name::from("body"));

        // Migration that drops body.createdAt
        let compiled = CompiledMigration {
            surviving_verts: HashSet::from(["body".into(), "body.text".into()]),
            surviving_edges: HashSet::from([edge_text]),
            vertex_remap: HashMap::new(),
            edge_remap: HashMap::new(),
            resolver: HashMap::new(),
            hyper_resolver: HashMap::new(),
            field_transforms: HashMap::new(),
            conditional_survival: HashMap::new(),
        };

        let result = lift_wtype(&compiled, &schema, &schema, &instance);
        assert!(result.is_ok(), "projection lift should succeed");
        let lifted = result.unwrap_or_else(|_| panic!("lift should succeed"));
        assert_eq!(lifted.node_count(), 2, "should have 2 surviving nodes");
        assert!(lifted.nodes.contains_key(&0), "root should survive");
        assert!(lifted.nodes.contains_key(&1), "text node should survive");
        assert!(
            !lifted.nodes.contains_key(&2),
            "createdAt node should be dropped"
        );
    }

    #[test]
    fn recursive_projection_via_wtype_restrict() {
        // Test 3: Recursive schema with nested children. Drop intermediate
        // vertices and verify the reachability filter prunes unreachable
        // subtrees via wtype_restrict.
        //
        // Schema: root -> container -> leaf1
        //                           -> leaf2
        //         root -> leaf3
        //
        // Migration drops "container", so leaf1 and leaf2 become
        // unreachable (no surviving ancestor path from root).
        let edge_root_container = Edge {
            src: "root".into(),
            tgt: "container".into(),
            kind: "prop".into(),
            name: Some("items".into()),
        };
        let edge_container_leaf1 = Edge {
            src: "container".into(),
            tgt: "leaf1".into(),
            kind: "prop".into(),
            name: Some("a".into()),
        };
        let edge_container_leaf2 = Edge {
            src: "container".into(),
            tgt: "leaf2".into(),
            kind: "prop".into(),
            name: Some("b".into()),
        };
        let edge_root_leaf3 = Edge {
            src: "root".into(),
            tgt: "leaf3".into(),
            kind: "prop".into(),
            name: Some("direct".into()),
        };

        let schema = test_schema(
            &[("root", "object"), ("leaf3", "string")],
            std::slice::from_ref(&edge_root_leaf3),
        );

        let mut nodes = HashMap::new();
        nodes.insert(0, Node::new(0, "root"));
        nodes.insert(1, Node::new(1, "container"));
        nodes.insert(
            2,
            Node::new(2, "leaf1").with_value(FieldPresence::Present(Value::Str("val1".into()))),
        );
        nodes.insert(
            3,
            Node::new(3, "leaf2").with_value(FieldPresence::Present(Value::Str("val2".into()))),
        );
        nodes.insert(
            4,
            Node::new(4, "leaf3").with_value(FieldPresence::Present(Value::Str("val3".into()))),
        );

        let arcs = vec![
            (0, 1, edge_root_container),
            (1, 2, edge_container_leaf1),
            (1, 3, edge_container_leaf2),
            (0, 4, edge_root_leaf3.clone()),
        ];
        let instance = WInstance::new(nodes, arcs, vec![], 0, panproto_gat::Name::from("root"));

        // Migration: drop "container", keep root and leaf3 only.
        let compiled = CompiledMigration {
            surviving_verts: HashSet::from(["root".into(), "leaf3".into()]),
            surviving_edges: HashSet::from([edge_root_leaf3]),
            vertex_remap: HashMap::new(),
            edge_remap: HashMap::new(),
            resolver: HashMap::new(),
            hyper_resolver: HashMap::new(),
            field_transforms: HashMap::new(),
            conditional_survival: HashMap::new(),
        };

        let result = lift_wtype(&compiled, &schema, &schema, &instance);
        assert!(result.is_ok(), "recursive projection should succeed");
        let lifted = result.unwrap_or_else(|_| panic!("lift should succeed"));

        // Only root (0) and leaf3 (4) should survive; container (1),
        // leaf1 (2), and leaf2 (3) are all pruned.
        assert_eq!(
            lifted.node_count(),
            2,
            "should have 2 surviving nodes (root + leaf3)"
        );
        assert!(lifted.nodes.contains_key(&0), "root should survive");
        assert!(lifted.nodes.contains_key(&4), "leaf3 should survive");
        assert!(!lifted.nodes.contains_key(&1), "container should be pruned");
        assert!(
            !lifted.nodes.contains_key(&2),
            "leaf1 should be pruned (unreachable)"
        );
        assert!(
            !lifted.nodes.contains_key(&3),
            "leaf2 should be pruned (unreachable)"
        );
    }
}