dlin-core 0.2.0-alpha.1

Core library for dbt model lineage analysis
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
use std::collections::HashMap;

use super::cache::{CACHE_DIR, COLUMN_LINEAGE_CACHE_FILENAME, ColumnLineageCacheFile};
use super::cross_model::normalize_table_name;
use super::impact::build_downstream_model_map;
use super::single_model::format_lineage_error;
use super::*;
use crate::parser::manifest::{
    DependsOn, ManifestColumn, ManifestConfig, ManifestNode, ManifestSource,
};
use polyglot_sql::Schema;

/// Build a minimal manifest for testing column lineage.
fn make_test_manifest() -> Manifest {
    let mut nodes = HashMap::new();

    // stg_orders: SELECT id as order_id, user_id as customer_id, order_date, status FROM raw.orders
    let mut stg_orders_cols = HashMap::new();
    for name in ["order_id", "customer_id", "order_date", "status"] {
        stg_orders_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.stg_orders".to_string(),
        ManifestNode {
            unique_id: "model.proj.stg_orders".to_string(),
            name: "stg_orders".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["source.proj.raw.orders".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: stg_orders_cols,
            compiled_code: Some(
                "select id as order_id, user_id as customer_id, order_date, status from raw.orders"
                    .to_string(),
            ),
            database: None,
            schema: None,
        },
    );

    // orders: SELECT o.order_id, o.customer_id, p.amount as total_amount FROM stg_orders o LEFT JOIN stg_payments p ON o.order_id = p.order_id
    let mut orders_cols = HashMap::new();
    for name in ["order_id", "customer_id", "total_amount"] {
        orders_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert("model.proj.orders".to_string(), ManifestNode {
            unique_id: "model.proj.orders".to_string(),
            name: "orders".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn { nodes: vec![
                "model.proj.stg_orders".to_string(),
                "model.proj.stg_payments".to_string(),
            ] },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: orders_cols,
            compiled_code: Some("select o.order_id, o.customer_id, p.amount as total_amount from stg_orders o left join stg_payments p on o.order_id = p.order_id".to_string()),
            database: None,
            schema: None,
        });

    // stg_payments (upstream, for schema)
    let mut stg_payments_cols = HashMap::new();
    for name in ["payment_id", "order_id", "amount", "payment_method"] {
        stg_payments_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.stg_payments".to_string(),
        ManifestNode {
            unique_id: "model.proj.stg_payments".to_string(),
            name: "stg_payments".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn { nodes: vec![] },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: stg_payments_cols,
            compiled_code: Some(
                "select id as payment_id, order_id, amount, payment_method from raw.payments"
                    .to_string(),
            ),
            database: None,
            schema: None,
        },
    );

    // Source: raw.orders
    let mut source_cols = HashMap::new();
    for name in ["id", "user_id", "order_date", "status"] {
        source_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    let mut sources = HashMap::new();
    sources.insert(
        "source.proj.raw.orders".to_string(),
        ManifestSource {
            unique_id: "source.proj.raw.orders".to_string(),
            name: "orders".to_string(),
            source_name: "raw".to_string(),
            resource_type: "source".to_string(),
            description: None,
            path: None,
            columns: source_cols,
            database: None,
            schema: None,
            identifier: None,
        },
    );

    Manifest {
        nodes,
        sources,
        exposures: HashMap::new(),
    }
}

fn make_cross_model_manifest() -> Manifest {
    let mut nodes = HashMap::new();

    // Source: raw.orders
    let mut raw_orders_cols = HashMap::new();
    for name in ["id", "user_id", "order_date", "status"] {
        raw_orders_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    let mut sources = HashMap::new();
    sources.insert(
        "source.proj.raw.orders".to_string(),
        ManifestSource {
            unique_id: "source.proj.raw.orders".to_string(),
            name: "orders".to_string(),
            source_name: "raw".to_string(),
            resource_type: "source".to_string(),
            description: None,
            path: None,
            columns: raw_orders_cols,
            database: None,
            schema: None,
            identifier: None,
        },
    );

    // Source: raw.payments
    let mut raw_payments_cols = HashMap::new();
    for name in ["id", "order_id", "amount", "payment_method"] {
        raw_payments_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    sources.insert(
        "source.proj.raw.payments".to_string(),
        ManifestSource {
            unique_id: "source.proj.raw.payments".to_string(),
            name: "payments".to_string(),
            source_name: "raw".to_string(),
            resource_type: "source".to_string(),
            description: None,
            path: None,
            columns: raw_payments_cols,
            database: None,
            schema: None,
            identifier: None,
        },
    );

    // stg_orders: renames id→order_id, user_id→customer_id
    let mut stg_orders_cols = HashMap::new();
    for name in ["order_id", "customer_id", "order_date", "status"] {
        stg_orders_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.stg_orders".to_string(),
        ManifestNode {
            unique_id: "model.proj.stg_orders".to_string(),
            name: "stg_orders".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["source.proj.raw.orders".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: stg_orders_cols,
            compiled_code: Some(
                "select id as order_id, user_id as customer_id, order_date, status from orders"
                    .to_string(),
            ),
            database: None,
            schema: None,
        },
    );

    // stg_payments: renames id→payment_id
    let mut stg_payments_cols = HashMap::new();
    for name in ["payment_id", "order_id", "amount", "payment_method"] {
        stg_payments_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.stg_payments".to_string(),
        ManifestNode {
            unique_id: "model.proj.stg_payments".to_string(),
            name: "stg_payments".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["source.proj.raw.payments".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: stg_payments_cols,
            compiled_code: Some(
                "select id as payment_id, order_id, amount, payment_method from payments"
                    .to_string(),
            ),
            database: None,
            schema: None,
        },
    );

    // orders: joins stg_orders + stg_payments (CTE pattern like real dbt compiled SQL)
    let mut orders_cols = HashMap::new();
    for name in ["order_id", "customer_id", "total_amount"] {
        orders_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.orders".to_string(),
        ManifestNode {
            unique_id: "model.proj.orders".to_string(),
            name: "orders".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec![
                    "model.proj.stg_orders".to_string(),
                    "model.proj.stg_payments".to_string(),
                ],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: orders_cols,
            compiled_code: Some(
                concat!(
                    "with stg_orders as (select * from stg_orders), ",
                    "stg_payments as (select * from stg_payments) ",
                    "select stg_orders.order_id, stg_orders.customer_id, ",
                    "stg_payments.amount as total_amount ",
                    "from stg_orders left join stg_payments ",
                    "on stg_orders.order_id = stg_payments.order_id"
                )
                .to_string(),
            ),
            database: None,
            schema: None,
        },
    );

    // customers: aggregates from orders model (CTE pattern)
    let mut customers_cols = HashMap::new();
    for name in ["customer_id", "order_count"] {
        customers_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.customers".to_string(),
        ManifestNode {
            unique_id: "model.proj.customers".to_string(),
            name: "customers".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["model.proj.orders".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: customers_cols,
            compiled_code: Some(
                concat!(
                    "with orders as (select * from orders) ",
                    "select customer_id, count(*) as order_count from orders group by customer_id"
                )
                .to_string(),
            ),
            database: None,
            schema: None,
        },
    );

    Manifest {
        nodes,
        sources,
        exposures: HashMap::new(),
    }
}

fn make_duplicate_name_manifest() -> Manifest {
    let mut nodes = HashMap::new();

    // stg_orders in pkg_a (the upstream model being traced)
    nodes.insert(
        "model.pkg_a.stg_orders".to_string(),
        ManifestNode {
            unique_id: "model.pkg_a.stg_orders".to_string(),
            name: "stg_orders".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn { nodes: vec![] },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: {
                let mut cols = HashMap::new();
                cols.insert(
                    "customer_id".to_string(),
                    ManifestColumn {
                        name: "customer_id".to_string(),
                    },
                );
                cols
            },
            compiled_code: Some("select customer_id from raw_customers".to_string()),
            database: None,
            schema: None,
        },
    );

    // customers in pkg_a — SELECT customer_id FROM stg_orders
    nodes.insert(
        "model.pkg_a.customers".to_string(),
        ManifestNode {
            unique_id: "model.pkg_a.customers".to_string(),
            name: "customers".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["model.pkg_a.stg_orders".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: {
                let mut cols = HashMap::new();
                cols.insert(
                    "customer_id".to_string(),
                    ManifestColumn {
                        name: "customer_id".to_string(),
                    },
                );
                cols
            },
            compiled_code: Some("select customer_id from stg_orders".to_string()),
            database: None,
            schema: None,
        },
    );

    // customers in pkg_b — same name, different package, same SQL pattern
    nodes.insert(
        "model.pkg_b.customers".to_string(),
        ManifestNode {
            unique_id: "model.pkg_b.customers".to_string(),
            name: "customers".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["model.pkg_a.stg_orders".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: {
                let mut cols = HashMap::new();
                cols.insert(
                    "customer_id".to_string(),
                    ManifestColumn {
                        name: "customer_id".to_string(),
                    },
                );
                cols
            },
            compiled_code: Some("select customer_id from stg_orders".to_string()),
            database: None,
            schema: None,
        },
    );

    Manifest {
        nodes,
        sources: HashMap::new(),
        exposures: HashMap::new(),
    }
}

#[test]
fn test_column_impact_duplicate_model_names_across_packages() {
    // Two packages each have a "customers" model depending on the same stg_orders.
    // Both should appear in impacted_columns with distinct unique_ids.
    let manifest = make_duplicate_name_manifest();
    let result = compute_column_impact(
        &manifest,
        "stg_orders",
        "customer_id",
        DialectType::Generic,
        &mut ColumnLineageCache::disabled(),
    );

    assert!(result.errors.is_empty(), "errors: {:?}", result.errors);

    let unique_ids: Vec<&str> = result
        .impacted_columns
        .iter()
        .filter(|ic| ic.column == "customer_id")
        .map(|ic| ic.unique_id.as_str())
        .collect();

    assert!(
        unique_ids.contains(&"model.pkg_a.customers"),
        "pkg_a customers should be impacted, got unique_ids: {:?}",
        unique_ids
    );
    assert!(
        unique_ids.contains(&"model.pkg_b.customers"),
        "pkg_b customers should be impacted, got unique_ids: {:?}",
        unique_ids
    );
    assert_eq!(
        unique_ids.len(),
        2,
        "both same-named models should appear separately, got: {:?}",
        result.impacted_columns
    );
}

/// Build a diamond DAG manifest where different columns flow through a shared model:
///
///   raw_data (x, y)
///      |
///   shared (x, y)   -- passes both columns through
///    /        \
///  left(x)  right(y)  -- each uses a different column from shared
///    \        /
///   diamond_out(lx, ry) -- combines left.x and right.y
fn make_diamond_manifest() -> Manifest {
    let mut nodes = HashMap::new();

    // raw_data: source with columns x, y
    let mut raw_cols = HashMap::new();
    for name in ["x", "y"] {
        raw_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.raw_data".to_string(),
        ManifestNode {
            unique_id: "model.proj.raw_data".to_string(),
            name: "raw_data".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn { nodes: vec![] },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: raw_cols,
            compiled_code: Some("select x, y from source_table".to_string()),
            database: None,
            schema: None,
        },
    );

    // shared: passes x and y through from raw_data
    let mut shared_cols = HashMap::new();
    for name in ["x", "y"] {
        shared_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.shared".to_string(),
        ManifestNode {
            unique_id: "model.proj.shared".to_string(),
            name: "shared".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["model.proj.raw_data".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: shared_cols,
            compiled_code: Some("select x, y from raw_data".to_string()),
            database: None,
            schema: None,
        },
    );

    // left: uses column x from shared
    let mut left_cols = HashMap::new();
    left_cols.insert(
        "x".to_string(),
        ManifestColumn {
            name: "x".to_string(),
        },
    );
    nodes.insert(
        "model.proj.left_model".to_string(),
        ManifestNode {
            unique_id: "model.proj.left_model".to_string(),
            name: "left_model".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["model.proj.shared".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: left_cols,
            compiled_code: Some("select x from shared".to_string()),
            database: None,
            schema: None,
        },
    );

    // right: uses column y from shared
    let mut right_cols = HashMap::new();
    right_cols.insert(
        "y".to_string(),
        ManifestColumn {
            name: "y".to_string(),
        },
    );
    nodes.insert(
        "model.proj.right_model".to_string(),
        ManifestNode {
            unique_id: "model.proj.right_model".to_string(),
            name: "right_model".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec!["model.proj.shared".to_string()],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: right_cols,
            compiled_code: Some("select y from shared".to_string()),
            database: None,
            schema: None,
        },
    );

    // diamond_out: combines left.x and right.y
    let mut out_cols = HashMap::new();
    for name in ["lx", "ry"] {
        out_cols.insert(
            name.to_string(),
            ManifestColumn {
                name: name.to_string(),
            },
        );
    }
    nodes.insert(
        "model.proj.diamond_out".to_string(),
        ManifestNode {
            unique_id: "model.proj.diamond_out".to_string(),
            name: "diamond_out".to_string(),
            resource_type: "model".to_string(),
            depends_on: DependsOn {
                nodes: vec![
                    "model.proj.left_model".to_string(),
                    "model.proj.right_model".to_string(),
                ],
            },
            config: ManifestConfig::default(),
            description: None,
            path: None,
            columns: out_cols,
            compiled_code: Some(
                "select l.x as lx, r.y as ry from left_model l join right_model r on 1=1"
                    .to_string(),
            ),
            database: None,
            schema: None,
        },
    );

    Manifest {
        nodes,
        sources: HashMap::new(),
        exposures: HashMap::new(),
    }
}

mod cache;
mod core;
mod impact;