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
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ updates:
directory: /
schedule:
interval: weekly

- package-ecosystem: cargo
directory: /
schedule:
interval: weekly
94 changes: 94 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: CI

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

env:
CARGO_TERM_COLOR: always

jobs:
fmt:
name: rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt

- run: cargo fmt --all --check

clippy:
name: clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

# sqllineage-python links against libpython at build time.
- uses: actions/setup-python@v7
with:
python-version: "3.12"

- uses: dtolnay/rust-toolchain@stable
with:
components: clippy

- uses: Swatinem/rust-cache@v2

- run: cargo clippy --workspace --all-targets --all-features -- -D warnings

test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

- uses: actions/setup-python@v7
with:
python-version: "3.12"

- uses: dtolnay/rust-toolchain@stable

- uses: Swatinem/rust-cache@v2

- run: cargo test --workspace --all-features

python:
name: Python smoke test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

- uses: actions/setup-python@v7
with:
python-version: "3.12"

- uses: PyO3/maturin-action@v1
with:
maturin-version: "v1.9.4"
args: >-
--out dist
--manifest-path sqllineage-python/Cargo.toml

# Mirrors the smoke test in release.yml so a broken binding fails on the
# pull request instead of on the release tag.
- name: Smoke test
run: |
pip install dist/*.whl
python -c "
import sqllineage
results = sqllineage.analyze('SELECT a FROM t')
assert len(results) == 1
assert len(results[0].tables.inputs) > 0
print('OK:', results[0])
"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# sqllineage

[![CI](https://github.com/funcpp/sqllineage/actions/workflows/ci.yml/badge.svg)](https://github.com/funcpp/sqllineage/actions/workflows/ci.yml)

Extract table-level and column-level data lineage from SQL statements.

`sqllineage` parses SQL (via [sqlparser](https://crates.io/crates/sqlparser)) and produces a structured lineage result showing which tables are read/written and which source columns each output column derives from.
Expand Down
72 changes: 59 additions & 13 deletions sqllineage/src/build/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ impl LineageBuilder {
vec![node]
}

Expr::Value(_) | Expr::TypedString { .. } | Expr::Wildcard(..) | Expr::QualifiedWildcard(..) => vec![],
Expr::Value(_)
| Expr::TypedString { .. }
| Expr::Wildcard(..)
| Expr::QualifiedWildcard(..) => vec![],

Expr::Cast { expr, .. }
| Expr::Nested(expr)
Expand All @@ -49,7 +52,12 @@ impl LineageBuilder {

Expr::Extract { expr, .. } => self.collect_ancestors(expr),

Expr::Trim { expr, trim_what, trim_characters, .. } => {
Expr::Trim {
expr,
trim_what,
trim_characters,
..
} => {
let mut v = self.collect_ancestors(expr);
if let Some(what) = trim_what {
v.extend(self.collect_ancestors(what));
Expand All @@ -62,7 +70,12 @@ impl LineageBuilder {
v
}

Expr::Substring { expr, substring_from, substring_for, .. } => {
Expr::Substring {
expr,
substring_from,
substring_for,
..
} => {
let mut v = self.collect_ancestors(expr);
if let Some(from) = substring_from {
v.extend(self.collect_ancestors(from));
Expand All @@ -73,7 +86,13 @@ impl LineageBuilder {
v
}

Expr::Overlay { expr, overlay_what, overlay_from, overlay_for, .. } => {
Expr::Overlay {
expr,
overlay_what,
overlay_from,
overlay_for,
..
} => {
let mut v = self.collect_ancestors(expr);
v.extend(self.collect_ancestors(overlay_what));
v.extend(self.collect_ancestors(overlay_from));
Expand All @@ -89,17 +108,36 @@ impl LineageBuilder {
v
}

Expr::AtTimeZone { timestamp, time_zone } => {
Expr::AtTimeZone {
timestamp,
time_zone,
} => {
let mut v = self.collect_ancestors(timestamp);
v.extend(self.collect_ancestors(time_zone));
v
}

Expr::BinaryOp { left, right, .. }
| Expr::Like { expr: left, pattern: right, .. }
| Expr::ILike { expr: left, pattern: right, .. }
| Expr::SimilarTo { expr: left, pattern: right, .. }
| Expr::RLike { expr: left, pattern: right, .. }
| Expr::Like {
expr: left,
pattern: right,
..
}
| Expr::ILike {
expr: left,
pattern: right,
..
}
| Expr::SimilarTo {
expr: left,
pattern: right,
..
}
| Expr::RLike {
expr: left,
pattern: right,
..
}
| Expr::IsDistinctFrom(left, right)
| Expr::IsNotDistinctFrom(left, right) => {
let mut v = self.collect_ancestors(left);
Expand All @@ -113,7 +151,9 @@ impl LineageBuilder {
v
}

Expr::InUnnest { expr, array_expr, .. } => {
Expr::InUnnest {
expr, array_expr, ..
} => {
let mut v = self.collect_ancestors(expr);
v.extend(self.collect_ancestors(array_expr));
v
Expand Down Expand Up @@ -192,7 +232,12 @@ impl LineageBuilder {
ancestors
}

Expr::Case { operand, conditions, else_result, .. } => {
Expr::Case {
operand,
conditions,
else_result,
..
} => {
let mut v = Vec::new();
if let Some(op) = operand {
v.extend(self.collect_ancestors(op));
Expand Down Expand Up @@ -229,7 +274,9 @@ impl LineageBuilder {
vec![]
}

Expr::Between { expr, low, high, .. } => {
Expr::Between {
expr, low, high, ..
} => {
let mut v = self.collect_ancestors(expr);
v.extend(self.collect_ancestors(low));
v.extend(self.collect_ancestors(high));
Expand All @@ -251,7 +298,6 @@ impl LineageBuilder {
| Expr::Interval(_)
| Expr::Lambda(_)
| Expr::MatchAgainst { .. } => vec![],

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sqllineage/src/build/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl LineageBuilder {
| Statement::UNCache { .. }
| Statement::UNLISTEN { .. }
| Statement::Unload { .. }
| Statement::UnlockTables { .. }
| Statement::UnlockTables
| Statement::Use(_)
| Statement::Vacuum { .. }
| Statement::WaitFor { .. }
Expand Down
78 changes: 68 additions & 10 deletions sqllineage/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ pub(crate) fn resolve(

if has_back {
mappings.push(ColumnMapping {
target: ColumnRef { table: output_table.clone(), column: name.clone() },
sources: vec![ColumnOrigin::Recursive { base_sources: sources }],
target: ColumnRef {
table: output_table.clone(),
column: name.clone(),
},
sources: vec![ColumnOrigin::Recursive {
base_sources: sources,
}],
transform,
});
} else {
mappings.push(ColumnMapping {
target: ColumnRef { table: output_table.clone(), column: name.clone() },
target: ColumnRef {
table: output_table.clone(),
column: name.clone(),
},
sources,
transform,
});
Expand Down Expand Up @@ -105,7 +113,12 @@ pub(crate) fn resolve(
_ => None,
})
.collect();
mappings.sort_by_key(|m| name_order.get(&m.target.column).copied().unwrap_or(usize::MAX));
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 Expand Up @@ -156,7 +169,15 @@ fn expand_star(
if let Some(t) = table {
let binding = graph.scopes.lookup(scope, &t.table).cloned();
if let Some(Binding::Cte(s) | Binding::DerivedTable(s)) = binding {
expand_scope_columns(s, graph, resolved, incoming, output_table, mappings, visited_scopes);
expand_scope_columns(
s,
graph,
resolved,
incoming,
output_table,
mappings,
visited_scopes,
);
} else {
mappings.push(wildcard_mapping(output_table, t.clone()));
}
Expand All @@ -165,12 +186,28 @@ fn expand_star(
match binding {
Binding::Table(tref) => mappings.push(wildcard_mapping(output_table, tref)),
Binding::Cte(s) | Binding::DerivedTable(s) => {
expand_scope_columns(s, graph, resolved, incoming, output_table, mappings, visited_scopes);
expand_scope_columns(
s,
graph,
resolved,
incoming,
output_table,
mappings,
visited_scopes,
);
}
}
}
for &child in graph.scopes.anonymous_derived(scope) {
expand_scope_columns(child, graph, resolved, incoming, output_table, mappings, visited_scopes);
expand_scope_columns(
child,
graph,
resolved,
incoming,
output_table,
mappings,
visited_scopes,
);
}
}
}
Expand All @@ -190,7 +227,16 @@ fn expand_scope_columns(
}
for col in graph.scopes.output_columns(scope_id) {
if let RawNode::Star { table, scope } = &graph.nodes[col.node_id] {
expand_star(table.as_ref(), *scope, graph, resolved, incoming, output_table, mappings, visited_scopes);
expand_star(
table.as_ref(),
*scope,
graph,
resolved,
incoming,
output_table,
mappings,
visited_scopes,
);
} else {
let mut visited = HashSet::new();
let (sources, edge_kinds, _) =
Expand Down Expand Up @@ -374,7 +420,14 @@ fn resolve_unqualified(
incoming: &[Vec<usize>],
visited: &mut HashSet<NodeId>,
) -> Option<ColumnOrigin> {
resolve_from_bindings(name, &effective_bindings(scope, graph), graph, resolved, incoming, visited)
resolve_from_bindings(
name,
&effective_bindings(scope, graph),
graph,
resolved,
incoming,
visited,
)
}

fn resolve_from_bindings(
Expand Down Expand Up @@ -406,7 +459,12 @@ fn resolve_from_bindings(
for (_, binding) in bindings {
match binding {
Binding::Cte(s) | Binding::DerivedTable(s) => {
if graph.scopes.output_columns(*s).iter().any(|c| c.name == name) {
if graph
.scopes
.output_columns(*s)
.iter()
.any(|c| c.name == name)
{
return resolve_through_scope(name, *s, graph, resolved, incoming, visited);
}
}
Expand Down
Loading