dsl-completion 2.0.0

Context-aware SQL completion for duck-sqllsp.
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
//! Text-level fallback scope builder.
//!
//! Used when the parser cannot make sense of the surrounding statement
//! (which happens constantly while the user is typing, e.g. `SELECT u.`).
//! We scan the raw source for FROM / JOIN clauses with regex-style passes
//! and produce a [`Scope`] mapping aliases to table names. The result is
//! "best effort" -- nested subqueries and CTEs are not fully handled --
//! but enough to recover useful completion in the half-written state.

use dsl_parse::TableRef;
use dsl_resolve::{Binding, Scope};
use text_size::TextRange;

/// Yield the CTE names declared by a `WITH [RECURSIVE] name [(cols)] AS
/// (body), name2 AS (body2) ...` prefix on `src`. Pure text-level scan
/// so it still works when the parser bails on an unfinished trailing
/// statement (the common case while the user is typing the outer FROM).
pub fn cte_names_from_text(src: &str) -> Vec<String> {
  let bytes = src.as_bytes();
  let n = bytes.len();
  let mut out = Vec::new();
  let mut i = 0;
  // Skip whitespace / line+block comments to find the leading WITH.
  loop {
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    if i + 1 < n && bytes[i] == b'-' && bytes[i + 1] == b'-' {
      while i < n && bytes[i] != b'\n' {
        i += 1;
      }
      continue;
    }
    if i + 1 < n && bytes[i] == b'/' && bytes[i + 1] == b'*' {
      i += 2;
      while i + 1 < n && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
        i += 1;
      }
      i = (i + 2).min(n);
      continue;
    }
    break;
  }
  if i + 4 > n || !src[i..i + 4].eq_ignore_ascii_case("WITH") {
    return out;
  }
  i += 4;
  // Optional RECURSIVE.
  while i < n && bytes[i].is_ascii_whitespace() {
    i += 1;
  }
  if i + 9 <= n && src[i..i + 9].eq_ignore_ascii_case("RECURSIVE") {
    i += 9;
  }
  // Loop over CTE definitions: <name> [(<cols>)] AS [...]? (<body>) [,]
  loop {
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    // Read CTE name (word identifier or quoted).
    let name_start = i;
    let name: String;
    if i < n && bytes[i] == b'"' {
      i += 1;
      let inner = i;
      while i < n && bytes[i] != b'"' {
        i += 1;
      }
      if i >= n {
        return out;
      }
      name = src[inner..i].to_string();
      i += 1;
    } else {
      while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
        i += 1;
      }
      if i == name_start {
        return out;
      }
      name = src[name_start..i].to_string();
    }
    // Skip whitespace, optional column list, AS, optional MATERIALIZED, then the body parens.
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    if i < n && bytes[i] == b'(' {
      let mut depth = 1i32;
      i += 1;
      while i < n && depth > 0 {
        match bytes[i] {
          b'(' => depth += 1,
          b')' => depth -= 1,
          _ => {},
        }
        i += 1;
      }
      while i < n && bytes[i].is_ascii_whitespace() {
        i += 1;
      }
    }
    // Require `AS` (case-insensitive); otherwise this isn't a real CTE def.
    if i + 2 > n || !src[i..i + 2].eq_ignore_ascii_case("AS") {
      // Still emit the name -- the user may not have typed AS yet.
      out.push(name);
      return out;
    }
    i += 2;
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    for kw in ["NOT MATERIALIZED", "MATERIALIZED"] {
      if i + kw.len() <= n && src[i..i + kw.len()].eq_ignore_ascii_case(kw) {
        i += kw.len();
        while i < n && bytes[i].is_ascii_whitespace() {
          i += 1;
        }
        break;
      }
    }
    out.push(name);
    if i >= n || bytes[i] != b'(' {
      return out;
    }
    let mut depth = 1i32;
    i += 1;
    while i < n && depth > 0 {
      match bytes[i] {
        b'(' => depth += 1,
        b')' => depth -= 1,
        b'\'' => {
          i += 1;
          while i < n && bytes[i] != b'\'' {
            i += 1;
          }
        },
        _ => {},
      }
      if i < n {
        i += 1;
      }
    }
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    if i < n && bytes[i] == b',' {
      i += 1;
      continue;
    }
    return out;
  }
}

/// Like [`cte_names_from_text`] but for a *single* named CTE: walk the
/// `WITH ... AS (body)` prefix and return the projected column names
/// of the CTE whose name matches `target` (case-insensitive). The body
/// is re-parsed as a standalone SELECT so the column names come from
/// the projection list -- including any aliases the user wrote.
///
/// Returns an empty vector when the CTE is found but its body doesn't
/// parse (the caller treats that as "I know the CTE exists but can't
/// enumerate its columns"). Returns `None` when no matching CTE is
/// declared in the leading WITH.
pub fn cte_columns_from_text(src: &str, target: &str) -> Option<Vec<String>> {
  let bytes = src.as_bytes();
  let n = bytes.len();
  let mut i = 0;
  // Skip whitespace / comments to find leading WITH.
  loop {
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    if i + 1 < n && bytes[i] == b'-' && bytes[i + 1] == b'-' {
      while i < n && bytes[i] != b'\n' {
        i += 1;
      }
      continue;
    }
    if i + 1 < n && bytes[i] == b'/' && bytes[i + 1] == b'*' {
      i += 2;
      while i + 1 < n && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
        i += 1;
      }
      i = (i + 2).min(n);
      continue;
    }
    break;
  }
  if i + 4 > n || !src[i..i + 4].eq_ignore_ascii_case("WITH") {
    return None;
  }
  i += 4;
  while i < n && bytes[i].is_ascii_whitespace() {
    i += 1;
  }
  if i + 9 <= n && src[i..i + 9].eq_ignore_ascii_case("RECURSIVE") {
    i += 9;
  }
  loop {
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    // CTE name.
    let name_start = i;
    let name: String;
    if i < n && bytes[i] == b'"' {
      i += 1;
      let inner = i;
      while i < n && bytes[i] != b'"' {
        i += 1;
      }
      if i >= n {
        return None;
      }
      name = src[inner..i].to_string();
      i += 1;
    } else {
      while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
        i += 1;
      }
      if i == name_start {
        return None;
      }
      name = src[name_start..i].to_string();
    }
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    // Optional column list `(col1, col2, ...)` -- if present, this IS
    // the projected schema; no need to re-parse the body.
    let mut explicit_cols: Option<Vec<String>> = None;
    if i < n && bytes[i] == b'(' {
      let start = i + 1;
      let mut depth = 1i32;
      i += 1;
      while i < n && depth > 0 {
        match bytes[i] {
          b'(' => depth += 1,
          b')' => depth -= 1,
          _ => {},
        }
        i += 1;
      }
      let end = (i - 1).min(n);
      explicit_cols = Some(
        src[start..end].split(',').map(|s| s.trim().trim_matches('"').to_string()).filter(|s| !s.is_empty()).collect(),
      );
      while i < n && bytes[i].is_ascii_whitespace() {
        i += 1;
      }
    }
    if i + 2 > n || !src[i..i + 2].eq_ignore_ascii_case("AS") {
      return if name.eq_ignore_ascii_case(target) { Some(explicit_cols.unwrap_or_default()) } else { None };
    }
    i += 2;
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    for kw in ["NOT MATERIALIZED", "MATERIALIZED"] {
      if i + kw.len() <= n && src[i..i + kw.len()].eq_ignore_ascii_case(kw) {
        i += kw.len();
        while i < n && bytes[i].is_ascii_whitespace() {
          i += 1;
        }
        break;
      }
    }
    if i >= n || bytes[i] != b'(' {
      return if name.eq_ignore_ascii_case(target) { Some(explicit_cols.unwrap_or_default()) } else { None };
    }
    let body_start = i + 1;
    let mut depth = 1i32;
    i += 1;
    while i < n && depth > 0 {
      match bytes[i] {
        b'(' => depth += 1,
        b')' => depth -= 1,
        b'\'' => {
          i += 1;
          while i < n && bytes[i] != b'\'' {
            i += 1;
          }
        },
        _ => {},
      }
      if i < n {
        i += 1;
      }
    }
    let body_end = (i - 1).min(n);
    if name.eq_ignore_ascii_case(target) {
      if let Some(cols) = explicit_cols {
        return Some(cols);
      }
      let body = &src[body_start..body_end];
      return Some(projection_columns_of(body));
    }
    while i < n && bytes[i].is_ascii_whitespace() {
      i += 1;
    }
    if i < n && bytes[i] == b',' {
      i += 1;
      continue;
    }
    return None;
  }
}

/// Re-parse `body` (a single SELECT) and return its projection column
/// names. Bare `Column { name }` projections use the column name;
/// `Expr { alias: Some(a) }` uses the alias; other shapes are skipped.
fn projection_columns_of(body: &str) -> Vec<String> {
  let file = dsl_parse::parse(body, dsl_parse::Dialect::Postgres);
  for stmt in &file.statements {
    if let dsl_parse::StatementKind::Select(s) = &stmt.kind {
      let mut out = Vec::new();
      for p in &s.projections {
        if let dsl_parse::Projection::Expr { expr, alias } = p {
          if let Some(a) = alias {
            out.push(a.clone());
          } else if let dsl_parse::Expr::Column { name, .. } = expr {
            out.push(name.clone());
          }
        }
      }
      return out;
    }
  }
  Vec::new()
}

/// Return a fallback scope, or None if no FROM / JOIN matches were found.
pub fn scope_from_text(src: &str) -> Option<Scope> {
  let mut scope = Scope::default();
  for (schema, table, alias) in iter_table_bindings(src) {
    let table_ref =
      TableRef { schema: schema.clone(), name: table.clone(), alias: Some(alias.clone()), range: TextRange::default() };
    let binding = Binding { alias: alias.clone(), table: table_ref.clone() };
    scope.bindings.insert(alias, binding.clone());
    // Bind by unaliased name too so qualified refs like `users.id` work.
    scope.bindings.entry(table).or_insert(Binding { alias: table_ref.name.clone(), table: table_ref });
  }
  if scope.is_empty() { None } else { Some(scope) }
}

/// Yield (schema, table, alias) tuples from FROM / JOIN / UPDATE / DELETE
/// / INTO clauses in the source. Consumes comma-separated relation lists
/// after FROM / UPDATE so `FROM users u, orders o` exposes both bindings.
/// `schema` is `None` for bare table refs and `Some(...)` for the dotted
/// `schema.table` form.
fn iter_table_bindings(src: &str) -> Vec<(Option<String>, String, String)> {
  let mut out = Vec::new();
  let chars: Vec<char> = src.chars().collect();
  let mut i = 0;
  while i < chars.len() {
    if let Some(consumed) = match_keyword(&chars, i, &["FROM", "JOIN", "UPDATE", "INTO", "USING"]) {
      i += consumed;
      // Read the first table, then any comma-continuations.
      loop {
        if let Some((schema, table, alias, consumed2)) = read_table_with_alias(&chars, i) {
          out.push((schema, table, alias));
          i += consumed2;
          // Skip whitespace; require an immediate `,` to take
          // another table in the same FROM/UPDATE list.
          let ws = skip_ws(&chars, i);
          if chars.get(i + ws) == Some(&',') {
            i += ws + 1;
            continue;
          }
        }
        break;
      }
      continue;
    }
    i += 1;
  }
  out
}

/// Return the number of consumed chars if any of `keywords` matches at
/// `pos`, as a whole word (preceded and followed by non-word chars).
fn match_keyword(chars: &[char], pos: usize, keywords: &[&str]) -> Option<usize> {
  // Must be at a word boundary.
  if pos > 0 && is_word(chars[pos - 1]) {
    return None;
  }
  for &kw in keywords {
    if matches_ci(chars, pos, kw) && chars.get(pos + kw.len()).is_none_or(|c| !is_word(*c)) {
      return Some(kw.len());
    }
  }
  None
}

fn matches_ci(chars: &[char], pos: usize, target: &str) -> bool {
  let tchars: Vec<char> = target.chars().collect();
  if chars.len() < pos + tchars.len() {
    return false;
  }
  for (i, t) in tchars.iter().enumerate() {
    if !chars[pos + i].eq_ignore_ascii_case(t) {
      return false;
    }
  }
  true
}

fn is_word(c: char) -> bool {
  c.is_alphanumeric() || c == '_'
}

/// Read `[whitespace] [<schema>.]<table> [AS <alias>] | <alias>` after a
/// FROM/JOIN. Returns (schema, table, alias, consumed chars). `schema`
/// is `Some(...)` only for the dotted form `schema.table`.
fn read_table_with_alias(chars: &[char], pos: usize) -> Option<(Option<String>, String, String, usize)> {
  let mut i = pos;
  let ws = skip_ws(chars, i);
  i += ws;

  let (first, f_len) = read_ident(chars, i)?;
  if first.is_empty() {
    return None;
  }
  i += f_len;
  // Schema-qualified form: `schema.table`. The first ident becomes the
  // schema, the ident after the dot becomes the table.
  let mut schema: Option<String> = None;
  let mut table = first;
  if chars.get(i) == Some(&'.')
    && let Some((after, a_len)) = read_ident(chars, i + 1)
    && !after.is_empty()
  {
    schema = Some(table);
    table = after;
    i += 1 + a_len;
  }

  // Optional AS alias  |  bare alias  |  no alias.
  let mut alias = table.clone();
  let after_table = i;
  i += skip_ws(chars, i);
  if matches_ci(chars, i, "AS") && chars.get(i + 2).is_none_or(|c| !is_word(*c)) {
    i += 2;
    i += skip_ws(chars, i);
    if let Some((ident, len)) = read_ident(chars, i) {
      alias = ident;
      i += len;
    }
  } else if let Some((ident, len)) = read_ident(chars, i) {
    // Bare alias only if the identifier doesn't look like a SQL keyword.
    // We catch the common keywords that legitimately follow a table
    // without an alias: ON, WHERE, JOIN, GROUP, ORDER, LIMIT, etc.
    let upper = ident.to_uppercase();
    const STOPWORDS: &[&str] = &[
      "ON",
      "WHERE",
      "JOIN",
      "INNER",
      "LEFT",
      "RIGHT",
      "FULL",
      "OUTER",
      "CROSS",
      "LATERAL",
      "USING",
      "GROUP",
      "ORDER",
      "LIMIT",
      "OFFSET",
      "HAVING",
      "UNION",
      "INTERSECT",
      "EXCEPT",
      "AS",
      // DML clause keywords that follow `UPDATE <table>` / `DELETE FROM
      // <table>` / `INSERT INTO <table>` -- without these, the next
      // token (`SET`, `VALUES`, `RETURNING`, ...) was being captured as
      // a bogus alias for the target table.
      "SET",
      "VALUES",
      "RETURNING",
      "DEFAULT",
      "FROM",
      "INTO",
      "WITH",
      "TABLESAMPLE",
      "FETCH",
      "FOR",
      "WINDOW",
      "NATURAL",
    ];
    if !STOPWORDS.contains(&upper.as_str()) {
      alias = ident;
      i += len;
    } else {
      i = after_table;
    }
  } else {
    i = after_table;
  }

  Some((schema, table, alias, i - pos))
}

fn skip_ws(chars: &[char], pos: usize) -> usize {
  let mut n = 0;
  while pos + n < chars.len() && chars[pos + n].is_whitespace() {
    n += 1;
  }
  n
}

fn read_ident(chars: &[char], pos: usize) -> Option<(String, usize)> {
  // Double-quoted identifier: `"Anything Goes"` -- return the inner
  // text (without quotes) so the rest of the pipeline can match it
  // against scope keys, which the resolver stores unquoted.
  if chars.get(pos) == Some(&'"') {
    let mut n = 1;
    while pos + n < chars.len() && chars[pos + n] != '"' {
      n += 1;
    }
    if pos + n >= chars.len() {
      return None;
    }
    let inner: String = chars[pos + 1..pos + n].iter().collect();
    if inner.is_empty() {
      return None;
    }
    return Some((inner, n + 1));
  }
  let mut n = 0;
  while pos + n < chars.len() && is_word(chars[pos + n]) {
    n += 1;
  }
  if n == 0 {
    return None;
  }
  Some((chars[pos..pos + n].iter().collect(), n))
}