sqllens 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +409 -255
- package/dist/api.d.ts +9 -7
- package/dist/api.js +15 -6
- package/dist/bigquery/parse.d.ts +5 -18
- package/dist/bigquery/parse.js +1 -1
- package/dist/completion/complete.d.ts +3 -1
- package/dist/completion/complete.js +4 -2
- package/dist/completion/config.d.ts +1 -1
- package/dist/completion/parser-factory.d.ts +1 -1
- package/dist/databricks/parse.d.ts +4 -17
- package/dist/derived-dialects.d.ts +8 -0
- package/dist/derived-dialects.js +50 -0
- package/dist/dialect-symbols.d.ts +1 -1
- package/dist/dialect.d.ts +3 -0
- package/dist/dialect.js +1 -0
- package/dist/document/document.d.ts +159 -5
- package/dist/document/document.js +473 -18
- package/dist/document/node-at.js +2 -132
- package/dist/document/shift.d.ts +6 -0
- package/dist/document/shift.js +11 -0
- package/dist/document/split.d.ts +1 -1
- package/dist/duckdb/parse.d.ts +4 -16
- package/dist/index.d.ts +12 -7
- package/dist/index.js +15 -10
- package/dist/ir/ir.d.ts +19 -1
- package/dist/ir/part-span.d.ts +11 -0
- package/dist/ir/part-span.js +35 -1
- package/dist/ir/walk.d.ts +9 -0
- package/dist/ir/walk.js +135 -0
- package/dist/lineage/lineage.d.ts +4 -1
- package/dist/lineage/lineage.js +5 -1
- package/dist/minijinja/apply-tags.d.ts +12 -3
- package/dist/minijinja/apply-tags.js +30 -16
- package/dist/minijinja/engine.d.ts +4 -0
- package/dist/minijinja/engine.js +11 -0
- package/dist/minijinja/index.d.ts +8 -0
- package/dist/minijinja/index.js +8 -0
- package/dist/minijinja/parse.d.ts +3 -44
- package/dist/minijinja/parse.js +71 -11
- package/dist/minijinja/tag-ast.js +16 -2
- package/dist/minijinja/variants.d.ts +10 -2
- package/dist/minijinja/variants.js +43 -6
- package/dist/parse-result.d.ts +11 -0
- package/dist/parse-result.js +1 -0
- package/dist/postgres/parse.d.ts +4 -16
- package/dist/qualify/qualify.d.ts +8 -1
- package/dist/qualify/qualify.js +21 -5
- package/dist/qualify/template-provider.d.ts +2 -19
- package/dist/redshift/parse.d.ts +4 -16
- package/dist/references/references.js +2 -0
- package/dist/scope/walk.d.ts +15 -0
- package/dist/scope/walk.js +73 -0
- package/dist/session.d.ts +83 -0
- package/dist/session.js +159 -0
- package/dist/signature/signatures.d.ts +1 -1
- package/dist/snowflake/parse.d.ts +4 -16
- package/dist/symbols/symbols.d.ts +14 -0
- package/dist/symbols/symbols.js +115 -3
- package/dist/template/engine.d.ts +65 -0
- package/dist/template/engine.js +1 -0
- package/dist/token/classify.d.ts +1 -1
- package/dist/token/map.d.ts +1 -1
- package/dist/token/tokenize.d.ts +1 -1
- package/dist/trino/parse.d.ts +4 -15
- package/dist/tsql/parse.d.ts +4 -16
- package/package.json +95 -91
- package/dist/adapters.d.ts +0 -8
- package/dist/adapters.js +0 -43
package/README.md
CHANGED
|
@@ -1,255 +1,409 @@
|
|
|
1
|
-
# sqllens
|
|
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
|
-
a
|
|
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
|
-
|
|
1
|
+
# sqllens
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/sqllens) [](LICENSE)
|
|
4
|
+
|
|
5
|
+
A TypeScript SQL parser and static analyzer. It parses SQL into a tree, lowers it
|
|
6
|
+
to a dialect-neutral intermediate representation (IR), and runs a semantic layer
|
|
7
|
+
over that IR: name resolution (scope), schema-fed qualification, type inference,
|
|
8
|
+
and column lineage. Give it a
|
|
9
|
+
query and it tells you the query's sources, its output columns, their types, and
|
|
10
|
+
where each column comes from. The parsers are generated TypeScript on the
|
|
11
|
+
[antlr4ng](https://github.com/mike-lischke/antlr4ng) runtime.
|
|
12
|
+
|
|
13
|
+
The front end is error-tolerant and token-first, so the library drives editor
|
|
14
|
+
features (completion, hover, diagnostics, go-to-definition) over incomplete,
|
|
15
|
+
mid-edit text. See [Editor / language tooling](#editor--language-tooling). An LSP
|
|
16
|
+
(Language Server Protocol) server built on it lives in the repo, but it is
|
|
17
|
+
experimental and not part of the published package.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install sqllens
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { analyze, Schema } from "sqllens";
|
|
25
|
+
|
|
26
|
+
const schema = new Schema({ orders: { id: "int", total: "decimal" } });
|
|
27
|
+
const q = analyze("SELECT total FROM orders WHERE total > 100", "postgres", { schema });
|
|
28
|
+
|
|
29
|
+
q.diagnostics; // [] — names and types check against the schema
|
|
30
|
+
q.lineage.originsOf("total"); // → orders.total
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Dialects
|
|
34
|
+
|
|
35
|
+
sqllens implements eight SQL dialects directly, each with its own grammar. Seven
|
|
36
|
+
more engines are covered as *derived dialects*: their SQL is already parsed by one
|
|
37
|
+
of those eight grammars, for 15 engines in total.
|
|
38
|
+
|
|
39
|
+
| Dialect | Derived dialects | Parse + lower | Semantic layer | Notes |
|
|
40
|
+
|---|---|---|---|---|
|
|
41
|
+
| Databricks (Spark SQL) | Apache Spark, AWS Glue | yes | yes | grammar forked from apache/spark |
|
|
42
|
+
| T-SQL | SQL Server, Microsoft Fabric, Azure Synapse | yes | yes | grammar forked from grammars-v4 `sql/tsql` |
|
|
43
|
+
| Snowflake | — | yes | yes | grammar forked from grammars-v4 `sql/snowflake` |
|
|
44
|
+
| BigQuery (GoogleSQL) | — | yes | yes | grammar forked from `bytebase/parser` `googlesql/`; gated against ZetaSQL's `.test` corpus |
|
|
45
|
+
| Redshift | — | yes | yes | grammar forked from Bytebase's Postgres-derived Redshift grammar (BSD-3) |
|
|
46
|
+
| PostgreSQL | — | yes | yes | grammar forked from `bytebase/parser` `postgresql/` (BSD-3, PG18 keywords) |
|
|
47
|
+
| DuckDB | — | yes | yes | grammar forked from this repo's own postgres pair (no open ANTLR grammar exists) |
|
|
48
|
+
| Trino | Presto, Amazon Athena | yes | yes | grammar is the first-party trinodb `SqlBase.g4` (release 482), mechanically split |
|
|
49
|
+
|
|
50
|
+
Each grammar began as a fork of the upstream noted above, but most are now far from
|
|
51
|
+
verbatim copies. They've had substantial extension and correction, driven by a full
|
|
52
|
+
comparison against each dialect's official reference documentation and the reference
|
|
53
|
+
corpus extracted from those docs, so they reach well past their fork points.
|
|
54
|
+
|
|
55
|
+
A **derived dialect** is an engine that has no grammar of its own but whose SQL
|
|
56
|
+
the primary grammar already parses, because its SQL is a subset of (or the same as)
|
|
57
|
+
the primary dialect's. Microsoft Fabric runs a restricted subset of T-SQL, Amazon
|
|
58
|
+
Athena's engine is Trino, and AWS Glue runs Spark. Each one is checked against real
|
|
59
|
+
SQL from that engine before it goes on the list.
|
|
60
|
+
|
|
61
|
+
In code, the `dialect` argument is one of `"databricks" | "tsql" | "snowflake" | "bigquery" | "redshift" | "postgres" | "duckdb" | "trino"`. `resolveDialect` turns an
|
|
62
|
+
engine name (or a dialect name) into the one that parses it: `resolveDialect("athena")`
|
|
63
|
+
returns `"trino"`.
|
|
64
|
+
|
|
65
|
+
The semantic layer is dialect-agnostic: it operates on the shared IR and runs
|
|
66
|
+
unchanged on every dialect. Only the parse and lower stages are dialect-specific.
|
|
67
|
+
|
|
68
|
+
## The pipeline
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
parse → lower → resolveScopes → qualify → infer / lineage / symbols
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Each stage produces one value, and that value is what a specific editor feature
|
|
75
|
+
reads from. Only the first two stages, parse and lower, are dialect-specific;
|
|
76
|
+
everything after them is shared and runs unchanged across all eight dialects.
|
|
77
|
+
|
|
78
|
+
**parse** turns SQL text into a *concrete syntax tree* (CST): the full parse tree,
|
|
79
|
+
every token and grammar node exactly as written, nothing dropped or simplified. It
|
|
80
|
+
also hands back the token stream and a syntax-error count. The CST is faithful but
|
|
81
|
+
verbose and dialect-shaped, so nothing downstream reads it directly. It backs
|
|
82
|
+
syntax squiggles (the underline under a parse error) and semantic tokens
|
|
83
|
+
(dialect-aware highlighting).
|
|
84
|
+
|
|
85
|
+
**lower** walks the CST into an *intermediate representation* (IR): a small,
|
|
86
|
+
dialect-neutral tree of nodes such as `QueryExpr`, `SelectExpr`, and `Expr` that
|
|
87
|
+
mean the same thing whether the SQL came from Snowflake or T-SQL. (In the API this
|
|
88
|
+
value is the `ast` field, the *abstract syntax tree*, a cleaned-up counterpart to
|
|
89
|
+
the CST.) It also tags each statement with its kind: a query, DML (data
|
|
90
|
+
manipulation: `INSERT` / `UPDATE` / `DELETE`), or DDL (data definition:
|
|
91
|
+
`CREATE` / `ALTER` / `DROP`). lower never throws, so even half-typed, broken SQL
|
|
92
|
+
still yields an IR the rest of the pipeline can run on.
|
|
93
|
+
|
|
94
|
+
**resolveScopes** builds a symbol table over the IR with no schema required. For
|
|
95
|
+
each query scope it works out the visible sources (tables, subqueries, and CTEs; a
|
|
96
|
+
*common table expression* is the `WITH name AS (…)` temporary result set), resolves
|
|
97
|
+
names against them, and computes the query's output columns. It needs no catalog,
|
|
98
|
+
so the features it powers work on any file with zero configuration: go-to-definition,
|
|
99
|
+
find-references, and document highlight.
|
|
100
|
+
|
|
101
|
+
**qualify** is the first stage that takes a *schema*, the catalog of tables and
|
|
102
|
+
their column types. With it, qualify expands `SELECT *` into the real column list,
|
|
103
|
+
raises unknown-table and unknown-column diagnostics, and binds each column
|
|
104
|
+
reference to the source it comes from, with the column's type. This is what turns
|
|
105
|
+
on the schema-dependent semantic squiggles (an unknown column can only be flagged
|
|
106
|
+
once the schema is known) and answers `bindingOf`, which tells you which source a
|
|
107
|
+
given column resolves to.
|
|
108
|
+
|
|
109
|
+
**infer** computes the type and nullability of every expression, from a bare
|
|
110
|
+
column to `a + b`, `COALESCE(…)`, a `CASE`, or a function call. It powers hover
|
|
111
|
+
(the type shown when you point at an expression) and inlay hints (inline type
|
|
112
|
+
annotations).
|
|
113
|
+
|
|
114
|
+
**lineage** traces each output column back to the base-table columns it derives
|
|
115
|
+
from, through CTEs, subqueries, and joins, and records every hop on the way. It
|
|
116
|
+
powers the lineage panel and go-to-origin (jump from an output column to the
|
|
117
|
+
physical column it ultimately reads).
|
|
118
|
+
|
|
119
|
+
**symbols** derives a `Sym` model: every named thing (source, column, CTE),
|
|
120
|
+
classified by kind and modifier. It backs the editor outline / document-symbols
|
|
121
|
+
list and code-lens annotations.
|
|
122
|
+
|
|
123
|
+
## Usage
|
|
124
|
+
|
|
125
|
+
Two ways in: `analyze` for one-shot analysis of a string, and a session for a
|
|
126
|
+
document you hold open and edit. Templated SQL (dbt models) is the same API with
|
|
127
|
+
one more option; it changes the input, not the shape of what you get back.
|
|
128
|
+
For a step-by-step walkthrough of the templated path, from a raw dbt model to
|
|
129
|
+
branch variants, see [TUTORIAL.md](TUTORIAL.md).
|
|
130
|
+
|
|
131
|
+
### One-shot: `analyze`
|
|
132
|
+
|
|
133
|
+
`analyze` runs the whole pipeline and hands back a result you read directly:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { analyze, Schema } from "sqllens";
|
|
137
|
+
|
|
138
|
+
const schema = new Schema({ t: { a: "int", b: "string" } });
|
|
139
|
+
const a = analyze("SELECT a, b FROM t", "tsql", { schema });
|
|
140
|
+
|
|
141
|
+
a.scopes; // name resolution (ScopeTree)
|
|
142
|
+
a.diagnostics; // unknown-table / column diagnostics
|
|
143
|
+
a.qualification.columnsOf(a.scopes.root); // * expansion
|
|
144
|
+
a.types.typeOf(expr, scope); // per-expression types
|
|
145
|
+
a.lineage.originsOf("a"); // base-table origins of an output column
|
|
146
|
+
a.symbols; // kind × modifier symbol model
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### A document you keep: the session
|
|
150
|
+
|
|
151
|
+
An editor holds a file that changes. The entry for that is a session: it parses on
|
|
152
|
+
construction, caches per statement, and an edit reuses everything it didn't touch.
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { SqlSession, Schema } from "sqllens";
|
|
156
|
+
|
|
157
|
+
const s = SqlSession.create("SELECT amount FROM sales", "databricks", { schema });
|
|
158
|
+
|
|
159
|
+
// properties — cheap reads of what construction already produced
|
|
160
|
+
s.ast; // the dialect-neutral IR (frozen)
|
|
161
|
+
s.tokens; // the token stream: every token, exact spans — present even mid-edit
|
|
162
|
+
s.scopes; // name resolution; needs no schema
|
|
163
|
+
|
|
164
|
+
// verbs — parentheses execute a pass (memoized against the schema's version)
|
|
165
|
+
s.diagnostics(); // syntax + schema-fed, one document-ordered list
|
|
166
|
+
s.lineage(); // column lineage for the output columns
|
|
167
|
+
s.deriveSymbols(); // the outline / symbol model
|
|
168
|
+
|
|
169
|
+
// cursor verbs — offset in, spans out
|
|
170
|
+
s.completeAt(14); // completions at an offset (works on broken, mid-keystroke text)
|
|
171
|
+
s.referencesAt(9); // declaration + every occurrence of the symbol under the cursor
|
|
172
|
+
s.typeAt(9); // inferred type of the expression under the cursor
|
|
173
|
+
|
|
174
|
+
// edits are immutable: a new session, caches carried over
|
|
175
|
+
const next = s.withText("SELECT amount, id FROM sales");
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The convention throughout: properties are cheap, parentheses do work. Every verb
|
|
179
|
+
is a one-line delegation to a free function (`qualify`, `lineage`, `deriveSymbols`,
|
|
180
|
+
`referencesAt`, …) that stays exported, so a slim consumer can skip the session,
|
|
181
|
+
import only the functions it calls, and bundle nothing else.
|
|
182
|
+
|
|
183
|
+
### Stage-wise building blocks
|
|
184
|
+
|
|
185
|
+
Every stage is also its own entry point, and each result is a value you can stop
|
|
186
|
+
at or pass to the next; hand a result forward and only the missing steps run:
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import { parse, qualify, lineage, deriveSymbols, toScopes, Schema } from "sqllens";
|
|
190
|
+
|
|
191
|
+
const { ast, errors, cst } = parse("SELECT a, b FROM t", "snowflake");
|
|
192
|
+
// ast = dialect-neutral IR (frozen); cst = the raw antlr tree (escape hatch)
|
|
193
|
+
|
|
194
|
+
const scopes = toScopes(ast, { dialect: "snowflake" }); // idempotent lift
|
|
195
|
+
qualify(scopes, schema); // reuses scopes — never re-parses or re-resolves
|
|
196
|
+
lineage(scopes, schema); // safe on the same scopes, in any order
|
|
197
|
+
deriveSymbols(scopes); // independent results
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
The per-dialect entries (`parseDatabricks` … `parseTrino`, each `lower`, and the
|
|
201
|
+
raw `resolveScopes` / `inferType`) stay exported for callers that want a single
|
|
202
|
+
stage.
|
|
203
|
+
|
|
204
|
+
### Templated SQL (dbt models)
|
|
205
|
+
|
|
206
|
+
A dbt model is not plain SQL; it is minijinja-templated SQL (`{{ ref('orders') }}`,
|
|
207
|
+
`{% if %}` …). sqllens parses that raw text natively, without rendering: tags get
|
|
208
|
+
exact spans, a `ref` in a FROM slot becomes a real table source that carries its
|
|
209
|
+
model name, and everything downstream (scopes, diagnostics, types, lineage,
|
|
210
|
+
completion) runs on the templated document unchanged.
|
|
211
|
+
|
|
212
|
+
Templating is declared, never guessed. You hand the session a template engine; no
|
|
213
|
+
engine means plain SQL:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { SqlSession } from "sqllens";
|
|
217
|
+
import { minijinja } from "sqllens/minijinja"; // its own entry point — plain-SQL consumers never load it
|
|
218
|
+
|
|
219
|
+
const s = SqlSession.create(modelText, "databricks", {
|
|
220
|
+
templating: minijinja(),
|
|
221
|
+
provider, // optional — template knowledge, see extension points below
|
|
222
|
+
schema,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
s.ast; // IR: {{ ref('orders') }} in FROM is a TableSource named "orders"
|
|
226
|
+
s.tokens; // ONE stream: SQL tokens + template tokens (channel 2, role "minijinja")
|
|
227
|
+
s.diagnostics(); // SQL + template + schema-fed, merged, all in document coordinates
|
|
228
|
+
s.tags; // every tag with exact spans (ref / source / macro / var / control)
|
|
229
|
+
s.regions; // {% if %} / {% for %} structure — folding, branch enumeration
|
|
230
|
+
s.tagOf(node); // the tag an IR node came from; nodeOf(tag) goes the other way
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Why no auto-detection: `{{ … }}` inside a SQL string literal is a template to dbt
|
|
234
|
+
and literal text to everyone else. No scanner can tell which was meant, and sqllens
|
|
235
|
+
never guesses. The host declares it (file association, language id, or config).
|
|
236
|
+
Declaring an engine on a file that turns out to have no tags costs nothing and
|
|
237
|
+
changes nothing: the result is byte-identical to a plain parse, with empty template
|
|
238
|
+
facets.
|
|
239
|
+
|
|
240
|
+
Everything works with no provider at all: the shipped defaults answer what they can
|
|
241
|
+
(a `ref` is a relation named by its literal argument; `config` renders nothing) and
|
|
242
|
+
everything else reports as unknown, never guessed. A provider only makes results
|
|
243
|
+
more precise.
|
|
244
|
+
|
|
245
|
+
### Extension points
|
|
246
|
+
|
|
247
|
+
sqllens knows SQL and template *syntax*. Everything it cannot know (your catalog,
|
|
248
|
+
what your macros expand to, what `var('x')` holds) enters through three interfaces.
|
|
249
|
+
All are optional, and every one answers misses the same way: a miss is "unknown",
|
|
250
|
+
never a guess, and no diagnostic fires on missing knowledge.
|
|
251
|
+
|
|
252
|
+
`SchemaProvider` is the catalog: which tables exist, with their column types.
|
|
253
|
+
`Schema` is the upfront form (a plain mapping, as in the examples above).
|
|
254
|
+
`CallbackSchema` is the lazy form for hosts with a live catalog: sqllens records
|
|
255
|
+
what it missed, your `prime()` resolves the misses asynchronously and bumps a
|
|
256
|
+
version, and the next read reflects it. An LSP republishes diagnostics on exactly
|
|
257
|
+
that signal.
|
|
258
|
+
|
|
259
|
+
`TemplateProvider` is template knowledge: what template calls *mean*. Subclass
|
|
260
|
+
`DefaultTemplateProvider` and override only what your host knows; each method
|
|
261
|
+
answers one question:
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
class MyDbtProvider extends DefaultTemplateProvider {
|
|
265
|
+
relationOf(call) { /* ref/source → the physical relation, with columns */ }
|
|
266
|
+
valueOf(call) { /* var/env_var → the scalar type it yields */ }
|
|
267
|
+
shapeOf(call) { /* a macro's expansion shape: "expr" | "predicate" | "column-list" | "statement" … */ }
|
|
268
|
+
columnsOf(call) { /* a column-list macro's output columns */ }
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
One instance per document. Answers are synchronous, from a warm cache, with the
|
|
273
|
+
same miss-recording + `prime()` + version protocol as the schema. The base class
|
|
274
|
+
alone is fully functional; it is what the zero-provider examples above run on. The
|
|
275
|
+
payoff of each override is direct: `relationOf` turns "`{{ ref('orders') }}` is
|
|
276
|
+
exempt from checks" into "`orders` has these columns, and `o.totall` is a real
|
|
277
|
+
unknown-column diagnostic"; `shapeOf` makes a macro standing in a statement slot
|
|
278
|
+
parse cleanly; `valueOf` gives `{{ var('limit') }}` a type that inference can use.
|
|
279
|
+
|
|
280
|
+
`TemplateEngine` is template syntax, and is rare. The engine owns how templated
|
|
281
|
+
text is parsed; minijinja ships as the only implementation and nearly every
|
|
282
|
+
consumer just passes it. Implementing your own (another template language over SQL)
|
|
283
|
+
is supported, but it is a contract, not a callback: your result must satisfy the
|
|
284
|
+
invariants the conformance gates check. Tokens tile the source byte-for-byte, every
|
|
285
|
+
span is in original document coordinates, broken input never throws, and tag-free
|
|
286
|
+
text is identical to a plain parse.
|
|
287
|
+
|
|
288
|
+
## Editor / language tooling
|
|
289
|
+
|
|
290
|
+
The front end is error-tolerant and token-first, so it serves editor features
|
|
291
|
+
that run on incomplete, mid-edit text. They never need a clean parse:
|
|
292
|
+
|
|
293
|
+
- `tokenize(sql, dialect)` and `parse(...).tokens` give a first-class token
|
|
294
|
+
stream: every token with its exact span, role, and channel. Available even when
|
|
295
|
+
the parse has errors.
|
|
296
|
+
- `lower()` never throws on broken or partial input; you get a flagged `query` IR
|
|
297
|
+
back, so every downstream pass stays total.
|
|
298
|
+
- `SqlDocument` is a persistent, immutable, position-addressable per-file model.
|
|
299
|
+
It runs `parse → resolveScopes` once (plus lazy `analyze(schema)`), caches the
|
|
300
|
+
result, and answers `tokenAt` / `nodeAt`. An edit yields a new document; an
|
|
301
|
+
O(log n) `LineIndex` maps positions to offsets.
|
|
302
|
+
- `completeAt(doc, offset, schema?)`: scope-aware completion (keywords, columns,
|
|
303
|
+
tables, functions) from an ATN (Augmented Transition Network, the grammar's
|
|
304
|
+
state-machine form) candidate walk over the grammar, our own, with no third-party
|
|
305
|
+
dependency.
|
|
306
|
+
- `signatureAt(doc, offset)`: parameter hints from a curated per-dialect
|
|
307
|
+
function-signature table; the long tail degrades to name + active-argument.
|
|
308
|
+
- `referencesAt(scopes, offset, schema?)`: every occurrence (plus the declaration)
|
|
309
|
+
of the symbol under the cursor; backs find-references, document highlight, and
|
|
310
|
+
code-lens reference counts.
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
import { SqlDocument, Schema } from "sqllens";
|
|
314
|
+
|
|
315
|
+
const doc = SqlDocument.create("SELECT amount FROM sales", "databricks");
|
|
316
|
+
doc.tokens; // first-class token stream (spans + roles)
|
|
317
|
+
doc.tokenAt(7); // token under an offset
|
|
318
|
+
const next = doc.withText("SELECT amount, id FROM sales", 2); // immutable edit → new doc
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## Language server (experimental)
|
|
322
|
+
|
|
323
|
+
An LSP (Language Server Protocol) server built on the library lives in `src/lsp/`.
|
|
324
|
+
It is experimental and **not part of the published npm package**: the package ships
|
|
325
|
+
the library only, and the server is source you run from the repo. It holds one
|
|
326
|
+
`SqlDocument` per open file (rebuilt on edit) and reaches the library only through
|
|
327
|
+
the public API, and adds no analysis of its own beyond protocol translation.
|
|
328
|
+
|
|
329
|
+
A SQL server needs only a subset of LSP's ~30 request types: some don't apply to
|
|
330
|
+
SQL (type hierarchy, document color, monikers), and a few are deferred (formatting,
|
|
331
|
+
project-wide navigation). Where the server stands today, feature by feature:
|
|
332
|
+
|
|
333
|
+
### Language features
|
|
334
|
+
|
|
335
|
+
| Feature | Status |
|
|
336
|
+
| --- | --- |
|
|
337
|
+
| Completion (+ resolve) | ✅ |
|
|
338
|
+
| Hover | ✅ |
|
|
339
|
+
| Hover — nullability | ✅ (` — not null` / ` — nullable` suffix when provable) |
|
|
340
|
+
| Signature help | ✅ |
|
|
341
|
+
| Go to definition | ✅ |
|
|
342
|
+
| Find references | ✅ |
|
|
343
|
+
| Document highlight | ✅ |
|
|
344
|
+
| Document symbols | ✅ |
|
|
345
|
+
| Folding range | ✅ |
|
|
346
|
+
| Selection range | ✅ |
|
|
347
|
+
| Semantic tokens (full / range / delta) | ✅ all three |
|
|
348
|
+
| Inlay hints | ✅ (no resolve) |
|
|
349
|
+
| Code lens | ✅ (no resolve) |
|
|
350
|
+
| Go to declaration | ◻️ not yet |
|
|
351
|
+
| Go to type definition | ◻️ not yet |
|
|
352
|
+
| Go to implementation | ◻️ not yet — name → its defining query (view / model); needs the project model |
|
|
353
|
+
| Call hierarchy | ◻️ not yet — the CTE / view / model dependency graph |
|
|
354
|
+
| Document link | ◻️ not yet |
|
|
355
|
+
| Linked editing range | ◻️ not yet — live alias / name sync-edit |
|
|
356
|
+
| Code action (quick fixes) | ◻️ next phase |
|
|
357
|
+
| Rename (+ prepare) | ◻️ next phase |
|
|
358
|
+
| Formatting / range / on-type | ◻️ deferred (external formatter) |
|
|
359
|
+
| Inline values | ◻️ debugger surface |
|
|
360
|
+
| Type hierarchy | — n/a — SQL has no type-inheritance relation |
|
|
361
|
+
| Document color | — n/a — no color literals |
|
|
362
|
+
| Moniker | — n/a — LSIF / cross-repo indexing concern |
|
|
363
|
+
|
|
364
|
+
### Diagnostics & document sync
|
|
365
|
+
|
|
366
|
+
| Feature | Status |
|
|
367
|
+
| --- | --- |
|
|
368
|
+
| Diagnostics — push (`publishDiagnostics`) | ✅ |
|
|
369
|
+
| Diagnostics — call signature (arity / argument type) | ✅ (curated tables; never-wrong, per-dialect coercion) |
|
|
370
|
+
| Diagnostics — pull (document) | ✅ |
|
|
371
|
+
| Diagnostics — pull (workspace) | ◻️ not yet |
|
|
372
|
+
| Text sync — open / change / close | ✅ (full-document) |
|
|
373
|
+
| Incremental sync | ◻️ full-document only (fine at SQL file sizes) |
|
|
374
|
+
| Save notifications (`didSave` / `willSave`) | ◻️ not yet |
|
|
375
|
+
| Notebook document sync | ◻️ not yet |
|
|
376
|
+
|
|
377
|
+
### Workspace features
|
|
378
|
+
|
|
379
|
+
| Feature | Status |
|
|
380
|
+
| --- | --- |
|
|
381
|
+
| Workspace symbols | ◻️ needs a project / multi-file model |
|
|
382
|
+
| Execute command | ◻️ not yet |
|
|
383
|
+
| Configuration / watched-files | ◻️ not yet (protocol config; file-based `.sqllens.json` config exists) |
|
|
384
|
+
| File operations (create / rename / delete) | ◻️ not yet |
|
|
385
|
+
|
|
386
|
+
Legend: ✅ implemented · ◻️ not yet / deferred · — not applicable to SQL. The
|
|
387
|
+
deferred items are tracked work: rename and
|
|
388
|
+
code actions are the next LSP phase, workspace symbols need the project model,
|
|
389
|
+
and formatting is expected to wrap an existing external formatter.
|
|
390
|
+
|
|
391
|
+
## Architecture
|
|
392
|
+
|
|
393
|
+
One folder per dialect; no shared "core" grammar and no grammar inheritance. Each
|
|
394
|
+
dialect is a standalone pair of split `.g4` files (a lexer grammar + a parser
|
|
395
|
+
grammar), forked from its best starting point and edited in place. Everything
|
|
396
|
+
downstream of `lower` is shared and dialect-neutral.
|
|
397
|
+
|
|
398
|
+
## Building from source & contributing
|
|
399
|
+
|
|
400
|
+
`npm install sqllens` needs no build step on your side. To build it yourself from
|
|
401
|
+
source, or to work on a grammar, you regenerate the parsers from the `.g4` files
|
|
402
|
+
first. [CONTRIBUTING.md](CONTRIBUTING.md) has the setup, the command list, and the
|
|
403
|
+
corpus-gate workflow.
|
|
404
|
+
|
|
405
|
+
## License
|
|
406
|
+
|
|
407
|
+
MIT. See [LICENSE](LICENSE). The forked grammars under `grammars/` keep their
|
|
408
|
+
upstream licenses (Apache-2.0 for Databricks; MIT for T-SQL and Snowflake; BSD-3
|
|
409
|
+
for BigQuery and Redshift); see [THIRD-PARTY-NOTICES.md](THIRD-PARTY-NOTICES.md).
|