Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions sqllineage/src/resolve/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod catalog;
mod topo;

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

use crate::graph::RawGraph;
use crate::graph::edge::EdgeKind;
Expand Down Expand Up @@ -53,12 +53,11 @@ pub(crate) fn resolve(

let root = ScopeTree::root();
let ordered_cols = graph.scopes.output_columns(root).to_vec();
let final_ids: HashSet<NodeId> = ordered_cols.iter().map(|c| c.node_id).collect();

let output_table = graph.tables.output.clone();
let mut mappings = Vec::new();

for &node_id in &final_ids {
for col in &ordered_cols {
let node_id = col.node_id;
match &graph.nodes[node_id] {
RawNode::Output { name, .. } => {
let mut visited = HashSet::new();
Expand Down Expand Up @@ -104,22 +103,6 @@ pub(crate) fn resolve(
}
}

let name_order: HashMap<String, usize> = ordered_cols
.iter()
.enumerate()
.filter_map(|(i, c)| match &graph.nodes[c.node_id] {
RawNode::Output { name, .. } => Some((name.clone(), i)),
RawNode::Star { .. } => Some(("*".to_string(), i)),
_ => None,
})
.collect();
mappings.sort_by_key(|m| {
name_order
.get(&m.target.column)
.copied()
.unwrap_or(usize::MAX)
});

if let Some(cat) = catalog {
catalog::apply_catalog(&mut mappings, cat);
}
Expand Down
19 changes: 19 additions & 0 deletions sqllineage/tests/column_lineage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ fn select_multiple_tables_qualified() {
assert_eq!(concrete_sources(m_b), vec![("t2".into(), "b".into())]);
}

#[test]
fn duplicate_output_names_preserve_projection_order() {
let result = analyze_one("SELECT a.id, b.id FROM a JOIN b ON a.id = b.bid");
let sources: Vec<_> = result
.columns
.mappings
.iter()
.map(concrete_sources)
.collect();

assert_eq!(
sources,
vec![
vec![("a".into(), "id".into())],
vec![("b".into(), "id".into())]
]
);
}

#[test]
fn select_case_expression() {
let result = analyze_one("SELECT CASE WHEN a > 0 THEN b ELSE c END AS d FROM t");
Expand Down
Loading