Skip to main content

dsl_completion/
fallback.rs

1//! Text-level fallback scope builder.
2//!
3//! Used when the parser cannot make sense of the surrounding statement
4//! (which happens constantly while the user is typing, e.g. `SELECT u.`).
5//! We scan the raw source for FROM / JOIN clauses with regex-style passes
6//! and produce a [`Scope`] mapping aliases to table names. The result is
7//! "best effort" -- nested subqueries and CTEs are not fully handled --
8//! but enough to recover useful completion in the half-written state.
9
10use dsl_parse::TableRef;
11use dsl_resolve::{Binding, Scope};
12use text_size::TextRange;
13
14/// Yield the CTE names declared by a `WITH [RECURSIVE] name [(cols)] AS
15/// (body), name2 AS (body2) ...` prefix on `src`. Pure text-level scan
16/// so it still works when the parser bails on an unfinished trailing
17/// statement (the common case while the user is typing the outer FROM).
18pub fn cte_names_from_text(src: &str) -> Vec<String> {
19  let bytes = src.as_bytes();
20  let n = bytes.len();
21  let mut out = Vec::new();
22  let mut i = 0;
23  // Skip whitespace / line+block comments to find the leading WITH.
24  loop {
25    while i < n && bytes[i].is_ascii_whitespace() {
26      i += 1;
27    }
28    if i + 1 < n && bytes[i] == b'-' && bytes[i + 1] == b'-' {
29      while i < n && bytes[i] != b'\n' {
30        i += 1;
31      }
32      continue;
33    }
34    if i + 1 < n && bytes[i] == b'/' && bytes[i + 1] == b'*' {
35      i += 2;
36      while i + 1 < n && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
37        i += 1;
38      }
39      i = (i + 2).min(n);
40      continue;
41    }
42    break;
43  }
44  if i + 4 > n || !src[i..i + 4].eq_ignore_ascii_case("WITH") {
45    return out;
46  }
47  i += 4;
48  // Optional RECURSIVE.
49  while i < n && bytes[i].is_ascii_whitespace() {
50    i += 1;
51  }
52  if i + 9 <= n && src[i..i + 9].eq_ignore_ascii_case("RECURSIVE") {
53    i += 9;
54  }
55  // Loop over CTE definitions: <name> [(<cols>)] AS [...]? (<body>) [,]
56  loop {
57    while i < n && bytes[i].is_ascii_whitespace() {
58      i += 1;
59    }
60    // Read CTE name (word identifier or quoted).
61    let name_start = i;
62    let name: String;
63    if i < n && bytes[i] == b'"' {
64      i += 1;
65      let inner = i;
66      while i < n && bytes[i] != b'"' {
67        i += 1;
68      }
69      if i >= n {
70        return out;
71      }
72      name = src[inner..i].to_string();
73      i += 1;
74    } else {
75      while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
76        i += 1;
77      }
78      if i == name_start {
79        return out;
80      }
81      name = src[name_start..i].to_string();
82    }
83    // Skip whitespace, optional column list, AS, optional MATERIALIZED, then the body parens.
84    while i < n && bytes[i].is_ascii_whitespace() {
85      i += 1;
86    }
87    if i < n && bytes[i] == b'(' {
88      let mut depth = 1i32;
89      i += 1;
90      while i < n && depth > 0 {
91        match bytes[i] {
92          b'(' => depth += 1,
93          b')' => depth -= 1,
94          _ => {},
95        }
96        i += 1;
97      }
98      while i < n && bytes[i].is_ascii_whitespace() {
99        i += 1;
100      }
101    }
102    // Require `AS` (case-insensitive); otherwise this isn't a real CTE def.
103    if i + 2 > n || !src[i..i + 2].eq_ignore_ascii_case("AS") {
104      // Still emit the name -- the user may not have typed AS yet.
105      out.push(name);
106      return out;
107    }
108    i += 2;
109    while i < n && bytes[i].is_ascii_whitespace() {
110      i += 1;
111    }
112    for kw in ["NOT MATERIALIZED", "MATERIALIZED"] {
113      if i + kw.len() <= n && src[i..i + kw.len()].eq_ignore_ascii_case(kw) {
114        i += kw.len();
115        while i < n && bytes[i].is_ascii_whitespace() {
116          i += 1;
117        }
118        break;
119      }
120    }
121    out.push(name);
122    if i >= n || bytes[i] != b'(' {
123      return out;
124    }
125    let mut depth = 1i32;
126    i += 1;
127    while i < n && depth > 0 {
128      match bytes[i] {
129        b'(' => depth += 1,
130        b')' => depth -= 1,
131        b'\'' => {
132          i += 1;
133          while i < n && bytes[i] != b'\'' {
134            i += 1;
135          }
136        },
137        _ => {},
138      }
139      if i < n {
140        i += 1;
141      }
142    }
143    while i < n && bytes[i].is_ascii_whitespace() {
144      i += 1;
145    }
146    if i < n && bytes[i] == b',' {
147      i += 1;
148      continue;
149    }
150    return out;
151  }
152}
153
154/// Like [`cte_names_from_text`] but for a *single* named CTE: walk the
155/// `WITH ... AS (body)` prefix and return the projected column names
156/// of the CTE whose name matches `target` (case-insensitive). The body
157/// is re-parsed as a standalone SELECT so the column names come from
158/// the projection list -- including any aliases the user wrote.
159///
160/// Returns an empty vector when the CTE is found but its body doesn't
161/// parse (the caller treats that as "I know the CTE exists but can't
162/// enumerate its columns"). Returns `None` when no matching CTE is
163/// declared in the leading WITH.
164pub fn cte_columns_from_text(src: &str, target: &str) -> Option<Vec<String>> {
165  let bytes = src.as_bytes();
166  let n = bytes.len();
167  let mut i = 0;
168  // Skip whitespace / comments to find leading WITH.
169  loop {
170    while i < n && bytes[i].is_ascii_whitespace() {
171      i += 1;
172    }
173    if i + 1 < n && bytes[i] == b'-' && bytes[i + 1] == b'-' {
174      while i < n && bytes[i] != b'\n' {
175        i += 1;
176      }
177      continue;
178    }
179    if i + 1 < n && bytes[i] == b'/' && bytes[i + 1] == b'*' {
180      i += 2;
181      while i + 1 < n && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
182        i += 1;
183      }
184      i = (i + 2).min(n);
185      continue;
186    }
187    break;
188  }
189  if i + 4 > n || !src[i..i + 4].eq_ignore_ascii_case("WITH") {
190    return None;
191  }
192  i += 4;
193  while i < n && bytes[i].is_ascii_whitespace() {
194    i += 1;
195  }
196  if i + 9 <= n && src[i..i + 9].eq_ignore_ascii_case("RECURSIVE") {
197    i += 9;
198  }
199  loop {
200    while i < n && bytes[i].is_ascii_whitespace() {
201      i += 1;
202    }
203    // CTE name.
204    let name_start = i;
205    let name: String;
206    if i < n && bytes[i] == b'"' {
207      i += 1;
208      let inner = i;
209      while i < n && bytes[i] != b'"' {
210        i += 1;
211      }
212      if i >= n {
213        return None;
214      }
215      name = src[inner..i].to_string();
216      i += 1;
217    } else {
218      while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
219        i += 1;
220      }
221      if i == name_start {
222        return None;
223      }
224      name = src[name_start..i].to_string();
225    }
226    while i < n && bytes[i].is_ascii_whitespace() {
227      i += 1;
228    }
229    // Optional column list `(col1, col2, ...)` -- if present, this IS
230    // the projected schema; no need to re-parse the body.
231    let mut explicit_cols: Option<Vec<String>> = None;
232    if i < n && bytes[i] == b'(' {
233      let start = i + 1;
234      let mut depth = 1i32;
235      i += 1;
236      while i < n && depth > 0 {
237        match bytes[i] {
238          b'(' => depth += 1,
239          b')' => depth -= 1,
240          _ => {},
241        }
242        i += 1;
243      }
244      let end = (i - 1).min(n);
245      explicit_cols = Some(
246        src[start..end].split(',').map(|s| s.trim().trim_matches('"').to_string()).filter(|s| !s.is_empty()).collect(),
247      );
248      while i < n && bytes[i].is_ascii_whitespace() {
249        i += 1;
250      }
251    }
252    if i + 2 > n || !src[i..i + 2].eq_ignore_ascii_case("AS") {
253      return if name.eq_ignore_ascii_case(target) { Some(explicit_cols.unwrap_or_default()) } else { None };
254    }
255    i += 2;
256    while i < n && bytes[i].is_ascii_whitespace() {
257      i += 1;
258    }
259    for kw in ["NOT MATERIALIZED", "MATERIALIZED"] {
260      if i + kw.len() <= n && src[i..i + kw.len()].eq_ignore_ascii_case(kw) {
261        i += kw.len();
262        while i < n && bytes[i].is_ascii_whitespace() {
263          i += 1;
264        }
265        break;
266      }
267    }
268    if i >= n || bytes[i] != b'(' {
269      return if name.eq_ignore_ascii_case(target) { Some(explicit_cols.unwrap_or_default()) } else { None };
270    }
271    let body_start = i + 1;
272    let mut depth = 1i32;
273    i += 1;
274    while i < n && depth > 0 {
275      match bytes[i] {
276        b'(' => depth += 1,
277        b')' => depth -= 1,
278        b'\'' => {
279          i += 1;
280          while i < n && bytes[i] != b'\'' {
281            i += 1;
282          }
283        },
284        _ => {},
285      }
286      if i < n {
287        i += 1;
288      }
289    }
290    let body_end = (i - 1).min(n);
291    if name.eq_ignore_ascii_case(target) {
292      if let Some(cols) = explicit_cols {
293        return Some(cols);
294      }
295      let body = &src[body_start..body_end];
296      return Some(projection_columns_of(body));
297    }
298    while i < n && bytes[i].is_ascii_whitespace() {
299      i += 1;
300    }
301    if i < n && bytes[i] == b',' {
302      i += 1;
303      continue;
304    }
305    return None;
306  }
307}
308
309/// Re-parse `body` (a single SELECT) and return its projection column
310/// names. Bare `Column { name }` projections use the column name;
311/// `Expr { alias: Some(a) }` uses the alias; other shapes are skipped.
312fn projection_columns_of(body: &str) -> Vec<String> {
313  let file = dsl_parse::parse(body, dsl_parse::Dialect::Postgres);
314  for stmt in &file.statements {
315    if let dsl_parse::StatementKind::Select(s) = &stmt.kind {
316      let mut out = Vec::new();
317      for p in &s.projections {
318        if let dsl_parse::Projection::Expr { expr, alias } = p {
319          if let Some(a) = alias {
320            out.push(a.clone());
321          } else if let dsl_parse::Expr::Column { name, .. } = expr {
322            out.push(name.clone());
323          }
324        }
325      }
326      return out;
327    }
328  }
329  Vec::new()
330}
331
332/// Return a fallback scope, or None if no FROM / JOIN matches were found.
333pub fn scope_from_text(src: &str) -> Option<Scope> {
334  let mut scope = Scope::default();
335  for (schema, table, alias) in iter_table_bindings(src) {
336    let table_ref =
337      TableRef { schema: schema.clone(), name: table.clone(), alias: Some(alias.clone()), range: TextRange::default() };
338    let binding = Binding { alias: alias.clone(), table: table_ref.clone() };
339    scope.bindings.insert(alias, binding.clone());
340    // Bind by unaliased name too so qualified refs like `users.id` work.
341    scope.bindings.entry(table).or_insert(Binding { alias: table_ref.name.clone(), table: table_ref });
342  }
343  if scope.is_empty() { None } else { Some(scope) }
344}
345
346/// Yield (schema, table, alias) tuples from FROM / JOIN / UPDATE / DELETE
347/// / INTO clauses in the source. Consumes comma-separated relation lists
348/// after FROM / UPDATE so `FROM users u, orders o` exposes both bindings.
349/// `schema` is `None` for bare table refs and `Some(...)` for the dotted
350/// `schema.table` form.
351fn iter_table_bindings(src: &str) -> Vec<(Option<String>, String, String)> {
352  let mut out = Vec::new();
353  let chars: Vec<char> = src.chars().collect();
354  let mut i = 0;
355  while i < chars.len() {
356    if let Some(consumed) = match_keyword(&chars, i, &["FROM", "JOIN", "UPDATE", "INTO", "USING"]) {
357      i += consumed;
358      // Read the first table, then any comma-continuations.
359      loop {
360        if let Some((schema, table, alias, consumed2)) = read_table_with_alias(&chars, i) {
361          out.push((schema, table, alias));
362          i += consumed2;
363          // Skip whitespace; require an immediate `,` to take
364          // another table in the same FROM/UPDATE list.
365          let ws = skip_ws(&chars, i);
366          if chars.get(i + ws) == Some(&',') {
367            i += ws + 1;
368            continue;
369          }
370        }
371        break;
372      }
373      continue;
374    }
375    i += 1;
376  }
377  out
378}
379
380/// Return the number of consumed chars if any of `keywords` matches at
381/// `pos`, as a whole word (preceded and followed by non-word chars).
382fn match_keyword(chars: &[char], pos: usize, keywords: &[&str]) -> Option<usize> {
383  // Must be at a word boundary.
384  if pos > 0 && is_word(chars[pos - 1]) {
385    return None;
386  }
387  for &kw in keywords {
388    if matches_ci(chars, pos, kw) && chars.get(pos + kw.len()).is_none_or(|c| !is_word(*c)) {
389      return Some(kw.len());
390    }
391  }
392  None
393}
394
395fn matches_ci(chars: &[char], pos: usize, target: &str) -> bool {
396  let tchars: Vec<char> = target.chars().collect();
397  if chars.len() < pos + tchars.len() {
398    return false;
399  }
400  for (i, t) in tchars.iter().enumerate() {
401    if !chars[pos + i].eq_ignore_ascii_case(t) {
402      return false;
403    }
404  }
405  true
406}
407
408fn is_word(c: char) -> bool {
409  c.is_alphanumeric() || c == '_'
410}
411
412/// Read `[whitespace] [<schema>.]<table> [AS <alias>] | <alias>` after a
413/// FROM/JOIN. Returns (schema, table, alias, consumed chars). `schema`
414/// is `Some(...)` only for the dotted form `schema.table`.
415fn read_table_with_alias(chars: &[char], pos: usize) -> Option<(Option<String>, String, String, usize)> {
416  let mut i = pos;
417  let ws = skip_ws(chars, i);
418  i += ws;
419
420  let (first, f_len) = read_ident(chars, i)?;
421  if first.is_empty() {
422    return None;
423  }
424  i += f_len;
425  // Schema-qualified form: `schema.table`. The first ident becomes the
426  // schema, the ident after the dot becomes the table.
427  let mut schema: Option<String> = None;
428  let mut table = first;
429  if chars.get(i) == Some(&'.')
430    && let Some((after, a_len)) = read_ident(chars, i + 1)
431    && !after.is_empty()
432  {
433    schema = Some(table);
434    table = after;
435    i += 1 + a_len;
436  }
437
438  // Optional AS alias  |  bare alias  |  no alias.
439  let mut alias = table.clone();
440  let after_table = i;
441  i += skip_ws(chars, i);
442  if matches_ci(chars, i, "AS") && chars.get(i + 2).is_none_or(|c| !is_word(*c)) {
443    i += 2;
444    i += skip_ws(chars, i);
445    if let Some((ident, len)) = read_ident(chars, i) {
446      alias = ident;
447      i += len;
448    }
449  } else if let Some((ident, len)) = read_ident(chars, i) {
450    // Bare alias only if the identifier doesn't look like a SQL keyword.
451    // We catch the common keywords that legitimately follow a table
452    // without an alias: ON, WHERE, JOIN, GROUP, ORDER, LIMIT, etc.
453    let upper = ident.to_uppercase();
454    const STOPWORDS: &[&str] = &[
455      "ON",
456      "WHERE",
457      "JOIN",
458      "INNER",
459      "LEFT",
460      "RIGHT",
461      "FULL",
462      "OUTER",
463      "CROSS",
464      "LATERAL",
465      "USING",
466      "GROUP",
467      "ORDER",
468      "LIMIT",
469      "OFFSET",
470      "HAVING",
471      "UNION",
472      "INTERSECT",
473      "EXCEPT",
474      "AS",
475      // DML clause keywords that follow `UPDATE <table>` / `DELETE FROM
476      // <table>` / `INSERT INTO <table>` -- without these, the next
477      // token (`SET`, `VALUES`, `RETURNING`, ...) was being captured as
478      // a bogus alias for the target table.
479      "SET",
480      "VALUES",
481      "RETURNING",
482      "DEFAULT",
483      "FROM",
484      "INTO",
485      "WITH",
486      "TABLESAMPLE",
487      "FETCH",
488      "FOR",
489      "WINDOW",
490      "NATURAL",
491    ];
492    if !STOPWORDS.contains(&upper.as_str()) {
493      alias = ident;
494      i += len;
495    } else {
496      i = after_table;
497    }
498  } else {
499    i = after_table;
500  }
501
502  Some((schema, table, alias, i - pos))
503}
504
505fn skip_ws(chars: &[char], pos: usize) -> usize {
506  let mut n = 0;
507  while pos + n < chars.len() && chars[pos + n].is_whitespace() {
508    n += 1;
509  }
510  n
511}
512
513fn read_ident(chars: &[char], pos: usize) -> Option<(String, usize)> {
514  // Double-quoted identifier: `"Anything Goes"` -- return the inner
515  // text (without quotes) so the rest of the pipeline can match it
516  // against scope keys, which the resolver stores unquoted.
517  if chars.get(pos) == Some(&'"') {
518    let mut n = 1;
519    while pos + n < chars.len() && chars[pos + n] != '"' {
520      n += 1;
521    }
522    if pos + n >= chars.len() {
523      return None;
524    }
525    let inner: String = chars[pos + 1..pos + n].iter().collect();
526    if inner.is_empty() {
527      return None;
528    }
529    return Some((inner, n + 1));
530  }
531  let mut n = 0;
532  while pos + n < chars.len() && is_word(chars[pos + n]) {
533    n += 1;
534  }
535  if n == 0 {
536    return None;
537  }
538  Some((chars[pos..pos + n].iter().collect(), n))
539}