dsl-parse 0.1.0

Facade over sqlparser / pg_query producing a unified SQL Statement representation 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! pg_query protobuf node -> internal AST conversion.
//!
//! Only the slots downstream crates actually use are populated:
//!
//! - CREATE TABLE -> [`StatementKind::CreateTable`] with columns
//! - CREATE INDEX -> not yet a dedicated variant; reported as Unknown
//! - SELECT -> [`StatementKind::Select`] with FROM tables + optional WHERE
//!   expression text
//! - INSERT/UPDATE/DELETE -> respective variants with target table +
//!   presence-of-WHERE bit
//! - ALTER TABLE / DROP TABLE -> respective variants
//! - Everything else -> `StatementKind::Unknown { text }` so the feature
//!   stack still sees the raw SQL.
//!
//! Goal: full PG18 syntax coverage at parse-time, even when our AST
//! doesn't model the new constructs.

use crate::ast::{
  AlterTableStmt, ColumnDef, CreateTableStmt, DeleteStmt, DropTableStmt, Expr, InsertStmt, JoinClause, JoinKind,
  Projection, SelectStmt, StatementKind, TableRef, UpdateStmt,
};
use pg_query::protobuf::node::Node as PgNode;
use pg_query::protobuf::{ColumnDef as PgCol, CreateStmt, RangeVar};
use text_size::TextRange;

pub fn statement(node: &PgNode, text: &str) -> StatementKind {
  match node {
    PgNode::CreateStmt(c) => create_table(c, text),
    PgNode::SelectStmt(s) => StatementKind::Select(select(s, text)),
    PgNode::InsertStmt(i) => StatementKind::Insert(insert(i, text)),
    PgNode::UpdateStmt(u) => StatementKind::Update(update(u, text)),
    PgNode::DeleteStmt(d) => StatementKind::Delete(delete(d, text)),
    PgNode::AlterTableStmt(a) => StatementKind::AlterTable(alter_table(a)),
    PgNode::DropStmt(d) => StatementKind::DropTable(drop_table(d)),
    _ => StatementKind::Unknown { text: text.to_string() },
  }
}

// ---------------------------------------------------------------------------
// CREATE TABLE
// ---------------------------------------------------------------------------

fn create_table(stmt: &CreateStmt, _text: &str) -> StatementKind {
  let table = stmt.relation.as_ref().map(rangevar_to_tableref).unwrap_or_default();
  let mut columns = Vec::new();
  for elt in &stmt.table_elts {
    if let Some(PgNode::ColumnDef(c)) = elt.node.as_ref() {
      columns.push(column_def(c));
    }
  }
  let if_not_exists = stmt.if_not_exists;
  StatementKind::CreateTable(CreateTableStmt { table, if_not_exists, columns })
}

fn column_def(c: &PgCol) -> ColumnDef {
  let name = c.colname.clone();
  let type_name = c
    .type_name
    .as_ref()
    .map(|t| {
      let base = t
        .names
        .iter()
        .filter_map(|n| n.node.as_ref())
        .filter_map(|n| match n {
          PgNode::String(s) => Some(s.sval.clone()),
          _ => None,
        })
        .collect::<Vec<_>>()
        .join(".");
      // Append `[]` per array dim so downstream rules (sql230 GIN on
      // scalar) see the array suffix and don't false-positive on text[].
      let arr = "[]".repeat(t.array_bounds.len());
      format!("{base}{arr}")
    })
    .unwrap_or_default();
  // pg_query exposes nullability through constraints. A NOT NULL
  // constraint sets `contype = ConstrNotnull`. Defaults live in
  // `ConstrDefault` and stringify as the raw text via cooked_expr.
  let mut nullable = true;
  let mut default: Option<String> = None;
  use pg_query::protobuf::ConstrType;
  for cons in &c.constraints {
    if let Some(PgNode::Constraint(con)) = cons.node.as_ref() {
      // Constraint type enum (see pg_query::protobuf::ConstrType).
      // PRIMARY KEY implies NOT NULL semantically; we propagate
      // that here so completion/hover reflect runtime behaviour.
      let ct = ConstrType::try_from(con.contype).unwrap_or(ConstrType::Undefined);
      match ct {
        ConstrType::ConstrNotnull | ConstrType::ConstrPrimary => nullable = false,
        ConstrType::ConstrDefault if !con.raw_expr_string().is_empty() => {
          default = Some(con.raw_expr_string());
        },
        _ => {},
      }
    }
  }
  ColumnDef { name, type_name, nullable, default, range: TextRange::default() }
}

// ---------------------------------------------------------------------------
// SELECT
// ---------------------------------------------------------------------------

fn select(s: &pg_query::protobuf::SelectStmt, _text: &str) -> SelectStmt {
  let mut from: Vec<TableRef> = Vec::new();
  let mut joins: Vec<JoinClause> = Vec::new();
  for f in &s.from_clause {
    match f.node.as_ref() {
      Some(PgNode::RangeVar(r)) => from.push(rangevar_to_tableref(r)),
      Some(PgNode::JoinExpr(j)) => walk_join(j, &mut from, &mut joins),
      // `FROM generate_series(1, 10) AS number` -- function-call FROM.
      // Sentinel schema `<func>` flags the binding as synthetic so
      // catalog-driven rules (sql001/sql002) skip the catalog lookup.
      Some(PgNode::RangeFunction(rf)) => {
        let alias = rf.alias.as_ref().map(|a| a.aliasname.clone()).filter(|n| !n.is_empty());
        if let Some(alias_str) = alias {
          from.push(TableRef {
            schema: Some("<func>".into()),
            name: alias_str.clone(),
            alias: Some(alias_str),
            range: TextRange::default(),
          });
        }
      },
      // `FROM (SELECT ...) AS sub` -- subquery alias. Sentinel `<subq>`.
      Some(PgNode::RangeSubselect(rs)) => {
        let alias = rs.alias.as_ref().map(|a| a.aliasname.clone()).filter(|n| !n.is_empty());
        if let Some(alias_str) = alias {
          from.push(TableRef {
            schema: Some("<subq>".into()),
            name: alias_str.clone(),
            alias: Some(alias_str),
            range: TextRange::default(),
          });
        }
      },
      _ => {},
    }
  }
  let projections: Vec<Projection> = s
    .target_list
    .iter()
    .filter_map(|n| n.node.as_ref())
    .filter_map(|n| match n {
      PgNode::ResTarget(t) => Some(target_to_projection(t)),
      _ => None,
    })
    .collect();
  let cte_names: Vec<String> = s
    .with_clause
    .as_ref()
    .map(|wc| {
      wc.ctes
        .iter()
        .filter_map(|n| n.node.as_ref())
        .filter_map(|n| match n {
          PgNode::CommonTableExpr(cte) => Some(cte.ctename.clone()),
          _ => None,
        })
        .collect()
    })
    .unwrap_or_default();
  SelectStmt {
    projections,
    from,
    joins,
    where_clause: s.where_clause.as_ref().map(|n| extract_column_refs_expr(n.node.as_ref())),
    cte_names,
  }
}

/// Walk a pg_query node, collecting every `ColumnRef` it contains as an
/// `Expr::Column`. Wraps the result in `Expr::List` so downstream
/// walkers (e.g. unknown_column rule) can recurse element-wise. When the
/// node contains no ColumnRefs we still return an empty List rather than
/// `Other` so the variant is uniform across WHERE / ON / argument
/// contexts.
fn extract_column_refs_expr(node: Option<&PgNode>) -> Expr {
  let mut out = Vec::new();
  if let Some(n) = node {
    collect_column_refs(n, &mut out);
  }
  Expr::List(out)
}

fn collect_column_refs(node: &PgNode, out: &mut Vec<Expr>) {
  use pg_query::protobuf::node::Node as N;
  match node {
    N::ColumnRef(c) => {
      // pg_query represents `a.b` as a `fields` Vec of String/A_Star nodes.
      let parts: Vec<String> = c
        .fields
        .iter()
        .filter_map(|f| f.node.as_ref())
        .filter_map(|f| match f {
          N::String(s) => Some(s.sval.clone()),
          _ => None,
        })
        .collect();
      let (qualifier, name) = match parts.len() {
        1 => (None, parts.into_iter().next().unwrap()),
        2 => {
          let mut it = parts.into_iter();
          let q = it.next().unwrap();
          let n = it.next().unwrap();
          (Some(q), n)
        },
        3 => {
          // schema.table.col
          let mut it = parts.into_iter();
          let schema = it.next().unwrap();
          let table = it.next().unwrap();
          let n = it.next().unwrap();
          (Some(format!("{schema}.{table}")), n)
        },
        _ => return,
      };
      if name.is_empty() {
        return;
      }
      out.push(Expr::Column { qualifier, name, range: TextRange::default() });
    },
    N::AExpr(a) => {
      if let Some(l) = a.lexpr.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(l, out);
      }
      if let Some(r) = a.rexpr.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(r, out);
      }
    },
    N::BoolExpr(b) => {
      for arg in &b.args {
        if let Some(n) = arg.node.as_ref() {
          collect_column_refs(n, out);
        }
      }
    },
    N::FuncCall(f) => {
      for arg in &f.args {
        if let Some(n) = arg.node.as_ref() {
          collect_column_refs(n, out);
        }
      }
    },
    N::List(l) => {
      for item in &l.items {
        if let Some(n) = item.node.as_ref() {
          collect_column_refs(n, out);
        }
      }
    },
    N::SubLink(s) => {
      if let Some(t) = s.testexpr.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(t, out);
      }
      // Subselect body: walk its targetList + WHERE so unknown columns
      // referenced inside `IN (SELECT noexist FROM ...)` / `EXISTS
      // (SELECT noexist FROM ...)` still surface as sql002.
      if let Some(PgNode::SelectStmt(subsel)) = s.subselect.as_ref().and_then(|n| n.node.as_ref()) {
        for target in &subsel.target_list {
          if let Some(n) = target.node.as_ref() {
            collect_column_refs(n, out);
          }
        }
        if let Some(w) = subsel.where_clause.as_ref().and_then(|n| n.node.as_ref()) {
          collect_column_refs(w, out);
        }
      }
    },
    N::ResTarget(rt) => {
      if let Some(n) = rt.val.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(n, out);
      }
    },
    N::NullTest(t) => {
      if let Some(n) = t.arg.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(n, out);
      }
    },
    N::CaseExpr(c) => {
      for w in &c.args {
        if let Some(n) = w.node.as_ref() {
          collect_column_refs(n, out);
        }
      }
      if let Some(d) = c.defresult.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(d, out);
      }
    },
    N::CaseWhen(c) => {
      if let Some(e) = c.expr.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(e, out);
      }
      if let Some(r) = c.result.as_ref().and_then(|n| n.node.as_ref()) {
        collect_column_refs(r, out);
      }
    },
    _ => {},
  }
}

/// Recursively unfold a JoinExpr -- the left arg can itself be another
/// JoinExpr (chained joins), the right arg is typically a RangeVar.
fn walk_join(j: &pg_query::protobuf::JoinExpr, from: &mut Vec<TableRef>, joins: &mut Vec<JoinClause>) {
  match j.larg.as_ref().and_then(|n| n.node.as_ref()) {
    Some(PgNode::RangeVar(r)) if from.is_empty() => {
      from.push(rangevar_to_tableref(r));
    },
    Some(PgNode::JoinExpr(inner)) => walk_join(inner, from, joins),
    Some(PgNode::RangeFunction(rf)) => {
      let alias = rf.alias.as_ref().map(|a| a.aliasname.clone()).filter(|n| !n.is_empty());
      if let Some(alias_str) = alias
        && from.is_empty()
      {
        from.push(TableRef {
          schema: Some("<func>".into()),
          name: alias_str.clone(),
          alias: Some(alias_str),
          range: TextRange::default(),
        });
      }
    },
    Some(PgNode::RangeSubselect(rs)) => {
      let alias = rs.alias.as_ref().map(|a| a.aliasname.clone()).filter(|n| !n.is_empty());
      if let Some(alias_str) = alias
        && from.is_empty()
      {
        from.push(TableRef {
          schema: Some("<subq>".into()),
          name: alias_str.clone(),
          alias: Some(alias_str),
          range: TextRange::default(),
        });
      }
    },
    _ => {},
  }
  match j.rarg.as_ref().and_then(|n| n.node.as_ref()) {
    Some(PgNode::RangeVar(r)) => {
      joins.push(JoinClause {
        kind: join_kind(j.jointype),
        table: rangevar_to_tableref(r),
        on: j.quals.as_ref().map(|n| extract_column_refs_expr(n.node.as_ref())),
      });
    },
    Some(PgNode::RangeFunction(rf)) => {
      let alias = rf.alias.as_ref().map(|a| a.aliasname.clone()).filter(|n| !n.is_empty());
      if let Some(alias_str) = alias {
        joins.push(JoinClause {
          kind: join_kind(j.jointype),
          table: TableRef {
            schema: Some("<func>".into()),
            name: alias_str.clone(),
            alias: Some(alias_str),
            range: TextRange::default(),
          },
          on: j.quals.as_ref().map(|n| extract_column_refs_expr(n.node.as_ref())),
        });
      }
    },
    Some(PgNode::RangeSubselect(rs)) => {
      let alias = rs.alias.as_ref().map(|a| a.aliasname.clone()).filter(|n| !n.is_empty());
      if let Some(alias_str) = alias {
        joins.push(JoinClause {
          kind: join_kind(j.jointype),
          table: TableRef {
            schema: Some("<subq>".into()),
            name: alias_str.clone(),
            alias: Some(alias_str),
            range: TextRange::default(),
          },
          on: j.quals.as_ref().map(|n| extract_column_refs_expr(n.node.as_ref())),
        });
      }
    },
    _ => {},
  }
}

/// Map the pg_query JoinType enum to our internal JoinKind.
fn join_kind(jt: i32) -> JoinKind {
  use pg_query::protobuf::JoinType;
  match JoinType::try_from(jt).unwrap_or(JoinType::Undefined) {
    JoinType::JoinLeft => JoinKind::Left,
    JoinType::JoinRight => JoinKind::Right,
    JoinType::JoinFull => JoinKind::Full,
    JoinType::JoinAnti => JoinKind::Left, // anti as left for highlight purposes
    JoinType::JoinSemi => JoinKind::Inner,
    _ => JoinKind::Inner,
  }
}

/// Convert a `ResTarget` (one item in the SELECT target list) into our
/// `Projection`. Recognises `*`, qualified `t.*`, and a bare column
/// reference; everything else stays as `Other`.
fn target_to_projection(t: &pg_query::protobuf::ResTarget) -> Projection {
  let alias = if t.name.is_empty() { None } else { Some(t.name.clone()) };
  let val = match t.val.as_ref().and_then(|n| n.node.as_ref()) {
    Some(v) => v,
    None => return Projection::Expr { expr: Expr::List(Vec::new()), alias },
  };
  match val {
    PgNode::ColumnRef(cref) => column_ref_to_projection(cref, alias),
    // Function calls / CASE / arithmetic / boolean expressions in the
    // projection: don't drop the entire predicate to `Other("")`.
    // Walk and collect every column reference so downstream rules
    // (sql002 unknown column, sql003 ambiguous, ...) can still flag
    // typos inside `length(noexist)` / `CASE WHEN noexist THEN ...`.
    other => Projection::Expr { expr: extract_column_refs_expr(Some(other)), alias },
  }
}

/// `ColumnRef.fields` holds an ordered list of identifier / A_Star
/// nodes. `["*"]` -> Projection::Star, `["t", "*"]` -> QualifiedStar,
/// `["col"]` or `["t", "col"]` -> column expression.
fn column_ref_to_projection(cref: &pg_query::protobuf::ColumnRef, alias: Option<String>) -> Projection {
  let parts: Vec<String> = cref
    .fields
    .iter()
    .filter_map(|n| n.node.as_ref())
    .map(|n| match n {
      PgNode::String(s) => s.sval.clone(),
      PgNode::AStar(_) => "*".into(),
      _ => String::new(),
    })
    .collect();
  // pg_query attaches a byte offset on the ColumnRef. Span it across
  // the full reference text (`q.name` or `name`) so the diagnostic
  // can highlight exactly the offending token, not the whole stmt.
  let range = if cref.location >= 0 {
    let start = cref.location as u32;
    let len = parts.iter().map(|s| s.len() as u32).sum::<u32>() + parts.len().saturating_sub(1) as u32; // dots
    TextRange::new(start.into(), (start + len.max(1)).into())
  } else {
    TextRange::default()
  };
  match parts.as_slice() {
    [s] if s == "*" => Projection::Star,
    [q, s] if s == "*" => Projection::QualifiedStar(q.clone()),
    [name] => Projection::Expr { expr: Expr::Column { qualifier: None, name: name.clone(), range }, alias },
    [q, name] => {
      Projection::Expr { expr: Expr::Column { qualifier: Some(q.clone()), name: name.clone(), range }, alias }
    },
    _ => Projection::Expr { expr: Expr::Other(String::new()), alias },
  }
}

// ---------------------------------------------------------------------------
// DML
// ---------------------------------------------------------------------------

fn insert(i: &pg_query::protobuf::InsertStmt, _text: &str) -> InsertStmt {
  let table = i.relation.as_ref().map(rangevar_to_tableref).unwrap_or_default();
  let columns = i
    .cols
    .iter()
    .filter_map(|n| match n.node.as_ref()? {
      PgNode::ResTarget(t) => Some(t.name.clone()),
      _ => None,
    })
    .collect();
  InsertStmt { table, columns }
}

fn update(u: &pg_query::protobuf::UpdateStmt, _text: &str) -> UpdateStmt {
  let table = u.relation.as_ref().map(rangevar_to_tableref).unwrap_or_default();
  // SET <col> = <expr>, ... -- target_list items are ResTargets with
  // `name` = column and `val` = expression node. The internal AST
  // only needs the column name + a placeholder expr; the live
  // catalog supplies type info.
  let assignments = u
    .target_list
    .iter()
    .filter_map(|n| match n.node.as_ref()? {
      PgNode::ResTarget(t) if !t.name.is_empty() => Some((t.name.clone(), Expr::Other(String::new()))),
      _ => None,
    })
    .collect();
  // `UPDATE t SET ... FROM <list> WHERE ...` -- the FROM list adds
  // additional table bindings visible in SET RHS and WHERE.
  let from_tables = collect_dml_from_tables(&u.from_clause);
  UpdateStmt {
    table,
    assignments,
    where_clause: u.where_clause.as_ref().map(|n| extract_column_refs_expr(n.node.as_ref())),
    from_tables,
  }
}

fn delete(d: &pg_query::protobuf::DeleteStmt, _text: &str) -> DeleteStmt {
  let table = d.relation.as_ref().map(rangevar_to_tableref).unwrap_or_default();
  // `DELETE FROM t USING <list> WHERE ...` -- USING list adds
  // additional bindings visible in WHERE and RETURNING.
  let using_tables = collect_dml_from_tables(&d.using_clause);
  DeleteStmt {
    table,
    where_clause: d.where_clause.as_ref().map(|n| extract_column_refs_expr(n.node.as_ref())),
    using_tables,
  }
}

/// Extract simple RangeVar table bindings from a DML FROM/USING list.
/// Subqueries, joins, and function calls are skipped -- only bare table
/// references contribute alias bindings.
fn collect_dml_from_tables(list: &[pg_query::protobuf::Node]) -> Vec<TableRef> {
  let mut out = Vec::new();
  for n in list {
    if let Some(PgNode::RangeVar(r)) = n.node.as_ref() {
      out.push(rangevar_to_tableref(r));
    }
  }
  out
}

fn alter_table(a: &pg_query::protobuf::AlterTableStmt) -> AlterTableStmt {
  AlterTableStmt { table: a.relation.as_ref().map(rangevar_to_tableref).unwrap_or_default() }
}

/// pg_query lumps DROP TABLE / VIEW / INDEX into the same DropStmt with
/// a removeType discriminator. The `objects` list holds the targets --
/// each is a List of strings forming `[schema, name]` (or just `[name]`).
fn drop_table(d: &pg_query::protobuf::DropStmt) -> DropTableStmt {
  let mut tables = Vec::new();
  for obj in &d.objects {
    if let Some(PgNode::List(list)) = obj.node.as_ref() {
      let parts: Vec<String> = list
        .items
        .iter()
        .filter_map(|n| match n.node.as_ref()? {
          PgNode::String(s) => Some(s.sval.clone()),
          _ => None,
        })
        .collect();
      let (schema, name) = match parts.as_slice() {
        [s, n] => (Some(s.clone()), n.clone()),
        [n] => (None, n.clone()),
        _ => continue,
      };
      tables.push(TableRef { schema, name, alias: None, range: TextRange::default() });
    }
  }
  DropTableStmt { tables, if_exists: d.missing_ok }
}

// ---------------------------------------------------------------------------
// helpers
// ---------------------------------------------------------------------------

fn rangevar_to_tableref(r: &RangeVar) -> TableRef {
  let schema = if r.schemaname.is_empty() { None } else { Some(r.schemaname.clone()) };
  let alias = r.alias.as_ref().map(|a| a.aliasname.clone()).filter(|n| !n.is_empty());
  // pg_query stores the source byte offset for the relation token on
  // `r.location`. Compute the name-token range by spanning that
  // start through `relname.len()`. Schema-qualified refs (`s.t`) still
  // narrow to just the relname; good enough for IDE highlighting.
  let range = if r.location >= 0 {
    let start = r.location as u32;
    let len = r.relname.len() as u32;
    TextRange::new(start.into(), (start + len).into())
  } else {
    TextRange::default()
  };
  TableRef { schema, name: r.relname.clone(), alias, range }
}

// pg_query doesn't always populate the cooked/raw expr string in a
// stable way; provide a small helper that picks the best available
// representation. Returns an empty string when nothing usable is set.
trait ConstraintExprText {
  fn raw_expr_string(&self) -> String;
}
impl ConstraintExprText for pg_query::protobuf::Constraint {
  fn raw_expr_string(&self) -> String {
    if !self.cooked_expr.is_empty() {
      return self.cooked_expr.clone();
    }
    // raw_expr is a Node; pg_query exposes deparse_protobuf to
    // round-trip a Node back to SQL but it requires the full
    // ParseResult shape. For now we return empty -- the live
    // catalog provides the authoritative default during
    // introspection.
    String::new()
  }
}