sqlglot-rust 0.9.27

A SQL parser, optimizer, and transpiler library inspired by Python's sqlglot
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
//! Column lineage tracking for SQL queries.
//!
//! Provides functionality to trace data flow from source columns through
//! query transformations to output columns. This is the foundation for
//! data governance tools and impact analysis.
//!
//! Inspired by Python sqlglot's `lineage.py`.
//!
//! # Example
//!
//! ```rust
//! use sqlglot_rust::parser::parse;
//! use sqlglot_rust::dialects::Dialect;
//! use sqlglot_rust::optimizer::lineage::{lineage, LineageConfig};
//! use sqlglot_rust::schema::MappingSchema;
//!
//! let sql = "SELECT a, b + 1 AS c FROM t";
//! let ast = parse(sql, Dialect::Ansi).unwrap();
//! let schema = MappingSchema::new(Dialect::Ansi);
//! let config = LineageConfig::default();
//!
//! let graph = lineage("c", &ast, &schema, &config).unwrap();
//! assert_eq!(graph.node.name, "c");
//! ```

use std::collections::{HashMap, HashSet};

use crate::ast::*;
use crate::dialects::Dialect;
use crate::errors::SqlglotError;
use crate::schema::{MappingSchema, Schema};

// ═══════════════════════════════════════════════════════════════════════
// Error types
// ═══════════════════════════════════════════════════════════════════════

/// Errors specific to lineage operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LineageError {
    /// The target column was not found in the output.
    ColumnNotFound(String),
    /// Ambiguous column reference (multiple sources).
    AmbiguousColumn(String),
    /// Invalid query structure for lineage analysis.
    InvalidQuery(String),
    /// A parsing error occurred.
    ParseError(String),
}

impl std::fmt::Display for LineageError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LineageError::ColumnNotFound(c) => write!(f, "Column not found in output: {c}"),
            LineageError::AmbiguousColumn(c) => write!(f, "Ambiguous column reference: {c}"),
            LineageError::InvalidQuery(msg) => write!(f, "Invalid query for lineage: {msg}"),
            LineageError::ParseError(msg) => write!(f, "Parse error: {msg}"),
        }
    }
}

impl std::error::Error for LineageError {}

impl From<LineageError> for SqlglotError {
    fn from(e: LineageError) -> Self {
        SqlglotError::Internal(e.to_string())
    }
}

/// Result type for lineage operations.
pub type LineageResult<T> = std::result::Result<T, LineageError>;

// ═══════════════════════════════════════════════════════════════════════
// Configuration
// ═══════════════════════════════════════════════════════════════════════

/// Configuration for lineage analysis.
#[derive(Debug, Clone)]
pub struct LineageConfig {
    /// SQL dialect for parsing and identifier normalization.
    pub dialect: Dialect,
    /// Whether to trim column qualifiers in output node names.
    pub trim_qualifiers: bool,
    /// External sources mapping for multi-query lineage.
    /// Maps source names to their SQL definitions (e.g., views).
    pub sources: HashMap<String, String>,
}

impl Default for LineageConfig {
    fn default() -> Self {
        Self {
            dialect: Dialect::Ansi,
            trim_qualifiers: true,
            sources: HashMap::new(),
        }
    }
}

impl LineageConfig {
    /// Create a new configuration with the specified dialect.
    #[must_use]
    pub fn new(dialect: Dialect) -> Self {
        Self {
            dialect,
            ..Default::default()
        }
    }

    /// Add external sources for multi-query lineage.
    #[must_use]
    pub fn with_sources(mut self, sources: HashMap<String, String>) -> Self {
        self.sources = sources;
        self
    }

    /// Set whether to trim table qualifiers from output names.
    #[must_use]
    pub fn with_trim_qualifiers(mut self, trim: bool) -> Self {
        self.trim_qualifiers = trim;
        self
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Lineage Node
// ═══════════════════════════════════════════════════════════════════════

/// A node in the lineage graph representing a column or expression.
#[derive(Debug, Clone)]
pub struct LineageNode {
    /// The name of this column/expression (e.g., "a", "SUM(b)", "t.col").
    pub name: String,
    /// The AST expression this node represents.
    pub expression: Option<Expr>,
    /// The source table/CTE/subquery name, if applicable.
    pub source_name: Option<String>,
    /// Reference to the source AST (for complex expressions).
    pub source: Option<Expr>,
    /// Child nodes (upstream lineage - where data comes from).
    pub downstream: Vec<LineageNode>,
    /// The alias, if this is an aliased expression.
    pub alias: Option<String>,
    /// Depth in the lineage graph (0 = root output column).
    pub depth: usize,
}

impl LineageNode {
    /// Create a new lineage node.
    #[must_use]
    pub fn new(name: String) -> Self {
        Self {
            name,
            expression: None,
            source_name: None,
            source: None,
            downstream: Vec::new(),
            alias: None,
            depth: 0,
        }
    }

    /// Create a node with source information.
    #[must_use]
    pub fn with_source(mut self, source_name: String) -> Self {
        self.source_name = Some(source_name);
        self
    }

    /// Create a node with an expression.
    #[must_use]
    pub fn with_expression(mut self, expr: Expr) -> Self {
        self.expression = Some(expr);
        self
    }

    /// Create a node with an alias.
    #[must_use]
    #[allow(dead_code)]
    pub fn with_alias(mut self, alias: String) -> Self {
        self.alias = Some(alias);
        self
    }

    /// Create a node with depth.
    #[must_use]
    pub fn with_depth(mut self, depth: usize) -> Self {
        self.depth = depth;
        self
    }

    /// Add a downstream (upstream lineage) node.
    #[allow(dead_code)]
    pub fn add_downstream(&mut self, node: LineageNode) {
        self.downstream.push(node);
    }

    /// Walk through all nodes in the lineage graph (pre-order).
    pub fn walk<F>(&self, visitor: &mut F)
    where
        F: FnMut(&LineageNode),
    {
        visitor(self);
        for child in &self.downstream {
            child.walk(visitor);
        }
    }

    /// Iterate over all nodes in the lineage graph.
    #[must_use]
    pub fn iter(&self) -> LineageIterator<'_> {
        LineageIterator { stack: vec![self] }
    }

    /// Get all source columns (leaf nodes) in this lineage.
    #[must_use]
    #[allow(dead_code)]
    pub fn source_columns(&self) -> Vec<&LineageNode> {
        self.iter().filter(|n| n.downstream.is_empty()).collect()
    }

    /// Get all source table names referenced in this lineage.
    #[must_use]
    pub fn source_tables(&self) -> Vec<String> {
        let mut tables = HashSet::new();
        for node in self.iter() {
            if let Some(ref source) = node.source_name {
                tables.insert(source.clone());
            }
        }
        tables.into_iter().collect()
    }

    /// Generate DOT format representation for visualization.
    #[must_use]
    pub fn to_dot(&self) -> String {
        let mut dot = String::from("digraph lineage {\n");
        dot.push_str("  rankdir=BT;\n");
        dot.push_str("  node [shape=box];\n");

        let mut node_id = 0;
        let mut node_ids = HashMap::new();

        // First pass: assign IDs and create nodes
        self.walk(&mut |node| {
            let id = format!("n{}", node_id);
            let label = if let Some(ref src) = node.source_name {
                format!("{}.{}", src, node.name)
            } else {
                node.name.clone()
            };
            dot.push_str(&format!("  {} [label=\"{}\"];\n", id, escape_dot(&label)));
            node_ids.insert(node as *const _ as usize, id);
            node_id += 1;
        });

        // Second pass: create edges
        self.walk(&mut |node| {
            let parent_id = node_ids.get(&(node as *const _ as usize)).unwrap();
            for child in &node.downstream {
                let child_id = node_ids.get(&(child as *const _ as usize)).unwrap();
                dot.push_str(&format!("  {} -> {};\n", child_id, parent_id));
            }
        });

        dot.push_str("}\n");
        dot
    }

    /// Generate Mermaid diagram representation.
    #[must_use]
    pub fn to_mermaid(&self) -> String {
        let mut mermaid = String::from("flowchart BT\n");

        let mut node_id = 0;
        let mut node_ids = HashMap::new();

        // First pass: assign IDs and create nodes
        self.walk(&mut |node| {
            let id = format!("n{}", node_id);
            let label = if let Some(ref src) = node.source_name {
                format!("{}.{}", src, node.name)
            } else {
                node.name.clone()
            };
            mermaid.push_str(&format!("  {}[\"{}\"]\n", id, escape_mermaid(&label)));
            node_ids.insert(node as *const _ as usize, id);
            node_id += 1;
        });

        // Second pass: create edges
        self.walk(&mut |node| {
            let parent_id = node_ids.get(&(node as *const _ as usize)).unwrap();
            for child in &node.downstream {
                let child_id = node_ids.get(&(child as *const _ as usize)).unwrap();
                mermaid.push_str(&format!("  {} --> {}\n", child_id, parent_id));
            }
        });

        mermaid
    }
}

/// Iterator over lineage nodes (pre-order traversal).
pub struct LineageIterator<'a> {
    stack: Vec<&'a LineageNode>,
}

impl<'a> Iterator for LineageIterator<'a> {
    type Item = &'a LineageNode;

    fn next(&mut self) -> Option<Self::Item> {
        self.stack.pop().map(|node| {
            // Push children in reverse order for pre-order traversal
            for child in node.downstream.iter().rev() {
                self.stack.push(child);
            }
            node
        })
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Lineage Graph
// ═══════════════════════════════════════════════════════════════════════

/// A lineage graph rooted at a specific output column.
#[derive(Debug, Clone)]
pub struct LineageGraph {
    /// The root node representing the target output column.
    pub node: LineageNode,
    /// The original SQL that was analyzed.
    pub sql: Option<String>,
    /// The dialect used for analysis.
    pub dialect: Dialect,
}

impl LineageGraph {
    /// Create a new lineage graph.
    #[must_use]
    pub fn new(node: LineageNode, dialect: Dialect) -> Self {
        Self {
            node,
            sql: None,
            dialect,
        }
    }

    /// Set the original SQL string.
    #[must_use]
    #[allow(dead_code)]
    pub fn with_sql(mut self, sql: String) -> Self {
        self.sql = Some(sql);
        self
    }

    /// Get all source tables in the lineage.
    #[must_use]
    pub fn source_tables(&self) -> Vec<String> {
        self.node.source_tables()
    }

    /// Get all source columns (leaf nodes).
    #[must_use]
    #[allow(dead_code)]
    pub fn source_columns(&self) -> Vec<&LineageNode> {
        self.node.source_columns()
    }

    /// Walk through all nodes in the graph.
    #[allow(dead_code)]
    pub fn walk<F>(&self, visitor: &mut F)
    where
        F: FnMut(&LineageNode),
    {
        self.node.walk(visitor);
    }

    /// Generate DOT format visualization.
    #[must_use]
    pub fn to_dot(&self) -> String {
        self.node.to_dot()
    }

    /// Generate Mermaid diagram visualization.
    #[must_use]
    pub fn to_mermaid(&self) -> String {
        self.node.to_mermaid()
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Context for lineage building
// ═══════════════════════════════════════════════════════════════════════

/// Internal context for building lineage graphs.
struct LineageContext {
    /// The schema for column resolution.
    schema: MappingSchema,
    /// Configuration options.
    config: LineageConfig,
    /// Current depth in the lineage graph.
    depth: usize,
    /// CTE definitions available in this scope (owned).
    ctes: HashMap<String, Statement>,
    /// Visible sources in current scope (alias/name → source info).
    sources: HashMap<String, SourceInfo>,
    /// External sources for multi-query lineage.
    external_sources: HashMap<String, Statement>,
    /// Sources currently being visited (to prevent infinite recursion).
    visiting: HashSet<String>,
}

/// Information about a source (table, CTE, derived table).
#[derive(Debug, Clone)]
struct SourceInfo {
    /// The source type.
    kind: SourceKind,
    /// For subqueries/CTEs, the SELECT columns.
    columns: Option<Vec<SelectItem>>,
    /// The underlying statement, if any (owned).
    statement: Option<Statement>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
enum SourceKind {
    Table,
    Cte,
    DerivedTable,
    External,
}

impl LineageContext {
    fn new(schema: &MappingSchema, config: &LineageConfig) -> Self {
        Self {
            schema: schema.clone(),
            config: config.clone(),
            depth: 0,
            ctes: HashMap::new(),
            sources: HashMap::new(),
            external_sources: HashMap::new(),
            visiting: HashSet::new(),
        }
    }

    fn with_depth(&self, depth: usize) -> Self {
        Self {
            schema: self.schema.clone(),
            config: self.config.clone(),
            depth,
            ctes: self.ctes.clone(),
            sources: self.sources.clone(),
            external_sources: self.external_sources.clone(),
            visiting: self.visiting.clone(),
        }
    }

    #[allow(dead_code)]
    fn resolve_source(&self, name: &str) -> Option<&SourceInfo> {
        let normalized = normalize_name(name, self.config.dialect);
        self.sources.get(&normalized)
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Public API
// ═══════════════════════════════════════════════════════════════════════

/// Build lineage for a specific output column in a SQL statement.
///
/// # Arguments
///
/// * `column` - The name of the output column to trace (can include table qualifier).
/// * `statement` - The parsed SQL statement.
/// * `schema` - Schema information for table/column resolution.
/// * `config` - Configuration options.
///
/// # Returns
///
/// A [`LineageGraph`] rooted at the target column, showing its upstream lineage.
///
/// # Errors
///
/// Returns [`LineageError::ColumnNotFound`] if the column is not in the output.
///
/// # Example
///
/// ```rust
/// use sqlglot_rust::parser::parse;
/// use sqlglot_rust::dialects::Dialect;
/// use sqlglot_rust::optimizer::lineage::{lineage, LineageConfig};
/// use sqlglot_rust::schema::MappingSchema;
///
/// let sql = "SELECT a, b AS c FROM t";
/// let ast = parse(sql, Dialect::Ansi).unwrap();
/// let schema = MappingSchema::new(Dialect::Ansi);
/// let config = LineageConfig::default();
///
/// let graph = lineage("c", &ast, &schema, &config).unwrap();
/// assert_eq!(graph.node.name, "c");
/// ```
pub fn lineage(
    column: &str,
    statement: &Statement,
    schema: &MappingSchema,
    config: &LineageConfig,
) -> LineageResult<LineageGraph> {
    // Parse external sources if provided
    let mut ctx = LineageContext::new(schema, config);

    for (name, sql) in &config.sources {
        match crate::parser::parse(sql, config.dialect) {
            Ok(stmt) => {
                ctx.external_sources
                    .insert(normalize_name(name, config.dialect), stmt);
            }
            Err(e) => return Err(LineageError::ParseError(e.to_string())),
        }
    }

    // Build lineage for the target column
    let node = build_lineage_for_column(column, statement, &mut ctx)?;

    Ok(LineageGraph::new(node, config.dialect))
}

/// Build lineage from a SQL string.
///
/// Convenience function that parses the SQL and builds lineage.
///
/// # Example
///
/// ```rust
/// use sqlglot_rust::dialects::Dialect;
/// use sqlglot_rust::optimizer::lineage::{lineage_sql, LineageConfig};
/// use sqlglot_rust::schema::MappingSchema;
///
/// let schema = MappingSchema::new(Dialect::Ansi);
/// let config = LineageConfig::default();
///
/// let graph = lineage_sql("c", "SELECT a + b AS c FROM t", &schema, &config).unwrap();
/// assert_eq!(graph.node.name, "c");
/// ```
pub fn lineage_sql(
    column: &str,
    sql: &str,
    schema: &MappingSchema,
    config: &LineageConfig,
) -> LineageResult<LineageGraph> {
    let statement = crate::parser::parse(sql, config.dialect)
        .map_err(|e| LineageError::ParseError(e.to_string()))?;

    let mut graph = lineage(column, &statement, schema, config)?;
    graph.sql = Some(sql.to_string());
    Ok(graph)
}

// ═══════════════════════════════════════════════════════════════════════
// Internal lineage building
// ═══════════════════════════════════════════════════════════════════════

/// Build lineage for a specific column in a statement.
fn build_lineage_for_column(
    column: &str,
    statement: &Statement,
    ctx: &mut LineageContext,
) -> LineageResult<LineageNode> {
    match statement {
        Statement::Select(sel) => build_lineage_for_select_column(column, sel, ctx),
        Statement::SetOperation(set_op) => build_lineage_for_set_operation(column, set_op, ctx),
        Statement::CreateView(cv) => build_lineage_for_column(column, &cv.query, ctx),
        _ => Err(LineageError::InvalidQuery(
            "Lineage analysis requires a SELECT or set operation statement".to_string(),
        )),
    }
}

/// Build lineage for a column in a SELECT statement.
fn build_lineage_for_select_column(
    column: &str,
    sel: &SelectStatement,
    ctx: &mut LineageContext,
) -> LineageResult<LineageNode> {
    // Register CTEs (cloning to avoid lifetime issues)
    for cte in &sel.ctes {
        let cte_name = normalize_name(&cte.name, ctx.config.dialect);
        ctx.ctes.insert(cte_name.clone(), (*cte.query).clone());
        ctx.sources.insert(
            cte_name,
            SourceInfo {
                kind: SourceKind::Cte,
                columns: extract_select_columns(&cte.query),
                statement: Some((*cte.query).clone()),
            },
        );
    }

    // Register FROM source
    if let Some(from) = &sel.from {
        register_table_source(&from.source, ctx);
    }

    // Register JOINs
    for join in &sel.joins {
        register_table_source(&join.table, ctx);
    }

    // Find the target column in the SELECT list
    let (col_name, table_qual) = parse_column_ref(column);

    for item in &sel.columns {
        match item {
            SelectItem::Expr { expr, alias, .. } => {
                let item_name = alias
                    .as_ref()
                    .map(String::as_str)
                    .unwrap_or_else(|| expr_output_name(expr));

                if matches_column_name(item_name, &col_name) {
                    return build_lineage_for_expr(expr, alias.clone(), ctx);
                }
            }
            SelectItem::Wildcard => {
                // Expand wildcard - check all sources
                for (source_name, source_info) in ctx.sources.clone() {
                    if let Some(cols) = &source_info.columns {
                        for col_item in cols {
                            if let SelectItem::Expr { expr, alias, .. } = col_item {
                                let item_name = alias
                                    .as_ref()
                                    .map(String::as_str)
                                    .unwrap_or_else(|| expr_output_name(expr));
                                if matches_column_name(item_name, &col_name) {
                                    return build_lineage_for_expr(expr, alias.clone(), ctx);
                                }
                            }
                        }
                    } else if source_info.kind == SourceKind::Table {
                        // Check schema for table columns
                        if let Ok(schema_cols) = ctx.schema.column_names(&[&source_name]) {
                            if schema_cols
                                .iter()
                                .any(|c| matches_column_name(c, &col_name))
                            {
                                // Found in schema
                                let mut node = LineageNode::new(col_name.clone())
                                    .with_source(source_name.clone())
                                    .with_depth(ctx.depth);
                                node.expression = Some(Expr::Column {
                                    table: Some(source_name.clone()),
                                    name: col_name.clone(),
                                    quote_style: QuoteStyle::None,
                                    table_quote_style: QuoteStyle::None,
                                });
                                return Ok(node);
                            }
                        }
                    }
                }
            }
            SelectItem::QualifiedWildcard { table } => {
                if table_qual
                    .as_ref()
                    .is_some_and(|t| matches_column_name(t, table))
                {
                    // Check if column exists in this table
                    if let Some(source_info) = ctx.sources.get(table).cloned() {
                        if let Some(cols) = &source_info.columns {
                            for col_item in cols {
                                if let SelectItem::Expr { expr, alias, .. } = col_item {
                                    let item_name = alias
                                        .as_ref()
                                        .map(String::as_str)
                                        .unwrap_or_else(|| expr_output_name(expr));
                                    if matches_column_name(item_name, &col_name) {
                                        return build_lineage_for_expr(expr, alias.clone(), ctx);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Err(LineageError::ColumnNotFound(column.to_string()))
}

/// Build lineage for a set operation (UNION, INTERSECT, EXCEPT).
fn build_lineage_for_set_operation(
    column: &str,
    set_op: &SetOperationStatement,
    ctx: &mut LineageContext,
) -> LineageResult<LineageNode> {
    let mut root = LineageNode::new(column.to_string()).with_depth(ctx.depth);

    // Build lineage from both branches
    let mut child_ctx = ctx.with_depth(ctx.depth + 1);

    let left_lineage = build_lineage_for_column(column, &set_op.left, &mut child_ctx)?;
    let right_lineage = build_lineage_for_column(column, &set_op.right, &mut child_ctx)?;

    root.downstream.push(left_lineage);
    root.downstream.push(right_lineage);

    Ok(root)
}

/// Build lineage for an expression.
fn build_lineage_for_expr(
    expr: &Expr,
    alias: Option<String>,
    ctx: &mut LineageContext,
) -> LineageResult<LineageNode> {
    let name = alias
        .clone()
        .unwrap_or_else(|| expr_to_name(expr, ctx.config.trim_qualifiers));
    let mut node = LineageNode::new(name.clone())
        .with_expression(expr.clone())
        .with_depth(ctx.depth);

    if let Some(a) = alias {
        node.alias = Some(a);
    }

    // Collect column references from the expression
    let columns = collect_expr_columns(expr);

    let mut child_ctx = ctx.with_depth(ctx.depth + 1);

    for col_ref in columns {
        let child_node = resolve_column_lineage(&col_ref, &mut child_ctx)?;
        node.downstream.push(child_node);
    }

    Ok(node)
}

/// Resolve lineage for a column reference.
fn resolve_column_lineage(
    col: &ColumnReference,
    ctx: &mut LineageContext,
) -> LineageResult<LineageNode> {
    let name = if ctx.config.trim_qualifiers {
        col.name.clone()
    } else {
        col.qualified_name()
    };

    // If table qualifier is provided, look up in that source
    if let Some(ref table) = col.table {
        let normalized_table = normalize_name(table, ctx.config.dialect);

        if let Some(source_info) = ctx.sources.get(&normalized_table).cloned() {
            match source_info.kind {
                SourceKind::Table => {
                    // Base table - this is a leaf node
                    let node = LineageNode::new(name)
                        .with_source(normalized_table)
                        .with_depth(ctx.depth);
                    return Ok(node);
                }
                SourceKind::Cte | SourceKind::DerivedTable => {
                    // Recurse into CTE/derived table (if not already visiting)
                    if !ctx.visiting.contains(&normalized_table) {
                        if let Some(stmt) = source_info.statement {
                            ctx.visiting.insert(normalized_table.clone());
                            let result = build_lineage_for_column(&col.name, &stmt, ctx);
                            ctx.visiting.remove(&normalized_table);
                            return result;
                        }
                    }
                    // If already visiting, treat as leaf
                    let node = LineageNode::new(name)
                        .with_source(normalized_table)
                        .with_depth(ctx.depth);
                    return Ok(node);
                }
                SourceKind::External => {
                    // Check external sources
                    if let Some(stmt) = ctx.external_sources.get(&normalized_table).cloned() {
                        return build_lineage_for_column(&col.name, &stmt, ctx);
                    }
                }
            }
        }
    }

    // No table qualifier - search all sources
    for (source_name, source_info) in ctx.sources.clone() {
        match source_info.kind {
            SourceKind::Table => {
                // Check if this table has the column
                if ctx.schema.has_column(&[&source_name], &col.name) {
                    let node = LineageNode::new(name)
                        .with_source(source_name.clone())
                        .with_depth(ctx.depth);
                    return Ok(node);
                }
            }
            SourceKind::Cte | SourceKind::DerivedTable => {
                // Skip if already visiting this source
                if ctx.visiting.contains(&source_name) {
                    continue;
                }
                // Check if CTE/derived table outputs this column
                if let Some(ref columns) = source_info.columns {
                    if columns.iter().any(|c| select_item_has_column(c, &col.name)) {
                        if let Some(stmt) = source_info.statement {
                            ctx.visiting.insert(source_name.clone());
                            let result = build_lineage_for_column(&col.name, &stmt, ctx);
                            ctx.visiting.remove(&source_name);
                            return result;
                        }
                    }
                }
            }
            SourceKind::External => {}
        }
    }

    // Column not found in any known source - treat as external/unknown
    let node = LineageNode::new(name).with_depth(ctx.depth);
    Ok(node)
}

/// Register a table source in the context.
fn register_table_source(source: &TableSource, ctx: &mut LineageContext) {
    match source {
        TableSource::Table(table_ref) => {
            let key = table_ref.alias.as_ref().unwrap_or(&table_ref.name).clone();
            let normalized = normalize_name(&key, ctx.config.dialect);
            // Don't overwrite CTEs or derived tables
            if !ctx.sources.contains_key(&normalized) {
                ctx.sources.insert(
                    normalized,
                    SourceInfo {
                        kind: SourceKind::Table,
                        columns: None,
                        statement: None,
                    },
                );
            }
        }
        TableSource::Subquery { query, alias, .. } => {
            if let Some(alias) = alias {
                let normalized = normalize_name(alias, ctx.config.dialect);
                ctx.sources.insert(
                    normalized,
                    SourceInfo {
                        kind: SourceKind::DerivedTable,
                        columns: extract_select_columns(query),
                        statement: Some((**query).clone()),
                    },
                );
            }
        }
        TableSource::Lateral { source } => {
            register_table_source(source, ctx);
        }
        TableSource::Pivot { source, alias, .. } | TableSource::Unpivot { source, alias, .. } => {
            register_table_source(source, ctx);
            // TODO: Track pivot/unpivot column transformations
            if let Some(alias) = alias {
                let normalized = normalize_name(alias, ctx.config.dialect);
                ctx.sources.insert(
                    normalized,
                    SourceInfo {
                        kind: SourceKind::DerivedTable,
                        columns: None,
                        statement: None,
                    },
                );
            }
        }
        TableSource::TableFunction { alias, .. } => {
            if let Some(alias) = alias {
                let normalized = normalize_name(alias, ctx.config.dialect);
                ctx.sources.insert(
                    normalized,
                    SourceInfo {
                        kind: SourceKind::Table,
                        columns: None,
                        statement: None,
                    },
                );
            }
        }
        TableSource::Unnest { alias, .. } => {
            if let Some(alias) = alias {
                let normalized = normalize_name(alias, ctx.config.dialect);
                ctx.sources.insert(
                    normalized,
                    SourceInfo {
                        kind: SourceKind::Table,
                        columns: None,
                        statement: None,
                    },
                );
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Helper types and functions
// ═══════════════════════════════════════════════════════════════════════

/// A column reference found in an expression.
#[derive(Debug, Clone)]
struct ColumnReference {
    table: Option<String>,
    name: String,
}

impl ColumnReference {
    fn qualified_name(&self) -> String {
        if let Some(ref table) = self.table {
            format!("{}.{}", table, self.name)
        } else {
            self.name.clone()
        }
    }
}

/// Collect all column references from an expression.
fn collect_expr_columns(expr: &Expr) -> Vec<ColumnReference> {
    let mut columns = Vec::new();

    expr.walk(&mut |e| {
        if let Expr::Column { table, name, .. } = e {
            columns.push(ColumnReference {
                table: table.clone(),
                name: name.clone(),
            });
            return false; // Don't recurse into column nodes
        }
        // Don't descend into subqueries
        !matches!(
            e,
            Expr::Subquery(_) | Expr::Exists { .. } | Expr::InSubquery { .. }
        )
    });

    columns
}

/// Extract SELECT columns from a statement.
fn extract_select_columns(stmt: &Statement) -> Option<Vec<SelectItem>> {
    match stmt {
        Statement::Select(sel) => Some(sel.columns.clone()),
        Statement::SetOperation(set_op) => extract_select_columns(&set_op.left),
        Statement::CreateView(cv) => extract_select_columns(&cv.query),
        _ => None,
    }
}

/// Get the output name of an expression.
fn expr_output_name(expr: &Expr) -> &str {
    match expr {
        Expr::Column { name, .. } => name,
        Expr::Alias { name, .. } => name,
        _ => "",
    }
}

/// Convert an expression to a displayable name.
fn expr_to_name(expr: &Expr, trim_qualifiers: bool) -> String {
    match expr {
        Expr::Column { table, name, .. } => {
            if trim_qualifiers {
                name.clone()
            } else if let Some(t) = table {
                format!("{}.{}", t, name)
            } else {
                name.clone()
            }
        }
        Expr::Alias { name, .. } => name.clone(),
        Expr::Function { name, .. } => format!("{}(...)", name),
        Expr::BinaryOp { op, .. } => format!("({:?})", op),
        Expr::Cast { data_type, .. } => format!("CAST AS {:?}", data_type),
        _ => "expr".to_string(),
    }
}

/// Parse a column reference string into (name, optional_table_qualifier).
fn parse_column_ref(column: &str) -> (String, Option<String>) {
    if let Some(idx) = column.rfind('.') {
        let table = column[..idx].to_string();
        let name = column[idx + 1..].to_string();
        (name, Some(table))
    } else {
        (column.to_string(), None)
    }
}

/// Check if a column name matches (case-insensitive for most dialects).
fn matches_column_name(item: &str, target: &str) -> bool {
    item.eq_ignore_ascii_case(target)
}

/// Normalize an identifier name for the given dialect.
fn normalize_name(name: &str, dialect: Dialect) -> String {
    crate::schema::normalize_identifier(name, dialect)
}

/// Check if a SELECT item outputs a column with the given name.
fn select_item_has_column(item: &SelectItem, name: &str) -> bool {
    match item {
        SelectItem::Expr { expr, alias, .. } => {
            let item_name = alias
                .as_ref()
                .map(String::as_str)
                .unwrap_or_else(|| expr_output_name(expr));
            matches_column_name(item_name, name)
        }
        SelectItem::Wildcard => true, // Could match any column
        SelectItem::QualifiedWildcard { .. } => true,
    }
}

/// Escape a string for DOT format.
fn escape_dot(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
}

/// Escape a string for Mermaid format.
fn escape_mermaid(s: &str) -> String {
    s.replace('"', "'")
        .replace('\n', " ")
        .replace('[', "(")
        .replace(']', ")")
}

// ═══════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::parse;

    fn test_config() -> LineageConfig {
        LineageConfig::new(Dialect::Ansi)
    }

    fn test_schema() -> MappingSchema {
        let mut schema = MappingSchema::new(Dialect::Ansi);
        schema
            .add_table(
                &["t"],
                vec![
                    ("a".to_string(), DataType::Int),
                    ("b".to_string(), DataType::Int),
                    ("c".to_string(), DataType::Int),
                ],
            )
            .unwrap();
        schema
            .add_table(
                &["users"],
                vec![
                    ("id".to_string(), DataType::Int),
                    ("name".to_string(), DataType::Varchar(Some(255))),
                    ("email".to_string(), DataType::Text),
                ],
            )
            .unwrap();
        schema
            .add_table(
                &["orders"],
                vec![
                    ("id".to_string(), DataType::Int),
                    ("user_id".to_string(), DataType::Int),
                    (
                        "amount".to_string(),
                        DataType::Decimal {
                            precision: Some(10),
                            scale: Some(2),
                        },
                    ),
                ],
            )
            .unwrap();
        schema
    }

    #[test]
    fn test_simple_column_lineage() {
        let sql = "SELECT a FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("a", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "a");
        assert_eq!(graph.node.depth, 0);
        // The root column depends on t.a
        assert_eq!(graph.node.downstream.len(), 1);
        assert_eq!(graph.node.downstream[0].source_name, Some("t".to_string()));
    }

    #[test]
    fn test_aliased_column_lineage() {
        let sql = "SELECT a AS col_a FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("col_a", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "col_a");
        assert_eq!(graph.node.alias, Some("col_a".to_string()));
    }

    #[test]
    fn test_expression_lineage() {
        let sql = "SELECT a + b AS sum FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("sum", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "sum");
        // The sum depends on both a and b
        assert_eq!(graph.node.downstream.len(), 2);
    }

    #[test]
    fn test_cte_lineage() {
        let sql = "WITH cte AS (SELECT a FROM t) SELECT a FROM cte";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("a", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "a");
        // Should trace through the CTE
        assert!(graph.source_tables().contains(&"t".to_string()));
    }

    #[test]
    fn test_join_lineage() {
        let sql = "SELECT u.name, o.amount FROM users u JOIN orders o ON u.id = o.user_id";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("name", &ast, &schema, &config).unwrap();
        assert_eq!(graph.node.name, "name");

        let graph2 = lineage("amount", &ast, &schema, &config).unwrap();
        assert_eq!(graph2.node.name, "amount");
    }

    #[test]
    fn test_union_lineage() {
        let sql = "SELECT a FROM t UNION SELECT b AS a FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("a", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "a");
        // Should have two branches
        assert_eq!(graph.node.downstream.len(), 2);
    }

    #[test]
    fn test_column_not_found() {
        let sql = "SELECT a FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let result = lineage("nonexistent", &ast, &schema, &config);
        assert!(matches!(result, Err(LineageError::ColumnNotFound(_))));
    }

    #[test]
    fn test_derived_table_lineage() {
        let sql = "SELECT x FROM (SELECT a AS x FROM t) sub";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("x", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "x");
        // Should trace through the derived table to t.a
        assert!(graph.source_tables().contains(&"t".to_string()));
    }

    #[test]
    fn test_function_lineage() {
        let sql = "SELECT SUM(a) AS total FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("total", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "total");
        assert_eq!(graph.node.downstream.len(), 1);
    }

    #[test]
    fn test_lineage_sql_convenience() {
        let schema = test_schema();
        let config = test_config();

        let graph = lineage_sql("b", "SELECT a, b FROM t", &schema, &config).unwrap();

        assert_eq!(graph.node.name, "b");
        assert_eq!(graph.sql, Some("SELECT a, b FROM t".to_string()));
    }

    #[test]
    fn test_source_tables() {
        let sql = "SELECT u.name, o.amount FROM users u JOIN orders o ON u.id = o.user_id";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("name", &ast, &schema, &config).unwrap();
        let tables = graph.source_tables();

        assert!(tables.contains(&"u".to_string()));
    }

    #[test]
    fn test_to_dot() {
        let sql = "SELECT a AS col FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("col", &ast, &schema, &config).unwrap();
        let dot = graph.to_dot();

        assert!(dot.contains("digraph lineage"));
        assert!(dot.contains("rankdir=BT"));
    }

    #[test]
    fn test_to_mermaid() {
        let sql = "SELECT a AS col FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("col", &ast, &schema, &config).unwrap();
        let mermaid = graph.to_mermaid();

        assert!(mermaid.contains("flowchart BT"));
    }

    #[test]
    fn test_case_expression_lineage() {
        let sql = "SELECT CASE WHEN a > 0 THEN b ELSE c END AS result FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("result", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "result");
        // Should depend on a, b, and c
        assert!(graph.node.downstream.len() >= 2);
    }

    #[test]
    fn test_coalesce_lineage() {
        let sql = "SELECT COALESCE(a, b, c) AS val FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("val", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "val");
        // Should depend on a, b, and c
        assert_eq!(graph.node.downstream.len(), 3);
    }

    #[test]
    fn test_nested_cte_lineage() {
        let sql = r#"
            WITH cte1 AS (SELECT a, b FROM t),
                 cte2 AS (SELECT a + b AS sum FROM cte1)
            SELECT sum FROM cte2
        "#;
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("sum", &ast, &schema, &config).unwrap();

        assert_eq!(graph.node.name, "sum");
        // Should trace through both CTEs to t
        let sources = graph.source_tables();
        assert!(sources.contains(&"t".to_string()));
    }

    #[test]
    fn test_lineage_iterator() {
        let sql = "SELECT a + b AS sum FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = test_config();

        let graph = lineage("sum", &ast, &schema, &config).unwrap();

        let nodes: Vec<_> = graph.node.iter().collect();
        assert!(!nodes.is_empty());
        assert_eq!(nodes[0].name, "sum");
    }

    #[test]
    fn test_external_sources() {
        let schema = test_schema();
        let mut sources = HashMap::new();
        sources.insert("view1".to_string(), "SELECT a FROM t".to_string());

        let config = LineageConfig::new(Dialect::Ansi).with_sources(sources);

        let sql = "SELECT a FROM view1";
        let result = lineage_sql("a", sql, &schema, &config);
        // Should parse and handle external sources
        assert!(result.is_ok() || matches!(result, Err(LineageError::ColumnNotFound(_))));
    }

    #[test]
    fn test_qualified_column() {
        let sql = "SELECT t.a FROM t";
        let ast = parse(sql, Dialect::Ansi).unwrap();
        let schema = test_schema();
        let config = LineageConfig::new(Dialect::Ansi).with_trim_qualifiers(false);

        let graph = lineage("a", &ast, &schema, &config).unwrap();

        // With trim_qualifiers=false, should preserve qualification
        assert!(graph.node.name.contains('a'));
    }
}