bestool-psql 1.7.1

psql-inspired client for PostgreSQL
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
use std::collections::HashSet;

use miette::Result;
use pg_query::{NodeEnum, parse};
use tracing::debug;

use crate::schema_cache::SchemaCache;

/// A tuple representing (schema, table, column)
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct ColumnRef {
	pub schema: String,
	pub table: String,
	pub column: String,
}

/// Extract column references from a SQL query
pub fn extract_column_refs(
	sql: &str,
	schema_cache: Option<&SchemaCache>,
) -> Result<Vec<ColumnRef>> {
	// Parse the SQL using pg_query
	let parse_result = match parse(sql) {
		Ok(result) => result,
		Err(e) => {
			debug!("Failed to parse SQL for column extraction: {}", e);
			return Ok(Vec::new());
		}
	};

	let mut column_refs = Vec::new();
	let mut context = ExtractionContext {
		schema_cache,
		column_refs: &mut column_refs,
		table_aliases: Default::default(),
		in_select_list: false,
	};

	// Process each statement in the parse tree
	for statement in parse_result.protobuf.stmts {
		if let Some(stmt) = statement.stmt {
			process_node(&stmt.node, &mut context);
		}
	}

	// Deduplicate while preserving order
	let mut seen = HashSet::new();
	column_refs.retain(|col_ref| seen.insert(col_ref.clone()));

	Ok(column_refs)
}

/// Best-effort derivation of the base table name for a query, for use as the target of
/// generated INSERT statements.
///
/// Returns the schema-qualified name of the leftmost `RangeVar` in the first SELECT's
/// FROM clause. Returns `None` when nothing satisfactory can be derived (no FROM clause,
/// a subquery or VALUES source, or a join whose leftmost arm is not a plain table).
pub fn derive_table_name(sql: &str) -> Option<String> {
	let parse_result = parse(sql).ok()?;

	for statement in parse_result.protobuf.stmts {
		let Some(stmt) = statement.stmt else { continue };
		if let Some(NodeEnum::SelectStmt(select)) = &stmt.node {
			let first = select.from_clause.first()?;
			return leftmost_table(&first.node);
		}
	}

	None
}

fn leftmost_table(node: &Option<NodeEnum>) -> Option<String> {
	match node.as_ref()? {
		NodeEnum::RangeVar(range) => {
			let name = if range.schemaname.is_empty() {
				range.relname.clone()
			} else {
				format!("{}.{}", range.schemaname, range.relname)
			};
			(!name.is_empty()).then_some(name)
		}
		NodeEnum::JoinExpr(join) => leftmost_table(&join.larg.as_ref()?.node),
		_ => None,
	}
}

struct ExtractionContext<'a> {
	schema_cache: Option<&'a SchemaCache>,
	column_refs: &'a mut Vec<ColumnRef>,
	table_aliases: std::collections::HashMap<String, (String, String)>,
	in_select_list: bool,
}

fn process_node(node: &Option<NodeEnum>, ctx: &mut ExtractionContext<'_>) {
	let Some(node) = node else { return };

	match node {
		NodeEnum::SelectStmt(select) => {
			// First, process FROM clause to build table aliases
			for from_item in &select.from_clause {
				if let Some(NodeEnum::RangeVar(range)) = &from_item.node {
					let table_name = range.relname.clone();
					let schema_name = if range.schemaname.is_empty() {
						if let Some(cache) = ctx.schema_cache {
							find_schema_for_table(cache, &table_name)
								.unwrap_or_else(|| "public".to_string())
						} else {
							"public".to_string()
						}
					} else {
						range.schemaname.clone()
					};

					let alias = if let Some(a) = &range.alias {
						a.aliasname.clone()
					} else {
						table_name.clone()
					};

					ctx.table_aliases.insert(alias, (schema_name, table_name));
				}
				// Process other types of FROM items (subqueries, joins, etc)
				process_from_item(&from_item.node, ctx);
			}

			// Process target list (SELECT items)
			let old_in_select_list = ctx.in_select_list;
			ctx.in_select_list = true;

			for target in &select.target_list {
				if let Some(NodeEnum::ResTarget(res)) = &target.node
					&& let Some(val) = &res.val
				{
					// Check if this is a simple ColumnRef (not a computed expression)
					if let Some(NodeEnum::ColumnRef(_)) = &val.node {
						process_node(&val.node, ctx);
					} else if let Some(NodeEnum::AStar(_)) = &val.node {
						// SELECT * - expand to all columns
						expand_wildcard(None, ctx);
					}
					// For other expressions (computed columns), we don't extract
				}
			}

			ctx.in_select_list = old_in_select_list;

			// Process WHERE clause
			if let Some(where_clause) = &select.where_clause {
				process_node(&where_clause.node, ctx);
			}

			// Process GROUP BY
			for group in &select.group_clause {
				process_node(&group.node, ctx);
			}

			// Process HAVING
			if let Some(having) = &select.having_clause {
				process_node(&having.node, ctx);
			}
		}
		NodeEnum::ColumnRef(col_ref) => {
			// Extract column reference
			process_column_ref(col_ref, ctx);
		}
		NodeEnum::AStar(_) => {
			// SELECT * or table.*
			expand_wildcard(None, ctx);
		}
		NodeEnum::RangeVar(_) => {
			// Already handled in FROM processing
		}
		NodeEnum::JoinExpr(join) => {
			// Process both sides of the join
			if let Some(larg) = &join.larg {
				process_node(&larg.node, ctx);
			}
			if let Some(rarg) = &join.rarg {
				process_node(&rarg.node, ctx);
			}
			// Process join condition
			if let Some(quals) = &join.quals {
				process_node(&quals.node, ctx);
			}
		}
		NodeEnum::AExpr(expr) => {
			// Binary/unary expressions - process operands but don't mark as direct refs
			let old_in_select_list = ctx.in_select_list;
			ctx.in_select_list = false;

			if let Some(lexpr) = &expr.lexpr {
				process_node(&lexpr.node, ctx);
			}
			if let Some(rexpr) = &expr.rexpr {
				process_node(&rexpr.node, ctx);
			}

			ctx.in_select_list = old_in_select_list;
		}
		NodeEnum::BoolExpr(expr) => {
			// Boolean expressions (AND, OR, NOT)
			for arg in &expr.args {
				process_node(&arg.node, ctx);
			}
		}
		NodeEnum::FuncCall(_) => {
			// Function calls are computed expressions, don't extract columns from them
			// even though they might reference columns
		}
		NodeEnum::SubLink(sublink) => {
			// Subquery - process it
			if let Some(subselect) = &sublink.subselect {
				process_node(&subselect.node, ctx);
			}
		}
		NodeEnum::RangeSubselect(range_sub) => {
			// Subquery in FROM clause
			if let Some(subquery) = &range_sub.subquery {
				process_node(&subquery.node, ctx);
			}
		}
		_ => {
			// For other node types, we don't need to extract columns
		}
	}
}

fn process_from_item(node: &Option<NodeEnum>, ctx: &mut ExtractionContext<'_>) {
	let Some(node) = node else { return };

	match node {
		NodeEnum::RangeVar(range) => {
			let table_name = range.relname.clone();
			let schema_name = if range.schemaname.is_empty() {
				if let Some(cache) = ctx.schema_cache {
					find_schema_for_table(cache, &table_name)
						.unwrap_or_else(|| "public".to_string())
				} else {
					"public".to_string()
				}
			} else {
				range.schemaname.clone()
			};

			let alias = if let Some(a) = &range.alias {
				a.aliasname.clone()
			} else {
				table_name.clone()
			};

			ctx.table_aliases.insert(alias, (schema_name, table_name));
		}
		NodeEnum::JoinExpr(join) => {
			if let Some(larg) = &join.larg {
				process_from_item(&larg.node, ctx);
			}
			if let Some(rarg) = &join.rarg {
				process_from_item(&rarg.node, ctx);
			}
		}
		NodeEnum::RangeSubselect(_) => {
			// Subquery - we could track this but for now skip
		}
		_ => {}
	}
}

fn process_column_ref(col_ref: &pg_query::protobuf::ColumnRef, ctx: &mut ExtractionContext<'_>) {
	let fields: Vec<String> = col_ref
		.fields
		.iter()
		.filter_map(|field| {
			if let Some(NodeEnum::String(s)) = &field.node {
				Some(s.sval.clone())
			} else if let Some(NodeEnum::AStar(_)) = &field.node {
				None // Handle * separately
			} else {
				None
			}
		})
		.collect();

	// Check if this is a wildcard (table.* or just *)
	let has_star = col_ref
		.fields
		.iter()
		.any(|field| matches!(&field.node, Some(NodeEnum::AStar(_))));

	if has_star {
		if !fields.is_empty() {
			// table.* case
			let table_name = &fields[0];
			expand_wildcard(Some(table_name), ctx);
		} else {
			// SELECT * case (unqualified wildcard)
			expand_wildcard(None, ctx);
		}
		return;
	}

	match fields.len() {
		1 => {
			// Simple column reference (no table qualifier)
			let column_name = &fields[0];

			// If there's only one table, use it
			if ctx.table_aliases.len() == 1
				&& let Some((schema, table)) = ctx.table_aliases.values().next()
			{
				ctx.column_refs.push(ColumnRef {
					schema: schema.clone(),
					table: table.clone(),
					column: column_name.clone(),
				});
			}
			// Otherwise, we can't determine which table without more analysis
		}
		2 => {
			// table.column
			let table_or_alias = &fields[0];
			let column_name = &fields[1];

			if let Some((schema, table)) = ctx.table_aliases.get(table_or_alias) {
				ctx.column_refs.push(ColumnRef {
					schema: schema.clone(),
					table: table.clone(),
					column: column_name.clone(),
				});
			}
		}
		3 => {
			// schema.table.column
			let schema = &fields[0];
			let table = &fields[1];
			let column = &fields[2];
			ctx.column_refs.push(ColumnRef {
				schema: schema.clone(),
				table: table.clone(),
				column: column.clone(),
			});
		}
		_ => {}
	}
}

fn expand_wildcard(table_qualifier: Option<&str>, ctx: &mut ExtractionContext<'_>) {
	let Some(cache) = ctx.schema_cache else {
		return;
	};

	if let Some(table_name) = table_qualifier {
		// Expand table.*
		if let Some((schema, table)) = ctx.table_aliases.get(table_name)
			&& let Some(columns) = cache.columns_for_table(table)
		{
			for column in columns {
				ctx.column_refs.push(ColumnRef {
					schema: schema.clone(),
					table: table.clone(),
					column: column.clone(),
				});
			}
		}
	} else {
		// Expand * - all columns from all tables
		for (schema, table) in ctx.table_aliases.values() {
			if let Some(columns) = cache.columns_for_table(table) {
				for column in columns {
					ctx.column_refs.push(ColumnRef {
						schema: schema.clone(),
						table: table.clone(),
						column: column.clone(),
					});
				}
			}
		}
	}
}

fn find_schema_for_table(cache: &SchemaCache, table: &str) -> Option<String> {
	// First check if it exists in public schema
	if cache
		.tables
		.get("public")
		.is_some_and(|tables| tables.contains(&table.to_string()))
	{
		return Some("public".to_string());
	}

	// Check other schemas
	for (schema_name, tables) in &cache.tables {
		if tables.contains(&table.to_string()) {
			return Some(schema_name.clone());
		}
	}

	None
}

#[cfg(test)]
mod tests {
	use super::*;

	fn create_test_cache() -> SchemaCache {
		let mut cache = SchemaCache::new();
		cache
			.tables
			.insert("public".to_string(), vec!["patient".to_string()]);
		cache.columns.insert(
			"public.patient".to_string(),
			vec!["foo".to_string(), "bar".to_string(), "baz".to_string()],
		);
		cache.columns.insert(
			"patient".to_string(),
			vec!["foo".to_string(), "bar".to_string(), "baz".to_string()],
		);
		cache
	}

	#[test]
	fn test_parse_structure() {
		let sql = "SELECT * FROM patient";
		let result = pg_query::parse(sql).unwrap();

		// Print the structure to understand it
		for stmt in &result.protobuf.stmts {
			if let Some(s) = &stmt.stmt
				&& let Some(pg_query::NodeEnum::SelectStmt(select)) = &s.node
			{
				eprintln!("Target list length: {}", select.target_list.len());
				for (i, target) in select.target_list.iter().enumerate() {
					eprintln!("Target {}: {:?}", i, target.node);
				}
			}
		}
	}

	#[test]
	fn test_simple_select() {
		let cache = create_test_cache();
		let sql = "SELECT foo, bar FROM patient WHERE bar = 123";
		let refs = extract_column_refs(sql, Some(&cache)).unwrap();

		assert_eq!(refs.len(), 2);
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "foo".into()
		}));
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "bar".into()
		}));
	}

	#[test]
	fn test_select_with_expression() {
		let cache = create_test_cache();
		let sql = "SELECT bar, foo + 2 FROM patient";
		let refs = extract_column_refs(sql, Some(&cache)).unwrap();

		// Should only return 'bar', not 'foo' because it's part of an expression
		assert_eq!(refs.len(), 1);
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "bar".into()
		}));
	}

	#[test]
	fn test_select_star() {
		let cache = create_test_cache();
		let sql = "SELECT * FROM patient";
		let refs = extract_column_refs(sql, Some(&cache)).unwrap();

		assert_eq!(refs.len(), 3);
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "foo".into()
		}));
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "bar".into()
		}));
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "baz".into()
		}));
	}

	#[test]
	fn test_derive_table_name() {
		assert_eq!(
			derive_table_name("SELECT * FROM patients"),
			Some("patients".to_string())
		);
		assert_eq!(
			derive_table_name("SELECT id, name FROM public.patients p WHERE id > 1"),
			Some("public.patients".to_string())
		);
		// Leftmost table of a join.
		assert_eq!(
			derive_table_name("SELECT * FROM patients p JOIN visits v ON v.pid = p.id"),
			Some("patients".to_string())
		);
		// No satisfactory base table.
		assert_eq!(derive_table_name("SELECT 1"), None);
		assert_eq!(derive_table_name("SELECT * FROM (SELECT 1) AS sub"), None);
		assert_eq!(
			derive_table_name("SELECT * FROM (VALUES (1), (2)) AS t(v)"),
			None
		);
		assert_eq!(derive_table_name("not valid sql"), None);
	}

	#[test]
	fn test_select_qualified_columns() {
		let cache = create_test_cache();
		let sql = "SELECT patient.foo, patient.bar FROM patient";
		let refs = extract_column_refs(sql, Some(&cache)).unwrap();

		assert_eq!(refs.len(), 2);
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "foo".into()
		}));
		assert!(refs.contains(&ColumnRef {
			schema: "public".into(),
			table: "patient".into(),
			column: "bar".into()
		}));
	}
}