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
//! Extracts the column-level operations a SQL statement performs.
//!
//! The column-granularity counterpart to
//! [`extract_table_operations`](crate::extractor::extract_table_operations):
//! the same three surfaces — `reads`, `writes`, `lineage` — as
//! [`ColumnOperation`], each lineage edge tagged
//! [`Passthrough` or `Transformation`](ColumnLineageKind). Per-surface
//! detail (which constructs fill each) lives on `ColumnOperation`'s fields.
//!
//! Two cross-cutting behaviours, stated once here:
//!
//! - **Value vs filter is structural.** A column that contributes a value
//! is a `lineage` source; one that only influences the result (e.g. a
//! `WHERE` predicate) is in `reads` but not `lineage`.
//! - **Strictness scales with the catalog.** Catalog-free, a `Table`
//! binding's schema is `Unknown` and an unqualified ref to a single-table
//! scope resolves unconditionally (best-effort). With a catalog it's
//! `Cataloged`, so an unqualified ref resolves only when that schema
//! lists the column — a typo that would silently resolve becomes
//! `Unresolved`. Synthetic-origin refs (CTE / derived / table function)
//! drop from `reads`; only real-table or unresolved names surface.
//! - **Table-only statements have no column surface.** A statement that
//! names a relation but no columns — `DROP` / `TRUNCATE`, an unconditional
//! `DELETE`, an `INSERT … DEFAULT VALUES` — yields empty `reads` / `writes`
//! / `lineage` here: there are no columns to enumerate (wildcards stay
//! unexpanded). The target relation is a *table*-level fact; read it from
//! [`extract_table_operations`](crate::extractor::extract_table_operations)
//! (its `writes`).
use crateIdentifierStyle;
use crateCatalog;
use crate;
use crateError;
use crate;
use crate;
use ;
use Dialect;
/// Convenience function to extract column-level operations from SQL using
/// the dialect defaults (no catalog, dialect-derived casing). For a
/// catalog or a casing override, use
/// [`extract_column_operations_with_options`]; with a catalog, table
/// references are matched against it (right-anchored, dialect-cased) and
/// column resolution turns strict.
///
/// ## Example
///
/// ```rust
/// use sql_insight::sqlparser::dialect::GenericDialect;
/// use sql_insight::ResolutionKind;
/// use sql_insight::extractor::{
/// extract_column_operations, ColumnLineageKind, ColumnTarget, StatementKind,
/// };
///
/// let dialect = GenericDialect {};
/// let result =
/// extract_column_operations(&dialect, "SELECT a FROM t1").unwrap();
/// let ops = result[0].as_ref().unwrap();
///
/// // SELECT contributes reads + lineage but no writes.
/// assert_eq!(ops.statement_kind, StatementKind::Select);
/// assert!(ops.writes.is_empty());
///
/// // `t1.a` surfaces as a single read, walk-time resolved to t1.
/// // Catalog-less mode → resolution is `Inferred` (we adopted the
/// // sole `Unknown`-schema candidate without firm evidence).
/// assert_eq!(ops.reads.len(), 1);
/// let read = &ops.reads[0];
/// assert_eq!(read.reference.name.value, "a");
/// assert_eq!(read.reference.table.as_ref().unwrap().name.value, "t1");
/// assert_eq!(read.resolution, ResolutionKind::Inferred);
///
/// // The projection emits one lineage edge into the SELECT's QueryOutput slot,
/// // marked Passthrough (no expression wrapping the column).
/// assert_eq!(ops.lineage.len(), 1);
/// let edge = &ops.lineage[0];
/// assert_eq!(edge.kind, ColumnLineageKind::Passthrough);
/// match &edge.target {
/// ColumnTarget::QueryOutput { name, position } => {
/// assert_eq!(name.as_ref().unwrap().value, "a");
/// assert_eq!(*position, 0);
/// }
/// other => panic!("expected QueryOutput, got {other:?}"),
/// }
/// ```
/// Like [`extract_column_operations`] but with [`ExtractorOptions`] — a
/// catalog and/or an identifier-casing override. `dialect` still drives
/// parsing; the options govern only the analysis.
/// Column-level operations performed by a single SQL statement.
///
/// Mirrors [`TableOperation`](crate::extractor::TableOperation)
/// with the same three surfaces — `reads`, `writes`, `lineage` — at
/// column granularity.
/// A column-level lineage edge: data from `source` contributes to
/// `target`. Emitted for both relation-target statements (INSERT /
/// UPDATE / MERGE / CTAS / CREATE VIEW, target = `ColumnTarget::Relation`)
/// and bare SELECT (target = `ColumnTarget::QueryOutput`).
///
/// One edge per (source, target) pair: `SELECT a + b FROM t1` emits two
/// edges, from `t1.a` and `t1.b` to the same query-output target, each
/// tagged `Transformation`.
///
/// Statements that physically move data emit collapsed end-to-end lineage
/// — `INSERT INTO t1 (col) SELECT b FROM t2` emits `t2.b → t1.col`
/// directly, with no intermediate query-output entry.
/// The target endpoint of a [`ColumnLineageEdge`] — a column in a named
/// relation ([`Relation`](Self::Relation)) or a transient SELECT output
/// ([`QueryOutput`](Self::QueryOutput)).
/// How a source column contributes to its target — the one clean,
/// exclusive distinction: is the value forwarded unchanged, or derived?
///
/// Finer sub-classification of `Transformation` (aggregate vs scalar,
/// cardinality, etc.) is deliberately not modelled — it is lossy for edge
/// cases (window aggregates, value-preserving `STRING_AGG`) and not
/// load-bearing for the core dependency / impact-analysis use case. A finer
/// variant can be added later if a concrete consumer needs it (a breaking
/// change while the crate is pre-1.0).
/// Struct-style entry point. Equivalent to the free
/// [`extract_column_operations`] function.
;