1use crate::binder;
2use crate::builtins::parse_builtins;
3use crate::db::{File, parse};
4use crate::offsets::token_from_offset;
5use crate::resolve;
6use rowan::{TextRange, TextSize};
7use salsa::Database as Db;
8use smallvec::{SmallVec, smallvec};
9use squawk_syntax::{
10 SyntaxKind,
11 ast::{self, AstNode},
12};
13
14#[salsa::tracked]
15pub fn goto_definition(db: &dyn Db, file: File, offset: TextSize) -> SmallVec<[Location; 1]> {
16 let parse = parse(db, file);
17 let source_file = &parse.tree();
18 let Some(token) = token_from_offset(source_file, offset) else {
19 return smallvec![];
20 };
21 let Some(parent) = token.parent() else {
22 return smallvec![];
23 };
24
25 if (token.kind() == SyntaxKind::WHEN_KW && parent.kind() == SyntaxKind::WHEN_CLAUSE)
27 || (token.kind() == SyntaxKind::ELSE_KW && parent.kind() == SyntaxKind::ELSE_CLAUSE)
28 || (token.kind() == SyntaxKind::END_KW && parent.kind() == SyntaxKind::CASE_EXPR)
29 {
30 for parent in token.parent_ancestors() {
31 if let Some(case_expr) = ast::CaseExpr::cast(parent)
32 && let Some(case_token) = case_expr.case_token()
33 {
34 return smallvec![Location::range(case_token.text_range())];
35 }
36 }
37 }
38
39 if ast::Commit::can_cast(parent.kind())
41 && let Some(begin_range) = find_preceding_begin(source_file, token.text_range().start())
42 {
43 return smallvec![Location::range(begin_range)];
44 }
45
46 if ast::Rollback::can_cast(parent.kind())
48 && let Some(begin_range) = find_preceding_begin(source_file, token.text_range().start())
49 {
50 return smallvec![Location::range(begin_range)];
51 }
52
53 if ast::Begin::can_cast(parent.kind())
55 && let Some(end_range) =
56 find_following_commit_or_rollback(source_file, token.text_range().end())
57 {
58 return smallvec![Location::range(end_range)];
59 }
60
61 if let Some(name) = ast::Name::cast(parent.clone()) {
62 return smallvec![Location::range(name.syntax().text_range())];
63 }
64
65 if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
66 for file_id in [FileId::Current, FileId::Builtins] {
67 let file = match file_id {
68 FileId::Current => source_file,
69 FileId::Builtins => &parse_builtins(db).tree(),
70 };
71 let binder_output = binder::bind(file);
73 let root = file.syntax();
74 if let Some(ptrs) = resolve::resolve_name_ref_ptrs(&binder_output, root, &name_ref) {
75 let ranges = ptrs
76 .iter()
77 .map(|ptr| ptr.to_node(file.syntax()).text_range())
78 .map(|range| Location {
79 file: file_id,
80 range,
81 })
82 .collect();
83 return ranges;
84 }
85 }
86 }
87
88 let type_node = ast::Type::cast(parent.clone()).or_else(|| {
89 if ast::Timezone::can_cast(parent.kind()) {
91 parent.parent().and_then(ast::Type::cast)
92 } else {
93 None
94 }
95 });
96 if let Some(ty) = type_node {
97 for file_id in [FileId::Current, FileId::Builtins] {
98 let file = match file_id {
99 FileId::Current => source_file,
100 FileId::Builtins => &parse_builtins(db).tree(),
101 };
102 let binder_output = binder::bind(file);
104 let position = token.text_range().start();
105 if let Some(ptr) = resolve::resolve_type_ptr_from_type(&binder_output, &ty, position) {
106 return smallvec![Location {
107 file: file_id,
108 range: ptr.to_node(file.syntax()).text_range(),
109 }];
110 }
111 }
112 }
113
114 smallvec![]
115}
116
117#[derive(Debug, PartialEq, Clone, Copy, Eq, PartialOrd, Ord)]
118pub enum FileId {
119 Current,
120 Builtins,
121}
122
123#[derive(Debug, Clone, PartialEq, Eq)]
124pub struct Location {
125 pub file: FileId,
126 pub range: TextRange,
127}
128
129impl Location {
130 fn range(range: TextRange) -> Location {
131 Location {
132 file: FileId::Current,
133 range,
134 }
135 }
136}
137
138fn find_preceding_begin(file: &ast::SourceFile, before: TextSize) -> Option<TextRange> {
139 let mut last_begin: Option<TextRange> = None;
140 for stmt in file.stmts() {
141 if let ast::Stmt::Begin(begin) = stmt {
142 let range = begin.syntax().text_range();
143 if range.end() <= before {
144 last_begin = Some(range);
145 }
146 }
147 }
148 last_begin
149}
150
151fn find_following_commit_or_rollback(file: &ast::SourceFile, after: TextSize) -> Option<TextRange> {
152 for stmt in file.stmts() {
153 let range = match &stmt {
154 ast::Stmt::Commit(commit) => commit.syntax().text_range(),
155 ast::Stmt::Rollback(rollback) => rollback.syntax().text_range(),
156 _ => continue,
157 };
158 if range.start() >= after {
159 return Some(range);
160 }
161 }
162 None
163}
164
165#[cfg(test)]
166mod test {
167 use crate::builtins::BUILTINS_SQL;
168 use crate::db::{Database, File};
169 use crate::goto_definition::{FileId, goto_definition};
170 use crate::test_utils::fixture;
171 use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
172 use insta::assert_snapshot;
173 use log::info;
174 use rowan::TextRange;
175
176 #[track_caller]
177 fn goto(sql: &str) -> String {
178 goto_(sql).expect("should always find a definition")
179 }
180
181 #[track_caller]
182 fn goto_(sql: &str) -> Option<String> {
183 info!("starting");
184 let (mut offset, sql) = fixture(sql);
185 offset = offset.checked_sub(1.into()).unwrap_or_default();
188 let db = Database::default();
189 let file = File::new(&db, sql.clone().into());
190 assert_eq!(crate::db::parse(&db, file).errors(), vec![]);
191 let results = goto_definition(&db, file, offset);
192 if !results.is_empty() {
193 let offset: usize = offset.into();
194 let mut current_dests = vec![];
195 let mut builtin_dests = vec![];
196 for (i, location) in results.iter().enumerate() {
197 let label_index = i + 2;
198 match location.file {
199 FileId::Current => current_dests.push((label_index, location.range)),
200 FileId::Builtins => builtin_dests.push((label_index, location.range)),
201 }
202 }
203
204 let has_builtins = !builtin_dests.is_empty();
205
206 let mut snippet = Snippet::source(&sql).fold(true);
207 if has_builtins {
208 snippet = snippet.path("current.sql");
210 } else {
211 snippet = annotate_destinations(snippet, current_dests);
212 }
213 snippet = snippet.annotation(
214 AnnotationKind::Context
215 .span(offset..offset + 1)
216 .label("1. source"),
217 );
218
219 let mut groups = vec![Level::INFO.primary_title("definition").element(snippet)];
220
221 if has_builtins {
222 let builtins_snippet = Snippet::source(BUILTINS_SQL).path("builtin.sql").fold(true);
223 let builtins_snippet = annotate_destinations(builtins_snippet, builtin_dests);
224 groups.push(
225 Level::INFO
226 .primary_title("definition")
227 .element(builtins_snippet),
228 );
229 }
230
231 let renderer = Renderer::plain().decor_style(DecorStyle::Unicode);
232 return Some(
233 renderer
234 .render(&groups)
235 .to_string()
236 .replace("info: definition", ""),
238 );
239 }
240 None
241 }
242
243 fn goto_not_found(sql: &str) {
244 assert!(goto_(sql).is_none(), "Should not find a definition");
245 }
246
247 fn annotate_destinations<'a>(
248 mut snippet: Snippet<'a, annotate_snippets::Annotation<'a>>,
249 destinations: Vec<(usize, TextRange)>,
250 ) -> Snippet<'a, annotate_snippets::Annotation<'a>> {
251 for (label_index, range) in destinations {
252 snippet = snippet.annotation(
253 AnnotationKind::Context
254 .span(range.into())
255 .label(format!("{}. destination", label_index)),
256 );
257 }
258
259 snippet
260 }
261
262 #[test]
263 fn goto_case_when() {
264 assert_snapshot!(goto("
265select case when$0 x > 1 then 1 else 2 end;
266"), @r"
267 ╭▸
268 2 │ select case when x > 1 then 1 else 2 end;
269 │ ┬─── ─ 1. source
270 │ │
271 ╰╴ 2. destination
272 ");
273 }
274
275 #[test]
276 fn goto_case_else() {
277 assert_snapshot!(goto("
278select case when x > 1 then 1 else$0 2 end;
279"), @r"
280 ╭▸
281 2 │ select case when x > 1 then 1 else 2 end;
282 ╰╴ ──── 2. destination ─ 1. source
283 ");
284 }
285
286 #[test]
287 fn goto_case_end() {
288 assert_snapshot!(goto("
289select case when x > 1 then 1 else 2 end$0;
290"), @r"
291 ╭▸
292 2 │ select case when x > 1 then 1 else 2 end;
293 ╰╴ ──── 2. destination ─ 1. source
294 ");
295 }
296
297 #[test]
298 fn goto_case_end_trailing_semi() {
299 assert_snapshot!(goto("
300select case when x > 1 then 1 else 2 end;$0
301"), @r"
302 ╭▸
303 2 │ select case when x > 1 then 1 else 2 end;
304 ╰╴ ──── 2. destination ─ 1. source
305 ");
306 }
307
308 #[test]
309 fn goto_case_then_not_found() {
310 goto_not_found(
311 "
312select case when x > 1 then$0 1 else 2 end;
313",
314 )
315 }
316
317 #[test]
318 fn rollback_to_begin() {
319 assert_snapshot!(goto(
320 "
321begin;
322select 1;
323rollback$0;
324",
325 ), @r"
326 ╭▸
327 2 │ begin;
328 │ ───── 2. destination
329 3 │ select 1;
330 4 │ rollback;
331 ╰╴ ─ 1. source
332 ");
333 }
334
335 #[test]
336 fn goto_drop_table() {
337 assert_snapshot!(goto("
338create table t();
339drop table t$0;
340"), @r"
341 ╭▸
342 2 │ create table t();
343 │ ─ 2. destination
344 3 │ drop table t;
345 ╰╴ ─ 1. source
346 ");
347 }
348
349 #[test]
350 fn goto_drop_foreign_table() {
351 assert_snapshot!(goto("
352create foreign table t(a int) server s;
353drop foreign table t$0;
354"), @r"
355 ╭▸
356 2 │ create foreign table t(a int) server s;
357 │ ─ 2. destination
358 3 │ drop foreign table t;
359 ╰╴ ─ 1. source
360 ");
361 }
362
363 #[test]
364 fn goto_definition_prefers_previous_token() {
365 assert_snapshot!(goto("
366create table t(a int);
367select t.$0a from t;
368"), @r"
369 ╭▸
370 2 │ create table t(a int);
371 │ ─ 2. destination
372 3 │ select t.a from t;
373 ╰╴ ─ 1. source
374 ");
375
376 assert_snapshot!(goto("
377create type ty as (a int, b int);
378with t as (select '(1,2)'::ty c)
379select (c)$0.a from t;
380"), @r"
381 ╭▸
382 3 │ with t as (select '(1,2)'::ty c)
383 │ ─ 2. destination
384 4 │ select (c).a from t;
385 ╰╴ ─ 1. source
386 ");
387 assert_snapshot!(goto("
388create function f() returns int as 'select 1' language sql;
389select f($0);
390"), @r"
391 ╭▸
392 2 │ create function f() returns int as 'select 1' language sql;
393 │ ─ 2. destination
394 3 │ select f();
395 ╰╴ ─ 1. source
396 ");
397
398 assert_snapshot!(goto("
399with t as (select array[1,2,3]::int[] c)
400select c[$01] from t;
401"), @r"
402 ╭▸
403 2 │ with t as (select array[1,2,3]::int[] c)
404 │ ─ 2. destination
405 3 │ select c[1] from t;
406 ╰╴ ─ 1. source
407 ");
408
409 assert_snapshot!(goto("
410with t as (select array[1,2,3]::int[] c, 1 b)
411select c[b]$0 from t;
412"), @r"
413 ╭▸
414 2 │ with t as (select array[1,2,3]::int[] c, 1 b)
415 │ ─ 2. destination
416 3 │ select c[b] from t;
417 ╰╴ ─ 1. source
418 ");
419 }
420
421 #[test]
422 fn goto_fetch_cursor() {
423 assert_snapshot!(goto("
424declare c scroll cursor for select * from t;
425fetch forward 5 from c$0;
426"), @r"
427 ╭▸
428 2 │ declare c scroll cursor for select * from t;
429 │ ─ 2. destination
430 3 │ fetch forward 5 from c;
431 ╰╴ ─ 1. source
432 ");
433 }
434
435 #[test]
436 fn goto_close_cursor() {
437 assert_snapshot!(goto("
438declare c scroll cursor for select * from t;
439close c$0;
440"), @r"
441 ╭▸
442 2 │ declare c scroll cursor for select * from t;
443 │ ─ 2. destination
444 3 │ close c;
445 ╰╴ ─ 1. source
446 ");
447 }
448
449 #[test]
450 fn goto_move_cursor() {
451 assert_snapshot!(goto("
452declare c scroll cursor for select * from t;
453move forward 10 from c$0;
454"), @r"
455 ╭▸
456 2 │ declare c scroll cursor for select * from t;
457 │ ─ 2. destination
458 3 │ move forward 10 from c;
459 ╰╴ ─ 1. source
460 ");
461 }
462
463 #[test]
464 fn goto_execute_prepared_statement() {
465 assert_snapshot!(goto("
466prepare stmt as select 1;
467execute stmt$0;
468"), @r"
469 ╭▸
470 2 │ prepare stmt as select 1;
471 │ ──── 2. destination
472 3 │ execute stmt;
473 ╰╴ ─ 1. source
474 ");
475 }
476
477 #[test]
478 fn goto_deallocate_prepared_statement() {
479 assert_snapshot!(goto("
480prepare stmt as select 1;
481deallocate stmt$0;
482"), @r"
483 ╭▸
484 2 │ prepare stmt as select 1;
485 │ ──── 2. destination
486 3 │ deallocate stmt;
487 ╰╴ ─ 1. source
488 ");
489 }
490
491 #[test]
492 fn goto_notify_channel() {
493 assert_snapshot!(goto("
494listen updates;
495notify updates$0;
496"), @r"
497 ╭▸
498 2 │ listen updates;
499 │ ─────── 2. destination
500 3 │ notify updates;
501 ╰╴ ─ 1. source
502 ");
503 }
504
505 #[test]
506 fn goto_unlisten_channel() {
507 assert_snapshot!(goto("
508listen updates;
509unlisten updates$0;
510"), @r"
511 ╭▸
512 2 │ listen updates;
513 │ ─────── 2. destination
514 3 │ unlisten updates;
515 ╰╴ ─ 1. source
516 ");
517 }
518
519 #[test]
520 fn goto_delete_where_current_of_cursor() {
521 assert_snapshot!(goto("
522declare c scroll cursor for select * from t;
523delete from t where current of c$0;
524"), @r"
525 ╭▸
526 2 │ declare c scroll cursor for select * from t;
527 │ ─ 2. destination
528 3 │ delete from t where current of c;
529 ╰╴ ─ 1. source
530 ");
531 }
532
533 #[test]
534 fn goto_update_where_current_of_cursor() {
535 assert_snapshot!(goto("
536declare c scroll cursor for select * from t;
537update t set a = a + 10 where current of c$0;
538"), @r"
539 ╭▸
540 2 │ declare c scroll cursor for select * from t;
541 │ ─ 2. destination
542 3 │ update t set a = a + 10 where current of c;
543 ╰╴ ─ 1. source
544 ");
545 }
546
547 #[test]
548 fn goto_with_table_star() {
549 assert_snapshot!(goto("
550with t as (select 1 a)
551select t$0.* from t;
552"), @r"
553 ╭▸
554 2 │ with t as (select 1 a)
555 │ ─ 2. destination
556 3 │ select t.* from t;
557 ╰╴ ─ 1. source
558 ");
559 }
560
561 #[test]
562 fn goto_cross_join_func_column() {
563 assert_snapshot!(goto(r#"
564with t(x) as (select $$[{"a":1,"b":2}]$$::json)
565select * from t, json_to_recordset(x$0) as r(a int, b int);
566"#), @r#"
567 ╭▸
568 2 │ with t(x) as (select $$[{"a":1,"b":2}]$$::json)
569 │ ─ 2. destination
570 3 │ select * from t, json_to_recordset(x) as r(a int, b int);
571 ╰╴ ─ 1. source
572 "#);
573 }
574
575 #[test]
576 fn goto_cross_join_func_qualified_column_table() {
577 assert_snapshot!(goto(r#"
578with t(x) as (select $$[{"a":1,"b":2}]$$::json)
579select * from t, json_to_recordset(t$0.x) as r(a int, b int);
580"#), @r#"
581 ╭▸
582 2 │ with t(x) as (select $$[{"a":1,"b":2}]$$::json)
583 │ ─ 2. destination
584 3 │ select * from t, json_to_recordset(t.x) as r(a int, b int);
585 ╰╴ ─ 1. source
586 "#);
587 }
588
589 #[test]
590 fn goto_cross_join_func_qualified_column_field() {
591 assert_snapshot!(goto(r#"
592with t(x) as (select $$[{"a":1,"b":2}]$$::json)
593select * from t, json_to_recordset(t.x$0) as r(a int, b int);
594"#), @r#"
595 ╭▸
596 2 │ with t(x) as (select $$[{"a":1,"b":2}]$$::json)
597 │ ─ 2. destination
598 3 │ select * from t, json_to_recordset(t.x) as r(a int, b int);
599 ╰╴ ─ 1. source
600 "#);
601 }
602
603 #[test]
604 fn goto_lateral_values_alias_in_subquery() {
605 assert_snapshot!(goto("
606select u.n, x.val
607from (values (1), (2)) u(n)
608cross join lateral (select u$0.n * 10 as val) x;
609"), @r"
610 ╭▸
611 3 │ from (values (1), (2)) u(n)
612 │ ─ 2. destination
613 4 │ cross join lateral (select u.n * 10 as val) x;
614 ╰╴ ─ 1. source
615 ");
616 }
617
618 #[test]
619 fn goto_lateral_missing_not_found() {
620 goto_not_found(
626 "
627select u.n, x.val
628from (values (1), (2)) u(n)
629cross join (select u$0.n * 10 as val) x;
630",
631 );
632 }
633
634 #[test]
635 fn goto_lateral_deeply_nested_paren_expr_values_alias_in_subquery() {
636 assert_snapshot!(goto("
637select u.n, x.val
638from (values (1), (2)) u(n)
639cross join lateral ((((select u$0.n * 10 as val)))) x;
640"), @r"
641 ╭▸
642 3 │ from (values (1), (2)) u(n)
643 │ ─ 2. destination
644 4 │ cross join lateral ((((select u.n * 10 as val)))) x;
645 ╰╴ ─ 1. source
646 ");
647 }
648
649 #[test]
650 fn goto_lateral_deeply_nested_paren_expr_values_alias_column() {
651 assert_snapshot!(goto("
652select u.n, x.val$0
653from (values (1), (2)) u(n)
654cross join lateral ((((select u.n * 10 as val)))) x;
655"), @r"
656 ╭▸
657 2 │ select u.n, x.val
658 │ ─ 1. source
659 3 │ from (values (1), (2)) u(n)
660 4 │ cross join lateral ((((select u.n * 10 as val)))) x;
661 ╰╴ ─── 2. destination
662 ");
663 }
664
665 #[test]
666 fn goto_lateral_deeply_nested_paren_expr_missing_not_found() {
667 goto_not_found(
673 "
674select u.n, x.val
675from (values (1), (2)) u(n)
676cross join ((((select u$0.n * 10 as val)))) x;
677",
678 );
679 }
680
681 #[test]
682 fn goto_drop_sequence() {
683 assert_snapshot!(goto("
684create sequence s;
685drop sequence s$0;
686"), @r"
687 ╭▸
688 2 │ create sequence s;
689 │ ─ 2. destination
690 3 │ drop sequence s;
691 ╰╴ ─ 1. source
692 ");
693 }
694
695 #[test]
696 fn goto_drop_trigger() {
697 assert_snapshot!(goto("
698create trigger tr before insert on t for each row execute function f();
699drop trigger tr$0 on t;
700"), @r"
701 ╭▸
702 2 │ create trigger tr before insert on t for each row execute function f();
703 │ ── 2. destination
704 3 │ drop trigger tr on t;
705 ╰╴ ─ 1. source
706 ");
707 }
708
709 #[test]
710 fn goto_drop_policy() {
711 assert_snapshot!(goto("
712create table t(c int);
713create table u(c int);
714create policy p on t;
715create policy p on u;
716drop policy if exists p$0 on t;
717"), @r"
718 ╭▸
719 4 │ create policy p on t;
720 │ ─ 2. destination
721 5 │ create policy p on u;
722 6 │ drop policy if exists p on t;
723 ╰╴ ─ 1. source
724 ");
725 }
726
727 #[test]
728 fn goto_alter_policy() {
729 assert_snapshot!(goto("
730create table t(c int);
731create policy p on t;
732alter policy p$0 on t
733 with check (c > 1);
734"), @r"
735 ╭▸
736 3 │ create policy p on t;
737 │ ─ 2. destination
738 4 │ alter policy p on t
739 ╰╴ ─ 1. source
740 ");
741 }
742
743 #[test]
744 fn goto_alter_policy_column() {
745 assert_snapshot!(goto("
746create table t(c int);
747create policy p on t;
748alter policy p on t
749 with check (c$0 > 1);
750"), @r"
751 ╭▸
752 3 │ create policy p on t;
753 │ ─ 2. destination
754 4 │ alter policy p on t
755 5 │ with check (c > 1);
756 ╰╴ ─ 1. source
757 ");
758 }
759
760 #[test]
761 fn goto_create_policy_column() {
762 assert_snapshot!(goto("
763create table t(c int, d int);
764create policy p on t
765 with check (c$0 > d);
766"), @r"
767 ╭▸
768 2 │ create table t(c int, d int);
769 │ ─ 2. destination
770 3 │ create policy p on t
771 4 │ with check (c > d);
772 ╰╴ ─ 1. source
773 ");
774 }
775
776 #[test]
777 fn goto_create_policy_using_column() {
778 assert_snapshot!(goto("
779create table t(c int, d int);
780create policy p on t
781 using (c$0 > d and 1 < 2);
782"), @r"
783 ╭▸
784 2 │ create table t(c int, d int);
785 │ ─ 2. destination
786 3 │ create policy p on t
787 4 │ using (c > d and 1 < 2);
788 ╰╴ ─ 1. source
789 ");
790 }
791
792 #[test]
793 fn goto_create_policy_qualified_column_table() {
794 assert_snapshot!(goto("
795create table t(c int, d int);
796create policy p on t
797 with check (t$0.c > d);
798"), @r"
799 ╭▸
800 2 │ create table t(c int, d int);
801 │ ─ 2. destination
802 3 │ create policy p on t
803 4 │ with check (t.c > d);
804 ╰╴ ─ 1. source
805 ");
806 }
807
808 #[test]
809 fn goto_create_policy_qualified_column() {
810 assert_snapshot!(goto("
811create table t(c int, d int);
812create policy p on t
813 with check (t.c$0 > d);
814"), @r"
815 ╭▸
816 2 │ create table t(c int, d int);
817 │ ─ 2. destination
818 3 │ create policy p on t
819 4 │ with check (t.c > d);
820 ╰╴ ─ 1. source
821 ");
822 }
823
824 #[test]
825 fn goto_create_policy_field_style_function_call() {
826 assert_snapshot!(goto("
827create table t(c int);
828create function x(t) returns int8
829 as 'select 1'
830 language sql;
831create policy p on t
832 with check (t.c > 1 and t.x$0 > 0);
833"), @r"
834 ╭▸
835 3 │ create function x(t) returns int8
836 │ ─ 2. destination
837 ‡
838 7 │ with check (t.c > 1 and t.x > 0);
839 ╰╴ ─ 1. source
840 ");
841 }
842
843 #[test]
844 fn goto_alter_policy_qualified_column_table() {
845 assert_snapshot!(goto("
846create table t(c int, d int);
847alter policy p on t
848 with check (t$0.c > d);
849"), @r"
850 ╭▸
851 2 │ create table t(c int, d int);
852 │ ─ 2. destination
853 3 │ alter policy p on t
854 4 │ with check (t.c > d);
855 ╰╴ ─ 1. source
856 ");
857 }
858
859 #[test]
860 fn goto_alter_policy_qualified_column() {
861 assert_snapshot!(goto("
862create table t(c int, d int);
863alter policy p on t
864 with check (t.c$0 > d);
865"), @r"
866 ╭▸
867 2 │ create table t(c int, d int);
868 │ ─ 2. destination
869 3 │ alter policy p on t
870 4 │ with check (t.c > d);
871 ╰╴ ─ 1. source
872 ");
873 }
874
875 #[test]
876 fn goto_builtin_now() {
877 assert_snapshot!(goto("
878select now$0();
879"), @"
880 ╭▸ current.sql:2:10
881 │
882 2 │ select now();
883 │ ─ 1. source
884 ╰╴
885
886 ╭▸ builtin.sql:11089:28
887 │
888 11089 │ create function pg_catalog.now() returns timestamp with time zone
889 ╰╴ ─── 2. destination
890 ");
891 }
892
893 #[test]
894 fn goto_current_timestamp() {
895 assert_snapshot!(goto("
896select current_timestamp$0;
897"), @"
898 ╭▸ current.sql:2:24
899 │
900 2 │ select current_timestamp;
901 │ ─ 1. source
902 ╰╴
903
904 ╭▸ builtin.sql:11089:28
905 │
906 11089 │ create function pg_catalog.now() returns timestamp with time zone
907 ╰╴ ─── 2. destination
908 ");
909 }
910
911 #[test]
912 fn goto_current_timestamp_cte_column() {
913 assert_snapshot!(goto("
914with t as (select 1 current_timestamp)
915select current_timestamp$0 from t;
916"), @r"
917 ╭▸
918 2 │ with t as (select 1 current_timestamp)
919 │ ───────────────── 2. destination
920 3 │ select current_timestamp from t;
921 ╰╴ ─ 1. source
922 ");
923 }
924
925 #[test]
926 fn goto_current_timestamp_in_where() {
927 assert_snapshot!(goto("
928create table t(created_at timestamptz);
929select * from t where current_timestamp$0 > t.created_at;
930"), @"
931 ╭▸ current.sql:3:39
932 │
933 3 │ select * from t where current_timestamp > t.created_at;
934 │ ─ 1. source
935 ╰╴
936
937 ╭▸ builtin.sql:11089:28
938 │
939 11089 │ create function pg_catalog.now() returns timestamp with time zone
940 ╰╴ ─── 2. destination
941 ");
942 }
943
944 #[test]
945 fn goto_create_policy_schema_qualified_table() {
946 assert_snapshot!(goto("
947create schema foo;
948create table foo.t(c int);
949create policy p on foo.t
950 with check (foo.t$0.c > 1);
951"), @r"
952 ╭▸
953 3 │ create table foo.t(c int);
954 │ ─ 2. destination
955 4 │ create policy p on foo.t
956 5 │ with check (foo.t.c > 1);
957 ╰╴ ─ 1. source
958 ");
959 }
960
961 #[test]
962 fn goto_create_policy_unqualified_table_with_schema_on_table() {
963 assert_snapshot!(goto("
964create schema foo;
965create table foo.t(c int);
966create policy p on foo.t
967 with check (t$0.c > 1);
968"), @r"
969 ╭▸
970 3 │ create table foo.t(c int);
971 │ ─ 2. destination
972 4 │ create policy p on foo.t
973 5 │ with check (t.c > 1);
974 ╰╴ ─ 1. source
975 ");
976 }
977
978 #[test]
979 fn goto_drop_event_trigger() {
980 assert_snapshot!(goto("
981create event trigger et on ddl_command_start execute function f();
982drop event trigger et$0;
983"), @r"
984 ╭▸
985 2 │ create event trigger et on ddl_command_start execute function f();
986 │ ── 2. destination
987 3 │ drop event trigger et;
988 ╰╴ ─ 1. source
989 ");
990 }
991
992 #[test]
993 fn goto_alter_event_trigger() {
994 assert_snapshot!(goto("
995create event trigger et on ddl_command_start execute function f();
996alter event trigger et$0 disable;
997"), @r"
998 ╭▸
999 2 │ create event trigger et on ddl_command_start execute function f();
1000 │ ── 2. destination
1001 3 │ alter event trigger et disable;
1002 ╰╴ ─ 1. source
1003 ");
1004 }
1005
1006 #[test]
1007 fn goto_create_event_trigger_function() {
1008 assert_snapshot!(goto("
1009create function f() returns event_trigger as 'select 1' language sql;
1010create event trigger et on ddl_command_start execute function f$0();
1011"), @r"
1012 ╭▸
1013 2 │ create function f() returns event_trigger as 'select 1' language sql;
1014 │ ─ 2. destination
1015 3 │ create event trigger et on ddl_command_start execute function f();
1016 ╰╴ ─ 1. source
1017 ");
1018 }
1019
1020 #[test]
1021 fn goto_create_event_trigger_procedure() {
1022 assert_snapshot!(goto("
1023create procedure p() language sql as 'select 1';
1024create event trigger tr
1025 on ddl_command_end
1026 execute procedure p$0();
1027"), @r"
1028 ╭▸
1029 2 │ create procedure p() language sql as 'select 1';
1030 │ ─ 2. destination
1031 ‡
1032 5 │ execute procedure p();
1033 ╰╴ ─ 1. source
1034 ");
1035 }
1036
1037 #[test]
1038 fn goto_create_trigger_function() {
1039 assert_snapshot!(goto("
1040create function f() returns trigger as 'select 1' language sql;
1041create trigger tr before insert on t for each row execute function f$0();
1042"), @r"
1043 ╭▸
1044 2 │ create function f() returns trigger as 'select 1' language sql;
1045 │ ─ 2. destination
1046 3 │ create trigger tr before insert on t for each row execute function f();
1047 ╰╴ ─ 1. source
1048 ");
1049 }
1050
1051 #[test]
1052 fn goto_create_trigger_procedure() {
1053 assert_snapshot!(goto("
1054create procedure a() language sql as 'select 1';
1055create trigger tr before truncate or delete or insert
1056on t
1057execute procedure a$0();
1058"), @r"
1059 ╭▸
1060 2 │ create procedure a() language sql as 'select 1';
1061 │ ─ 2. destination
1062 ‡
1063 5 │ execute procedure a();
1064 ╰╴ ─ 1. source
1065 ");
1066 }
1067
1068 #[test]
1069 fn goto_drop_trigger_table_specific() {
1070 assert_snapshot!(goto("
1071create table u(a int);
1072create trigger tr before truncate
1073on u
1074execute function noop();
1075
1076create table t(b int);
1077create trigger tr before truncate
1078on t
1079execute function noop();
1080
1081drop trigger tr$0 on t;
1082"), @r"
1083 ╭▸
1084 8 │ create trigger tr before truncate
1085 │ ── 2. destination
1086 ‡
1087 12 │ drop trigger tr on t;
1088 ╰╴ ─ 1. source
1089 ");
1090 }
1091
1092 #[test]
1093 fn goto_create_trigger_table() {
1094 assert_snapshot!(goto("
1095create table t(b int);
1096create trigger tr before truncate
1097on t$0
1098execute function noop();
1099"), @r"
1100 ╭▸
1101 2 │ create table t(b int);
1102 │ ─ 2. destination
1103 3 │ create trigger tr before truncate
1104 4 │ on t
1105 ╰╴ ─ 1. source
1106 ");
1107 }
1108
1109 #[test]
1110 fn goto_create_sequence_owned_by() {
1111 assert_snapshot!(goto("
1112create table t(c serial);
1113create sequence s
1114 owned by t.c$0;
1115"), @r"
1116 ╭▸
1117 2 │ create table t(c serial);
1118 │ ─ 2. destination
1119 3 │ create sequence s
1120 4 │ owned by t.c;
1121 ╰╴ ─ 1. source
1122 ");
1123 }
1124
1125 #[test]
1126 fn goto_drop_tablespace() {
1127 assert_snapshot!(goto("
1128create tablespace ts location '/tmp/ts';
1129drop tablespace ts$0;
1130"), @r"
1131 ╭▸
1132 2 │ create tablespace ts location '/tmp/ts';
1133 │ ── 2. destination
1134 3 │ drop tablespace ts;
1135 ╰╴ ─ 1. source
1136 ");
1137 }
1138
1139 #[test]
1140 fn goto_create_table_tablespace() {
1141 assert_snapshot!(goto("
1142create tablespace bar location '/tmp/ts';
1143create table t (a int) tablespace b$0ar;
1144"), @r"
1145 ╭▸
1146 2 │ create tablespace bar location '/tmp/ts';
1147 │ ─── 2. destination
1148 3 │ create table t (a int) tablespace bar;
1149 ╰╴ ─ 1. source
1150 ");
1151 }
1152
1153 #[test]
1154 fn goto_drop_database() {
1155 assert_snapshot!(goto("
1156create database mydb;
1157drop database my$0db;
1158"), @r"
1159 ╭▸
1160 2 │ create database mydb;
1161 │ ──── 2. destination
1162 3 │ drop database mydb;
1163 ╰╴ ─ 1. source
1164 ");
1165 }
1166
1167 #[test]
1168 fn goto_drop_role() {
1169 assert_snapshot!(goto("
1170create role reader;
1171drop role read$0er;
1172"), @r"
1173 ╭▸
1174 2 │ create role reader;
1175 │ ────── 2. destination
1176 3 │ drop role reader;
1177 ╰╴ ─ 1. source
1178 ");
1179 }
1180
1181 #[test]
1182 fn goto_alter_role() {
1183 assert_snapshot!(goto("
1184create role reader;
1185alter role read$0er rename to writer;
1186"), @r"
1187 ╭▸
1188 2 │ create role reader;
1189 │ ────── 2. destination
1190 3 │ alter role reader rename to writer;
1191 ╰╴ ─ 1. source
1192 ");
1193 }
1194
1195 #[test]
1196 fn goto_set_role() {
1197 assert_snapshot!(goto("
1198create role reader;
1199set role read$0er;
1200"), @r"
1201 ╭▸
1202 2 │ create role reader;
1203 │ ────── 2. destination
1204 3 │ set role reader;
1205 ╰╴ ─ 1. source
1206 ");
1207 }
1208
1209 #[test]
1210 fn goto_create_tablespace_owner_role() {
1211 assert_snapshot!(goto("
1212create role reader;
1213create tablespace t owner read$0er location 'foo';
1214"), @r"
1215 ╭▸
1216 2 │ create role reader;
1217 │ ────── 2. destination
1218 3 │ create tablespace t owner reader location 'foo';
1219 ╰╴ ─ 1. source
1220 ");
1221 }
1222
1223 #[test]
1224 fn goto_role_definition_returns_self() {
1225 assert_snapshot!(goto("
1226create role read$0er;
1227"), @r"
1228 ╭▸
1229 2 │ create role reader;
1230 │ ┬──┬──
1231 │ │ │
1232 │ │ 1. source
1233 ╰╴ 2. destination
1234 ");
1235 }
1236
1237 #[test]
1238 fn goto_drop_database_defined_after() {
1239 assert_snapshot!(goto("
1240drop database my$0db;
1241create database mydb;
1242"), @r"
1243 ╭▸
1244 2 │ drop database mydb;
1245 │ ─ 1. source
1246 3 │ create database mydb;
1247 ╰╴ ──── 2. destination
1248 ");
1249 }
1250
1251 #[test]
1252 fn goto_database_definition_returns_self() {
1253 assert_snapshot!(goto("
1254create database my$0db;
1255"), @r"
1256 ╭▸
1257 2 │ create database mydb;
1258 │ ┬┬──
1259 │ ││
1260 │ │1. source
1261 ╰╴ 2. destination
1262 ");
1263 }
1264
1265 #[test]
1266 fn goto_drop_server() {
1267 assert_snapshot!(goto("
1268create server myserver foreign data wrapper fdw;
1269drop server my$0server;
1270"), @r"
1271 ╭▸
1272 2 │ create server myserver foreign data wrapper fdw;
1273 │ ──────── 2. destination
1274 3 │ drop server myserver;
1275 ╰╴ ─ 1. source
1276 ");
1277 }
1278
1279 #[test]
1280 fn goto_drop_server_defined_after() {
1281 assert_snapshot!(goto("
1282drop server my$0server;
1283create server myserver foreign data wrapper fdw;
1284"), @r"
1285 ╭▸
1286 2 │ drop server myserver;
1287 │ ─ 1. source
1288 3 │ create server myserver foreign data wrapper fdw;
1289 ╰╴ ──────── 2. destination
1290 ");
1291 }
1292
1293 #[test]
1294 fn goto_alter_server() {
1295 assert_snapshot!(goto("
1296create server myserver foreign data wrapper fdw;
1297alter server my$0server options (add foo 'bar');
1298"), @r"
1299 ╭▸
1300 2 │ create server myserver foreign data wrapper fdw;
1301 │ ──────── 2. destination
1302 3 │ alter server myserver options (add foo 'bar');
1303 ╰╴ ─ 1. source
1304 ");
1305 }
1306
1307 #[test]
1308 fn goto_server_definition_returns_self() {
1309 assert_snapshot!(goto("
1310create server my$0server foreign data wrapper fdw;
1311"), @r"
1312 ╭▸
1313 2 │ create server myserver foreign data wrapper fdw;
1314 │ ┬┬──────
1315 │ ││
1316 │ │1. source
1317 ╰╴ 2. destination
1318 ");
1319 }
1320
1321 #[test]
1322 fn goto_drop_extension() {
1323 assert_snapshot!(goto("
1324create extension myext;
1325drop extension my$0ext;
1326"), @r"
1327 ╭▸
1328 2 │ create extension myext;
1329 │ ───── 2. destination
1330 3 │ drop extension myext;
1331 ╰╴ ─ 1. source
1332 ");
1333 }
1334
1335 #[test]
1336 fn goto_drop_extension_defined_after() {
1337 assert_snapshot!(goto("
1338drop extension my$0ext;
1339create extension myext;
1340"), @r"
1341 ╭▸
1342 2 │ drop extension myext;
1343 │ ─ 1. source
1344 3 │ create extension myext;
1345 ╰╴ ───── 2. destination
1346 ");
1347 }
1348
1349 #[test]
1350 fn goto_alter_extension() {
1351 assert_snapshot!(goto("
1352create extension myext;
1353alter extension my$0ext update to '2.0';
1354"), @r"
1355 ╭▸
1356 2 │ create extension myext;
1357 │ ───── 2. destination
1358 3 │ alter extension myext update to '2.0';
1359 ╰╴ ─ 1. source
1360 ");
1361 }
1362
1363 #[test]
1364 fn goto_extension_definition_returns_self() {
1365 assert_snapshot!(goto("
1366create extension my$0ext;
1367"), @r"
1368 ╭▸
1369 2 │ create extension myext;
1370 │ ┬┬───
1371 │ ││
1372 │ │1. source
1373 ╰╴ 2. destination
1374 ");
1375 }
1376
1377 #[test]
1378 fn goto_drop_sequence_with_schema() {
1379 assert_snapshot!(goto("
1380create sequence foo.s;
1381drop sequence foo.s$0;
1382"), @r"
1383 ╭▸
1384 2 │ create sequence foo.s;
1385 │ ─ 2. destination
1386 3 │ drop sequence foo.s;
1387 ╰╴ ─ 1. source
1388 ");
1389 }
1390
1391 #[test]
1392 fn goto_drop_table_with_schema() {
1393 assert_snapshot!(goto("
1394create table public.t();
1395drop table t$0;
1396"), @r"
1397 ╭▸
1398 2 │ create table public.t();
1399 │ ─ 2. destination
1400 3 │ drop table t;
1401 ╰╴ ─ 1. source
1402 ");
1403
1404 assert_snapshot!(goto("
1405create table foo.t();
1406drop table foo.t$0;
1407"), @r"
1408 ╭▸
1409 2 │ create table foo.t();
1410 │ ─ 2. destination
1411 3 │ drop table foo.t;
1412 ╰╴ ─ 1. source
1413 ");
1414
1415 goto_not_found(
1416 "
1417-- defaults to public schema
1418create table t();
1419drop table foo.t$0;
1420",
1421 );
1422 }
1423
1424 #[test]
1425 fn goto_drop_temp_table() {
1426 assert_snapshot!(goto("
1427create temp table t();
1428drop table t$0;
1429"), @r"
1430 ╭▸
1431 2 │ create temp table t();
1432 │ ─ 2. destination
1433 3 │ drop table t;
1434 ╰╴ ─ 1. source
1435 ");
1436 }
1437
1438 #[test]
1439 fn goto_drop_temporary_table() {
1440 assert_snapshot!(goto("
1441create temporary table t();
1442drop table t$0;
1443"), @r"
1444 ╭▸
1445 2 │ create temporary table t();
1446 │ ─ 2. destination
1447 3 │ drop table t;
1448 ╰╴ ─ 1. source
1449 ");
1450 }
1451
1452 #[test]
1453 fn goto_drop_temp_table_with_pg_temp_schema() {
1454 assert_snapshot!(goto("
1455create temp table t();
1456drop table pg_temp.t$0;
1457"), @r"
1458 ╭▸
1459 2 │ create temp table t();
1460 │ ─ 2. destination
1461 3 │ drop table pg_temp.t;
1462 ╰╴ ─ 1. source
1463 ");
1464 }
1465
1466 #[test]
1467 fn goto_table_definition_returns_self() {
1468 assert_snapshot!(goto("
1469create table t$0(x bigint, y bigint);
1470"), @r"
1471 ╭▸
1472 2 │ create table t(x bigint, y bigint);
1473 │ ┬
1474 │ │
1475 │ 2. destination
1476 ╰╴ 1. source
1477 ");
1478 }
1479
1480 #[test]
1481 fn goto_foreign_table_column() {
1482 assert_snapshot!(goto("
1483create foreign table ft(a int)
1484 server s;
1485
1486select a$0 from ft;
1487"), @r"
1488 ╭▸
1489 2 │ create foreign table ft(a int)
1490 │ ─ 2. destination
1491 ‡
1492 5 │ select a from ft;
1493 ╰╴ ─ 1. source
1494 ");
1495 }
1496
1497 #[test]
1498 fn goto_foreign_table_definition() {
1499 assert_snapshot!(goto("
1500create foreign table ft(a int)
1501 server s;
1502
1503select a from ft$0;
1504"), @r"
1505 ╭▸
1506 2 │ create foreign table ft(a int)
1507 │ ── 2. destination
1508 ‡
1509 5 │ select a from ft;
1510 ╰╴ ─ 1. source
1511 ");
1512 }
1513
1514 #[test]
1515 fn goto_foreign_table_server_name() {
1516 assert_snapshot!(goto("
1517create server myserver foreign data wrapper fdw;
1518create foreign table ft(a int)
1519 server my$0server;
1520"), @r"
1521 ╭▸
1522 2 │ create server myserver foreign data wrapper fdw;
1523 │ ──────── 2. destination
1524 3 │ create foreign table ft(a int)
1525 4 │ server myserver;
1526 ╰╴ ─ 1. source
1527 ");
1528 }
1529
1530 #[test]
1531 fn goto_foreign_table_server_name_defined_after() {
1532 assert_snapshot!(goto("
1533create foreign table ft(a int)
1534 server my$0server;
1535create server myserver foreign data wrapper fdw;
1536"), @r"
1537 ╭▸
1538 3 │ server myserver;
1539 │ ─ 1. source
1540 4 │ create server myserver foreign data wrapper fdw;
1541 ╰╴ ──────── 2. destination
1542 ");
1543 }
1544
1545 #[test]
1546 fn goto_user_mapping_server_name() {
1547 assert_snapshot!(goto("
1548create server myserver foreign data wrapper fdw;
1549create user mapping for current_user server my$0server;
1550"), @r"
1551 ╭▸
1552 2 │ create server myserver foreign data wrapper fdw;
1553 │ ──────── 2. destination
1554 3 │ create user mapping for current_user server myserver;
1555 ╰╴ ─ 1. source
1556 ");
1557 }
1558
1559 #[test]
1560 fn goto_foreign_key_references_table() {
1561 assert_snapshot!(goto("
1562create table foo(id int);
1563create table bar(
1564 id int,
1565 foo_id int,
1566 foreign key (foo_id) references foo$0(id)
1567);
1568"), @r"
1569 ╭▸
1570 2 │ create table foo(id int);
1571 │ ─── 2. destination
1572 ‡
1573 6 │ foreign key (foo_id) references foo(id)
1574 ╰╴ ─ 1. source
1575 ");
1576 }
1577
1578 #[test]
1579 fn goto_foreign_key_on_delete_set_null_column() {
1580 assert_snapshot!(goto("
1581create table users (
1582 user_id integer not null,
1583 primary key (user_id)
1584);
1585
1586create table posts (
1587 post_id integer not null,
1588 author_id integer,
1589 primary key (post_id),
1590 foreign key (author_id) references users on delete set null (author_id$0)
1591);
1592"), @r"
1593 ╭▸
1594 9 │ author_id integer,
1595 │ ───────── 2. destination
1596 10 │ primary key (post_id),
1597 11 │ foreign key (author_id) references users on delete set null (author_id)
1598 ╰╴ ─ 1. source
1599 ");
1600 }
1601
1602 #[test]
1603 fn goto_references_constraint_table() {
1604 assert_snapshot!(goto("
1605create table t (
1606 id serial primary key
1607);
1608
1609create table u (
1610 id serial primary key,
1611 t_id int references t$0
1612);
1613"), @r"
1614 ╭▸
1615 2 │ create table t (
1616 │ ─ 2. destination
1617 ‡
1618 8 │ t_id int references t
1619 ╰╴ ─ 1. source
1620 ");
1621 }
1622
1623 #[test]
1624 fn goto_references_constraint_column() {
1625 assert_snapshot!(goto("
1626create table t (
1627 id serial primary key
1628);
1629
1630create table u (
1631 id serial primary key,
1632 t_id int references t(id$0)
1633);
1634"), @r"
1635 ╭▸
1636 3 │ id serial primary key
1637 │ ── 2. destination
1638 ‡
1639 8 │ t_id int references t(id)
1640 ╰╴ ─ 1. source
1641 ");
1642 }
1643
1644 #[test]
1645 fn goto_foreign_key_references_column() {
1646 assert_snapshot!(goto("
1647create table foo(id int);
1648create table bar(
1649 id int,
1650 foo_id int,
1651 foreign key (foo_id) references foo(id$0)
1652);
1653"), @r"
1654 ╭▸
1655 2 │ create table foo(id int);
1656 │ ── 2. destination
1657 ‡
1658 6 │ foreign key (foo_id) references foo(id)
1659 ╰╴ ─ 1. source
1660 ");
1661 }
1662
1663 #[test]
1664 fn goto_foreign_key_local_column() {
1665 assert_snapshot!(goto("
1666create table bar(
1667 id int,
1668 foo_id int,
1669 foreign key (foo_id$0) references foo(id)
1670);
1671"), @r"
1672 ╭▸
1673 4 │ foo_id int,
1674 │ ────── 2. destination
1675 5 │ foreign key (foo_id) references foo(id)
1676 ╰╴ ─ 1. source
1677 ");
1678 }
1679
1680 #[test]
1681 fn goto_alter_table_foreign_key_local_column() {
1682 assert_snapshot!(goto("
1683create table t (
1684 id bigserial primary key
1685);
1686
1687create table u (
1688 id bigserial primary key,
1689 t_id bigint
1690);
1691
1692alter table u
1693 add constraint fooo_fkey
1694 foreign key (t_id$0) references t (id);
1695"), @r"
1696 ╭▸
1697 8 │ t_id bigint
1698 │ ──── 2. destination
1699 ‡
1700 13 │ foreign key (t_id) references t (id);
1701 ╰╴ ─ 1. source
1702 ");
1703 }
1704
1705 #[test]
1706 fn goto_check_constraint_column() {
1707 assert_snapshot!(goto("
1708create table t (
1709 b int check (b > 10),
1710 c int check (c$0 > 10) no inherit
1711);
1712"), @r"
1713 ╭▸
1714 4 │ c int check (c > 10) no inherit
1715 │ ┬ ─ 1. source
1716 │ │
1717 ╰╴ 2. destination
1718 ");
1719 }
1720
1721 #[test]
1722 fn goto_generated_column() {
1723 assert_snapshot!(goto("
1724create table t (
1725 a int,
1726 b int generated always as (
1727 a$0 * 2
1728 ) stored
1729);
1730"), @r"
1731 ╭▸
1732 3 │ a int,
1733 │ ─ 2. destination
1734 4 │ b int generated always as (
1735 5 │ a * 2
1736 ╰╴ ─ 1. source
1737 ");
1738 }
1739
1740 #[test]
1741 fn goto_generated_column_function_call() {
1742 assert_snapshot!(goto("
1743create function pg_catalog.lower(text) returns text
1744 language internal;
1745
1746create table articles (
1747 id serial primary key,
1748 title text not null,
1749 body text not null,
1750 title_lower text generated always as (
1751 lower$0(title)
1752 ) stored
1753);
1754"), @r"
1755 ╭▸
1756 2 │ create function pg_catalog.lower(text) returns text
1757 │ ───── 2. destination
1758 ‡
1759 10 │ lower(title)
1760 ╰╴ ─ 1. source
1761 ");
1762 }
1763
1764 #[test]
1765 fn goto_index_expr_function_call() {
1766 assert_snapshot!(goto("
1767create function lower(text) returns text language internal;
1768create table articles (
1769 id serial primary key,
1770 title text not null
1771);
1772create index on articles (lower$0(title));
1773"), @r"
1774 ╭▸
1775 2 │ create function lower(text) returns text language internal;
1776 │ ───── 2. destination
1777 ‡
1778 7 │ create index on articles (lower(title));
1779 ╰╴ ─ 1. source
1780 ");
1781 }
1782
1783 #[test]
1784 fn goto_exclude_constraint_expr_function_call() {
1785 assert_snapshot!(goto("
1786create function lower(text) returns text language internal;
1787create table articles (
1788 title text not null,
1789 exclude using btree (lower$0(title) with =)
1790);
1791"), @r"
1792 ╭▸
1793 2 │ create function lower(text) returns text language internal;
1794 │ ───── 2. destination
1795 ‡
1796 5 │ exclude using btree (lower(title) with =)
1797 ╰╴ ─ 1. source
1798 ");
1799 }
1800
1801 #[test]
1802 fn goto_partition_by_expr_function_call() {
1803 assert_snapshot!(goto("
1804create function lower(text) returns text language internal;
1805create table articles (
1806 id serial primary key,
1807 title text not null
1808) partition by range (lower$0(title));
1809"), @r"
1810 ╭▸
1811 2 │ create function lower(text) returns text language internal;
1812 │ ───── 2. destination
1813 ‡
1814 6 │ ) partition by range (lower(title));
1815 ╰╴ ─ 1. source
1816 ");
1817 }
1818
1819 #[test]
1820 fn goto_table_check_constraint_column() {
1821 assert_snapshot!(goto("
1822create table t (
1823 a int,
1824 b text,
1825 check (a$0 > b)
1826);
1827"), @r"
1828 ╭▸
1829 3 │ a int,
1830 │ ─ 2. destination
1831 4 │ b text,
1832 5 │ check (a > b)
1833 ╰╴ ─ 1. source
1834 ");
1835 }
1836
1837 #[test]
1838 fn goto_table_unique_constraint_column() {
1839 assert_snapshot!(goto("
1840create table t (
1841 a int,
1842 b text,
1843 unique (a$0)
1844);
1845"), @r"
1846 ╭▸
1847 3 │ a int,
1848 │ ─ 2. destination
1849 4 │ b text,
1850 5 │ unique (a)
1851 ╰╴ ─ 1. source
1852 ");
1853 }
1854
1855 #[test]
1856 fn goto_table_primary_key_constraint_column() {
1857 assert_snapshot!(goto("
1858create table t (
1859 id bigint generated always as identity,
1860 inserted_at timestamptz not null default now(),
1861 primary key (id, inserted_at$0)
1862);
1863"), @r"
1864 ╭▸
1865 4 │ inserted_at timestamptz not null default now(),
1866 │ ─────────── 2. destination
1867 5 │ primary key (id, inserted_at)
1868 ╰╴ ─ 1. source
1869 ");
1870 }
1871
1872 #[test]
1873 fn goto_table_not_null_constraint_column() {
1874 assert_snapshot!(goto("
1875create table t (
1876 id integer,
1877 name text,
1878 not null name$0
1879);
1880"), @r"
1881 ╭▸
1882 4 │ name text,
1883 │ ──── 2. destination
1884 5 │ not null name
1885 ╰╴ ─ 1. source
1886 ");
1887 }
1888
1889 #[test]
1890 fn goto_table_exclude_constraint_column() {
1891 assert_snapshot!(goto("
1892create table circles (
1893 c circle,
1894 exclude using gist (c$0 with &&)
1895);
1896"), @r"
1897 ╭▸
1898 3 │ c circle,
1899 │ ─ 2. destination
1900 4 │ exclude using gist (c with &&)
1901 ╰╴ ─ 1. source
1902 ");
1903 }
1904
1905 #[test]
1906 fn goto_table_exclude_constraint_include_column() {
1907 assert_snapshot!(goto("
1908create table t (
1909 a int,
1910 b text,
1911 exclude using btree ( a with > )
1912 include (a$0, b)
1913);
1914"), @r"
1915 ╭▸
1916 3 │ a int,
1917 │ ─ 2. destination
1918 ‡
1919 6 │ include (a, b)
1920 ╰╴ ─ 1. source
1921 ");
1922 }
1923
1924 #[test]
1925 fn goto_table_exclude_constraint_where_column() {
1926 assert_snapshot!(goto("
1927create table t (
1928 a int,
1929 b text,
1930 exclude using btree ( a with > )
1931 where ( a$0 > 10 and b like '%foo' )
1932);
1933"), @r"
1934 ╭▸
1935 3 │ a int,
1936 │ ─ 2. destination
1937 ‡
1938 6 │ where ( a > 10 and b like '%foo' )
1939 ╰╴ ─ 1. source
1940 ");
1941 }
1942
1943 #[test]
1944 fn goto_table_partition_by_column() {
1945 assert_snapshot!(goto("
1946create table t (
1947 id bigint generated always as identity,
1948 inserted_at timestamptz not null default now()
1949) partition by range (inserted_at$0);
1950"), @r"
1951 ╭▸
1952 4 │ inserted_at timestamptz not null default now()
1953 │ ─────────── 2. destination
1954 5 │ ) partition by range (inserted_at);
1955 ╰╴ ─ 1. source
1956 ");
1957 }
1958
1959 #[test]
1960 fn goto_table_partition_of_table() {
1961 assert_snapshot!(goto("
1962create table t ();
1963create table t_2026_01_02 partition of t$0
1964 for values from ('2026-01-02') to ('2026-01-03');
1965"), @r"
1966 ╭▸
1967 2 │ create table t ();
1968 │ ─ 2. destination
1969 3 │ create table t_2026_01_02 partition of t
1970 ╰╴ ─ 1. source
1971 ");
1972 }
1973
1974 #[test]
1975 fn goto_table_partition_of_cycle() {
1976 goto_not_found(
1977 "
1978create table part1 partition of part2
1979 for values from ('2026-01-02') to ('2026-01-03');
1980create table part2 partition of part1
1981 for values from ('2026-01-02') to ('2026-01-03');
1982select a$0 from part2;
1983",
1984 );
1985 }
1986
1987 #[test]
1988 fn goto_partition_table_column() {
1989 assert_snapshot!(goto("
1990create table part (
1991 a int,
1992 inserted_at timestamptz not null default now()
1993) partition by range (inserted_at);
1994create table part_2026_01_02 partition of part
1995 for values from ('2026-01-02') to ('2026-01-03');
1996select a$0 from part_2026_01_02;
1997"), @r"
1998 ╭▸
1999 3 │ a int,
2000 │ ─ 2. destination
2001 ‡
2002 8 │ select a from part_2026_01_02;
2003 ╰╴ ─ 1. source
2004 ");
2005 }
2006
2007 #[test]
2008 fn goto_alter_index_attach_partition() {
2009 assert_snapshot!(goto("
2010create table t (
2011 inserted_at timestamptz not null default now()
2012) partition by range (inserted_at);
2013create table part partition of t
2014 for values from ('2026-01-02') to ('2026-01-03');
2015alter index t attach partition part$0;
2016"), @r"
2017 ╭▸
2018 5 │ create table part partition of t
2019 │ ──── 2. destination
2020 6 │ for values from ('2026-01-02') to ('2026-01-03');
2021 7 │ alter index t attach partition part;
2022 ╰╴ ─ 1. source
2023 ");
2024 }
2025
2026 #[test]
2027 fn goto_create_table_like_clause() {
2028 assert_snapshot!(goto("
2029create table large_data_table(a text);
2030create table t (
2031 a text,
2032 like large_data_table$0,
2033 b integer
2034);
2035"), @r"
2036 ╭▸
2037 2 │ create table large_data_table(a text);
2038 │ ──────────────── 2. destination
2039 ‡
2040 5 │ like large_data_table,
2041 ╰╴ ─ 1. source
2042 ");
2043 }
2044
2045 #[test]
2046 fn goto_create_table_inherits() {
2047 assert_snapshot!(goto("
2048create table bar(a int);
2049create table t (a int)
2050inherits (foo.bar, bar$0, buzz);
2051"), @r"
2052 ╭▸
2053 2 │ create table bar(a int);
2054 │ ─── 2. destination
2055 3 │ create table t (a int)
2056 4 │ inherits (foo.bar, bar, buzz);
2057 ╰╴ ─ 1. source
2058 ");
2059 }
2060
2061 #[test]
2062 fn goto_create_table_like_clause_columns() {
2063 assert_snapshot!(goto("
2064create table t(a int, b int);
2065create table u(like t, c int);
2066select a$0, c from u;
2067"), @r"
2068 ╭▸
2069 2 │ create table t(a int, b int);
2070 │ ─ 2. destination
2071 3 │ create table u(like t, c int);
2072 4 │ select a, c from u;
2073 ╰╴ ─ 1. source
2074 ");
2075 }
2076
2077 #[test]
2078 fn goto_create_table_like_clause_local_column() {
2079 assert_snapshot!(goto("
2080create table t(a int, b int);
2081create table u(like t, c int);
2082select a, c$0 from u;
2083"), @r"
2084 ╭▸
2085 3 │ create table u(like t, c int);
2086 │ ─ 2. destination
2087 4 │ select a, c from u;
2088 ╰╴ ─ 1. source
2089 ");
2090 }
2091
2092 #[test]
2093 fn goto_create_table_like_clause_multi() {
2094 assert_snapshot!(goto("
2095create table t(a int, b int);
2096create table u(x int, y int);
2097create table k(like t, like u, c int);
2098select y$0 from k;
2099"), @r"
2100 ╭▸
2101 3 │ create table u(x int, y int);
2102 │ ─ 2. destination
2103 4 │ create table k(like t, like u, c int);
2104 5 │ select y from k;
2105 ╰╴ ─ 1. source
2106 ");
2107 }
2108
2109 #[test]
2110 fn goto_create_table_inherits_column() {
2111 assert_snapshot!(goto("
2112create table t (
2113 a int, b text
2114);
2115create table u (
2116 c int
2117) inherits (t);
2118select a$0 from u;
2119"), @r"
2120 ╭▸
2121 3 │ a int, b text
2122 │ ─ 2. destination
2123 ‡
2124 8 │ select a from u;
2125 ╰╴ ─ 1. source
2126 ");
2127 }
2128
2129 #[test]
2130 fn goto_create_table_inherits_local_column() {
2131 assert_snapshot!(goto("
2132create table t (
2133 a int, b text
2134);
2135create table u (
2136 c int
2137) inherits (t);
2138select c$0 from u;
2139"), @r"
2140 ╭▸
2141 6 │ c int
2142 │ ─ 2. destination
2143 7 │ ) inherits (t);
2144 8 │ select c from u;
2145 ╰╴ ─ 1. source
2146 ");
2147 }
2148
2149 #[test]
2150 fn goto_create_table_inherits_multiple_parents() {
2151 assert_snapshot!(goto("
2152create table t1 (
2153 a int
2154);
2155create table t2 (
2156 b text
2157);
2158create table u (
2159 c int
2160) inherits (t1, t2);
2161select b$0 from u;
2162"), @r"
2163 ╭▸
2164 6 │ b text
2165 │ ─ 2. destination
2166 ‡
2167 11 │ select b from u;
2168 ╰╴ ─ 1. source
2169 ");
2170 }
2171
2172 #[test]
2173 fn goto_create_foreign_table_inherits_column() {
2174 assert_snapshot!(goto("
2175create server myserver foreign data wrapper postgres_fdw;
2176create table t (
2177 a int, b text
2178);
2179create foreign table u (
2180 c int
2181) inherits (t) server myserver;
2182select a$0 from u;
2183"), @r"
2184 ╭▸
2185 4 │ a int, b text
2186 │ ─ 2. destination
2187 ‡
2188 9 │ select a from u;
2189 ╰╴ ─ 1. source
2190 ");
2191 }
2192
2193 #[test]
2194 fn goto_drop_temp_table_shadows_public() {
2195 assert_snapshot!(goto("
2197create table t();
2198create temp table t();
2199drop table t$0;
2200"), @r"
2201 ╭▸
2202 2 │ create table t();
2203 │ ─ 2. destination
2204 3 │ create temp table t();
2205 4 │ drop table t;
2206 ╰╴ ─ 1. source
2207 ");
2208 }
2209
2210 #[test]
2211 fn goto_drop_public_table_when_temp_exists() {
2212 assert_snapshot!(goto("
2214create table t();
2215create temp table t();
2216drop table public.t$0;
2217"), @r"
2218 ╭▸
2219 2 │ create table t();
2220 │ ─ 2. destination
2221 3 │ create temp table t();
2222 4 │ drop table public.t;
2223 ╰╴ ─ 1. source
2224 ");
2225 }
2226
2227 #[test]
2228 fn goto_drop_table_defined_after() {
2229 assert_snapshot!(goto("
2230drop table t$0;
2231create table t();
2232"), @r"
2233 ╭▸
2234 2 │ drop table t;
2235 │ ─ 1. source
2236 3 │ create table t();
2237 ╰╴ ─ 2. destination
2238 ");
2239 }
2240
2241 #[test]
2242 fn goto_drop_type() {
2243 assert_snapshot!(goto("
2244create type t as enum ('a', 'b');
2245drop type t$0;
2246"), @r"
2247 ╭▸
2248 2 │ create type t as enum ('a', 'b');
2249 │ ─ 2. destination
2250 3 │ drop type t;
2251 ╰╴ ─ 1. source
2252 ");
2253 }
2254
2255 #[test]
2256 fn goto_drop_type_with_schema() {
2257 assert_snapshot!(goto("
2258create type public.t as enum ('a', 'b');
2259drop type t$0;
2260"), @r"
2261 ╭▸
2262 2 │ create type public.t as enum ('a', 'b');
2263 │ ─ 2. destination
2264 3 │ drop type t;
2265 ╰╴ ─ 1. source
2266 ");
2267
2268 assert_snapshot!(goto("
2269create type foo.t as enum ('a', 'b');
2270drop type foo.t$0;
2271"), @r"
2272 ╭▸
2273 2 │ create type foo.t as enum ('a', 'b');
2274 │ ─ 2. destination
2275 3 │ drop type foo.t;
2276 ╰╴ ─ 1. source
2277 ");
2278
2279 goto_not_found(
2280 "
2281create type t as enum ('a', 'b');
2282drop type foo.t$0;
2283",
2284 );
2285 }
2286
2287 #[test]
2288 fn goto_drop_type_defined_after() {
2289 assert_snapshot!(goto("
2290drop type t$0;
2291create type t as enum ('a', 'b');
2292"), @r"
2293 ╭▸
2294 2 │ drop type t;
2295 │ ─ 1. source
2296 3 │ create type t as enum ('a', 'b');
2297 ╰╴ ─ 2. destination
2298 ");
2299 }
2300
2301 #[test]
2302 fn goto_drop_type_composite() {
2303 assert_snapshot!(goto("
2304create type person as (name text, age int);
2305drop type person$0;
2306"), @r"
2307 ╭▸
2308 2 │ create type person as (name text, age int);
2309 │ ────── 2. destination
2310 3 │ drop type person;
2311 ╰╴ ─ 1. source
2312 ");
2313 }
2314
2315 #[test]
2316 fn goto_create_table_type_reference() {
2317 assert_snapshot!(goto("
2318create type person_info as (name text, email text);
2319create table users(id int, member person_info$0);
2320"), @"
2321 ╭▸
2322 2 │ create type person_info as (name text, email text);
2323 │ ─────────── 2. destination
2324 3 │ create table users(id int, member person_info);
2325 ╰╴ ─ 1. source
2326 ");
2327 }
2328
2329 #[test]
2330 fn goto_function_param_table_type() {
2331 assert_snapshot!(goto("
2332create table t(a int, b int);
2333create function b(t$0) returns int as 'select 1' language sql;
2334"), @r"
2335 ╭▸
2336 2 │ create table t(a int, b int);
2337 │ ─ 2. destination
2338 3 │ create function b(t) returns int as 'select 1' language sql;
2339 ╰╴ ─ 1. source
2340 ");
2341 }
2342
2343 #[test]
2344 fn goto_function_param_time_type() {
2345 assert_snapshot!(goto("
2346create type timestamp;
2347create function f(timestamp$0 without time zone) returns text language internal;
2348"), @r"
2349 ╭▸
2350 2 │ create type timestamp;
2351 │ ───────── 2. destination
2352 3 │ create function f(timestamp without time zone) returns text language internal;
2353 ╰╴ ─ 1. source
2354 ");
2355 }
2356
2357 #[test]
2358 fn goto_function_param_time_type_no_timezone() {
2359 assert_snapshot!(goto("
2360create type time;
2361create function f(time$0) returns text language internal;
2362"), @r"
2363 ╭▸
23642 │ create type time;
2365 │ ──── 2. destination
23663 │ create function f(time) returns text language internal;
2367 ╰╴ ─ 1. source
2368");
2369 }
2370
2371 #[test]
2372 fn goto_create_table_type_reference_enum() {
2373 assert_snapshot!(goto("
2374create type mood as enum ('sad', 'ok', 'happy');
2375create table users(id int, mood mood$0);
2376"), @r"
2377 ╭▸
2378 2 │ create type mood as enum ('sad', 'ok', 'happy');
2379 │ ──── 2. destination
2380 3 │ create table users(id int, mood mood);
2381 ╰╴ ─ 1. source
2382 ");
2383 }
2384
2385 #[test]
2386 fn goto_create_table_type_reference_range() {
2387 assert_snapshot!(goto("
2388create type int4_range as range (subtype = int4);
2389create table metrics(id int, span int4_range$0);
2390"), @r"
2391 ╭▸
2392 2 │ create type int4_range as range (subtype = int4);
2393 │ ────────── 2. destination
2394 3 │ create table metrics(id int, span int4_range);
2395 ╰╴ ─ 1. source
2396 ");
2397 }
2398
2399 #[test]
2400 fn goto_create_table_type_reference_input_output() {
2401 assert_snapshot!(goto("
2402create type myint (input = myintin, output = myintout, like = int4);
2403create table data(id int, value myint$0);
2404"), @r"
2405 ╭▸
2406 2 │ create type myint (input = myintin, output = myintout, like = int4);
2407 │ ───── 2. destination
2408 3 │ create table data(id int, value myint);
2409 ╰╴ ─ 1. source
2410 ");
2411 }
2412
2413 #[test]
2414 fn goto_composite_type_field() {
2415 assert_snapshot!(goto("
2416create type person_info as (name text, email text);
2417create table users(id int, member person_info);
2418select (member).name$0 from users;
2419"), @"
2420 ╭▸
2421 2 │ create type person_info as (name text, email text);
2422 │ ──── 2. destination
2423 3 │ create table users(id int, member person_info);
2424 4 │ select (member).name from users;
2425 ╰╴ ─ 1. source
2426 ");
2427 }
2428
2429 #[test]
2430 fn goto_drop_type_range() {
2431 assert_snapshot!(goto("
2432create type int4_range as range (subtype = int4);
2433drop type int4_range$0;
2434"), @r"
2435 ╭▸
2436 2 │ create type int4_range as range (subtype = int4);
2437 │ ────────── 2. destination
2438 3 │ drop type int4_range;
2439 ╰╴ ─ 1. source
2440 ");
2441 }
2442
2443 #[test]
2444 fn goto_drop_domain() {
2445 assert_snapshot!(goto("
2446create domain posint as integer check (value > 0);
2447drop domain posint$0;
2448"), @r"
2449 ╭▸
2450 2 │ create domain posint as integer check (value > 0);
2451 │ ────── 2. destination
2452 3 │ drop domain posint;
2453 ╰╴ ─ 1. source
2454 ");
2455 }
2456
2457 #[test]
2458 fn goto_cast_to_domain() {
2459 assert_snapshot!(goto("
2460create domain posint as integer check (value > 0);
2461select 1::posint$0;
2462"), @r"
2463 ╭▸
2464 2 │ create domain posint as integer check (value > 0);
2465 │ ────── 2. destination
2466 3 │ select 1::posint;
2467 ╰╴ ─ 1. source
2468 ");
2469 }
2470
2471 #[test]
2472 fn goto_drop_type_domain() {
2473 assert_snapshot!(goto("
2474create domain posint as integer check (value > 0);
2475drop type posint$0;
2476"), @r"
2477 ╭▸
2478 2 │ create domain posint as integer check (value > 0);
2479 │ ────── 2. destination
2480 3 │ drop type posint;
2481 ╰╴ ─ 1. source
2482 ");
2483 }
2484
2485 #[test]
2486 fn goto_drop_view() {
2487 assert_snapshot!(goto("
2488create view v as select 1;
2489drop view v$0;
2490"), @r"
2491 ╭▸
2492 2 │ create view v as select 1;
2493 │ ─ 2. destination
2494 3 │ drop view v;
2495 ╰╴ ─ 1. source
2496 ");
2497 }
2498
2499 #[test]
2500 fn goto_drop_materialized_view() {
2501 assert_snapshot!(goto("
2502create materialized view v as select 1;
2503drop materialized view v$0;
2504"), @r"
2505 ╭▸
2506 2 │ create materialized view v as select 1;
2507 │ ─ 2. destination
2508 3 │ drop materialized view v;
2509 ╰╴ ─ 1. source
2510 ");
2511 }
2512
2513 #[test]
2514 fn goto_drop_view_with_schema() {
2515 assert_snapshot!(goto("
2516create view public.v as select 1;
2517drop view v$0;
2518"), @r"
2519 ╭▸
2520 2 │ create view public.v as select 1;
2521 │ ─ 2. destination
2522 3 │ drop view v;
2523 ╰╴ ─ 1. source
2524 ");
2525
2526 assert_snapshot!(goto("
2527create view foo.v as select 1;
2528drop view foo.v$0;
2529"), @r"
2530 ╭▸
2531 2 │ create view foo.v as select 1;
2532 │ ─ 2. destination
2533 3 │ drop view foo.v;
2534 ╰╴ ─ 1. source
2535 ");
2536
2537 goto_not_found(
2538 "
2539create view v as select 1;
2540drop view foo.v$0;
2541",
2542 );
2543 }
2544
2545 #[test]
2546 fn goto_drop_temp_view() {
2547 assert_snapshot!(goto("
2548create temp view v as select 1;
2549drop view v$0;
2550"), @r"
2551 ╭▸
2552 2 │ create temp view v as select 1;
2553 │ ─ 2. destination
2554 3 │ drop view v;
2555 ╰╴ ─ 1. source
2556 ");
2557 }
2558
2559 #[test]
2560 fn goto_select_from_view() {
2561 assert_snapshot!(goto("
2562create view v as select 1;
2563select * from v$0;
2564"), @r"
2565 ╭▸
2566 2 │ create view v as select 1;
2567 │ ─ 2. destination
2568 3 │ select * from v;
2569 ╰╴ ─ 1. source
2570 ");
2571 }
2572
2573 #[test]
2574 fn goto_select_from_materialized_view() {
2575 assert_snapshot!(goto("
2576create materialized view v as select 1;
2577select * from v$0;
2578"), @r"
2579 ╭▸
2580 2 │ create materialized view v as select 1;
2581 │ ─ 2. destination
2582 3 │ select * from v;
2583 ╰╴ ─ 1. source
2584 ");
2585 }
2586
2587 #[test]
2588 fn goto_select_from_view_with_schema() {
2589 assert_snapshot!(goto("
2590create view public.v as select 1;
2591select * from public.v$0;
2592"), @r"
2593 ╭▸
2594 2 │ create view public.v as select 1;
2595 │ ─ 2. destination
2596 3 │ select * from public.v;
2597 ╰╴ ─ 1. source
2598 ");
2599 }
2600
2601 #[test]
2602 fn goto_view_column() {
2603 assert_snapshot!(goto("
2604create view v as select 1 as a;
2605select a$0 from v;
2606"), @r"
2607 ╭▸
2608 2 │ create view v as select 1 as a;
2609 │ ─ 2. destination
2610 3 │ select a from v;
2611 ╰╴ ─ 1. source
2612 ");
2613 }
2614
2615 #[test]
2616 fn goto_view_column_qualified() {
2617 assert_snapshot!(goto("
2618create view v as select 1 as a;
2619select v.a$0 from v;
2620"), @r"
2621 ╭▸
2622 2 │ create view v as select 1 as a;
2623 │ ─ 2. destination
2624 3 │ select v.a from v;
2625 ╰╴ ─ 1. source
2626 ");
2627 }
2628
2629 #[test]
2630 fn goto_create_table_as_column() {
2631 assert_snapshot!(goto("
2632create table t as select 1 a;
2633select a$0 from t;
2634"), @r"
2635 ╭▸
2636 2 │ create table t as select 1 a;
2637 │ ─ 2. destination
2638 3 │ select a from t;
2639 ╰╴ ─ 1. source
2640 ");
2641 }
2642
2643 #[test]
2644 fn goto_select_from_create_table_as() {
2645 assert_snapshot!(goto("
2646create table t as select 1 a;
2647select a from t$0;
2648"), @r"
2649 ╭▸
2650 2 │ create table t as select 1 a;
2651 │ ─ 2. destination
2652 3 │ select a from t;
2653 ╰╴ ─ 1. source
2654 ");
2655 }
2656
2657 #[test]
2658 fn goto_view_with_explicit_column_list() {
2659 assert_snapshot!(goto("
2660create view v(col1) as select 1;
2661select * from v$0;
2662"), @r"
2663 ╭▸
2664 2 │ create view v(col1) as select 1;
2665 │ ─ 2. destination
2666 3 │ select * from v;
2667 ╰╴ ─ 1. source
2668 ");
2669 }
2670
2671 #[test]
2672 fn goto_view_column_with_explicit_column_list() {
2673 assert_snapshot!(goto("
2674 create view v(col1) as select 1;
2675 select col1$0 from v;
2676 "), @r"
2677 ╭▸
2678 2 │ create view v(col1) as select 1;
2679 │ ──── 2. destination
2680 3 │ select col1 from v;
2681 ╰╴ ─ 1. source
2682 ");
2683 }
2684
2685 #[test]
2686 fn goto_view_column_with_schema() {
2687 assert_snapshot!(goto("
2688create view public.v as select 1 as a;
2689select a$0 from public.v;
2690"), @r"
2691 ╭▸
2692 2 │ create view public.v as select 1 as a;
2693 │ ─ 2. destination
2694 3 │ select a from public.v;
2695 ╰╴ ─ 1. source
2696 ");
2697 }
2698
2699 #[test]
2700 fn goto_view_multiple_columns() {
2701 assert_snapshot!(goto("
2702create view v as select 1 as a, 2 as b;
2703select b$0 from v;
2704"), @r"
2705 ╭▸
2706 2 │ create view v as select 1 as a, 2 as b;
2707 │ ─ 2. destination
2708 3 │ select b from v;
2709 ╰╴ ─ 1. source
2710 ");
2711 }
2712
2713 #[test]
2714 fn goto_view_column_from_table() {
2715 assert_snapshot!(goto("
2716create table t(x int, y int);
2717create view v as select x, y from t;
2718select x$0 from v;
2719"), @r"
2720 ╭▸
2721 3 │ create view v as select x, y from t;
2722 │ ─ 2. destination
2723 4 │ select x from v;
2724 ╰╴ ─ 1. source
2725 ");
2726 }
2727
2728 #[test]
2729 fn goto_view_column_with_table_preference() {
2730 assert_snapshot!(goto("
2731create table v(a int);
2732create view vw as select 1 as a;
2733select a$0 from v;
2734"), @r"
2735 ╭▸
2736 2 │ create table v(a int);
2737 │ ─ 2. destination
2738 3 │ create view vw as select 1 as a;
2739 4 │ select a from v;
2740 ╰╴ ─ 1. source
2741 ");
2742 }
2743
2744 #[test]
2745 fn goto_cast_operator() {
2746 assert_snapshot!(goto("
2747create type foo as enum ('a', 'b');
2748select x::foo$0;
2749"), @r"
2750 ╭▸
2751 2 │ create type foo as enum ('a', 'b');
2752 │ ─── 2. destination
2753 3 │ select x::foo;
2754 ╰╴ ─ 1. source
2755 ");
2756 }
2757
2758 #[test]
2759 fn goto_cast_function() {
2760 assert_snapshot!(goto("
2761create type bar as enum ('x', 'y');
2762select cast(x as bar$0);
2763"), @r"
2764 ╭▸
2765 2 │ create type bar as enum ('x', 'y');
2766 │ ─── 2. destination
2767 3 │ select cast(x as bar);
2768 ╰╴ ─ 1. source
2769 ");
2770 }
2771
2772 #[test]
2773 fn goto_cast_with_schema() {
2774 assert_snapshot!(goto("
2775create type public.baz as enum ('m', 'n');
2776select x::public.baz$0;
2777"), @r"
2778 ╭▸
2779 2 │ create type public.baz as enum ('m', 'n');
2780 │ ─── 2. destination
2781 3 │ select x::public.baz;
2782 ╰╴ ─ 1. source
2783 ");
2784 }
2785
2786 #[test]
2787 fn goto_cast_timestamp_without_time_zone() {
2788 assert_snapshot!(goto("
2789create type pg_catalog.timestamp;
2790select ''::timestamp without$0 time zone;
2791"), @r"
2792 ╭▸
2793 2 │ create type pg_catalog.timestamp;
2794 │ ───────── 2. destination
2795 3 │ select ''::timestamp without time zone;
2796 ╰╴ ─ 1. source
2797 ");
2798 }
2799
2800 #[test]
2801 fn goto_cast_timestamp_with_time_zone() {
2802 assert_snapshot!(goto("
2803create type pg_catalog.timestamptz;
2804select ''::timestamp with$0 time zone;
2805"), @r"
2806 ╭▸
2807 2 │ create type pg_catalog.timestamptz;
2808 │ ─────────── 2. destination
2809 3 │ select ''::timestamp with time zone;
2810 ╰╴ ─ 1. source
2811 ");
2812 }
2813
2814 #[test]
2815 fn goto_cast_multirange_type_from_range() {
2816 assert_snapshot!(goto("
2817create type floatrange as range (
2818 subtype = float8,
2819 subtype_diff = float8mi
2820);
2821select '{[1.234, 5.678]}'::floatmultirange$0;
2822"), @r"
2823 ╭▸
2824 2 │ create type floatrange as range (
2825 │ ────────── 2. destination
2826 ‡
2827 6 │ select '{[1.234, 5.678]}'::floatmultirange;
2828 ╰╴ ─ 1. source
2829 ");
2830 }
2831
2832 #[test]
2833 fn goto_cast_multirange_special_type_name_string() {
2834 assert_snapshot!(goto("
2835create type floatrange as range (
2836 subtype = float8,
2837 subtype_diff = float8mi,
2838 multirange_type_name = 'floatmulirangething'
2839);
2840select '{[1.234, 5.678]}'::floatmulirangething$0;
2841"), @r"
2842 ╭▸
2843 2 │ create type floatrange as range (
2844 │ ────────── 2. destination
2845 ‡
2846 7 │ select '{[1.234, 5.678]}'::floatmulirangething;
2847 ╰╴ ─ 1. source
2848 ");
2849 }
2850
2851 #[test]
2852 fn goto_cast_multirange_special_type_name_ident() {
2853 assert_snapshot!(goto("
2854create type floatrange as range (
2855 subtype = float8,
2856 subtype_diff = float8mi,
2857 multirange_type_name = floatrangemutirange
2858);
2859select '{[1.234, 5.678]}'::floatrangemutirange$0;
2860"), @r"
2861 ╭▸
2862 2 │ create type floatrange as range (
2863 │ ────────── 2. destination
2864 ‡
2865 7 │ select '{[1.234, 5.678]}'::floatrangemutirange;
2866 ╰╴ ─ 1. source
2867 ");
2868 }
2869
2870 #[test]
2871 fn goto_cast_multirange_edge_case_type_from_range() {
2872 assert_snapshot!(goto("
2874create type floatrangerange as range (
2875 subtype = float8,
2876 subtype_diff = float8mi
2877);
2878select '{[1.234, 5.678]}'::floatmultirangerange$0;
2879"), @r"
2880 ╭▸
2881 2 │ create type floatrangerange as range (
2882 │ ─────────────── 2. destination
2883 ‡
2884 6 │ select '{[1.234, 5.678]}'::floatmultirangerange;
2885 ╰╴ ─ 1. source
2886 ");
2887 }
2888
2889 #[test]
2890 fn goto_cast_boolean_falls_back_to_bool() {
2891 assert_snapshot!(goto("
2892create type pg_catalog.bool;
2893select '1'::boolean$0;
2894"), @"
2895 ╭▸
2896 2 │ create type pg_catalog.bool;
2897 │ ──── 2. destination
2898 3 │ select '1'::boolean;
2899 ╰╴ ─ 1. source
2900 ");
2901 }
2902
2903 #[test]
2904 fn goto_cast_decimal_falls_back_to_numeric() {
2905 assert_snapshot!(goto("
2906create type pg_catalog.numeric;
2907select 1::decimal$0(10, 2);
2908"), @"
2909 ╭▸
2910 2 │ create type pg_catalog.numeric;
2911 │ ─────── 2. destination
2912 3 │ select 1::decimal(10, 2);
2913 ╰╴ ─ 1. source
2914 ");
2915 }
2916
2917 #[test]
2918 fn goto_cast_float_falls_back_to_float8() {
2919 assert_snapshot!(goto("
2920create type pg_catalog.float8;
2921select 1::float$0;
2922"), @"
2923 ╭▸
2924 2 │ create type pg_catalog.float8;
2925 │ ────── 2. destination
2926 3 │ select 1::float;
2927 ╰╴ ─ 1. source
2928 ");
2929 }
2930
2931 #[test]
2932 fn goto_cast_bigint_falls_back_to_int8() {
2933 assert_snapshot!(goto("
2934create type pg_catalog.int8;
2935select 1::bigint$0;
2936"), @r"
2937 ╭▸
2938 2 │ create type pg_catalog.int8;
2939 │ ──── 2. destination
2940 3 │ select 1::bigint;
2941 ╰╴ ─ 1. source
2942 ");
2943 }
2944
2945 #[test]
2946 fn goto_cast_real_falls_back_to_float4() {
2947 assert_snapshot!(goto("
2948create type pg_catalog.float4;
2949select 1::real$0;
2950"), @"
2951 ╭▸
2952 2 │ create type pg_catalog.float4;
2953 │ ────── 2. destination
2954 3 │ select 1::real;
2955 ╰╴ ─ 1. source
2956 ");
2957 }
2958
2959 #[test]
2960 fn goto_cast_bigint_prefers_user_type() {
2961 assert_snapshot!(goto("
2962create type bigint;
2963create type pg_catalog.int8;
2964select 1::bigint$0;
2965"), @r"
2966 ╭▸
2967 2 │ create type bigint;
2968 │ ────── 2. destination
2969 3 │ create type pg_catalog.int8;
2970 4 │ select 1::bigint;
2971 ╰╴ ─ 1. source
2972 ");
2973 }
2974
2975 #[test]
2976 fn goto_cast_smallserial_falls_back_to_int2() {
2977 assert_snapshot!(goto("
2978create type pg_catalog.int2;
2979select 1::smallserial$0;
2980"), @r"
2981 ╭▸
2982 2 │ create type pg_catalog.int2;
2983 │ ──── 2. destination
2984 3 │ select 1::smallserial;
2985 ╰╴ ─ 1. source
2986 ");
2987 }
2988
2989 #[test]
2990 fn goto_cast_serial2_falls_back_to_int2() {
2991 assert_snapshot!(goto("
2992create type pg_catalog.int2;
2993select 1::serial2$0;
2994"), @r"
2995 ╭▸
2996 2 │ create type pg_catalog.int2;
2997 │ ──── 2. destination
2998 3 │ select 1::serial2;
2999 ╰╴ ─ 1. source
3000 ");
3001 }
3002
3003 #[test]
3004 fn goto_cast_serial_falls_back_to_int4() {
3005 assert_snapshot!(goto("
3006create type pg_catalog.int4;
3007select 1::serial$0;
3008"), @r"
3009 ╭▸
3010 2 │ create type pg_catalog.int4;
3011 │ ──── 2. destination
3012 3 │ select 1::serial;
3013 ╰╴ ─ 1. source
3014 ");
3015 }
3016
3017 #[test]
3018 fn goto_cast_serial4_falls_back_to_int4() {
3019 assert_snapshot!(goto("
3020create type pg_catalog.int4;
3021select 1::serial4$0;
3022"), @r"
3023 ╭▸
3024 2 │ create type pg_catalog.int4;
3025 │ ──── 2. destination
3026 3 │ select 1::serial4;
3027 ╰╴ ─ 1. source
3028 ");
3029 }
3030
3031 #[test]
3032 fn goto_cast_bigserial_falls_back_to_int8() {
3033 assert_snapshot!(goto("
3034create type pg_catalog.int8;
3035select 1::bigserial$0;
3036"), @r"
3037 ╭▸
3038 2 │ create type pg_catalog.int8;
3039 │ ──── 2. destination
3040 3 │ select 1::bigserial;
3041 ╰╴ ─ 1. source
3042 ");
3043 }
3044
3045 #[test]
3046 fn goto_cast_serial8_falls_back_to_int8() {
3047 assert_snapshot!(goto("
3048create type pg_catalog.int8;
3049select 1::serial8$0;
3050"), @r"
3051 ╭▸
3052 2 │ create type pg_catalog.int8;
3053 │ ──── 2. destination
3054 3 │ select 1::serial8;
3055 ╰╴ ─ 1. source
3056 ");
3057 }
3058
3059 #[test]
3060 fn goto_cast_int_falls_back_to_int4() {
3061 assert_snapshot!(goto("
3062create type pg_catalog.int4;
3063select 1::int$0;
3064"), @r"
3065 ╭▸
3066 2 │ create type pg_catalog.int4;
3067 │ ──── 2. destination
3068 3 │ select 1::int;
3069 ╰╴ ─ 1. source
3070 ");
3071 }
3072
3073 #[test]
3074 fn goto_cast_integer_falls_back_to_int4() {
3075 assert_snapshot!(goto("
3076create type pg_catalog.int4;
3077select 1::integer$0;
3078"), @r"
3079 ╭▸
3080 2 │ create type pg_catalog.int4;
3081 │ ──── 2. destination
3082 3 │ select 1::integer;
3083 ╰╴ ─ 1. source
3084 ");
3085 }
3086
3087 #[test]
3088 fn goto_cast_smallint_falls_back_to_int2() {
3089 assert_snapshot!(goto("
3090create type pg_catalog.int2;
3091select 1::smallint$0;
3092"), @r"
3093 ╭▸
3094 2 │ create type pg_catalog.int2;
3095 │ ──── 2. destination
3096 3 │ select 1::smallint;
3097 ╰╴ ─ 1. source
3098 ");
3099 }
3100
3101 #[test]
3102 fn goto_cast_double_precision_falls_back_to_float8() {
3103 assert_snapshot!(goto("
3104create type pg_catalog.float8;
3105select '1'::double precision[]$0;
3106"), @r"
3107 ╭▸
3108 2 │ create type pg_catalog.float8;
3109 │ ────── 2. destination
3110 3 │ select '1'::double precision[];
3111 ╰╴ ─ 1. source
3112 ");
3113 }
3114
3115 #[test]
3116 fn goto_cast_varchar_with_modifier() {
3117 assert_snapshot!(goto("
3118create type pg_catalog.varchar;
3119select '1'::varchar$0(1);
3120"), @r"
3121 ╭▸
3122 2 │ create type pg_catalog.varchar;
3123 │ ─────── 2. destination
3124 3 │ select '1'::varchar(1);
3125 ╰╴ ─ 1. source
3126 ");
3127 }
3128
3129 #[test]
3130 fn goto_cast_composite_type() {
3131 assert_snapshot!(goto("
3132create type person_info as (name varchar(50), age int);
3133select ('Alice', 30)::person_info$0;
3134"), @r"
3135 ╭▸
3136 2 │ create type person_info as (name varchar(50), age int);
3137 │ ─────────── 2. destination
3138 3 │ select ('Alice', 30)::person_info;
3139 ╰╴ ─ 1. source
3140 ");
3141 }
3142
3143 #[test]
3144 fn goto_cast_composite_type_in_cte() {
3145 assert_snapshot!(goto("
3146create type person_info as (name varchar(50), age int);
3147with team as (
3148 select 1 as id, ('Alice', 30)::person_info$0 as member
3149)
3150select * from team;
3151"), @r"
3152 ╭▸
3153 2 │ create type person_info as (name varchar(50), age int);
3154 │ ─────────── 2. destination
3155 3 │ with team as (
3156 4 │ select 1 as id, ('Alice', 30)::person_info as member
3157 ╰╴ ─ 1. source
3158 ");
3159 }
3160
3161 #[test]
3162 fn goto_composite_type_field_name() {
3163 assert_snapshot!(goto("
3164create type person_info as (name varchar(50), age int);
3165with team as (
3166 select 1 as id, ('Alice', 30)::person_info as member
3167)
3168select (member).name$0, (member).age from team;
3169"), @r"
3170 ╭▸
3171 2 │ create type person_info as (name varchar(50), age int);
3172 │ ──── 2. destination
3173 ‡
3174 6 │ select (member).name, (member).age from team;
3175 ╰╴ ─ 1. source
3176 ");
3177 }
3178
3179 #[test]
3180 fn goto_composite_type_field_in_where() {
3181 assert_snapshot!(goto("
3182create type person_info as (name varchar(50), age int);
3183with team as (
3184 select 1 as id, ('Alice', 30)::person_info as member
3185 union all
3186 select 2, ('Bob', 25)::person_info
3187)
3188select (member).name, (member).age
3189from team
3190where (member).age$0 >= 18;
3191"), @r"
3192 ╭▸
3193 2 │ create type person_info as (name varchar(50), age int);
3194 │ ─── 2. destination
3195 ‡
3196 10 │ where (member).age >= 18;
3197 ╰╴ ─ 1. source
3198 ");
3199 }
3200
3201 #[test]
3202 fn goto_composite_type_field_base() {
3203 assert_snapshot!(goto("
3204create type person_info as (name varchar(50), age int);
3205with team as (
3206 select 1 as id, ('Alice', 30)::person_info as member
3207)
3208select (member$0).age from team;
3209"), @r"
3210 ╭▸
3211 4 │ select 1 as id, ('Alice', 30)::person_info as member
3212 │ ────── 2. destination
3213 5 │ )
3214 6 │ select (member).age from team;
3215 ╰╴ ─ 1. source
3216 ");
3217 }
3218
3219 #[test]
3220 fn goto_composite_type_field_nested_parens() {
3221 assert_snapshot!(goto("
3222create type person_info as (name varchar(50), age int);
3223with team as (
3224 select 1 as id, ('Alice', 30)::person_info as member
3225)
3226select ((((member))).name$0) from team;
3227"), @r"
3228 ╭▸
3229 2 │ create type person_info as (name varchar(50), age int);
3230 │ ──── 2. destination
3231 ‡
3232 6 │ select ((((member))).name) from team;
3233 ╰╴ ─ 1. source
3234 ");
3235 }
3236
3237 #[test]
3238 fn begin_to_rollback() {
3239 assert_snapshot!(goto(
3240 "
3241begin$0;
3242select 1;
3243rollback;
3244commit;
3245",
3246 ), @r"
3247 ╭▸
3248 2 │ begin;
3249 │ ─ 1. source
3250 3 │ select 1;
3251 4 │ rollback;
3252 ╰╴──────── 2. destination
3253 ");
3254 }
3255
3256 #[test]
3257 fn commit_to_begin() {
3258 assert_snapshot!(goto(
3259 "
3260begin;
3261select 1;
3262commit$0;
3263",
3264 ), @r"
3265 ╭▸
3266 2 │ begin;
3267 │ ───── 2. destination
3268 3 │ select 1;
3269 4 │ commit;
3270 ╰╴ ─ 1. source
3271 ");
3272 }
3273
3274 #[test]
3275 fn begin_to_commit() {
3276 assert_snapshot!(goto(
3277 "
3278begin$0;
3279select 1;
3280commit;
3281",
3282 ), @r"
3283 ╭▸
3284 2 │ begin;
3285 │ ─ 1. source
3286 3 │ select 1;
3287 4 │ commit;
3288 ╰╴────── 2. destination
3289 ");
3290 }
3291
3292 #[test]
3293 fn commit_to_start_transaction() {
3294 assert_snapshot!(goto(
3295 "
3296start transaction;
3297select 1;
3298commit$0;
3299",
3300 ), @r"
3301 ╭▸
3302 2 │ start transaction;
3303 │ ───────────────── 2. destination
3304 3 │ select 1;
3305 4 │ commit;
3306 ╰╴ ─ 1. source
3307 ");
3308 }
3309
3310 #[test]
3311 fn start_transaction_to_commit() {
3312 assert_snapshot!(goto(
3313 "
3314start$0 transaction;
3315select 1;
3316commit;
3317",
3318 ), @r"
3319 ╭▸
3320 2 │ start transaction;
3321 │ ─ 1. source
3322 3 │ select 1;
3323 4 │ commit;
3324 ╰╴────── 2. destination
3325 ");
3326 }
3327
3328 #[test]
3329 fn goto_with_search_path() {
3330 assert_snapshot!(goto(r#"
3331set search_path to "foo", public;
3332create table foo.t();
3333drop table t$0;
3334"#), @r"
3335 ╭▸
3336 3 │ create table foo.t();
3337 │ ─ 2. destination
3338 4 │ drop table t;
3339 ╰╴ ─ 1. source
3340 ");
3341 }
3342
3343 #[test]
3344 fn goto_with_search_path_and_unspecified_table() {
3345 assert_snapshot!(goto(r#"
3346set search_path to foo,bar;
3347create table t();
3348drop table foo.t$0;
3349"#), @r"
3350 ╭▸
3351 3 │ create table t();
3352 │ ─ 2. destination
3353 4 │ drop table foo.t;
3354 ╰╴ ─ 1. source
3355 ");
3356 }
3357
3358 #[test]
3359 fn goto_column_not_in_cte_but_in_table() {
3360 goto_not_found(
3362 r"
3363create table t (c int);
3364with t as (select 1 a)
3365select c$0 from t;
3366",
3367 );
3368 }
3369
3370 #[test]
3371 fn goto_with_search_path_empty() {
3372 goto_not_found(
3373 r#"
3374set search_path = '';
3375create table t();
3376drop table t$0;
3377"#,
3378 );
3379 }
3380
3381 #[test]
3382 fn goto_with_search_path_like_variable() {
3383 goto_not_found(
3385 "
3386set bar.search_path to foo, public;
3387create table foo.t();
3388drop table t$0;
3389",
3390 )
3391 }
3392
3393 #[test]
3394 fn goto_with_search_path_second_schema() {
3395 assert_snapshot!(goto("
3396set search_path to foo, bar, public;
3397create table bar.t();
3398drop table t$0;
3399"), @r"
3400 ╭▸
3401 3 │ create table bar.t();
3402 │ ─ 2. destination
3403 4 │ drop table t;
3404 ╰╴ ─ 1. source
3405 ");
3406 }
3407
3408 #[test]
3409 fn goto_with_search_path_skips_first() {
3410 assert_snapshot!(goto("
3411set search_path to foo, bar, public;
3412create table foo.t();
3413create table bar.t();
3414drop table t$0;
3415"), @r"
3416 ╭▸
3417 3 │ create table foo.t();
3418 │ ─ 2. destination
3419 4 │ create table bar.t();
3420 5 │ drop table t;
3421 ╰╴ ─ 1. source
3422 ");
3423 }
3424
3425 #[test]
3426 fn goto_without_search_path_uses_default() {
3427 assert_snapshot!(goto("
3428create table foo.t();
3429create table public.t();
3430drop table t$0;
3431"), @r"
3432 ╭▸
3433 3 │ create table public.t();
3434 │ ─ 2. destination
3435 4 │ drop table t;
3436 ╰╴ ─ 1. source
3437 ");
3438 }
3439
3440 #[test]
3441 fn goto_with_set_schema() {
3442 assert_snapshot!(goto("
3443set schema 'myschema';
3444create table myschema.t();
3445drop table t$0;
3446"), @r"
3447 ╭▸
3448 3 │ create table myschema.t();
3449 │ ─ 2. destination
3450 4 │ drop table t;
3451 ╰╴ ─ 1. source
3452 ");
3453 }
3454
3455 #[test]
3456 fn goto_with_set_schema_ignores_other_schemas() {
3457 assert_snapshot!(goto("
3458set schema 'myschema';
3459create table public.t();
3460create table myschema.t();
3461drop table t$0;
3462"), @r"
3463 ╭▸
3464 4 │ create table myschema.t();
3465 │ ─ 2. destination
3466 5 │ drop table t;
3467 ╰╴ ─ 1. source
3468 ");
3469 }
3470
3471 #[test]
3472 fn goto_with_search_path_changed_twice() {
3473 assert_snapshot!(goto("
3474set search_path to foo;
3475create table foo.t();
3476set search_path to bar;
3477create table bar.t();
3478drop table t$0;
3479"), @r"
3480 ╭▸
3481 5 │ create table bar.t();
3482 │ ─ 2. destination
3483 6 │ drop table t;
3484 ╰╴ ─ 1. source
3485 ");
3486
3487 assert_snapshot!(goto("
3488set search_path to foo;
3489create table foo.t();
3490drop table t$0;
3491set search_path to bar;
3492create table bar.t();
3493drop table t;
3494"), @r"
3495 ╭▸
3496 3 │ create table foo.t();
3497 │ ─ 2. destination
3498 4 │ drop table t;
3499 ╰╴ ─ 1. source
3500 ");
3501 }
3502
3503 #[test]
3504 fn goto_with_empty_search_path() {
3505 goto_not_found(
3506 "
3507set search_path to '';
3508create table public.t();
3509drop table t$0;
3510",
3511 )
3512 }
3513
3514 #[test]
3515 fn goto_with_search_path_uppercase() {
3516 assert_snapshot!(goto("
3517SET SEARCH_PATH TO foo;
3518create table foo.t();
3519drop table t$0;
3520"), @r"
3521 ╭▸
3522 3 │ create table foo.t();
3523 │ ─ 2. destination
3524 4 │ drop table t;
3525 ╰╴ ─ 1. source
3526 ");
3527 }
3528
3529 #[test]
3530 fn goto_table_stmt() {
3531 assert_snapshot!(goto("
3532create table t();
3533table t$0;
3534"), @r"
3535 ╭▸
3536 2 │ create table t();
3537 │ ─ 2. destination
3538 3 │ table t;
3539 ╰╴ ─ 1. source
3540 ");
3541 }
3542
3543 #[test]
3544 fn goto_table_stmt_with_schema() {
3545 assert_snapshot!(goto("
3546create table public.t();
3547table public.t$0;
3548"), @r"
3549 ╭▸
3550 2 │ create table public.t();
3551 │ ─ 2. destination
3552 3 │ table public.t;
3553 ╰╴ ─ 1. source
3554 ");
3555 }
3556
3557 #[test]
3558 fn goto_table_stmt_with_search_path() {
3559 assert_snapshot!(goto("
3560set search_path to foo;
3561create table foo.t();
3562table t$0;
3563"), @r"
3564 ╭▸
3565 3 │ create table foo.t();
3566 │ ─ 2. destination
3567 4 │ table t;
3568 ╰╴ ─ 1. source
3569 ");
3570 }
3571
3572 #[test]
3573 fn goto_drop_index() {
3574 assert_snapshot!(goto("
3575create index idx_name on t(x);
3576drop index idx_name$0;
3577"), @r"
3578 ╭▸
3579 2 │ create index idx_name on t(x);
3580 │ ──────── 2. destination
3581 3 │ drop index idx_name;
3582 ╰╴ ─ 1. source
3583 ");
3584 }
3585
3586 #[test]
3587 fn goto_drop_index_with_schema() {
3588 assert_snapshot!(goto(r#"
3589set search_path to public;
3590create index idx_name on t(x);
3591drop index public.idx_name$0;
3592"#), @r"
3593 ╭▸
3594 3 │ create index idx_name on t(x);
3595 │ ──────── 2. destination
3596 4 │ drop index public.idx_name;
3597 ╰╴ ─ 1. source
3598 ");
3599 }
3600
3601 #[test]
3602 fn goto_drop_index_defined_after() {
3603 assert_snapshot!(goto("
3604drop index idx_name$0;
3605create index idx_name on t(x);
3606"), @r"
3607 ╭▸
3608 2 │ drop index idx_name;
3609 │ ─ 1. source
3610 3 │ create index idx_name on t(x);
3611 ╰╴ ──────── 2. destination
3612 ");
3613 }
3614
3615 #[test]
3616 fn goto_index_definition_returns_self() {
3617 assert_snapshot!(goto("
3618create index idx_name$0 on t(x);
3619"), @r"
3620 ╭▸
3621 2 │ create index idx_name on t(x);
3622 │ ┬──────┬
3623 │ │ │
3624 │ │ 1. source
3625 ╰╴ 2. destination
3626 ");
3627 }
3628
3629 #[test]
3630 fn goto_drop_index_with_search_path() {
3631 assert_snapshot!(goto(r#"
3632create index idx_name on t(x);
3633set search_path to bar;
3634create index idx_name on f(x);
3635set search_path to default;
3636drop index idx_name$0;
3637"#), @r"
3638 ╭▸
3639 2 │ create index idx_name on t(x);
3640 │ ──────── 2. destination
3641 ‡
3642 6 │ drop index idx_name;
3643 ╰╴ ─ 1. source
3644 ");
3645 }
3646
3647 #[test]
3648 fn goto_drop_index_multiple() {
3649 assert_snapshot!(goto("
3650create index idx1 on t(x);
3651create index idx2 on t(y);
3652drop index idx1, idx2$0;
3653"), @r"
3654 ╭▸
3655 3 │ create index idx2 on t(y);
3656 │ ──── 2. destination
3657 4 │ drop index idx1, idx2;
3658 ╰╴ ─ 1. source
3659 ");
3660 }
3661
3662 #[test]
3663 fn goto_create_index_table() {
3664 assert_snapshot!(goto("
3665create table users(id int);
3666create index idx_users on users$0(id);
3667"), @r"
3668 ╭▸
3669 2 │ create table users(id int);
3670 │ ───── 2. destination
3671 3 │ create index idx_users on users(id);
3672 ╰╴ ─ 1. source
3673 ");
3674 }
3675
3676 #[test]
3677 fn goto_create_index_table_with_schema() {
3678 assert_snapshot!(goto("
3679create table public.users(id int);
3680create index idx_users on public.users$0(id);
3681"), @r"
3682 ╭▸
3683 2 │ create table public.users(id int);
3684 │ ───── 2. destination
3685 3 │ create index idx_users on public.users(id);
3686 ╰╴ ─ 1. source
3687 ");
3688 }
3689
3690 #[test]
3691 fn goto_create_index_table_with_search_path() {
3692 assert_snapshot!(goto(r#"
3693set search_path to foo;
3694create table foo.users(id int);
3695create index idx_users on users$0(id);
3696"#), @r"
3697 ╭▸
3698 3 │ create table foo.users(id int);
3699 │ ───── 2. destination
3700 4 │ create index idx_users on users(id);
3701 ╰╴ ─ 1. source
3702 ");
3703 }
3704
3705 #[test]
3706 fn goto_create_index_temp_table() {
3707 assert_snapshot!(goto("
3708create temp table users(id int);
3709create index idx_users on users$0(id);
3710"), @r"
3711 ╭▸
3712 2 │ create temp table users(id int);
3713 │ ───── 2. destination
3714 3 │ create index idx_users on users(id);
3715 ╰╴ ─ 1. source
3716 ");
3717 }
3718
3719 #[test]
3720 fn goto_create_index_column() {
3721 assert_snapshot!(goto("
3722create table users(id int, email text);
3723create index idx_email on users(email$0);
3724"), @r"
3725 ╭▸
3726 2 │ create table users(id int, email text);
3727 │ ───── 2. destination
3728 3 │ create index idx_email on users(email);
3729 ╰╴ ─ 1. source
3730 ");
3731 }
3732
3733 #[test]
3734 fn goto_create_index_first_column() {
3735 assert_snapshot!(goto("
3736create table users(id int, email text);
3737create index idx_id on users(id$0);
3738"), @r"
3739 ╭▸
3740 2 │ create table users(id int, email text);
3741 │ ── 2. destination
3742 3 │ create index idx_id on users(id);
3743 ╰╴ ─ 1. source
3744 ");
3745 }
3746
3747 #[test]
3748 fn goto_create_index_multiple_columns() {
3749 assert_snapshot!(goto("
3750create table users(id int, email text, name text);
3751create index idx_users on users(id, email$0, name);
3752"), @r"
3753 ╭▸
3754 2 │ create table users(id int, email text, name text);
3755 │ ───── 2. destination
3756 3 │ create index idx_users on users(id, email, name);
3757 ╰╴ ─ 1. source
3758 ");
3759 }
3760
3761 #[test]
3762 fn goto_create_index_column_with_schema() {
3763 assert_snapshot!(goto("
3764create table public.users(id int, email text);
3765create index idx_email on public.users(email$0);
3766"), @r"
3767 ╭▸
3768 2 │ create table public.users(id int, email text);
3769 │ ───── 2. destination
3770 3 │ create index idx_email on public.users(email);
3771 ╰╴ ─ 1. source
3772 ");
3773 }
3774
3775 #[test]
3776 fn goto_create_index_column_temp_table() {
3777 assert_snapshot!(goto("
3778create temp table users(id int, email text);
3779create index idx_email on users(email$0);
3780"), @r"
3781 ╭▸
3782 2 │ create temp table users(id int, email text);
3783 │ ───── 2. destination
3784 3 │ create index idx_email on users(email);
3785 ╰╴ ─ 1. source
3786 ");
3787 }
3788
3789 #[test]
3790 fn goto_drop_function() {
3791 assert_snapshot!(goto("
3792create function foo() returns int as $$ select 1 $$ language sql;
3793drop function foo$0();
3794"), @r"
3795 ╭▸
3796 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3797 │ ─── 2. destination
3798 3 │ drop function foo();
3799 ╰╴ ─ 1. source
3800 ");
3801 }
3802
3803 #[test]
3804 fn goto_drop_function_with_schema() {
3805 assert_snapshot!(goto("
3806set search_path to public;
3807create function foo() returns int as $$ select 1 $$ language sql;
3808drop function public.foo$0();
3809"), @r"
3810 ╭▸
3811 3 │ create function foo() returns int as $$ select 1 $$ language sql;
3812 │ ─── 2. destination
3813 4 │ drop function public.foo();
3814 ╰╴ ─ 1. source
3815 ");
3816 }
3817
3818 #[test]
3819 fn goto_drop_function_defined_after() {
3820 assert_snapshot!(goto("
3821drop function foo$0();
3822create function foo() returns int as $$ select 1 $$ language sql;
3823"), @r"
3824 ╭▸
3825 2 │ drop function foo();
3826 │ ─ 1. source
3827 3 │ create function foo() returns int as $$ select 1 $$ language sql;
3828 ╰╴ ─── 2. destination
3829 ");
3830 }
3831
3832 #[test]
3833 fn goto_function_definition_returns_self() {
3834 assert_snapshot!(goto("
3835create function foo$0() returns int as $$ select 1 $$ language sql;
3836"), @r"
3837 ╭▸
3838 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3839 │ ┬─┬
3840 │ │ │
3841 │ │ 1. source
3842 ╰╴ 2. destination
3843 ");
3844 }
3845
3846 #[test]
3847 fn goto_drop_function_with_search_path() {
3848 assert_snapshot!(goto("
3849create function foo() returns int as $$ select 1 $$ language sql;
3850set search_path to bar;
3851create function foo() returns int as $$ select 1 $$ language sql;
3852set search_path to default;
3853drop function foo$0();
3854"), @r"
3855 ╭▸
3856 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3857 │ ─── 2. destination
3858 ‡
3859 6 │ drop function foo();
3860 ╰╴ ─ 1. source
3861 ");
3862 }
3863
3864 #[test]
3865 fn goto_drop_function_multiple() {
3866 assert_snapshot!(goto("
3867create function foo() returns int as $$ select 1 $$ language sql;
3868create function bar() returns int as $$ select 1 $$ language sql;
3869drop function foo(), bar$0();
3870"), @r"
3871 ╭▸
3872 3 │ create function bar() returns int as $$ select 1 $$ language sql;
3873 │ ─── 2. destination
3874 4 │ drop function foo(), bar();
3875 ╰╴ ─ 1. source
3876 ");
3877 }
3878
3879 #[test]
3880 fn goto_drop_function_overloaded() {
3881 assert_snapshot!(goto("
3882create function add(complex) returns complex as $$ select null $$ language sql;
3883create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3884drop function add$0(complex);
3885"), @r"
3886 ╭▸
3887 2 │ create function add(complex) returns complex as $$ select null $$ language sql;
3888 │ ─── 2. destination
3889 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3890 4 │ drop function add(complex);
3891 ╰╴ ─ 1. source
3892 ");
3893 }
3894
3895 #[test]
3896 fn goto_drop_function_second_overload() {
3897 assert_snapshot!(goto("
3898create function add(complex) returns complex as $$ select null $$ language sql;
3899create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3900drop function add$0(bigint);
3901"), @r"
3902 ╭▸
3903 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3904 │ ─── 2. destination
3905 4 │ drop function add(bigint);
3906 ╰╴ ─ 1. source
3907 ");
3908 }
3909
3910 #[test]
3911 fn goto_select_function_call() {
3912 assert_snapshot!(goto("
3913create function foo() returns int as $$ select 1 $$ language sql;
3914select foo$0();
3915"), @r"
3916 ╭▸
3917 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3918 │ ─── 2. destination
3919 3 │ select foo();
3920 ╰╴ ─ 1. source
3921 ");
3922 }
3923
3924 #[test]
3925 fn goto_select_column_from_function_return_table() {
3926 assert_snapshot!(goto(r#"
3927create function dup(int) returns table(f1 int, f2 text)
3928 as ''
3929 language sql;
3930
3931select f1$0 from dup(42);
3932"#), @r"
3933 ╭▸
3934 2 │ create function dup(int) returns table(f1 int, f2 text)
3935 │ ── 2. destination
3936 ‡
3937 6 │ select f1 from dup(42);
3938 ╰╴ ─ 1. source
3939 ");
3940 }
3941
3942 #[test]
3943 fn goto_select_column_from_function_return_table_with_schema() {
3944 assert_snapshot!(goto(r#"
3945create function myschema.dup(int) returns table(f1 int, f2 text)
3946 as ''
3947 language sql;
3948create function otherschema.dup(int) returns table(f1 int, f2 text)
3949 as ''
3950 language sql;
3951
3952select f1$0 from myschema.dup(42);
3953"#), @r"
3954 ╭▸
3955 2 │ create function myschema.dup(int) returns table(f1 int, f2 text)
3956 │ ── 2. destination
3957 ‡
3958 9 │ select f1 from myschema.dup(42);
3959 ╰╴ ─ 1. source
3960 ");
3961 }
3962
3963 #[test]
3964 fn goto_select_column_from_function_return_table_paren() {
3965 assert_snapshot!(goto(r#"
3966create function dup(int) returns table(f1 int, f2 text)
3967 as ''
3968 language sql;
3969
3970select (dup(42)).f2$0;
3971"#), @r"
3972 ╭▸
3973 2 │ create function dup(int) returns table(f1 int, f2 text)
3974 │ ── 2. destination
3975 ‡
3976 6 │ select (dup(42)).f2;
3977 ╰╴ ─ 1. source
3978 ");
3979 }
3980
3981 #[test]
3982 fn goto_select_column_from_function_return_table_qualified() {
3983 assert_snapshot!(goto(r#"
3984create function dup(int) returns table(f1 int, f2 text)
3985 as ''
3986 language sql;
3987
3988select dup.f1$0 from dup(42);
3989"#), @r"
3990 ╭▸
3991 2 │ create function dup(int) returns table(f1 int, f2 text)
3992 │ ── 2. destination
3993 ‡
3994 6 │ select dup.f1 from dup(42);
3995 ╰╴ ─ 1. source
3996 ");
3997 }
3998
3999 #[test]
4000 fn goto_select_column_from_function_return_table_qualified_function_name() {
4001 assert_snapshot!(goto(r#"
4002create function dup(int) returns table(f1 int, f2 text)
4003 as ''
4004 language sql;
4005
4006select dup$0.f1 from dup(42);
4007"#), @r"
4008 ╭▸
4009 2 │ create function dup(int) returns table(f1 int, f2 text)
4010 │ ─── 2. destination
4011 ‡
4012 6 │ select dup.f1 from dup(42);
4013 ╰╴ ─ 1. source
4014 ");
4015 }
4016
4017 #[test]
4018 fn goto_select_column_from_function_return_table_qualified_function_name_with_alias() {
4019 assert_snapshot!(goto(r#"
4020create function dup(int) returns table(f1 int, f2 text)
4021 as ''
4022 language sql;
4023
4024select dup$0.f2 from dup(42) as dup;
4025"#), @r"
4026 ╭▸
4027 6 │ select dup.f2 from dup(42) as dup;
4028 ╰╴ ─ 1. source ─── 2. destination
4029 ");
4030 }
4031
4032 #[test]
4033 fn goto_select_column_from_function_return_table_alias_list() {
4034 assert_snapshot!(goto(r#"
4035create function dup(int) returns table(f1 int, f2 text)
4036 as ''
4037 language sql;
4038
4039select a$0 from dup(42) t(a, b);
4040"#), @r"
4041 ╭▸
4042 6 │ select a from dup(42) t(a, b);
4043 ╰╴ ─ 1. source ─ 2. destination
4044 ");
4045 }
4046
4047 #[test]
4048 fn goto_select_column_from_function_return_table_alias_list_qualified_partial() {
4049 assert_snapshot!(goto(r#"
4050create function dup(int) returns table(f1 int, f2 text)
4051 as ''
4052 language sql;
4053
4054select u.f2$0 from dup(42) as u(x);
4055"#), @r"
4056 ╭▸
4057 2 │ create function dup(int) returns table(f1 int, f2 text)
4058 │ ── 2. destination
4059 ‡
4060 6 │ select u.f2 from dup(42) as u(x);
4061 ╰╴ ─ 1. source
4062 ");
4063 }
4064
4065 #[test]
4066 fn goto_select_column_from_function_return_table_alias_list_unqualified_partial() {
4067 assert_snapshot!(goto(r#"
4068create function dup(int) returns table(f1 int, f2 text)
4069 as ''
4070 language sql;
4071
4072select f2$0 from dup(42) as u(x);
4073"#), @r"
4074 ╭▸
4075 2 │ create function dup(int) returns table(f1 int, f2 text)
4076 │ ── 2. destination
4077 ‡
4078 6 │ select f2 from dup(42) as u(x);
4079 ╰╴ ─ 1. source
4080 ");
4081 }
4082
4083 #[test]
4084 fn goto_select_column_from_function_return_table_alias_list_unqualified_not_found() {
4085 goto_not_found(
4086 r#"
4087create function dup(int) returns table(f1 int, f2 text)
4088 as ''
4089 language sql;
4090
4091select f2$0 from dup(42) as u(x, y);
4092"#,
4093 );
4094 }
4095
4096 #[test]
4097 fn goto_select_aggregate_call() {
4098 assert_snapshot!(goto("
4099create aggregate foo(int) (
4100 sfunc = int4pl,
4101 stype = int,
4102 initcond = '0'
4103);
4104
4105select foo$0(1);
4106"), @r"
4107 ╭▸
4108 2 │ create aggregate foo(int) (
4109 │ ─── 2. destination
4110 ‡
4111 8 │ select foo(1);
4112 ╰╴ ─ 1. source
4113 ");
4114 }
4115
4116 #[test]
4117 fn goto_create_aggregate_sfunc() {
4118 assert_snapshot!(goto("
4119create function pg_catalog.int8inc(bigint) returns bigint
4120 language internal;
4121
4122create aggregate pg_catalog.count(*) (
4123 sfunc = int8inc$0,
4124 stype = bigint,
4125 combinefunc = int8pl,
4126 initcond = '0'
4127);
4128"), @r"
4129 ╭▸
4130 2 │ create function pg_catalog.int8inc(bigint) returns bigint
4131 │ ─────── 2. destination
4132 ‡
4133 6 │ sfunc = int8inc,
4134 ╰╴ ─ 1. source
4135 "
4136 );
4137 }
4138
4139 #[test]
4140 fn goto_create_aggregate_combinefunc() {
4141 assert_snapshot!(goto("
4142create function pg_catalog.int8pl(bigint, bigint) returns bigint
4143 language internal;
4144
4145create aggregate pg_catalog.count(*) (
4146 sfunc = int8inc,
4147 stype = bigint,
4148 combinefunc = int8pl$0,
4149 initcond = '0'
4150);
4151"), @r"
4152 ╭▸
4153 2 │ create function pg_catalog.int8pl(bigint, bigint) returns bigint
4154 │ ────── 2. destination
4155 ‡
4156 8 │ combinefunc = int8pl,
4157 ╰╴ ─ 1. source
4158 "
4159 );
4160 }
4161
4162 #[test]
4163 fn goto_default_constraint_function_call() {
4164 assert_snapshot!(goto("
4165create function f() returns int as 'select 1' language sql;
4166create table t(
4167 a int default f$0()
4168);
4169"), @r"
4170 ╭▸
4171 2 │ create function f() returns int as 'select 1' language sql;
4172 │ ─ 2. destination
4173 3 │ create table t(
4174 4 │ a int default f()
4175 ╰╴ ─ 1. source
4176 ");
4177 }
4178
4179 #[test]
4180 fn goto_select_function_call_with_schema() {
4181 assert_snapshot!(goto("
4182create function public.foo() returns int as $$ select 1 $$ language sql;
4183select public.foo$0();
4184"), @r"
4185 ╭▸
4186 2 │ create function public.foo() returns int as $$ select 1 $$ language sql;
4187 │ ─── 2. destination
4188 3 │ select public.foo();
4189 ╰╴ ─ 1. source
4190 ");
4191 }
4192
4193 #[test]
4194 fn goto_select_function_call_with_search_path() {
4195 assert_snapshot!(goto("
4196set search_path to myschema;
4197create function foo() returns int as $$ select 1 $$ language sql;
4198select myschema.foo$0();
4199"), @r"
4200 ╭▸
4201 3 │ create function foo() returns int as $$ select 1 $$ language sql;
4202 │ ─── 2. destination
4203 4 │ select myschema.foo();
4204 ╰╴ ─ 1. source
4205 ");
4206 }
4207
4208 #[test]
4209 fn goto_function_call_style_column_access() {
4210 assert_snapshot!(goto("
4211create table t(a int, b int);
4212select a$0(t) from t;
4213"), @r"
4214 ╭▸
4215 2 │ create table t(a int, b int);
4216 │ ─ 2. destination
4217 3 │ select a(t) from t;
4218 ╰╴ ─ 1. source
4219 ");
4220 }
4221
4222 #[test]
4223 fn goto_function_call_style_column_access_with_function_precedence() {
4224 assert_snapshot!(goto("
4225create table t(a int, b int);
4226create function b(t) returns int as 'select 1' LANGUAGE sql;
4227select b$0(t) from t;
4228"), @r"
4229 ╭▸
4230 3 │ create function b(t) returns int as 'select 1' LANGUAGE sql;
4231 │ ─ 2. destination
4232 4 │ select b(t) from t;
4233 ╰╴ ─ 1. source
4234 ");
4235 }
4236
4237 #[test]
4238 fn goto_function_call_style_column_access_table_arg() {
4239 assert_snapshot!(goto("
4240create table t(a int, b int);
4241select a(t$0) from t;
4242"), @r"
4243 ╭▸
4244 2 │ create table t(a int, b int);
4245 │ ─ 2. destination
4246 3 │ select a(t) from t;
4247 ╰╴ ─ 1. source
4248 ");
4249 }
4250
4251 #[test]
4252 fn goto_function_call_style_column_access_table_arg_with_function() {
4253 assert_snapshot!(goto("
4254create table t(a int, b int);
4255create function b(t) returns int as 'select 1' LANGUAGE sql;
4256select b(t$0) from t;
4257"), @r"
4258 ╭▸
4259 2 │ create table t(a int, b int);
4260 │ ─ 2. destination
4261 3 │ create function b(t) returns int as 'select 1' LANGUAGE sql;
4262 4 │ select b(t) from t;
4263 ╰╴ ─ 1. source
4264 ");
4265 }
4266
4267 #[test]
4268 fn goto_function_call_multiple_args_not_column_access() {
4269 goto_not_found(
4270 "
4271create table t(a int, b int);
4272select a$0(t, 1) from t;
4273",
4274 );
4275 }
4276
4277 #[test]
4278 fn goto_function_call_nested() {
4279 assert_snapshot!(goto("
4280create function f() returns int8
4281 as 'select 1'
4282 language sql;
4283select format('foo%d', f$0());
4284"), @r"
4285 ╭▸
4286 2 │ create function f() returns int8
4287 │ ─ 2. destination
4288 ‡
4289 5 │ select format('foo%d', f());
4290 ╰╴ ─ 1. source
4291 ");
4292 }
4293
4294 #[test]
4295 fn goto_field_style_function_call() {
4296 assert_snapshot!(goto("
4297create table t(a int);
4298create function b(t) returns int as 'select 1' language sql;
4299select t.b$0 from t;
4300"), @r"
4301 ╭▸
4302 3 │ create function b(t) returns int as 'select 1' language sql;
4303 │ ─ 2. destination
4304 4 │ select t.b from t;
4305 ╰╴ ─ 1. source
4306 ");
4307 }
4308
4309 #[test]
4310 fn goto_field_style_function_call_column_precedence() {
4311 assert_snapshot!(goto("
4312create table t(a int, b int);
4313create function b(t) returns int as 'select 1' language sql;
4314select t.b$0 from t;
4315"), @r"
4316 ╭▸
4317 2 │ create table t(a int, b int);
4318 │ ─ 2. destination
4319 3 │ create function b(t) returns int as 'select 1' language sql;
4320 4 │ select t.b from t;
4321 ╰╴ ─ 1. source
4322 ");
4323 }
4324
4325 #[test]
4326 fn goto_field_style_function_call_table_ref() {
4327 assert_snapshot!(goto("
4328create table t(a int);
4329create function b(t) returns int as 'select 1' language sql;
4330select t$0.b from t;
4331"), @r"
4332 ╭▸
4333 2 │ create table t(a int);
4334 │ ─ 2. destination
4335 3 │ create function b(t) returns int as 'select 1' language sql;
4336 4 │ select t.b from t;
4337 ╰╴ ─ 1. source
4338 ");
4339 }
4340
4341 #[test]
4342 fn goto_function_call_style_in_where() {
4343 assert_snapshot!(goto("
4344create table t(a int, b int);
4345select * from t where a$0(t) > 0;
4346"), @r"
4347 ╭▸
4348 2 │ create table t(a int, b int);
4349 │ ─ 2. destination
4350 3 │ select * from t where a(t) > 0;
4351 ╰╴ ─ 1. source
4352 ");
4353 }
4354
4355 #[test]
4356 fn goto_function_call_style_in_where_function_precedence() {
4357 assert_snapshot!(goto("
4358create table t(a int, b int);
4359create function b(t) returns int as 'select 1' language sql;
4360select * from t where b$0(t) > 0;
4361"), @r"
4362 ╭▸
4363 3 │ create function b(t) returns int as 'select 1' language sql;
4364 │ ─ 2. destination
4365 4 │ select * from t where b(t) > 0;
4366 ╰╴ ─ 1. source
4367 ");
4368 }
4369
4370 #[test]
4371 fn goto_field_style_function_call_in_where() {
4372 assert_snapshot!(goto("
4373create table t(a int);
4374create function b(t) returns int as 'select 1' language sql;
4375select * from t where t.b$0 > 0;
4376"), @r"
4377 ╭▸
4378 3 │ create function b(t) returns int as 'select 1' language sql;
4379 │ ─ 2. destination
4380 4 │ select * from t where t.b > 0;
4381 ╰╴ ─ 1. source
4382 ");
4383 }
4384
4385 #[test]
4386 fn goto_field_style_in_where_column_precedence() {
4387 assert_snapshot!(goto("
4388create table t(a int, b int);
4389create function b(t) returns int as 'select 1' language sql;
4390select * from t where t.b$0 > 0;
4391"), @r"
4392 ╭▸
4393 2 │ create table t(a int, b int);
4394 │ ─ 2. destination
4395 3 │ create function b(t) returns int as 'select 1' language sql;
4396 4 │ select * from t where t.b > 0;
4397 ╰╴ ─ 1. source
4398 ");
4399 }
4400
4401 #[test]
4402 fn goto_function_call_style_table_arg_in_where() {
4403 assert_snapshot!(goto("
4404create table t(a int);
4405select * from t where a(t$0) > 2;
4406"), @r"
4407 ╭▸
4408 2 │ create table t(a int);
4409 │ ─ 2. destination
4410 3 │ select * from t where a(t) > 2;
4411 ╰╴ ─ 1. source
4412 ");
4413 }
4414
4415 #[test]
4416 fn goto_qualified_table_ref_in_where() {
4417 assert_snapshot!(goto("
4418create table t(a int);
4419create function b(t) returns int as 'select 1' language sql;
4420select * from t where t$0.b > 2;
4421"), @r"
4422 ╭▸
4423 2 │ create table t(a int);
4424 │ ─ 2. destination
4425 3 │ create function b(t) returns int as 'select 1' language sql;
4426 4 │ select * from t where t.b > 2;
4427 ╰╴ ─ 1. source
4428 ");
4429 }
4430
4431 #[test]
4432 fn goto_function_call_style_in_order_by() {
4433 assert_snapshot!(goto("
4434create table t(a int, b int);
4435create function b(t) returns int as 'select 1' language sql;
4436select * from t order by b$0(t);
4437"), @r"
4438 ╭▸
4439 3 │ create function b(t) returns int as 'select 1' language sql;
4440 │ ─ 2. destination
4441 4 │ select * from t order by b(t);
4442 ╰╴ ─ 1. source
4443 ");
4444 }
4445
4446 #[test]
4447 fn goto_field_style_in_order_by() {
4448 assert_snapshot!(goto("
4449create table t(a int);
4450create function b(t) returns int as 'select 1' language sql;
4451select * from t order by t.b$0;
4452"), @r"
4453 ╭▸
4454 3 │ create function b(t) returns int as 'select 1' language sql;
4455 │ ─ 2. destination
4456 4 │ select * from t order by t.b;
4457 ╰╴ ─ 1. source
4458 ");
4459 }
4460
4461 #[test]
4462 fn goto_function_call_style_in_group_by() {
4463 assert_snapshot!(goto("
4464create table t(a int, b int);
4465select * from t group by a$0(t);
4466"), @r"
4467 ╭▸
4468 2 │ create table t(a int, b int);
4469 │ ─ 2. destination
4470 3 │ select * from t group by a(t);
4471 ╰╴ ─ 1. source
4472 ");
4473 }
4474
4475 #[test]
4476 fn goto_field_style_in_group_by() {
4477 assert_snapshot!(goto("
4478create table t(a int);
4479create function b(t) returns int as 'select 1' language sql;
4480select * from t group by t.b$0;
4481"), @r"
4482 ╭▸
4483 3 │ create function b(t) returns int as 'select 1' language sql;
4484 │ ─ 2. destination
4485 4 │ select * from t group by t.b;
4486 ╰╴ ─ 1. source
4487 ");
4488 }
4489
4490 #[test]
4491 fn goto_cte_table() {
4492 assert_snapshot!(goto("
4493with x as (select 1 as a)
4494select a from x$0;
4495"), @r"
4496 ╭▸
4497 2 │ with x as (select 1 as a)
4498 │ ─ 2. destination
4499 3 │ select a from x;
4500 ╰╴ ─ 1. source
4501 ");
4502 }
4503
4504 #[test]
4505 fn goto_cte_column() {
4506 assert_snapshot!(goto("
4507with x as (select 1 as a)
4508select a$0 from x;
4509"), @r"
4510 ╭▸
4511 2 │ with x as (select 1 as a)
4512 │ ─ 2. destination
4513 3 │ select a from x;
4514 ╰╴ ─ 1. source
4515 ");
4516 }
4517
4518 #[test]
4519 fn goto_cte_qualified_column_prefers_cte_over_table() {
4520 assert_snapshot!(goto("
4521create table u(id int, b int);
4522with u as (select 1 id, 2 b)
4523select u.id$0 from u;
4524"), @r"
4525 ╭▸
4526 3 │ with u as (select 1 id, 2 b)
4527 │ ── 2. destination
4528 4 │ select u.id from u;
4529 ╰╴ ─ 1. source
4530 ");
4531 }
4532
4533 #[test]
4534 fn goto_subquery_qualified_column() {
4535 assert_snapshot!(goto("
4536select t.a$0 from (select 1 a) t;
4537"), @r"
4538 ╭▸
4539 2 │ select t.a from (select 1 a) t;
4540 ╰╴ ─ 1. source ─ 2. destination
4541 ");
4542 }
4543
4544 #[test]
4545 fn goto_cte_multiple_columns() {
4546 assert_snapshot!(goto("
4547with x as (select 1 as a, 2 as b)
4548select b$0 from x;
4549"), @r"
4550 ╭▸
4551 2 │ with x as (select 1 as a, 2 as b)
4552 │ ─ 2. destination
4553 3 │ select b from x;
4554 ╰╴ ─ 1. source
4555 ");
4556 }
4557
4558 #[test]
4559 fn goto_cte_nested() {
4560 assert_snapshot!(goto("
4561with x as (select 1 as a),
4562 y as (select a from x)
4563select a$0 from y;
4564"), @r"
4565 ╭▸
4566 3 │ y as (select a from x)
4567 │ ─ 2. destination
4568 4 │ select a from y;
4569 ╰╴ ─ 1. source
4570 ");
4571 }
4572
4573 #[test]
4574 fn goto_cte_unnamed_column() {
4575 assert_snapshot!(goto(r#"
4576with x as (select 1)
4577select "?column?"$0 from x;
4578"#), @r#"
4579 ╭▸
4580 2 │ with x as (select 1)
4581 │ ─ 2. destination
4582 3 │ select "?column?" from x;
4583 ╰╴ ─ 1. source
4584 "#);
4585 }
4586
4587 #[test]
4588 fn goto_cte_star_expansion() {
4589 assert_snapshot!(goto("
4590with t as (select 1 a),
4591 y as (select * from t)
4592select a$0 from y;
4593"), @r"
4594 ╭▸
4595 2 │ with t as (select 1 a),
4596 │ ─ 2. destination
4597 3 │ y as (select * from t)
4598 4 │ select a from y;
4599 ╰╴ ─ 1. source
4600 ");
4601 }
4602
4603 #[test]
4604 fn goto_cte_qualified_star_join_column() {
4605 assert_snapshot!(goto("
4606create table u(id int, b int);
4607create table t(id int, a int);
4608
4609with k as (
4610 select u.* from t join u on a = b
4611)
4612select b$0 from k;
4613"), @r"
4614 ╭▸
4615 2 │ create table u(id int, b int);
4616 │ ─ 2. destination
4617 ‡
4618 8 │ select b from k;
4619 ╰╴ ─ 1. source
4620 ");
4621 }
4622
4623 #[test]
4624 fn goto_cte_qualified_star_join_column_with_partial_column_list() {
4625 assert_snapshot!(goto("
4626with
4627 u as (
4628 select 1 id, 2 b
4629 ),
4630 t as (
4631 select 1 id, 2 a
4632 ),
4633 k(x) as (
4634 select u.* from t join u on a = b
4635 )
4636select b$0 from k;
4637"), @r"
4638 ╭▸
4639 4 │ select 1 id, 2 b
4640 │ ─ 2. destination
4641 ‡
4642 12 │ select b from k;
4643 ╰╴ ─ 1. source
4644 ");
4645 }
4646
4647 #[test]
4648 fn goto_cte_reference_inside_cte() {
4649 assert_snapshot!(goto("
4650with t as (select 1 a),
4651 y as (select a$0 from t)
4652select a from y;
4653"), @r"
4654 ╭▸
4655 2 │ with t as (select 1 a),
4656 │ ─ 2. destination
4657 3 │ y as (select a from t)
4658 ╰╴ ─ 1. source
4659 ");
4660 }
4661
4662 #[test]
4663 fn goto_recursive_cte_reference_inside_cte() {
4664 assert_snapshot!(goto("
4665with recursive nums as (
4666 select 1 as n
4667 union all
4668 select n + 1 from nums$0 where n < 5
4669)
4670select * from nums;
4671"), @r"
4672 ╭▸
4673 2 │ with recursive nums as (
4674 │ ──── 2. destination
4675 ‡
4676 5 │ select n + 1 from nums where n < 5
4677 ╰╴ ─ 1. source
4678 ");
4679 }
4680
4681 #[test]
4682 fn goto_cte_with_column_list() {
4683 assert_snapshot!(goto("
4684with t(a) as (select 1)
4685select a$0 from t;
4686"), @r"
4687 ╭▸
4688 2 │ with t(a) as (select 1)
4689 │ ─ 2. destination
4690 3 │ select a from t;
4691 ╰╴ ─ 1. source
4692 ");
4693 }
4694
4695 #[test]
4696 fn goto_cte_with_partial_column_list() {
4697 assert_snapshot!(goto("
4698with t(x) as (select 1 as a, 2 as b)
4699select b$0 from t;
4700"), @r"
4701 ╭▸
4702 2 │ with t(x) as (select 1 as a, 2 as b)
4703 │ ─ 2. destination
4704 3 │ select b from t;
4705 ╰╴ ─ 1. source
4706 ");
4707 }
4708
4709 #[test]
4710 fn goto_cte_with_partial_column_list_renamed() {
4711 assert_snapshot!(goto("
4712with t(x) as (select 1 as a, 2 as b)
4713select x$0 from t;
4714"), @r"
4715 ╭▸
4716 2 │ with t(x) as (select 1 as a, 2 as b)
4717 │ ─ 2. destination
4718 3 │ select x from t;
4719 ╰╴ ─ 1. source
4720 ");
4721 }
4722
4723 #[test]
4724 fn goto_cte_insert_returning_star_column() {
4725 assert_snapshot!(goto("
4726create table t(a int, b int);
4727with inserted as (
4728 insert into t values (1, 2), (3, 4)
4729 returning *
4730)
4731select a$0 from inserted;
4732"), @r"
4733 ╭▸
4734 2 │ create table t(a int, b int);
4735 │ ─ 2. destination
4736 ‡
4737 7 │ select a from inserted;
4738 ╰╴ ─ 1. source
4739 ");
4740 }
4741
4742 #[test]
4743 fn goto_cte_delete_returning_star_column() {
4744 assert_snapshot!(goto("
4745create table t(a int, b int);
4746with deleted as (
4747 delete from t
4748 returning *
4749)
4750select a$0 from deleted;
4751"), @r"
4752 ╭▸
4753 2 │ create table t(a int, b int);
4754 │ ─ 2. destination
4755 ‡
4756 7 │ select a from deleted;
4757 ╰╴ ─ 1. source
4758 ");
4759 }
4760
4761 #[test]
4762 fn goto_cte_update_returning_star_column() {
4763 assert_snapshot!(goto("
4764create table t(a int, b int);
4765with updated as (
4766 update t set a = 42
4767 returning *
4768)
4769select a$0 from updated;
4770"), @r"
4771 ╭▸
4772 2 │ create table t(a int, b int);
4773 │ ─ 2. destination
4774 ‡
4775 7 │ select a from updated;
4776 ╰╴ ─ 1. source
4777 ");
4778 }
4779
4780 #[test]
4781 fn goto_cte_update_returning_column_list_overwrites_column() {
4782 goto_not_found(
4783 "
4784create table t(a int, b int);
4785with updated(c) as (
4786 update t set a = 10
4787 returning a
4788)
4789select a$0 from updated;
4790",
4791 );
4792 }
4793
4794 #[test]
4795 fn goto_cte_column_list_overwrites_column() {
4796 goto_not_found(
4797 "
4798with t(x) as (select 1 as a)
4799select a$0 from t;
4800",
4801 );
4802 }
4803
4804 #[test]
4805 fn goto_cte_shadows_table() {
4806 assert_snapshot!(goto("
4807create table t(a int);
4808with t as (select a$0 from t)
4809select a from t;
4810"), @r"
4811 ╭▸
4812 2 │ create table t(a int);
4813 │ ─ 2. destination
4814 3 │ with t as (select a from t)
4815 ╰╴ ─ 1. source
4816 ");
4817 }
4818
4819 #[test]
4820 fn goto_subquery_column() {
4821 assert_snapshot!(goto("
4822select a$0 from (select 1 a);
4823"), @r"
4824 ╭▸
4825 2 │ select a from (select 1 a);
4826 ╰╴ ─ 1. source ─ 2. destination
4827 ");
4828 }
4829
4830 #[test]
4831 fn goto_subquery_column_with_as() {
4832 assert_snapshot!(goto("
4833select a$0 from (select 1 as a);
4834"), @r"
4835 ╭▸
4836 2 │ select a from (select 1 as a);
4837 ╰╴ ─ 1. source ─ 2. destination
4838 ");
4839 }
4840
4841 #[test]
4842 fn goto_subquery_compound_select_column() {
4843 assert_snapshot!(goto("
4844select c$0 from (select 1 c union select 2 c);
4845"), @r"
4846 ╭▸
4847 2 │ select c from (select 1 c union select 2 c);
4848 ╰╴ ─ 1. source ─ 2. destination
4849 ");
4850 }
4851
4852 #[test]
4853 fn goto_subquery_compound_select_column_with_nested_parens() {
4854 assert_snapshot!(goto("
4855with t as (
4856 select 1 as c
4857)
4858select c$0 from ((select * from t) union all (select * from t));
4859"), @r"
4860 ╭▸
4861 3 │ select 1 as c
4862 │ ─ 2. destination
4863 4 │ )
4864 5 │ select c from ((select * from t) union all (select * from t));
4865 ╰╴ ─ 1. source
4866 ");
4867 }
4868
4869 #[test]
4870 fn goto_subquery_column_multiple_columns() {
4871 assert_snapshot!(goto("
4872select b$0 from (select 1 a, 2 b);
4873"), @r"
4874 ╭▸
4875 2 │ select b from (select 1 a, 2 b);
4876 ╰╴ ─ 1. source ─ 2. destination
4877 ");
4878 }
4879
4880 #[test]
4881 fn goto_subquery_column_nested_parens() {
4882 assert_snapshot!(goto("
4883select a$0 from ((select 1 a));
4884"), @r"
4885 ╭▸
4886 2 │ select a from ((select 1 a));
4887 ╰╴ ─ 1. source ─ 2. destination
4888 ");
4889 }
4890
4891 #[test]
4892 fn goto_subquery_column_star_table() {
4893 assert_snapshot!(goto("
4894create table foo.t(a int);
4895select a$0 from (select * from foo.t);
4896"), @r"
4897 ╭▸
4898 2 │ create table foo.t(a int);
4899 │ ─ 2. destination
4900 3 │ select a from (select * from foo.t);
4901 ╰╴ ─ 1. source
4902 ");
4903 }
4904
4905 #[test]
4906 fn goto_subquery_column_qualified_star_join() {
4907 assert_snapshot!(goto("
4908create table t(a int);
4909create table u(b int);
4910select b$0 from (select u.* from t join u on a = b);
4911"), @r"
4912 ╭▸
4913 3 │ create table u(b int);
4914 │ ─ 2. destination
4915 4 │ select b from (select u.* from t join u on a = b);
4916 ╰╴ ─ 1. source
4917 ");
4918 }
4919
4920 #[test]
4921 fn goto_subquery_column_qualified_star_join_not_found() {
4922 goto_not_found(
4923 "
4924create table t(a int);
4925create table u(b int);
4926select a$0 from (select u.* from t join u on a = b);
4927",
4928 );
4929 }
4930
4931 #[test]
4932 fn goto_subquery_column_alias_list() {
4933 assert_snapshot!(goto("
4934select c$0, t.c from (select 1) t(c);
4935"), @r"
4936 ╭▸
4937 2 │ select c, t.c from (select 1) t(c);
4938 ╰╴ ─ 1. source ─ 2. destination
4939 ");
4940 }
4941
4942 #[test]
4943 fn goto_subquery_column_alias_list_qualified() {
4944 assert_snapshot!(goto("
4945select t.c$0 from (select 1) t(c);
4946"), @r"
4947 ╭▸
4948 2 │ select t.c from (select 1) t(c);
4949 ╰╴ ─ 1. source ─ 2. destination
4950 ");
4951 }
4952
4953 #[test]
4954 fn goto_subquery_column_alias_list_multiple() {
4955 assert_snapshot!(goto("
4956select b$0 from (select 1, 2) t(a, b);
4957"), @r"
4958 ╭▸
4959 2 │ select b from (select 1, 2) t(a, b);
4960 ╰╴ ─ 1. source ─ 2. destination
4961 ");
4962 }
4963
4964 #[test]
4965 fn goto_cte_column_alias_list() {
4966 assert_snapshot!(goto("
4967with x as (select 1)
4968select c$0 from x t(c);
4969"), @r"
4970 ╭▸
4971 3 │ select c from x t(c);
4972 │ ┬ ─ 2. destination
4973 │ │
4974 ╰╴ 1. source
4975 ");
4976 }
4977
4978 #[test]
4979 fn goto_cte_column_alias_list_qualified() {
4980 assert_snapshot!(goto("
4981with x as (select 1)
4982select t.c$0 from x t(c);
4983"), @r"
4984 ╭▸
4985 3 │ select t.c from x t(c);
4986 │ ┬ ─ 2. destination
4987 │ │
4988 ╰╴ 1. source
4989 ");
4990 }
4991
4992 #[test]
4993 fn goto_cte_column_alias_list_multiple() {
4994 assert_snapshot!(goto("
4995with x as (select 1, 2)
4996select b$0 from x t(a, b);
4997"), @r"
4998 ╭▸
4999 3 │ select b from x t(a, b);
5000 ╰╴ ─ 1. source ─ 2. destination
5001 ");
5002 }
5003
5004 #[test]
5005 fn goto_values_column_alias_list() {
5006 assert_snapshot!(goto("
5007select c$0 from (values (1)) t(c);
5008"), @r"
5009 ╭▸
5010 2 │ select c from (values (1)) t(c);
5011 ╰╴ ─ 1. source ─ 2. destination
5012 ");
5013 }
5014
5015 #[test]
5016 fn goto_values_column_alias_list_qualified() {
5017 assert_snapshot!(goto("
5018select t.c$0 from (values (1)) t(c);
5019"), @r"
5020 ╭▸
5021 2 │ select t.c from (values (1)) t(c);
5022 ╰╴ ─ 1. source ─ 2. destination
5023 ");
5024 }
5025
5026 #[test]
5027 fn goto_values_column_alias_list_multiple() {
5028 assert_snapshot!(goto("
5029select b$0 from (values (1, 2)) t(a, b);
5030"), @r"
5031 ╭▸
5032 2 │ select b from (values (1, 2)) t(a, b);
5033 ╰╴ ─ 1. source ─ 2. destination
5034 ");
5035 }
5036
5037 #[test]
5038 fn goto_values_column_alias_list_nested_parens() {
5039 assert_snapshot!(goto("
5040select n$0
5041from ((values (1), (2))) u(n);
5042"), @r"
5043 ╭▸
5044 2 │ select n
5045 │ ─ 1. source
5046 3 │ from ((values (1), (2))) u(n);
5047 ╰╴ ─ 2. destination
5048 ");
5049 }
5050
5051 #[test]
5052 fn goto_table_expr_column() {
5053 assert_snapshot!(goto("
5054create table t(a int, b int);
5055select a$0 from (table t);
5056"), @r"
5057 ╭▸
5058 2 │ create table t(a int, b int);
5059 │ ─ 2. destination
5060 3 │ select a from (table t);
5061 ╰╴ ─ 1. source
5062 ");
5063 }
5064
5065 #[test]
5066 fn goto_table_expr_column_with_cte() {
5067 assert_snapshot!(goto("
5068with x as (select 1 a)
5069select a$0 from (table x);
5070"), @r"
5071 ╭▸
5072 2 │ with x as (select 1 a)
5073 │ ─ 2. destination
5074 3 │ select a from (table x);
5075 ╰╴ ─ 1. source
5076 ");
5077 }
5078
5079 #[test]
5080 fn goto_table_expr_cte_table() {
5081 assert_snapshot!(goto("
5082with t as (select 1 a, 2 b)
5083select * from (table t$0);
5084"), @r"
5085 ╭▸
5086 2 │ with t as (select 1 a, 2 b)
5087 │ ─ 2. destination
5088 3 │ select * from (table t);
5089 ╰╴ ─ 1. source
5090 ");
5091 }
5092
5093 #[test]
5094 fn goto_table_expr_partial_column_alias_list() {
5095 assert_snapshot!(goto("
5096with t as (select 1 a, 2 b)
5097select c, b$0 from (table t) u(c);
5098"), @r"
5099 ╭▸
5100 2 │ with t as (select 1 a, 2 b)
5101 │ ─ 2. destination
5102 3 │ select c, b from (table t) u(c);
5103 ╰╴ ─ 1. source
5104 ");
5105 }
5106
5107 #[test]
5108 fn goto_subquery_partial_column_alias_list() {
5109 assert_snapshot!(goto("
5110select x, b$0 from (select 1 a, 2 b) t(x);
5111"), @r"
5112 ╭▸
5113 2 │ select x, b from (select 1 a, 2 b) t(x);
5114 ╰╴ ─ 1. source ─ 2. destination
5115 ");
5116 }
5117
5118 #[test]
5119 fn goto_subquery_alias_with_column_list_table_ref() {
5120 assert_snapshot!(goto("
5121with t as (select 1 a, 2 b)
5122select z$0 from (select * from t) as z(x, y);
5123"), @"
5124 ╭▸
5125 3 │ select z from (select * from t) as z(x, y);
5126 ╰╴ ─ 1. source ─ 2. destination
5127 ");
5128 }
5129
5130 #[test]
5131 fn goto_subquery_alias_with_column_list_table_ref_shadows_column() {
5132 assert_snapshot!(goto("
5133with t as (select 1 a, 2 b)
5134select z$0 from (select a as z, b from t) as z(x, y);
5135"), @"
5136 ╭▸
5137 3 │ select z from (select a as z, b from t) as z(x, y);
5138 ╰╴ ─ 1. source ─ 2. destination
5139 ");
5140 }
5141
5142 #[test]
5143 fn goto_subquery_nested_paren_alias_with_column_list_table_ref() {
5144 assert_snapshot!(goto("
5145with t as (select 1 a, 2 b, 3 c)
5146select z$0 from ((select * from t)) as z(x, y);
5147"), @"
5148 ╭▸
5149 3 │ select z from ((select * from t)) as z(x, y);
5150 ╰╴ ─ 1. source ─ 2. destination
5151 ");
5152 }
5153
5154 #[test]
5155 fn goto_table_expr_values_cte_partial_alias() {
5156 assert_snapshot!(goto("
5157with t as (values (1, 2), (3, 4))
5158select column2$0 from (table t) u(a);
5159"), @r"
5160 ╭▸
5161 2 │ with t as (values (1, 2), (3, 4))
5162 │ ─ 2. destination
5163 3 │ select column2 from (table t) u(a);
5164 ╰╴ ─ 1. source
5165 ");
5166 }
5167
5168 #[test]
5169 fn goto_cte_with_table_expr() {
5170 assert_snapshot!(goto("
5171create table t(a int, b int);
5172with u as (table t)
5173select a$0 from u;
5174"), @r"
5175 ╭▸
5176 2 │ create table t(a int, b int);
5177 │ ─ 2. destination
5178 3 │ with u as (table t)
5179 4 │ select a from u;
5180 ╰╴ ─ 1. source
5181 ");
5182 }
5183
5184 #[test]
5185 fn goto_cte_with_table_expr_nested() {
5186 assert_snapshot!(goto("
5187with t as (select 1 a, 2 b),
5188 u as (table t)
5189select b$0 from u;
5190"), @r"
5191 ╭▸
5192 2 │ with t as (select 1 a, 2 b),
5193 │ ─ 2. destination
5194 3 │ u as (table t)
5195 4 │ select b from u;
5196 ╰╴ ─ 1. source
5197 ");
5198 }
5199
5200 #[test]
5201 fn goto_insert_table() {
5202 assert_snapshot!(goto("
5203create table users(id int, email text);
5204insert into users$0(id, email) values (1, 'test@example.com');
5205"), @r"
5206 ╭▸
5207 2 │ create table users(id int, email text);
5208 │ ───── 2. destination
5209 3 │ insert into users(id, email) values (1, 'test@example.com');
5210 ╰╴ ─ 1. source
5211 ");
5212 }
5213
5214 #[test]
5215 fn goto_insert_table_with_schema() {
5216 assert_snapshot!(goto("
5217create table public.users(id int, email text);
5218insert into public.users$0(id, email) values (1, 'test@example.com');
5219"), @r"
5220 ╭▸
5221 2 │ create table public.users(id int, email text);
5222 │ ───── 2. destination
5223 3 │ insert into public.users(id, email) values (1, 'test@example.com');
5224 ╰╴ ─ 1. source
5225 ");
5226 }
5227
5228 #[test]
5229 fn goto_insert_column() {
5230 assert_snapshot!(goto("
5231create table users(id int, email text);
5232insert into users(id$0, email) values (1, 'test@example.com');
5233"), @r"
5234 ╭▸
5235 2 │ create table users(id int, email text);
5236 │ ── 2. destination
5237 3 │ insert into users(id, email) values (1, 'test@example.com');
5238 ╰╴ ─ 1. source
5239 ");
5240 }
5241
5242 #[test]
5243 fn goto_insert_column_second() {
5244 assert_snapshot!(goto("
5245create table users(id int, email text);
5246insert into users(id, email$0) values (1, 'test@example.com');
5247"), @r"
5248 ╭▸
5249 2 │ create table users(id int, email text);
5250 │ ───── 2. destination
5251 3 │ insert into users(id, email) values (1, 'test@example.com');
5252 ╰╴ ─ 1. source
5253 ");
5254 }
5255
5256 #[test]
5257 fn goto_insert_column_with_schema() {
5258 assert_snapshot!(goto("
5259create table public.users(id int, email text);
5260insert into public.users(email$0) values ('test@example.com');
5261"), @r"
5262 ╭▸
5263 2 │ create table public.users(id int, email text);
5264 │ ───── 2. destination
5265 3 │ insert into public.users(email) values ('test@example.com');
5266 ╰╴ ─ 1. source
5267 ");
5268 }
5269
5270 #[test]
5271 fn goto_insert_table_with_search_path() {
5272 assert_snapshot!(goto("
5273set search_path to foo;
5274create table foo.users(id int, email text);
5275insert into users$0(id, email) values (1, 'test@example.com');
5276"), @r"
5277 ╭▸
5278 3 │ create table foo.users(id int, email text);
5279 │ ───── 2. destination
5280 4 │ insert into users(id, email) values (1, 'test@example.com');
5281 ╰╴ ─ 1. source
5282 ");
5283 }
5284
5285 #[test]
5286 fn goto_insert_column_with_search_path() {
5287 assert_snapshot!(goto("
5288set search_path to myschema;
5289create table myschema.users(id int, email text, name text);
5290insert into users(email$0, name) values ('test@example.com', 'Test');
5291"), @r"
5292 ╭▸
5293 3 │ create table myschema.users(id int, email text, name text);
5294 │ ───── 2. destination
5295 4 │ insert into users(email, name) values ('test@example.com', 'Test');
5296 ╰╴ ─ 1. source
5297 ");
5298 }
5299
5300 #[test]
5301 fn goto_delete_table() {
5302 assert_snapshot!(goto("
5303create table users(id int, email text);
5304delete from users$0 where id = 1;
5305"), @r"
5306 ╭▸
5307 2 │ create table users(id int, email text);
5308 │ ───── 2. destination
5309 3 │ delete from users where id = 1;
5310 ╰╴ ─ 1. source
5311 ");
5312 }
5313
5314 #[test]
5315 fn goto_delete_table_with_schema() {
5316 assert_snapshot!(goto("
5317create table public.users(id int, email text);
5318delete from public.users$0 where id = 1;
5319"), @r"
5320 ╭▸
5321 2 │ create table public.users(id int, email text);
5322 │ ───── 2. destination
5323 3 │ delete from public.users where id = 1;
5324 ╰╴ ─ 1. source
5325 ");
5326 }
5327
5328 #[test]
5329 fn goto_delete_table_with_search_path() {
5330 assert_snapshot!(goto("
5331set search_path to foo;
5332create table foo.users(id int, email text);
5333delete from users$0 where id = 1;
5334"), @r"
5335 ╭▸
5336 3 │ create table foo.users(id int, email text);
5337 │ ───── 2. destination
5338 4 │ delete from users where id = 1;
5339 ╰╴ ─ 1. source
5340 ");
5341 }
5342
5343 #[test]
5344 fn goto_delete_temp_table() {
5345 assert_snapshot!(goto("
5346create temp table users(id int, email text);
5347delete from users$0 where id = 1;
5348"), @r"
5349 ╭▸
5350 2 │ create temp table users(id int, email text);
5351 │ ───── 2. destination
5352 3 │ delete from users where id = 1;
5353 ╰╴ ─ 1. source
5354 ");
5355 }
5356
5357 #[test]
5358 fn goto_delete_where_column() {
5359 assert_snapshot!(goto("
5360create table users(id int, email text);
5361delete from users where id$0 = 1;
5362"), @r"
5363 ╭▸
5364 2 │ create table users(id int, email text);
5365 │ ── 2. destination
5366 3 │ delete from users where id = 1;
5367 ╰╴ ─ 1. source
5368 ");
5369 }
5370
5371 #[test]
5372 fn goto_delete_where_column_second() {
5373 assert_snapshot!(goto("
5374create table users(id int, email text);
5375delete from users where email$0 = 'test@example.com';
5376"), @r"
5377 ╭▸
5378 2 │ create table users(id int, email text);
5379 │ ───── 2. destination
5380 3 │ delete from users where email = 'test@example.com';
5381 ╰╴ ─ 1. source
5382 ");
5383 }
5384
5385 #[test]
5386 fn goto_delete_where_column_with_schema() {
5387 assert_snapshot!(goto("
5388create table public.users(id int, email text, name text);
5389delete from public.users where name$0 = 'Test';
5390"), @r"
5391 ╭▸
5392 2 │ create table public.users(id int, email text, name text);
5393 │ ──── 2. destination
5394 3 │ delete from public.users where name = 'Test';
5395 ╰╴ ─ 1. source
5396 ");
5397 }
5398
5399 #[test]
5400 fn goto_delete_where_column_with_search_path() {
5401 assert_snapshot!(goto("
5402set search_path to myschema;
5403create table myschema.users(id int, email text, active boolean);
5404delete from users where active$0 = true;
5405"), @r"
5406 ╭▸
5407 3 │ create table myschema.users(id int, email text, active boolean);
5408 │ ────── 2. destination
5409 4 │ delete from users where active = true;
5410 ╰╴ ─ 1. source
5411 ");
5412 }
5413
5414 #[test]
5415 fn goto_delete_where_multiple_columns() {
5416 assert_snapshot!(goto("
5417create table users(id int, email text, active boolean);
5418delete from users where id$0 = 1 and active = true;
5419"), @r"
5420 ╭▸
5421 2 │ create table users(id int, email text, active boolean);
5422 │ ── 2. destination
5423 3 │ delete from users where id = 1 and active = true;
5424 ╰╴ ─ 1. source
5425 ");
5426 }
5427
5428 #[test]
5429 fn goto_delete_using_table() {
5430 assert_snapshot!(goto("
5431create table t(id int, f_id int);
5432create table f(id int, name text);
5433delete from t using f$0 where f_id = f.id and f.name = 'foo';
5434"), @r"
5435 ╭▸
5436 3 │ create table f(id int, name text);
5437 │ ─ 2. destination
5438 4 │ delete from t using f where f_id = f.id and f.name = 'foo';
5439 ╰╴ ─ 1. source
5440 ");
5441 }
5442
5443 #[test]
5444 fn goto_delete_using_table_with_schema() {
5445 assert_snapshot!(goto("
5446create table t(id int, f_id int);
5447create table public.f(id int, name text);
5448delete from t using public.f$0 where f_id = f.id;
5449"), @r"
5450 ╭▸
5451 3 │ create table public.f(id int, name text);
5452 │ ─ 2. destination
5453 4 │ delete from t using public.f where f_id = f.id;
5454 ╰╴ ─ 1. source
5455 ");
5456 }
5457
5458 #[test]
5459 fn goto_delete_using_column_in_where() {
5460 assert_snapshot!(goto("
5461create table t(id int, f_id int);
5462create table f(id int, name text);
5463delete from t using f where f_id = f.id$0 and f.name = 'foo';
5464"), @r"
5465 ╭▸
5466 2 │ create table t(id int, f_id int);
5467 │ ── 2. destination
5468 3 │ create table f(id int, name text);
5469 4 │ delete from t using f where f_id = f.id and f.name = 'foo';
5470 ╰╴ ─ 1. source
5471 ");
5472 }
5473
5474 #[test]
5475 fn goto_select_from_table() {
5476 assert_snapshot!(goto("
5477create table users(id int, email text);
5478select * from users$0;
5479"), @r"
5480 ╭▸
5481 2 │ create table users(id int, email text);
5482 │ ───── 2. destination
5483 3 │ select * from users;
5484 ╰╴ ─ 1. source
5485 ");
5486 }
5487
5488 #[test]
5489 fn goto_select_from_table_with_schema() {
5490 assert_snapshot!(goto("
5491create table public.users(id int, email text);
5492select * from public.users$0;
5493"), @r"
5494 ╭▸
5495 2 │ create table public.users(id int, email text);
5496 │ ───── 2. destination
5497 3 │ select * from public.users;
5498 ╰╴ ─ 1. source
5499 ");
5500 }
5501
5502 #[test]
5503 fn goto_select_from_table_with_search_path() {
5504 assert_snapshot!(goto("
5505set search_path to foo;
5506create table foo.users(id int, email text);
5507select * from users$0;
5508"), @r"
5509 ╭▸
5510 3 │ create table foo.users(id int, email text);
5511 │ ───── 2. destination
5512 4 │ select * from users;
5513 ╰╴ ─ 1. source
5514 ");
5515 }
5516
5517 #[test]
5518 fn goto_select_from_temp_table() {
5519 assert_snapshot!(goto("
5520create temp table users(id int, email text);
5521select * from users$0;
5522"), @r"
5523 ╭▸
5524 2 │ create temp table users(id int, email text);
5525 │ ───── 2. destination
5526 3 │ select * from users;
5527 ╰╴ ─ 1. source
5528 ");
5529 }
5530
5531 #[test]
5532 fn goto_select_from_table_defined_after() {
5533 assert_snapshot!(goto("
5534select * from users$0;
5535create table users(id int, email text);
5536"), @r"
5537 ╭▸
5538 2 │ select * from users;
5539 │ ─ 1. source
5540 3 │ create table users(id int, email text);
5541 ╰╴ ───── 2. destination
5542 ");
5543 }
5544
5545 #[test]
5546 fn goto_select_column() {
5547 assert_snapshot!(goto("
5548create table users(id int, email text);
5549select id$0 from users;
5550"), @r"
5551 ╭▸
5552 2 │ create table users(id int, email text);
5553 │ ── 2. destination
5554 3 │ select id from users;
5555 ╰╴ ─ 1. source
5556 ");
5557 }
5558
5559 #[test]
5560 fn goto_select_column_second() {
5561 assert_snapshot!(goto("
5562create table users(id int, email text);
5563select id, email$0 from users;
5564"), @r"
5565 ╭▸
5566 2 │ create table users(id int, email text);
5567 │ ───── 2. destination
5568 3 │ select id, email from users;
5569 ╰╴ ─ 1. source
5570 ");
5571 }
5572
5573 #[test]
5574 fn goto_select_column_with_schema() {
5575 assert_snapshot!(goto("
5576create table public.users(id int, email text);
5577select email$0 from public.users;
5578"), @r"
5579 ╭▸
5580 2 │ create table public.users(id int, email text);
5581 │ ───── 2. destination
5582 3 │ select email from public.users;
5583 ╰╴ ─ 1. source
5584 ");
5585 }
5586
5587 #[test]
5588 fn goto_select_column_with_search_path() {
5589 assert_snapshot!(goto("
5590set search_path to foo;
5591create table foo.users(id int, email text);
5592select id$0 from users;
5593"), @r"
5594 ╭▸
5595 3 │ create table foo.users(id int, email text);
5596 │ ── 2. destination
5597 4 │ select id from users;
5598 ╰╴ ─ 1. source
5599 ");
5600 }
5601
5602 #[test]
5603 fn goto_select_table_as_column() {
5604 assert_snapshot!(goto("
5605create table t(x bigint, y bigint);
5606select t$0 from t;
5607"), @r"
5608 ╭▸
5609 2 │ create table t(x bigint, y bigint);
5610 │ ─ 2. destination
5611 3 │ select t from t;
5612 ╰╴ ─ 1. source
5613 ");
5614 }
5615
5616 #[test]
5617 fn goto_select_table_star_expansion() {
5618 assert_snapshot!(goto("
5619create table t(id int, a int);
5620select t$0.* from t;
5621"), @r"
5622 ╭▸
5623 2 │ create table t(id int, a int);
5624 │ ─ 2. destination
5625 3 │ select t.* from t;
5626 ╰╴ ─ 1. source
5627 ");
5628 }
5629
5630 #[test]
5631 fn goto_select_table_as_column_with_schema() {
5632 assert_snapshot!(goto("
5633create table public.t(x bigint, y bigint);
5634select t$0 from public.t;
5635"), @r"
5636 ╭▸
5637 2 │ create table public.t(x bigint, y bigint);
5638 │ ─ 2. destination
5639 3 │ select t from public.t;
5640 ╰╴ ─ 1. source
5641 ");
5642 }
5643
5644 #[test]
5645 fn goto_select_table_as_column_with_search_path() {
5646 assert_snapshot!(goto("
5647set search_path to foo;
5648create table foo.users(id int, email text);
5649select users$0 from users;
5650"), @r"
5651 ╭▸
5652 3 │ create table foo.users(id int, email text);
5653 │ ───── 2. destination
5654 4 │ select users from users;
5655 ╰╴ ─ 1. source
5656 ");
5657 }
5658
5659 #[test]
5660 fn goto_select_column_with_same_name_as_table() {
5661 assert_snapshot!(goto("
5662create table t(t int);
5663select t$0 from t;
5664"), @r"
5665 ╭▸
5666 2 │ create table t(t int);
5667 │ ─ 2. destination
5668 3 │ select t from t;
5669 ╰╴ ─ 1. source
5670 ");
5671 }
5672
5673 #[test]
5674 fn goto_drop_schema() {
5675 assert_snapshot!(goto("
5676create schema foo;
5677drop schema foo$0;
5678"), @r"
5679 ╭▸
5680 2 │ create schema foo;
5681 │ ─── 2. destination
5682 3 │ drop schema foo;
5683 ╰╴ ─ 1. source
5684 ");
5685 }
5686
5687 #[test]
5688 fn goto_create_schema_authorization() {
5689 assert_snapshot!(goto("
5690create schema authorization foo$0;
5691"), @r"
5692 ╭▸
5693 2 │ create schema authorization foo;
5694 │ ┬─┬
5695 │ │ │
5696 │ │ 1. source
5697 ╰╴ 2. destination
5698 ");
5699 }
5700
5701 #[test]
5702 fn goto_drop_schema_authorization() {
5703 assert_snapshot!(goto("
5704create schema authorization foo;
5705drop schema foo$0;
5706"), @r"
5707 ╭▸
5708 2 │ create schema authorization foo;
5709 │ ─── 2. destination
5710 3 │ drop schema foo;
5711 ╰╴ ─ 1. source
5712 ");
5713 }
5714
5715 #[test]
5716 fn goto_drop_schema_defined_after() {
5717 assert_snapshot!(goto("
5718drop schema foo$0;
5719create schema foo;
5720"), @r"
5721 ╭▸
5722 2 │ drop schema foo;
5723 │ ─ 1. source
5724 3 │ create schema foo;
5725 ╰╴ ─── 2. destination
5726 ");
5727 }
5728
5729 #[test]
5730 fn goto_schema_qualifier_in_table() {
5731 assert_snapshot!(goto("
5732create schema foo;
5733create table foo$0.t(a int);
5734"), @r"
5735 ╭▸
5736 2 │ create schema foo;
5737 │ ─── 2. destination
5738 3 │ create table foo.t(a int);
5739 ╰╴ ─ 1. source
5740 ");
5741 }
5742
5743 #[test]
5744 fn goto_schema_qualifier_in_drop_table() {
5745 assert_snapshot!(goto("
5746create schema foo;
5747create table foo.t(a int);
5748drop table foo$0.t;
5749"), @r"
5750 ╭▸
5751 2 │ create schema foo;
5752 │ ─── 2. destination
5753 3 │ create table foo.t(a int);
5754 4 │ drop table foo.t;
5755 ╰╴ ─ 1. source
5756 ");
5757 }
5758
5759 #[test]
5760 fn goto_schema_qualifier_multiple_schemas() {
5761 assert_snapshot!(goto("
5762create schema foo;
5763create schema bar;
5764create table bar$0.t(a int);
5765"), @r"
5766 ╭▸
5767 3 │ create schema bar;
5768 │ ─── 2. destination
5769 4 │ create table bar.t(a int);
5770 ╰╴ ─ 1. source
5771 ");
5772 }
5773
5774 #[test]
5775 fn goto_schema_qualifier_in_function_call() {
5776 assert_snapshot!(goto(r#"
5777create schema foo;
5778create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
5779select foo$0.bar();
5780"#), @r"
5781 ╭▸
5782 2 │ create schema foo;
5783 │ ─── 2. destination
5784 3 │ create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
5785 4 │ select foo.bar();
5786 ╰╴ ─ 1. source
5787 ");
5788 }
5789
5790 #[test]
5791 fn goto_schema_qualifier_in_function_call_from_clause() {
5792 assert_snapshot!(goto(r#"
5793create schema myschema;
5794create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
5795select * from myschema$0.get_data();
5796"#), @r"
5797 ╭▸
5798 2 │ create schema myschema;
5799 │ ──────── 2. destination
5800 3 │ create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
5801 4 │ select * from myschema.get_data();
5802 ╰╴ ─ 1. source
5803 ");
5804 }
5805
5806 #[test]
5807 fn goto_schema_qualifier_in_select_from() {
5808 assert_snapshot!(goto("
5809create schema foo;
5810create table foo.t(x int);
5811select x from foo$0.t;
5812"), @r"
5813 ╭▸
5814 2 │ create schema foo;
5815 │ ─── 2. destination
5816 3 │ create table foo.t(x int);
5817 4 │ select x from foo.t;
5818 ╰╴ ─ 1. source
5819 ");
5820 }
5821
5822 #[test]
5823 fn goto_qualified_column_table() {
5824 assert_snapshot!(goto("
5825create table t(a int);
5826select t$0.a from t;
5827"), @r"
5828 ╭▸
5829 2 │ create table t(a int);
5830 │ ─ 2. destination
5831 3 │ select t.a from t;
5832 ╰╴ ─ 1. source
5833 ");
5834 }
5835
5836 #[test]
5837 fn goto_qualified_column_column() {
5838 assert_snapshot!(goto("
5839create table t(a int);
5840select t.a$0 from t;
5841"), @r"
5842 ╭▸
5843 2 │ create table t(a int);
5844 │ ─ 2. destination
5845 3 │ select t.a from t;
5846 ╰╴ ─ 1. source
5847 ");
5848 }
5849
5850 #[test]
5851 fn goto_three_part_qualified_column_schema() {
5852 assert_snapshot!(goto("
5853create schema foo;
5854create table foo.t(a int);
5855select foo$0.t.a from t;
5856"), @r"
5857 ╭▸
5858 2 │ create schema foo;
5859 │ ─── 2. destination
5860 3 │ create table foo.t(a int);
5861 4 │ select foo.t.a from t;
5862 ╰╴ ─ 1. source
5863 ");
5864 }
5865
5866 #[test]
5867 fn goto_three_part_qualified_column_table() {
5868 assert_snapshot!(goto("
5869create schema foo;
5870create table foo.t(a int);
5871select foo.t$0.a from t;
5872"), @r"
5873 ╭▸
5874 3 │ create table foo.t(a int);
5875 │ ─ 2. destination
5876 4 │ select foo.t.a from t;
5877 ╰╴ ─ 1. source
5878 ");
5879 }
5880
5881 #[test]
5882 fn goto_three_part_qualified_column_column() {
5883 assert_snapshot!(goto("
5884create schema foo;
5885create table foo.t(a int);
5886select foo.t.a$0 from t;
5887"), @r"
5888 ╭▸
5889 3 │ create table foo.t(a int);
5890 │ ─ 2. destination
5891 4 │ select foo.t.a from t;
5892 ╰╴ ─ 1. source
5893 ");
5894 }
5895
5896 #[test]
5897 fn goto_cte_values_column1() {
5898 assert_snapshot!(goto("
5899with t as (
5900 values (1, 2), (3, 4)
5901)
5902select column1$0, column2 from t;
5903"), @r"
5904 ╭▸
5905 3 │ values (1, 2), (3, 4)
5906 │ ─ 2. destination
5907 4 │ )
5908 5 │ select column1, column2 from t;
5909 ╰╴ ─ 1. source
5910 ");
5911 }
5912
5913 #[test]
5914 fn goto_cte_values_column2() {
5915 assert_snapshot!(goto("
5916with t as (
5917 values (1, 2), (3, 4)
5918)
5919select column1, column2$0 from t;
5920"), @r"
5921 ╭▸
5922 3 │ values (1, 2), (3, 4)
5923 │ ─ 2. destination
5924 4 │ )
5925 5 │ select column1, column2 from t;
5926 ╰╴ ─ 1. source
5927 ");
5928 }
5929
5930 #[test]
5931 fn goto_cte_values_single_column() {
5932 assert_snapshot!(goto("
5933with t as (
5934 values (1), (2), (3)
5935)
5936select column1$0 from t;
5937"), @r"
5938 ╭▸
5939 3 │ values (1), (2), (3)
5940 │ ─ 2. destination
5941 4 │ )
5942 5 │ select column1 from t;
5943 ╰╴ ─ 1. source
5944 ");
5945 }
5946
5947 #[test]
5948 fn goto_cte_values_multiple_rows() {
5949 assert_snapshot!(goto("
5950with t as (
5951 values
5952 (1, 2, 3),
5953 (4, 5, 6),
5954 (7, 8, 9)
5955)
5956select column3$0 from t;
5957"), @r"
5958 ╭▸
5959 4 │ (1, 2, 3),
5960 │ ─ 2. destination
5961 ‡
5962 8 │ select column3 from t;
5963 ╰╴ ─ 1. source
5964 ");
5965 }
5966
5967 #[test]
5968 fn goto_cte_values_uppercase_column_names() {
5969 assert_snapshot!(goto("
5970with t as (
5971 values (1, 2), (3, 4)
5972)
5973select COLUMN1$0, COLUMN2 from t;
5974"), @r"
5975 ╭▸
5976 3 │ values (1, 2), (3, 4)
5977 │ ─ 2. destination
5978 4 │ )
5979 5 │ select COLUMN1, COLUMN2 from t;
5980 ╰╴ ─ 1. source
5981 ");
5982 }
5983
5984 #[test]
5985 fn goto_qualified_column_with_schema_in_from_table() {
5986 assert_snapshot!(goto("
5987create table foo.t(a int, b int);
5988select t$0.a from foo.t;
5989"), @r"
5990 ╭▸
5991 2 │ create table foo.t(a int, b int);
5992 │ ─ 2. destination
5993 3 │ select t.a from foo.t;
5994 ╰╴ ─ 1. source
5995 ");
5996 }
5997
5998 #[test]
5999 fn goto_qualified_column_with_schema_in_from_column() {
6000 assert_snapshot!(goto("
6001create table foo.t(a int, b int);
6002select t.a$0 from foo.t;
6003"), @r"
6004 ╭▸
6005 2 │ create table foo.t(a int, b int);
6006 │ ─ 2. destination
6007 3 │ select t.a from foo.t;
6008 ╰╴ ─ 1. source
6009 ");
6010 }
6011
6012 #[test]
6013 fn goto_cte_union_all_column() {
6014 assert_snapshot!(goto("
6015with t as (
6016 select 1 as a, 2 as b
6017 union all
6018 select 3, 4
6019)
6020select a$0, b from t;
6021"), @r"
6022 ╭▸
6023 3 │ select 1 as a, 2 as b
6024 │ ─ 2. destination
6025 ‡
6026 7 │ select a, b from t;
6027 ╰╴ ─ 1. source
6028 ");
6029 }
6030
6031 #[test]
6032 fn goto_cte_union_all_column_second() {
6033 assert_snapshot!(goto("
6034with t as (
6035 select 1 as a, 2 as b
6036 union all
6037 select 3, 4
6038)
6039select a, b$0 from t;
6040"), @r"
6041 ╭▸
6042 3 │ select 1 as a, 2 as b
6043 │ ─ 2. destination
6044 ‡
6045 7 │ select a, b from t;
6046 ╰╴ ─ 1. source
6047 ");
6048 }
6049
6050 #[test]
6051 fn goto_cte_union_column() {
6052 assert_snapshot!(goto("
6053with t as (
6054 select 1 as a, 2 as b
6055 union
6056 select 3, 4
6057)
6058select a$0 from t;
6059"), @r"
6060 ╭▸
6061 3 │ select 1 as a, 2 as b
6062 │ ─ 2. destination
6063 ‡
6064 7 │ select a from t;
6065 ╰╴ ─ 1. source
6066 ");
6067 }
6068
6069 #[test]
6070 fn goto_cte_insert_returning_column() {
6071 assert_snapshot!(goto("
6072create table t(a int, b int);
6073with inserted as (
6074 insert into t values (1, 2), (3, 4)
6075 returning a, b
6076)
6077select a$0 from inserted;
6078"), @r"
6079 ╭▸
6080 5 │ returning a, b
6081 │ ─ 2. destination
6082 6 │ )
6083 7 │ select a from inserted;
6084 ╰╴ ─ 1. source
6085 ");
6086 }
6087
6088 #[test]
6089 fn goto_cte_insert_returning_aliased_column() {
6090 assert_snapshot!(goto("
6091create table t(a int, b int);
6092with inserted as (
6093 insert into t values (1, 2), (3, 4)
6094 returning a as x
6095)
6096select x$0 from inserted;
6097"), @r"
6098 ╭▸
6099 5 │ returning a as x
6100 │ ─ 2. destination
6101 6 │ )
6102 7 │ select x from inserted;
6103 ╰╴ ─ 1. source
6104 ");
6105 }
6106
6107 #[test]
6108 fn goto_drop_aggregate() {
6109 assert_snapshot!(goto("
6110create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6111drop aggregate myavg$0(int);
6112"), @r"
6113 ╭▸
6114 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6115 │ ───── 2. destination
6116 3 │ drop aggregate myavg(int);
6117 ╰╴ ─ 1. source
6118 ");
6119 }
6120
6121 #[test]
6122 fn goto_drop_aggregate_with_schema() {
6123 assert_snapshot!(goto("
6124set search_path to public;
6125create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6126drop aggregate public.myavg$0(int);
6127"), @r"
6128 ╭▸
6129 3 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6130 │ ───── 2. destination
6131 4 │ drop aggregate public.myavg(int);
6132 ╰╴ ─ 1. source
6133 ");
6134 }
6135
6136 #[test]
6137 fn goto_drop_aggregate_defined_after() {
6138 assert_snapshot!(goto("
6139drop aggregate myavg$0(int);
6140create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6141"), @r"
6142 ╭▸
6143 2 │ drop aggregate myavg(int);
6144 │ ─ 1. source
6145 3 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6146 ╰╴ ───── 2. destination
6147 ");
6148 }
6149
6150 #[test]
6151 fn goto_aggregate_definition_returns_self() {
6152 assert_snapshot!(goto("
6153create aggregate myavg$0(int) (sfunc = int4_avg_accum, stype = _int8);
6154"), @r"
6155 ╭▸
6156 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6157 │ ┬───┬
6158 │ │ │
6159 │ │ 1. source
6160 ╰╴ 2. destination
6161 ");
6162 }
6163
6164 #[test]
6165 fn goto_drop_aggregate_with_search_path() {
6166 assert_snapshot!(goto("
6167create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6168set search_path to bar;
6169create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6170set search_path to default;
6171drop aggregate myavg$0(int);
6172"), @r"
6173 ╭▸
6174 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6175 │ ───── 2. destination
6176 ‡
6177 6 │ drop aggregate myavg(int);
6178 ╰╴ ─ 1. source
6179 ");
6180 }
6181
6182 #[test]
6183 fn goto_drop_aggregate_multiple() {
6184 assert_snapshot!(goto("
6185create aggregate avg1(int) (sfunc = int4_avg_accum, stype = _int8);
6186create aggregate avg2(int) (sfunc = int4_avg_accum, stype = _int8);
6187drop aggregate avg1(int), avg2$0(int);
6188"), @r"
6189 ╭▸
6190 3 │ create aggregate avg2(int) (sfunc = int4_avg_accum, stype = _int8);
6191 │ ──── 2. destination
6192 4 │ drop aggregate avg1(int), avg2(int);
6193 ╰╴ ─ 1. source
6194 ");
6195 }
6196
6197 #[test]
6198 fn goto_drop_aggregate_overloaded() {
6199 assert_snapshot!(goto("
6200create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
6201create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
6202drop aggregate sum$0(complex);
6203"), @r"
6204 ╭▸
6205 2 │ create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
6206 │ ─── 2. destination
6207 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
6208 4 │ drop aggregate sum(complex);
6209 ╰╴ ─ 1. source
6210 ");
6211 }
6212
6213 #[test]
6214 fn goto_drop_aggregate_second_overload() {
6215 assert_snapshot!(goto("
6216create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
6217create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
6218drop aggregate sum$0(bigint);
6219"), @r"
6220 ╭▸
6221 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
6222 │ ─── 2. destination
6223 4 │ drop aggregate sum(bigint);
6224 ╰╴ ─ 1. source
6225 ");
6226 }
6227
6228 #[test]
6229 fn goto_drop_routine_function() {
6230 assert_snapshot!(goto("
6231create function foo() returns int as $$ select 1 $$ language sql;
6232drop routine foo$0();
6233"), @r"
6234 ╭▸
6235 2 │ create function foo() returns int as $$ select 1 $$ language sql;
6236 │ ─── 2. destination
6237 3 │ drop routine foo();
6238 ╰╴ ─ 1. source
6239 ");
6240 }
6241
6242 #[test]
6243 fn goto_drop_routine_aggregate() {
6244 assert_snapshot!(goto("
6245create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6246drop routine myavg$0(int);
6247"), @r"
6248 ╭▸
6249 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
6250 │ ───── 2. destination
6251 3 │ drop routine myavg(int);
6252 ╰╴ ─ 1. source
6253 ");
6254 }
6255
6256 #[test]
6257 fn goto_drop_routine_with_schema() {
6258 assert_snapshot!(goto("
6259set search_path to public;
6260create function foo() returns int as $$ select 1 $$ language sql;
6261drop routine public.foo$0();
6262"), @r"
6263 ╭▸
6264 3 │ create function foo() returns int as $$ select 1 $$ language sql;
6265 │ ─── 2. destination
6266 4 │ drop routine public.foo();
6267 ╰╴ ─ 1. source
6268 ");
6269 }
6270
6271 #[test]
6272 fn goto_drop_routine_defined_after() {
6273 assert_snapshot!(goto("
6274drop routine foo$0();
6275create function foo() returns int as $$ select 1 $$ language sql;
6276"), @r"
6277 ╭▸
6278 2 │ drop routine foo();
6279 │ ─ 1. source
6280 3 │ create function foo() returns int as $$ select 1 $$ language sql;
6281 ╰╴ ─── 2. destination
6282 ");
6283 }
6284
6285 #[test]
6286 fn goto_drop_routine_with_search_path() {
6287 assert_snapshot!(goto("
6288create function foo() returns int as $$ select 1 $$ language sql;
6289set search_path to bar;
6290create function foo() returns int as $$ select 1 $$ language sql;
6291set search_path to default;
6292drop routine foo$0();
6293"), @r"
6294 ╭▸
6295 2 │ create function foo() returns int as $$ select 1 $$ language sql;
6296 │ ─── 2. destination
6297 ‡
6298 6 │ drop routine foo();
6299 ╰╴ ─ 1. source
6300 ");
6301 }
6302
6303 #[test]
6304 fn goto_drop_routine_overloaded() {
6305 assert_snapshot!(goto("
6306create function add(complex) returns complex as $$ select null $$ language sql;
6307create function add(bigint) returns bigint as $$ select 1 $$ language sql;
6308drop routine add$0(complex);
6309"), @r"
6310 ╭▸
6311 2 │ create function add(complex) returns complex as $$ select null $$ language sql;
6312 │ ─── 2. destination
6313 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
6314 4 │ drop routine add(complex);
6315 ╰╴ ─ 1. source
6316 ");
6317 }
6318
6319 #[test]
6320 fn goto_drop_routine_second_overload() {
6321 assert_snapshot!(goto("
6322create function add(complex) returns complex as $$ select null $$ language sql;
6323create function add(bigint) returns bigint as $$ select 1 $$ language sql;
6324drop routine add$0(bigint);
6325"), @r"
6326 ╭▸
6327 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
6328 │ ─── 2. destination
6329 4 │ drop routine add(bigint);
6330 ╰╴ ─ 1. source
6331 ");
6332 }
6333
6334 #[test]
6335 fn goto_drop_routine_aggregate_overloaded() {
6336 assert_snapshot!(goto("
6337create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
6338create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
6339drop routine sum$0(complex);
6340"), @r"
6341 ╭▸
6342 2 │ create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
6343 │ ─── 2. destination
6344 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
6345 4 │ drop routine sum(complex);
6346 ╰╴ ─ 1. source
6347 ");
6348 }
6349
6350 #[test]
6351 fn goto_drop_routine_multiple() {
6352 assert_snapshot!(goto("
6353create function foo() returns int as $$ select 1 $$ language sql;
6354create function bar() returns int as $$ select 1 $$ language sql;
6355drop routine foo(), bar$0();
6356"), @r"
6357 ╭▸
6358 3 │ create function bar() returns int as $$ select 1 $$ language sql;
6359 │ ─── 2. destination
6360 4 │ drop routine foo(), bar();
6361 ╰╴ ─ 1. source
6362 ");
6363 }
6364
6365 #[test]
6366 fn goto_drop_procedure() {
6367 assert_snapshot!(goto("
6368create procedure foo() language sql as $$ select 1 $$;
6369drop procedure foo$0();
6370"), @r"
6371 ╭▸
6372 2 │ create procedure foo() language sql as $$ select 1 $$;
6373 │ ─── 2. destination
6374 3 │ drop procedure foo();
6375 ╰╴ ─ 1. source
6376 ");
6377 }
6378
6379 #[test]
6380 fn goto_drop_procedure_with_schema() {
6381 assert_snapshot!(goto("
6382set search_path to public;
6383create procedure foo() language sql as $$ select 1 $$;
6384drop procedure public.foo$0();
6385"), @r"
6386 ╭▸
6387 3 │ create procedure foo() language sql as $$ select 1 $$;
6388 │ ─── 2. destination
6389 4 │ drop procedure public.foo();
6390 ╰╴ ─ 1. source
6391 ");
6392 }
6393
6394 #[test]
6395 fn goto_drop_procedure_defined_after() {
6396 assert_snapshot!(goto("
6397drop procedure foo$0();
6398create procedure foo() language sql as $$ select 1 $$;
6399"), @r"
6400 ╭▸
6401 2 │ drop procedure foo();
6402 │ ─ 1. source
6403 3 │ create procedure foo() language sql as $$ select 1 $$;
6404 ╰╴ ─── 2. destination
6405 ");
6406 }
6407
6408 #[test]
6409 fn goto_drop_procedure_with_search_path() {
6410 assert_snapshot!(goto("
6411create procedure foo() language sql as $$ select 1 $$;
6412set search_path to bar;
6413create procedure foo() language sql as $$ select 1 $$;
6414set search_path to default;
6415drop procedure foo$0();
6416"), @r"
6417 ╭▸
6418 2 │ create procedure foo() language sql as $$ select 1 $$;
6419 │ ─── 2. destination
6420 ‡
6421 6 │ drop procedure foo();
6422 ╰╴ ─ 1. source
6423 ");
6424 }
6425
6426 #[test]
6427 fn goto_drop_procedure_overloaded() {
6428 assert_snapshot!(goto("
6429create procedure add(complex) language sql as $$ select null $$;
6430create procedure add(bigint) language sql as $$ select 1 $$;
6431drop procedure add$0(complex);
6432"), @r"
6433 ╭▸
6434 2 │ create procedure add(complex) language sql as $$ select null $$;
6435 │ ─── 2. destination
6436 3 │ create procedure add(bigint) language sql as $$ select 1 $$;
6437 4 │ drop procedure add(complex);
6438 ╰╴ ─ 1. source
6439 ");
6440 }
6441
6442 #[test]
6443 fn goto_drop_procedure_second_overload() {
6444 assert_snapshot!(goto("
6445create procedure add(complex) language sql as $$ select null $$;
6446create procedure add(bigint) language sql as $$ select 1 $$;
6447drop procedure add$0(bigint);
6448"), @r"
6449 ╭▸
6450 3 │ create procedure add(bigint) language sql as $$ select 1 $$;
6451 │ ─── 2. destination
6452 4 │ drop procedure add(bigint);
6453 ╰╴ ─ 1. source
6454 ");
6455 }
6456
6457 #[test]
6458 fn goto_drop_procedure_multiple() {
6459 assert_snapshot!(goto("
6460create procedure foo() language sql as $$ select 1 $$;
6461create procedure bar() language sql as $$ select 1 $$;
6462drop procedure foo(), bar$0();
6463"), @r"
6464 ╭▸
6465 3 │ create procedure bar() language sql as $$ select 1 $$;
6466 │ ─── 2. destination
6467 4 │ drop procedure foo(), bar();
6468 ╰╴ ─ 1. source
6469 ");
6470 }
6471
6472 #[test]
6473 fn goto_procedure_definition_returns_self() {
6474 assert_snapshot!(goto("
6475create procedure foo$0() language sql as $$ select 1 $$;
6476"), @r"
6477 ╭▸
6478 2 │ create procedure foo() language sql as $$ select 1 $$;
6479 │ ┬─┬
6480 │ │ │
6481 │ │ 1. source
6482 ╰╴ 2. destination
6483 ");
6484 }
6485
6486 #[test]
6487 fn goto_call_procedure() {
6488 assert_snapshot!(goto("
6489create procedure foo() language sql as $$ select 1 $$;
6490call foo$0();
6491"), @r"
6492 ╭▸
6493 2 │ create procedure foo() language sql as $$ select 1 $$;
6494 │ ─── 2. destination
6495 3 │ call foo();
6496 ╰╴ ─ 1. source
6497 ");
6498 }
6499
6500 #[test]
6501 fn goto_call_procedure_with_schema() {
6502 assert_snapshot!(goto("
6503create procedure public.foo() language sql as $$ select 1 $$;
6504call public.foo$0();
6505"), @r"
6506 ╭▸
6507 2 │ create procedure public.foo() language sql as $$ select 1 $$;
6508 │ ─── 2. destination
6509 3 │ call public.foo();
6510 ╰╴ ─ 1. source
6511 ");
6512 }
6513
6514 #[test]
6515 fn goto_call_procedure_with_search_path() {
6516 assert_snapshot!(goto("
6517set search_path to myschema;
6518create procedure foo() language sql as $$ select 1 $$;
6519call myschema.foo$0();
6520"), @r"
6521 ╭▸
6522 3 │ create procedure foo() language sql as $$ select 1 $$;
6523 │ ─── 2. destination
6524 4 │ call myschema.foo();
6525 ╰╴ ─ 1. source
6526 ");
6527 }
6528
6529 #[test]
6530 fn goto_drop_routine_procedure() {
6531 assert_snapshot!(goto("
6532create procedure foo() language sql as $$ select 1 $$;
6533drop routine foo$0();
6534"), @r"
6535 ╭▸
6536 2 │ create procedure foo() language sql as $$ select 1 $$;
6537 │ ─── 2. destination
6538 3 │ drop routine foo();
6539 ╰╴ ─ 1. source
6540 ");
6541 }
6542
6543 #[test]
6544 fn goto_drop_routine_prefers_function_over_procedure() {
6545 assert_snapshot!(goto("
6546create function foo() returns int as $$ select 1 $$ language sql;
6547create procedure foo() language sql as $$ select 1 $$;
6548drop routine foo$0();
6549"), @r"
6550 ╭▸
6551 2 │ create function foo() returns int as $$ select 1 $$ language sql;
6552 │ ─── 2. destination
6553 3 │ create procedure foo() language sql as $$ select 1 $$;
6554 4 │ drop routine foo();
6555 ╰╴ ─ 1. source
6556 ");
6557 }
6558
6559 #[test]
6560 fn goto_drop_routine_prefers_aggregate_over_procedure() {
6561 assert_snapshot!(goto("
6562create aggregate foo(int) (sfunc = int4_avg_accum, stype = _int8);
6563create procedure foo(int) language sql as $$ select 1 $$;
6564drop routine foo$0(int);
6565"), @r"
6566 ╭▸
6567 2 │ create aggregate foo(int) (sfunc = int4_avg_accum, stype = _int8);
6568 │ ─── 2. destination
6569 3 │ create procedure foo(int) language sql as $$ select 1 $$;
6570 4 │ drop routine foo(int);
6571 ╰╴ ─ 1. source
6572 ");
6573 }
6574
6575 #[test]
6576 fn goto_table_alias_in_qualified_column() {
6577 assert_snapshot!(goto("
6578create table t(a int8, b text);
6579select f$0.a from t as f;
6580"), @r"
6581 ╭▸
6582 3 │ select f.a from t as f;
6583 ╰╴ ─ 1. source ─ 2. destination
6584 ");
6585 }
6586
6587 #[test]
6588 fn goto_column_through_table_alias() {
6589 assert_snapshot!(goto("
6590create table t(a int8, b text);
6591select f.a$0 from t as f;
6592"), @r"
6593 ╭▸
6594 2 │ create table t(a int8, b text);
6595 │ ─ 2. destination
6596 3 │ select f.a from t as f;
6597 ╰╴ ─ 1. source
6598 ");
6599 }
6600
6601 #[test]
6602 fn goto_cte_alias_renamed_column() {
6603 assert_snapshot!(goto("
6604with t as (select 1 a, 2 b)
6605select f.x$0 from t as f(x);
6606"), @r"
6607 ╭▸
6608 3 │ select f.x from t as f(x);
6609 ╰╴ ─ 1. source ─ 2. destination
6610 ");
6611 }
6612
6613 #[test]
6614 fn goto_cte_alias_unrenamed_column() {
6615 assert_snapshot!(goto("
6616with t as (select 1 a, 2 b)
6617select f.b$0 from t as f(x);
6618"), @r"
6619 ╭▸
6620 2 │ with t as (select 1 a, 2 b)
6621 │ ─ 2. destination
6622 3 │ select f.b from t as f(x);
6623 ╰╴ ─ 1. source
6624 ");
6625 }
6626
6627 #[test]
6628 fn goto_join_table() {
6629 assert_snapshot!(goto("
6630create table users(id int, email text);
6631create table messages(id int, user_id int, message text);
6632select * from users join messages$0 on users.id = messages.user_id;
6633"), @r"
6634 ╭▸
6635 3 │ create table messages(id int, user_id int, message text);
6636 │ ──────── 2. destination
6637 4 │ select * from users join messages on users.id = messages.user_id;
6638 ╰╴ ─ 1. source
6639 ");
6640 }
6641
6642 #[test]
6643 fn goto_join_qualified_column_from_joined_table() {
6644 assert_snapshot!(goto("
6645create table users(id int, email text);
6646create table messages(id int, user_id int, message text);
6647select messages.user_id$0 from users join messages on users.id = messages.user_id;
6648"), @r"
6649 ╭▸
6650 3 │ create table messages(id int, user_id int, message text);
6651 │ ─────── 2. destination
6652 4 │ select messages.user_id from users join messages on users.id = messages.user_id;
6653 ╰╴ ─ 1. source
6654 ");
6655 }
6656
6657 #[test]
6658 fn goto_join_qualified_column_from_base_table() {
6659 assert_snapshot!(goto("
6660create table users(id int, email text);
6661create table messages(id int, user_id int, message text);
6662select users.id$0 from users join messages on users.id = messages.user_id;
6663"), @r"
6664 ╭▸
6665 2 │ create table users(id int, email text);
6666 │ ── 2. destination
6667 3 │ create table messages(id int, user_id int, message text);
6668 4 │ select users.id from users join messages on users.id = messages.user_id;
6669 ╰╴ ─ 1. source
6670 ");
6671 }
6672
6673 #[test]
6674 fn goto_join_multiple_joins() {
6675 assert_snapshot!(goto("
6676create table users(id int, name text);
6677create table messages(id int, user_id int, message text);
6678create table comments(id int, message_id int, text text);
6679select comments.text$0 from users
6680 join messages on users.id = messages.user_id
6681 join comments on messages.id = comments.message_id;
6682"), @r"
6683 ╭▸
6684 4 │ create table comments(id int, message_id int, text text);
6685 │ ──── 2. destination
6686 5 │ select comments.text from users
6687 ╰╴ ─ 1. source
6688 ");
6689 }
6690
6691 #[test]
6692 fn goto_join_with_aliases() {
6693 assert_snapshot!(goto("
6694create table users(id int, name text);
6695create table messages(id int, user_id int, message text);
6696select m.message$0 from users as u join messages as m on u.id = m.user_id;
6697"), @r"
6698 ╭▸
6699 3 │ create table messages(id int, user_id int, message text);
6700 │ ─────── 2. destination
6701 4 │ select m.message from users as u join messages as m on u.id = m.user_id;
6702 ╰╴ ─ 1. source
6703 ");
6704 }
6705
6706 #[test]
6707 fn goto_join_unqualified_column() {
6708 assert_snapshot!(goto("
6709create table users(id int, email text);
6710create table messages(id int, user_id int, message text);
6711select message$0 from users join messages on users.id = messages.user_id;
6712"), @r"
6713 ╭▸
6714 3 │ create table messages(id int, user_id int, message text);
6715 │ ─────── 2. destination
6716 4 │ select message from users join messages on users.id = messages.user_id;
6717 ╰╴ ─ 1. source
6718 ");
6719 }
6720
6721 #[test]
6722 fn goto_join_with_many_tables() {
6723 assert_snapshot!(goto("
6724create table users(id int, email text);
6725create table messages(id int, user_id int, message text);
6726create table logins(id int, user_id int, at timestamptz);
6727create table posts(id int, user_id int, post text);
6728
6729select post$0
6730 from users
6731 join messages
6732 on users.id = messages.user_id
6733 join logins
6734 on users.id = logins.user_id
6735 join posts
6736 on users.id = posts.user_id
6737"), @r"
6738 ╭▸
6739 5 │ create table posts(id int, user_id int, post text);
6740 │ ──── 2. destination
6741 6 │
6742 7 │ select post
6743 ╰╴ ─ 1. source
6744 ");
6745 }
6746
6747 #[test]
6748 fn goto_join_with_schema() {
6749 assert_snapshot!(goto("
6750create schema foo;
6751create table foo.users(id int, email text);
6752create table foo.messages(id int, user_id int, message text);
6753select foo.messages.message$0 from foo.users join foo.messages on foo.users.id = foo.messages.user_id;
6754"), @r"
6755 ╭▸
6756 4 │ create table foo.messages(id int, user_id int, message text);
6757 │ ─────── 2. destination
6758 5 │ select foo.messages.message from foo.users join foo.messages on foo.users.id = foo.messages.user_id;
6759 ╰╴ ─ 1. source
6760 ");
6761 }
6762
6763 #[test]
6764 fn goto_join_left_join() {
6765 assert_snapshot!(goto("
6766create table users(id int, email text);
6767create table messages(id int, user_id int, message text);
6768select messages.message$0 from users left join messages on users.id = messages.user_id;
6769"), @r"
6770 ╭▸
6771 3 │ create table messages(id int, user_id int, message text);
6772 │ ─────── 2. destination
6773 4 │ select messages.message from users left join messages on users.id = messages.user_id;
6774 ╰╴ ─ 1. source
6775 ");
6776 }
6777
6778 #[test]
6779 fn goto_join_on_table_qualifier() {
6780 assert_snapshot!(goto("
6781create table t(a int);
6782create table u(a int);
6783select * from t join u on u$0.a = t.a;
6784"), @r"
6785 ╭▸
6786 3 │ create table u(a int);
6787 │ ─ 2. destination
6788 4 │ select * from t join u on u.a = t.a;
6789 ╰╴ ─ 1. source
6790 ");
6791 }
6792
6793 #[test]
6794 fn goto_join_on_column() {
6795 assert_snapshot!(goto("
6796create table t(a int);
6797create table u(a int);
6798select * from t join u on u.a$0 = t.a;
6799"), @r"
6800 ╭▸
6801 3 │ create table u(a int);
6802 │ ─ 2. destination
6803 4 │ select * from t join u on u.a = t.a;
6804 ╰╴ ─ 1. source
6805 ");
6806 }
6807
6808 #[test]
6809 fn goto_join_using_column() {
6810 assert_snapshot!(goto("
6811create table t(a int);
6812create table u(a int);
6813select * from t join u using (a$0);
6814"), @r"
6815 ╭▸
6816 2 │ create table t(a int);
6817 │ ─ 2. destination
6818 3 │ create table u(a int);
6819 │ ─ 3. destination
6820 4 │ select * from t join u using (a);
6821 ╰╴ ─ 1. source
6822 ");
6823 }
6824
6825 #[test]
6826 fn goto_insert_select_cte_column() {
6827 assert_snapshot!(goto("
6828create table users(id int, email text);
6829with new_data as (
6830 select 1 as id, 'test@example.com' as email
6831)
6832insert into users (id, email)
6833select id$0, email from new_data;
6834"), @r"
6835 ╭▸
6836 4 │ select 1 as id, 'test@example.com' as email
6837 │ ── 2. destination
6838 ‡
6839 7 │ select id, email from new_data;
6840 ╰╴ ─ 1. source
6841 ");
6842 }
6843
6844 #[test]
6845 fn goto_insert_select_cte_column_second() {
6846 assert_snapshot!(goto("
6847create table users(id int, email text);
6848with new_data as (
6849 select 1 as id, 'test@example.com' as email
6850)
6851insert into users (id, email)
6852select id, email$0 from new_data;
6853"), @r"
6854 ╭▸
6855 4 │ select 1 as id, 'test@example.com' as email
6856 │ ───── 2. destination
6857 ‡
6858 7 │ select id, email from new_data;
6859 ╰╴ ─ 1. source
6860 ");
6861 }
6862
6863 #[test]
6864 fn goto_insert_select_cte_table() {
6865 assert_snapshot!(goto("
6866create table users(id int, email text);
6867with new_data as (
6868 select 1 as id, 'test@example.com' as email
6869)
6870insert into users (id, email)
6871select id, email from new_data$0;
6872"), @r"
6873 ╭▸
6874 3 │ with new_data as (
6875 │ ──────── 2. destination
6876 ‡
6877 7 │ select id, email from new_data;
6878 ╰╴ ─ 1. source
6879 ");
6880 }
6881
6882 #[test]
6883 fn goto_delete_cte_column() {
6884 assert_snapshot!(goto("
6885create table users(id int, email text);
6886with old_data as (
6887 select 1 as id
6888)
6889delete from users where id in (select id$0 from old_data);
6890"), @r"
6891 ╭▸
6892 4 │ select 1 as id
6893 │ ── 2. destination
6894 5 │ )
6895 6 │ delete from users where id in (select id from old_data);
6896 ╰╴ ─ 1. source
6897 ");
6898 }
6899
6900 #[test]
6901 fn goto_update_table() {
6902 assert_snapshot!(goto("
6903create table users(id int, email text);
6904update users$0 set email = 'new@example.com';
6905"), @r"
6906 ╭▸
6907 2 │ create table users(id int, email text);
6908 │ ───── 2. destination
6909 3 │ update users set email = 'new@example.com';
6910 ╰╴ ─ 1. source
6911 ");
6912 }
6913
6914 #[test]
6915 fn goto_update_table_with_schema() {
6916 assert_snapshot!(goto("
6917create table public.users(id int, email text);
6918update public.users$0 set email = 'new@example.com';
6919"), @r"
6920 ╭▸
6921 2 │ create table public.users(id int, email text);
6922 │ ───── 2. destination
6923 3 │ update public.users set email = 'new@example.com';
6924 ╰╴ ─ 1. source
6925 ");
6926 }
6927
6928 #[test]
6929 fn goto_update_table_with_search_path() {
6930 assert_snapshot!(goto("
6931set search_path to foo;
6932create table foo.users(id int, email text);
6933update users$0 set email = 'new@example.com';
6934"), @r"
6935 ╭▸
6936 3 │ create table foo.users(id int, email text);
6937 │ ───── 2. destination
6938 4 │ update users set email = 'new@example.com';
6939 ╰╴ ─ 1. source
6940 ");
6941 }
6942
6943 #[test]
6944 fn goto_update_where_column() {
6945 assert_snapshot!(goto("
6946create table users(id int, email text);
6947update users set email = 'new@example.com' where id$0 = 1;
6948"), @r"
6949 ╭▸
6950 2 │ create table users(id int, email text);
6951 │ ── 2. destination
6952 3 │ update users set email = 'new@example.com' where id = 1;
6953 ╰╴ ─ 1. source
6954 ");
6955 }
6956
6957 #[test]
6958 fn goto_update_where_column_with_schema() {
6959 assert_snapshot!(goto("
6960create table public.users(id int, email text);
6961update public.users set email = 'new@example.com' where id$0 = 1;
6962"), @r"
6963 ╭▸
6964 2 │ create table public.users(id int, email text);
6965 │ ── 2. destination
6966 3 │ update public.users set email = 'new@example.com' where id = 1;
6967 ╰╴ ─ 1. source
6968 ");
6969 }
6970
6971 #[test]
6972 fn goto_update_where_column_with_search_path() {
6973 assert_snapshot!(goto("
6974set search_path to foo;
6975create table foo.users(id int, email text);
6976update users set email = 'new@example.com' where id$0 = 1;
6977"), @r"
6978 ╭▸
6979 3 │ create table foo.users(id int, email text);
6980 │ ── 2. destination
6981 4 │ update users set email = 'new@example.com' where id = 1;
6982 ╰╴ ─ 1. source
6983 ");
6984 }
6985
6986 #[test]
6987 fn goto_update_set_column() {
6988 assert_snapshot!(goto("
6989create table users(id int, email text);
6990update users set email$0 = 'new@example.com' where id = 1;
6991"), @r"
6992 ╭▸
6993 2 │ create table users(id int, email text);
6994 │ ───── 2. destination
6995 3 │ update users set email = 'new@example.com' where id = 1;
6996 ╰╴ ─ 1. source
6997 ");
6998 }
6999
7000 #[test]
7001 fn goto_update_set_column_with_schema() {
7002 assert_snapshot!(goto("
7003create table public.users(id int, email text);
7004update public.users set email$0 = 'new@example.com' where id = 1;
7005"), @r"
7006 ╭▸
7007 2 │ create table public.users(id int, email text);
7008 │ ───── 2. destination
7009 3 │ update public.users set email = 'new@example.com' where id = 1;
7010 ╰╴ ─ 1. source
7011 ");
7012 }
7013
7014 #[test]
7015 fn goto_update_set_column_with_search_path() {
7016 assert_snapshot!(goto("
7017set search_path to foo;
7018create table foo.users(id int, email text);
7019update users set email$0 = 'new@example.com' where id = 1;
7020"), @r"
7021 ╭▸
7022 3 │ create table foo.users(id int, email text);
7023 │ ───── 2. destination
7024 4 │ update users set email = 'new@example.com' where id = 1;
7025 ╰╴ ─ 1. source
7026 ");
7027 }
7028
7029 #[test]
7030 fn goto_update_from_table() {
7031 assert_snapshot!(goto("
7032create table users(id int, email text);
7033create table messages(id int, user_id int, email text);
7034update users set email = messages.email from messages$0 where users.id = messages.user_id;
7035"), @r"
7036 ╭▸
7037 3 │ create table messages(id int, user_id int, email text);
7038 │ ──────── 2. destination
7039 4 │ update users set email = messages.email from messages where users.id = messages.user_id;
7040 ╰╴ ─ 1. source
7041 ");
7042 }
7043
7044 #[test]
7045 fn goto_update_from_table_with_schema() {
7046 assert_snapshot!(goto("
7047create table users(id int, email text);
7048create table public.messages(id int, user_id int, email text);
7049update users set email = messages.email from public.messages$0 where users.id = messages.user_id;
7050"), @r"
7051 ╭▸
7052 3 │ create table public.messages(id int, user_id int, email text);
7053 │ ──────── 2. destination
7054 4 │ update users set email = messages.email from public.messages where users.id = messages.user_id;
7055 ╰╴ ─ 1. source
7056 ");
7057 }
7058
7059 #[test]
7060 fn goto_update_from_table_with_search_path() {
7061 assert_snapshot!(goto("
7062set search_path to foo;
7063create table users(id int, email text);
7064create table foo.messages(id int, user_id int, email text);
7065update users set email = messages.email from messages$0 where users.id = messages.user_id;
7066"), @r"
7067 ╭▸
7068 4 │ create table foo.messages(id int, user_id int, email text);
7069 │ ──────── 2. destination
7070 5 │ update users set email = messages.email from messages where users.id = messages.user_id;
7071 ╰╴ ─ 1. source
7072 ");
7073 }
7074
7075 #[test]
7076 fn goto_update_with_cte_table() {
7077 assert_snapshot!(goto("
7078create table users(id int, email text);
7079with new_data as (
7080 select 1 as id, 'new@example.com' as email
7081)
7082update users set email = new_data.email from new_data$0 where users.id = new_data.id;
7083"), @r"
7084 ╭▸
7085 3 │ with new_data as (
7086 │ ──────── 2. destination
7087 ‡
7088 6 │ update users set email = new_data.email from new_data where users.id = new_data.id;
7089 ╰╴ ─ 1. source
7090 ");
7091 }
7092
7093 #[test]
7094 fn goto_update_with_cte_column_in_set() {
7095 assert_snapshot!(goto("
7096create table users(id int, email text);
7097with new_data as (
7098 select 1 as id, 'new@example.com' as email
7099)
7100update users set email = new_data.email$0 from new_data where users.id = new_data.id;
7101"), @r"
7102 ╭▸
7103 4 │ select 1 as id, 'new@example.com' as email
7104 │ ───── 2. destination
7105 5 │ )
7106 6 │ update users set email = new_data.email from new_data where users.id = new_data.id;
7107 ╰╴ ─ 1. source
7108 ");
7109 }
7110
7111 #[test]
7112 fn goto_update_with_cte_column_in_where() {
7113 assert_snapshot!(goto("
7114create table users(id int, email text);
7115with new_data as (
7116 select 1 as id, 'new@example.com' as email
7117)
7118update users set email = new_data.email from new_data where new_data.id$0 = users.id;
7119"), @r"
7120 ╭▸
7121 4 │ select 1 as id, 'new@example.com' as email
7122 │ ── 2. destination
7123 5 │ )
7124 6 │ update users set email = new_data.email from new_data where new_data.id = users.id;
7125 ╰╴ ─ 1. source
7126 ");
7127 }
7128
7129 #[test]
7130 fn goto_update_with_cte_values() {
7131 assert_snapshot!(goto("
7132create table users(id int, email text);
7133with new_data as (
7134 values (1, 'new@example.com')
7135)
7136update users set email = new_data.column2$0 from new_data where users.id = new_data.column1;
7137"), @r"
7138 ╭▸
7139 4 │ values (1, 'new@example.com')
7140 │ ───────────────── 2. destination
7141 5 │ )
7142 6 │ update users set email = new_data.column2 from new_data where users.id = new_data.column1;
7143 ╰╴ ─ 1. source
7144 ");
7145 }
7146
7147 #[test]
7148 fn goto_truncate_table() {
7149 assert_snapshot!(goto("
7150create table t();
7151truncate table t$0;
7152"), @r"
7153 ╭▸
7154 2 │ create table t();
7155 │ ─ 2. destination
7156 3 │ truncate table t;
7157 ╰╴ ─ 1. source
7158 ");
7159 }
7160
7161 #[test]
7162 fn goto_truncate_table_without_table_keyword() {
7163 assert_snapshot!(goto("
7164create table t();
7165truncate t$0;
7166"), @r"
7167 ╭▸
7168 2 │ create table t();
7169 │ ─ 2. destination
7170 3 │ truncate t;
7171 ╰╴ ─ 1. source
7172 ");
7173 }
7174
7175 #[test]
7176 fn goto_truncate_multiple_tables() {
7177 assert_snapshot!(goto("
7178create table t1();
7179create table t2();
7180truncate t1, t2$0;
7181"), @r"
7182 ╭▸
7183 3 │ create table t2();
7184 │ ── 2. destination
7185 4 │ truncate t1, t2;
7186 ╰╴ ─ 1. source
7187 ");
7188 }
7189
7190 #[test]
7191 fn goto_lock_table() {
7192 assert_snapshot!(goto("
7193create table t();
7194lock table t$0;
7195"), @r"
7196 ╭▸
7197 2 │ create table t();
7198 │ ─ 2. destination
7199 3 │ lock table t;
7200 ╰╴ ─ 1. source
7201 ");
7202 }
7203
7204 #[test]
7205 fn goto_lock_table_without_table_keyword() {
7206 assert_snapshot!(goto("
7207create table t();
7208lock t$0;
7209"), @r"
7210 ╭▸
7211 2 │ create table t();
7212 │ ─ 2. destination
7213 3 │ lock t;
7214 ╰╴ ─ 1. source
7215 ");
7216 }
7217
7218 #[test]
7219 fn goto_lock_multiple_tables() {
7220 assert_snapshot!(goto("
7221create table t1();
7222create table t2();
7223lock t1, t2$0;
7224"), @r"
7225 ╭▸
7226 3 │ create table t2();
7227 │ ── 2. destination
7228 4 │ lock t1, t2;
7229 ╰╴ ─ 1. source
7230 ");
7231 }
7232
7233 #[test]
7234 fn goto_vacuum_table() {
7235 assert_snapshot!(goto("
7236create table users(id int, email text);
7237vacuum users$0;
7238"), @r"
7239 ╭▸
7240 2 │ create table users(id int, email text);
7241 │ ───── 2. destination
7242 3 │ vacuum users;
7243 ╰╴ ─ 1. source
7244 ");
7245 }
7246
7247 #[test]
7248 fn goto_vacuum_multiple_tables() {
7249 assert_snapshot!(goto("
7250create table t1();
7251create table t2();
7252vacuum t1, t2$0;
7253"), @r"
7254 ╭▸
7255 3 │ create table t2();
7256 │ ── 2. destination
7257 4 │ vacuum t1, t2;
7258 ╰╴ ─ 1. source
7259 ");
7260 }
7261
7262 #[test]
7263 fn goto_alter_table() {
7264 assert_snapshot!(goto("
7265create table users(id int, email text);
7266alter table users$0 alter email set not null;
7267"), @r"
7268 ╭▸
7269 2 │ create table users(id int, email text);
7270 │ ───── 2. destination
7271 3 │ alter table users alter email set not null;
7272 ╰╴ ─ 1. source
7273 ");
7274 }
7275
7276 #[test]
7277 fn goto_alter_table_column() {
7278 assert_snapshot!(goto("
7279create table users(id int, email text);
7280alter table users alter email$0 set not null;
7281"), @r"
7282 ╭▸
7283 2 │ create table users(id int, email text);
7284 │ ───── 2. destination
7285 3 │ alter table users alter email set not null;
7286 ╰╴ ─ 1. source
7287 ");
7288 }
7289
7290 #[test]
7291 fn goto_alter_table_column_with_column_keyword() {
7292 assert_snapshot!(goto("
7293create table users(id int, email text);
7294alter table users alter column email$0 set not null;
7295"), @r"
7296 ╭▸
7297 2 │ create table users(id int, email text);
7298 │ ───── 2. destination
7299 3 │ alter table users alter column email set not null;
7300 ╰╴ ─ 1. source
7301 ");
7302 }
7303
7304 #[test]
7305 fn goto_alter_table_add_column() {
7306 assert_snapshot!(goto("
7307create table users(id int);
7308alter table users$0 add column email text;
7309"), @r"
7310 ╭▸
7311 2 │ create table users(id int);
7312 │ ───── 2. destination
7313 3 │ alter table users add column email text;
7314 ╰╴ ─ 1. source
7315 ");
7316 }
7317
7318 #[test]
7319 fn goto_alter_table_drop_column() {
7320 assert_snapshot!(goto("
7321create table users(id int, email text);
7322alter table users drop column email$0;
7323"), @r"
7324 ╭▸
7325 2 │ create table users(id int, email text);
7326 │ ───── 2. destination
7327 3 │ alter table users drop column email;
7328 ╰╴ ─ 1. source
7329 ");
7330 }
7331
7332 #[test]
7333 fn goto_alter_table_drop_column_table_name() {
7334 assert_snapshot!(goto("
7335create table users(id int, email text);
7336alter table users$0 drop column email;
7337"), @r"
7338 ╭▸
7339 2 │ create table users(id int, email text);
7340 │ ───── 2. destination
7341 3 │ alter table users drop column email;
7342 ╰╴ ─ 1. source
7343 ");
7344 }
7345
7346 #[test]
7347 fn goto_alter_table_add_constraint_using_index() {
7348 assert_snapshot!(goto("
7349create table u(id int);
7350create index my_index on u (id);
7351alter table u add constraint uq unique using index my_in$0dex;
7352"), @r"
7353 ╭▸
7354 3 │ create index my_index on u (id);
7355 │ ──────── 2. destination
7356 4 │ alter table u add constraint uq unique using index my_index;
7357 ╰╴ ─ 1. source
7358 ");
7359 }
7360
7361 #[test]
7362 fn goto_alter_table_owner_to_role() {
7363 assert_snapshot!(goto("
7364create role reader;
7365create table t(id int);
7366alter table t owner to read$0er;
7367"), @r"
7368 ╭▸
7369 2 │ create role reader;
7370 │ ────── 2. destination
7371 3 │ create table t(id int);
7372 4 │ alter table t owner to reader;
7373 ╰╴ ─ 1. source
7374 ");
7375 }
7376
7377 #[test]
7378 fn goto_alter_table_set_tablespace() {
7379 assert_snapshot!(goto("
7380create tablespace ts location '/tmp/ts';
7381create table t(id int);
7382alter table t set tablespace t$0s;
7383"), @r"
7384 ╭▸
7385 2 │ create tablespace ts location '/tmp/ts';
7386 │ ── 2. destination
7387 3 │ create table t(id int);
7388 4 │ alter table t set tablespace ts;
7389 ╰╴ ─ 1. source
7390 ");
7391 }
7392
7393 #[test]
7394 fn goto_alter_table_set_schema() {
7395 assert_snapshot!(goto("
7396create schema foo;
7397create table t(id int);
7398alter table t set schema fo$0o;
7399"), @r"
7400 ╭▸
7401 2 │ create schema foo;
7402 │ ─── 2. destination
7403 3 │ create table t(id int);
7404 4 │ alter table t set schema foo;
7405 ╰╴ ─ 1. source
7406 ");
7407 }
7408
7409 #[test]
7410 fn goto_alter_table_attach_partition() {
7411 assert_snapshot!(goto("
7412create table parent (id int) partition by range (id);
7413create table child (id int);
7414alter table parent attach partition ch$0ild for values from (1) to (10);
7415"), @r"
7416 ╭▸
7417 3 │ create table child (id int);
7418 │ ───── 2. destination
7419 4 │ alter table parent attach partition child for values from (1) to (10);
7420 ╰╴ ─ 1. source
7421 ");
7422 }
7423
7424 #[test]
7425 fn goto_alter_table_detach_partition() {
7426 assert_snapshot!(goto("
7427create table parent (id int) partition by range (id);
7428create table child partition of parent for values from (1) to (10);
7429alter table parent detach partition ch$0ild;
7430"), @r"
7431 ╭▸
7432 3 │ create table child partition of parent for values from (1) to (10);
7433 │ ───── 2. destination
7434 4 │ alter table parent detach partition child;
7435 ╰╴ ─ 1. source
7436 ");
7437 }
7438
7439 #[test]
7440 fn goto_comment_on_table() {
7441 assert_snapshot!(goto("
7442create table t(id int);
7443comment on table t$0 is '';
7444"), @r"
7445 ╭▸
7446 2 │ create table t(id int);
7447 │ ─ 2. destination
7448 3 │ comment on table t is '';
7449 ╰╴ ─ 1. source
7450 ");
7451 }
7452
7453 #[test]
7454 fn goto_comment_on_column() {
7455 assert_snapshot!(goto("
7456create table t(id int);
7457comment on column t.id$0 is '';
7458"), @r"
7459 ╭▸
7460 2 │ create table t(id int);
7461 │ ── 2. destination
7462 3 │ comment on column t.id is '';
7463 ╰╴ ─ 1. source
7464 ");
7465 }
7466
7467 #[test]
7468 fn goto_refresh_materialized_view() {
7469 assert_snapshot!(goto("
7470create materialized view mv as select 1;
7471refresh materialized view mv$0;
7472"), @r"
7473 ╭▸
7474 2 │ create materialized view mv as select 1;
7475 │ ── 2. destination
7476 3 │ refresh materialized view mv;
7477 ╰╴ ─ 1. source
7478 ");
7479 }
7480
7481 #[test]
7482 fn goto_refresh_materialized_view_concurrently() {
7483 assert_snapshot!(goto("
7484create materialized view mv as select 1;
7485refresh materialized view concurrently mv$0;
7486"), @r"
7487 ╭▸
7488 2 │ create materialized view mv as select 1;
7489 │ ── 2. destination
7490 3 │ refresh materialized view concurrently mv;
7491 ╰╴ ─ 1. source
7492 ");
7493 }
7494
7495 #[test]
7496 fn goto_reindex_table() {
7497 assert_snapshot!(goto("
7498create table users(id int);
7499reindex table users$0;
7500"), @r"
7501 ╭▸
7502 2 │ create table users(id int);
7503 │ ───── 2. destination
7504 3 │ reindex table users;
7505 ╰╴ ─ 1. source
7506 ");
7507 }
7508
7509 #[test]
7510 fn goto_reindex_index() {
7511 assert_snapshot!(goto("
7512create table t(c int);
7513create index idx on t(c);
7514reindex index idx$0;
7515"), @r"
7516 ╭▸
7517 3 │ create index idx on t(c);
7518 │ ─── 2. destination
7519 4 │ reindex index idx;
7520 ╰╴ ─ 1. source
7521 ");
7522 }
7523
7524 #[test]
7525 fn goto_select_exists_column() {
7526 assert_snapshot!(goto("
7527select exists$0 from (
7528 select exists(select 1)
7529);
7530"), @r"
7531 ╭▸
7532 2 │ select exists from (
7533 │ ─ 1. source
7534 3 │ select exists(select 1)
7535 ╰╴ ──────────────── 2. destination
7536 ");
7537 }
7538
7539 #[test]
7540 fn goto_reindex_schema() {
7541 assert_snapshot!(goto("
7542create schema app;
7543reindex schema app$0;
7544"), @r"
7545 ╭▸
7546 2 │ create schema app;
7547 │ ─── 2. destination
7548 3 │ reindex schema app;
7549 ╰╴ ─ 1. source
7550 ");
7551 }
7552
7553 #[test]
7554 fn goto_reindex_database() {
7555 assert_snapshot!(goto("
7556create database appdb;
7557reindex database appdb$0;
7558"), @r"
7559 ╭▸
7560 2 │ create database appdb;
7561 │ ───── 2. destination
7562 3 │ reindex database appdb;
7563 ╰╴ ─ 1. source
7564 ");
7565 }
7566
7567 #[test]
7568 fn goto_reindex_system() {
7569 assert_snapshot!(goto("
7570create database systemdb;
7571reindex system systemdb$0;
7572"), @r"
7573 ╭▸
7574 2 │ create database systemdb;
7575 │ ──────── 2. destination
7576 3 │ reindex system systemdb;
7577 ╰╴ ─ 1. source
7578 ");
7579 }
7580
7581 #[test]
7582 fn goto_merge_returning_aliased_column() {
7583 assert_snapshot!(goto(
7584 "
7585create table t(a int, b int);
7586with u(x, y) as (
7587 select 1, 2
7588),
7589merged as (
7590 merge into t
7591 using u
7592 on t.a = u.x
7593 when matched then
7594 do nothing
7595 when not matched then
7596 do nothing
7597 returning a as x, b as y
7598)
7599select x$0 from merged;
7600",
7601 ), @r"
7602 ╭▸
7603 14 │ returning a as x, b as y
7604 │ ─ 2. destination
7605 15 │ )
7606 16 │ select x from merged;
7607 ╰╴ ─ 1. source
7608 ");
7609 }
7610
7611 #[test]
7612 fn goto_cte_update_returning_column() {
7613 assert_snapshot!(goto("
7614create table t(a int, b int);
7615with updated(c) as (
7616 update t set a = 10
7617 returning a, b
7618)
7619select c, b$0 from updated;"
7620 ), @r"
7621 ╭▸
7622 5 │ returning a, b
7623 │ ─ 2. destination
7624 6 │ )
7625 7 │ select c, b from updated;
7626 ╰╴ ─ 1. source
7627 ");
7628 }
7629
7630 #[test]
7631 fn goto_update_returning_column_to_table_def() {
7632 assert_snapshot!(goto("
7633create table t(a int, b int);
7634with updated(c) as (
7635 update t set a = 10
7636 returning a, b$0
7637)
7638select c, b from updated;"
7639 ), @r"
7640 ╭▸
7641 2 │ create table t(a int, b int);
7642 │ ─ 2. destination
7643 ‡
7644 5 │ returning a, b
7645 ╰╴ ─ 1. source
7646 ");
7647 }
7648
7649 #[test]
7650 fn goto_insert_returning_column_to_table_def() {
7651 assert_snapshot!(goto("
7652create table t(a int, b int);
7653with inserted as (
7654 insert into t values (1, 2)
7655 returning a$0, b
7656)
7657select a, b from inserted;"
7658 ), @r"
7659 ╭▸
7660 2 │ create table t(a int, b int);
7661 │ ─ 2. destination
7662 ‡
7663 5 │ returning a, b
7664 ╰╴ ─ 1. source
7665 ");
7666 }
7667
7668 #[test]
7669 fn goto_delete_returning_column_to_table_def() {
7670 assert_snapshot!(goto("
7671create table t(a int, b int);
7672with deleted as (
7673 delete from t
7674 returning a, b$0
7675)
7676select a, b from deleted;"
7677 ), @r"
7678 ╭▸
7679 2 │ create table t(a int, b int);
7680 │ ─ 2. destination
7681 ‡
7682 5 │ returning a, b
7683 ╰╴ ─ 1. source
7684 ");
7685 }
7686
7687 #[test]
7688 fn goto_update_returning_qualified_star_table() {
7689 assert_snapshot!(goto("
7690create table t(a int, b int);
7691update t set a = 10
7692returning t$0.*;"
7693 ), @r"
7694 ╭▸
7695 2 │ create table t(a int, b int);
7696 │ ─ 2. destination
7697 3 │ update t set a = 10
7698 4 │ returning t.*;
7699 ╰╴ ─ 1. source
7700 ");
7701 }
7702
7703 #[test]
7704 fn goto_insert_returning_qualified_star_table() {
7705 assert_snapshot!(goto("
7706create table t(a int, b int);
7707insert into t values (1, 2)
7708returning t$0.*;"
7709 ), @r"
7710 ╭▸
7711 2 │ create table t(a int, b int);
7712 │ ─ 2. destination
7713 3 │ insert into t values (1, 2)
7714 4 │ returning t.*;
7715 ╰╴ ─ 1. source
7716 ");
7717 }
7718
7719 #[test]
7720 fn goto_delete_returning_qualified_star_table() {
7721 assert_snapshot!(goto("
7722create table t(a int, b int);
7723delete from t
7724returning t$0.*;"
7725 ), @r"
7726 ╭▸
7727 2 │ create table t(a int, b int);
7728 │ ─ 2. destination
7729 3 │ delete from t
7730 4 │ returning t.*;
7731 ╰╴ ─ 1. source
7732 ");
7733 }
7734
7735 #[test]
7736 fn goto_update_alias_in_set_clause() {
7737 assert_snapshot!(goto("
7738create table t(a int, b int);
7739update t as f set f$0.a = 10;"
7740 ), @r"
7741 ╭▸
7742 3 │ update t as f set f.a = 10;
7743 │ ┬ ─ 1. source
7744 │ │
7745 ╰╴ 2. destination
7746 ");
7747 }
7748
7749 #[test]
7750 fn goto_update_alias_in_where_clause() {
7751 assert_snapshot!(goto("
7752create table t(a int, b int);
7753update t as f set a = 10 where f$0.b = 5;"
7754 ), @r"
7755 ╭▸
7756 3 │ update t as f set a = 10 where f.b = 5;
7757 ╰╴ ─ 2. destination ─ 1. source
7758 ");
7759 }
7760
7761 #[test]
7762 fn goto_update_alias_in_from_clause() {
7763 assert_snapshot!(goto("
7764create table t(a int, b int);
7765create table u(c int);
7766update t as f set a = 10 from u where f$0.b = u.c;"
7767 ), @r"
7768 ╭▸
7769 4 │ update t as f set a = 10 from u where f.b = u.c;
7770 ╰╴ ─ 2. destination ─ 1. source
7771 ");
7772 }
7773
7774 #[test]
7775 fn goto_insert_alias_in_on_conflict() {
7776 assert_snapshot!(goto("
7777create table t(a int primary key, b int);
7778insert into t as f values (1, 2) on conflict (f$0.a) do nothing;"
7779 ), @r"
7780 ╭▸
7781 3 │ insert into t as f values (1, 2) on conflict (f.a) do nothing;
7782 ╰╴ ─ 2. destination ─ 1. source
7783 ");
7784 }
7785
7786 #[test]
7787 fn goto_insert_alias_in_returning() {
7788 assert_snapshot!(goto("
7789create table t(a int, b int);
7790insert into t as f values (1, 2) returning f$0.a;"
7791 ), @r"
7792 ╭▸
7793 3 │ insert into t as f values (1, 2) returning f.a;
7794 ╰╴ ─ 2. destination ─ 1. source
7795 ");
7796 }
7797
7798 #[test]
7799 fn goto_insert_alias_returning_column() {
7800 assert_snapshot!(goto("
7801create table t(a int, b int);
7802insert into t as f values (1, 2) returning f.a$0;"
7803 ), @r"
7804 ╭▸
7805 2 │ create table t(a int, b int);
7806 │ ─ 2. destination
7807 3 │ insert into t as f values (1, 2) returning f.a;
7808 ╰╴ ─ 1. source
7809 ");
7810 }
7811
7812 #[test]
7813 fn goto_insert_on_conflict_target_column() {
7814 assert_snapshot!(goto("
7815create table t(c text);
7816insert into t values ('c') on conflict (c$0) do nothing;"
7817 ), @r"
7818 ╭▸
7819 2 │ create table t(c text);
7820 │ ─ 2. destination
7821 3 │ insert into t values ('c') on conflict (c) do nothing;
7822 ╰╴ ─ 1. source
7823 ");
7824 }
7825
7826 #[test]
7827 fn goto_insert_on_conflict_set_column() {
7828 assert_snapshot!(goto("
7829create table t(c text, d text);
7830insert into t values ('c', 'd') on conflict (c) do update set c$0 = excluded.c;"
7831 ), @r"
7832 ╭▸
7833 2 │ create table t(c text, d text);
7834 │ ─ 2. destination
7835 3 │ insert into t values ('c', 'd') on conflict (c) do update set c = excluded.c;
7836 ╰╴ ─ 1. source
7837 ");
7838 }
7839
7840 #[test]
7841 fn goto_insert_on_conflict_excluded_column() {
7842 assert_snapshot!(goto("
7843create table t(c text, d text);
7844insert into t values ('c', 'd') on conflict (c) do update set c = excluded.c$0;"
7845 ), @r"
7846 ╭▸
7847 2 │ create table t(c text, d text);
7848 │ ─ 2. destination
7849 3 │ insert into t values ('c', 'd') on conflict (c) do update set c = excluded.c;
7850 ╰╴ ─ 1. source
7851 ");
7852 }
7853
7854 #[test]
7855 fn goto_insert_on_conflict_qualified_function() {
7856 assert_snapshot!(goto("
7857create function foo.lower(text) returns text
7858 language internal;
7859create table t(c text);
7860insert into t values ('c')
7861 on conflict (foo.lower$0(c))
7862 do nothing;"
7863 ), @r"
7864 ╭▸
7865 2 │ create function foo.lower(text) returns text
7866 │ ───── 2. destination
7867 ‡
7868 6 │ on conflict (foo.lower(c))
7869 ╰╴ ─ 1. source
7870 ");
7871 }
7872
7873 #[test]
7874 fn goto_delete_from_alias() {
7875 assert_snapshot!(goto("
7876create table t(a int, b int);
7877delete from t as f where f$0.a = 10;"
7878 ), @r"
7879 ╭▸
7880 3 │ delete from t as f where f.a = 10;
7881 │ ┬ ─ 1. source
7882 │ │
7883 ╰╴ 2. destination
7884 ");
7885 }
7886
7887 #[test]
7888 fn goto_delete_from_alias_column() {
7889 assert_snapshot!(goto("
7890create table t(a int, b int);
7891delete from t as f where f.a$0 = 10;"
7892 ), @r"
7893 ╭▸
7894 2 │ create table t(a int, b int);
7895 │ ─ 2. destination
7896 3 │ delete from t as f where f.a = 10;
7897 ╰╴ ─ 1. source
7898 ");
7899 }
7900
7901 #[test]
7902 fn goto_delete_from_alias_returning() {
7903 assert_snapshot!(goto("
7904create table t(a int, b int);
7905delete from t as f returning f$0.a"
7906 ), @r"
7907 ╭▸
7908 3 │ delete from t as f returning f.a
7909 │ ┬ ─ 1. source
7910 │ │
7911 ╰╴ 2. destination
7912 ");
7913
7914 assert_snapshot!(goto("
7915create table t(a int, b int);
7916delete from t as f returning f.a$0"
7917 ), @r"
7918 ╭▸
7919 2 │ create table t(a int, b int);
7920 │ ─ 2. destination
7921 3 │ delete from t as f returning f.a
7922 ╰╴ ─ 1. source
7923 ");
7924 }
7925
7926 #[test]
7927 fn goto_merge_alias_on_table() {
7928 assert_snapshot!(goto("
7929create table t(a int, b int);
7930create table u(a int, b int);
7931merge into t as f
7932 using u on u.a = f$0.a
7933 when matched then do nothing;
7934"
7935
7936 ), @r"
7937 ╭▸
7938 4 │ merge into t as f
7939 │ ─ 2. destination
7940 5 │ using u on u.a = f.a
7941 ╰╴ ─ 1. source
7942 ");
7943 }
7944
7945 #[test]
7946 fn goto_merge_alias_on_column() {
7947 assert_snapshot!(goto("
7948create table t(a int, b int);
7949create table u(a int, b int);
7950merge into t as f
7951 using u on u.a = f.a$0
7952 when matched then do nothing;
7953"
7954
7955 ), @r"
7956 ╭▸
7957 2 │ create table t(a int, b int);
7958 │ ─ 2. destination
7959 ‡
7960 5 │ using u on u.a = f.a
7961 ╰╴ ─ 1. source
7962 ");
7963 }
7964
7965 #[test]
7966 fn goto_merge_alias_returning() {
7967 assert_snapshot!(goto("
7968create table t(a int, b int);
7969create table u(a int, b int);
7970merge into t as f
7971 using u on u.a = f.a
7972 when matched then do nothing
7973 returning f$0.a;
7974"
7975
7976 ), @r"
7977 ╭▸
7978 4 │ merge into t as f
7979 │ ─ 2. destination
7980 ‡
7981 7 │ returning f.a;
7982 ╰╴ ─ 1. source
7983 ");
7984
7985 assert_snapshot!(goto("
7986create table t(a int, b int);
7987create table u(a int, b int);
7988merge into t as f
7989 using u on u.a = f.a
7990 when matched then do nothing
7991 returning f.a$0;
7992"
7993 ), @r"
7994 ╭▸
7995 2 │ create table t(a int, b int);
7996 │ ─ 2. destination
7997 ‡
7998 7 │ returning f.a;
7999 ╰╴ ─ 1. source
8000 ");
8001 }
8002
8003 #[test]
8004 fn goto_merge_using_table_in_when_clause() {
8005 assert_snapshot!(goto("
8006create table t(a int, b int);
8007create table u(a int, b int);
8008merge into t
8009 using u on true
8010 when matched and u$0.a = t.a
8011 then do nothing;
8012"
8013 ), @r"
8014 ╭▸
8015 3 │ create table u(a int, b int);
8016 │ ─ 2. destination
8017 ‡
8018 6 │ when matched and u.a = t.a
8019 ╰╴ ─ 1. source
8020 ");
8021 }
8022
8023 #[test]
8024 fn goto_merge_using_table_column_in_when_clause() {
8025 assert_snapshot!(goto("
8026create table t(a int, b int);
8027create table u(a int, b int);
8028merge into t
8029 using u on true
8030 when matched and u.a$0 = t.a
8031 then do nothing;
8032"
8033 ), @r"
8034 ╭▸
8035 3 │ create table u(a int, b int);
8036 │ ─ 2. destination
8037 ‡
8038 6 │ when matched and u.a = t.a
8039 ╰╴ ─ 1. source
8040 ");
8041 }
8042
8043 #[test]
8044 fn goto_merge_unqualified_column_target_table() {
8045 assert_snapshot!(goto("
8046create table x(a int, b int);
8047create table y(c int, d int);
8048merge into x
8049 using y
8050 on true
8051 when matched and a$0 = c
8052 then do nothing;
8053"
8054 ), @r"
8055 ╭▸
8056 2 │ create table x(a int, b int);
8057 │ ─ 2. destination
8058 ‡
8059 7 │ when matched and a = c
8060 ╰╴ ─ 1. source
8061 ");
8062 }
8063
8064 #[test]
8065 fn goto_merge_unqualified_column_source_table() {
8066 assert_snapshot!(goto("
8067create table x(a int, b int);
8068create table y(c int, d int);
8069merge into x
8070 using y
8071 on true
8072 when matched and a = c$0
8073 then do nothing;
8074"
8075 ), @r"
8076 ╭▸
8077 3 │ create table y(c int, d int);
8078 │ ─ 2. destination
8079 ‡
8080 7 │ when matched and a = c
8081 ╰╴ ─ 1. source
8082 ");
8083 }
8084
8085 #[test]
8086 fn goto_merge_into_table() {
8087 assert_snapshot!(goto("
8088create table x(a int, b int);
8089create table y(c int, d int);
8090merge into x$0
8091 using y
8092 on true
8093 when matched and a = c
8094 then do nothing;
8095"
8096 ), @r"
8097 ╭▸
8098 2 │ create table x(a int, b int);
8099 │ ─ 2. destination
8100 3 │ create table y(c int, d int);
8101 4 │ merge into x
8102 ╰╴ ─ 1. source
8103 ");
8104 }
8105
8106 #[test]
8107 fn goto_merge_using_clause_table() {
8108 assert_snapshot!(goto("
8109create table x(a int, b int);
8110create table y(c int, d int);
8111merge into x
8112 using y$0
8113 on true
8114 when matched and a = c
8115 then do nothing;
8116"
8117 ), @r"
8118 ╭▸
8119 3 │ create table y(c int, d int);
8120 │ ─ 2. destination
8121 4 │ merge into x
8122 5 │ using y
8123 ╰╴ ─ 1. source
8124 ");
8125 }
8126
8127 #[test]
8128 fn goto_merge_using_clause_alias() {
8129 assert_snapshot!(goto("
8130create table x(a int, b int);
8131create table y(c int, d int);
8132merge into x as g
8133 using y as k
8134 on true
8135 when matched and a = k.c$0
8136 then do nothing;
8137"
8138 ), @r"
8139 ╭▸
8140 3 │ create table y(c int, d int);
8141 │ ─ 2. destination
8142 ‡
8143 7 │ when matched and a = k.c
8144 ╰╴ ─ 1. source
8145 ");
8146 }
8147
8148 #[test]
8149 fn goto_merge_on_clause_unqualified_source_column() {
8150 assert_snapshot!(goto("
8151create table x(a int, b int);
8152create table y(c int, d int);
8153merge into x as g
8154 using y as k
8155 on g.a = c$0 and a = c
8156 when matched and g.a = k.c
8157 then do nothing;
8158"
8159 ), @r"
8160 ╭▸
8161 3 │ create table y(c int, d int);
8162 │ ─ 2. destination
8163 ‡
8164 6 │ on g.a = c and a = c
8165 ╰╴ ─ 1. source
8166 ");
8167 }
8168
8169 #[test]
8170 fn goto_merge_returning_old_table() {
8171 assert_snapshot!(goto("
8172create table x(a int, b int);
8173create table y(c int, d int);
8174merge into x as g
8175 using y as k
8176 on g.a = c and a = k.c
8177 when matched and g.a = k.c
8178 then do nothing
8179 returning old$0.a, new.a;
8180"
8181 ), @r"
8182 ╭▸
8183 2 │ create table x(a int, b int);
8184 │ ─ 2. destination
8185 ‡
8186 9 │ returning old.a, new.a;
8187 ╰╴ ─ 1. source
8188 ");
8189 }
8190
8191 #[test]
8192 fn goto_merge_returning_old_column() {
8193 assert_snapshot!(goto("
8194create table x(a int, b int);
8195create table y(c int, d int);
8196merge into x as g
8197 using y as k
8198 on g.a = c and a = k.c
8199 when matched and g.a = k.c
8200 then do nothing
8201 returning old.a$0, new.a;
8202"
8203 ), @r"
8204 ╭▸
8205 2 │ create table x(a int, b int);
8206 │ ─ 2. destination
8207 ‡
8208 9 │ returning old.a, new.a;
8209 ╰╴ ─ 1. source
8210 ");
8211 }
8212
8213 #[test]
8214 fn goto_merge_returning_new_table() {
8215 assert_snapshot!(goto("
8216create table x(a int, b int);
8217create table y(c int, d int);
8218merge into x as g
8219 using y as k
8220 on g.a = c and a = k.c
8221 when matched and g.a = k.c
8222 then do nothing
8223 returning old.a, new$0.a;
8224"
8225 ), @r"
8226 ╭▸
8227 2 │ create table x(a int, b int);
8228 │ ─ 2. destination
8229 ‡
8230 9 │ returning old.a, new.a;
8231 ╰╴ ─ 1. source
8232 ");
8233 }
8234
8235 #[test]
8236 fn goto_merge_returning_new_column() {
8237 assert_snapshot!(goto("
8238create table x(a int, b int);
8239create table y(c int, d int);
8240merge into x as g
8241 using y as k
8242 on g.a = c and a = k.c
8243 when matched and g.a = k.c
8244 then do nothing
8245 returning old.a, new.a$0;
8246"
8247 ), @r"
8248 ╭▸
8249 2 │ create table x(a int, b int);
8250 │ ─ 2. destination
8251 ‡
8252 9 │ returning old.a, new.a;
8253 ╰╴ ─ 1. source
8254 ");
8255 }
8256
8257 #[test]
8258 fn goto_merge_with_tables_named_old_new_old_table() {
8259 assert_snapshot!(goto("
8260create table old(a int, b int);
8261create table new(c int, d int);
8262merge into old
8263 using new
8264 on true
8265 when matched
8266 then do nothing
8267 returning old$0.a, new.d;
8268"
8269 ), @r"
8270 ╭▸
8271 2 │ create table old(a int, b int);
8272 │ ─── 2. destination
8273 ‡
8274 9 │ returning old.a, new.d;
8275 ╰╴ ─ 1. source
8276 ");
8277 }
8278
8279 #[test]
8280 fn goto_merge_with_tables_named_old_new_old_column() {
8281 assert_snapshot!(goto("
8282create table old(a int, b int);
8283create table new(c int, d int);
8284merge into old
8285 using new
8286 on true
8287 when matched
8288 then do nothing
8289 returning old.a$0, new.d;
8290"
8291 ), @r"
8292 ╭▸
8293 2 │ create table old(a int, b int);
8294 │ ─ 2. destination
8295 ‡
8296 9 │ returning old.a, new.d;
8297 ╰╴ ─ 1. source
8298 ");
8299 }
8300
8301 #[test]
8302 fn goto_merge_with_tables_named_old_new_new_table() {
8303 assert_snapshot!(goto("
8304create table old(a int, b int);
8305create table new(c int, d int);
8306merge into old
8307 using new
8308 on true
8309 when matched
8310 then do nothing
8311 returning old.a, new$0.d;
8312"
8313 ), @r"
8314 ╭▸
8315 3 │ create table new(c int, d int);
8316 │ ─── 2. destination
8317 ‡
8318 9 │ returning old.a, new.d;
8319 ╰╴ ─ 1. source
8320 ");
8321 }
8322
8323 #[test]
8324 fn goto_merge_with_tables_named_old_new_new_column() {
8325 assert_snapshot!(goto("
8326create table old(a int, b int);
8327create table new(c int, d int);
8328merge into old
8329 using new
8330 on true
8331 when matched
8332 then do nothing
8333 returning old.a, new.d$0;
8334"
8335 ), @r"
8336 ╭▸
8337 3 │ create table new(c int, d int);
8338 │ ─ 2. destination
8339 ‡
8340 9 │ returning old.a, new.d;
8341 ╰╴ ─ 1. source
8342 ");
8343 }
8344
8345 #[test]
8346 fn goto_merge_returning_with_aliases_before_table() {
8347 assert_snapshot!(goto("
8348create table x(a int, b int);
8349create table y(c int, d int);
8350merge into x
8351 using y on true
8352 when matched then do nothing
8353 returning
8354 with (old as before, new as after)
8355 before$0.a, after.a;
8356"
8357 ), @r"
8358 ╭▸
8359 8 │ with (old as before, new as after)
8360 │ ────── 2. destination
8361 9 │ before.a, after.a;
8362 ╰╴ ─ 1. source
8363 ");
8364 }
8365
8366 #[test]
8367 fn goto_merge_returning_with_aliases_before_column() {
8368 assert_snapshot!(goto("
8369create table x(a int, b int);
8370create table y(c int, d int);
8371merge into x
8372 using y on true
8373 when matched then do nothing
8374 returning
8375 with (old as before, new as after)
8376 before.a$0, after.a;
8377"
8378 ), @r"
8379 ╭▸
8380 2 │ create table x(a int, b int);
8381 │ ─ 2. destination
8382 ‡
8383 9 │ before.a, after.a;
8384 ╰╴ ─ 1. source
8385 ");
8386 }
8387
8388 #[test]
8389 fn goto_merge_returning_with_aliases_after_table() {
8390 assert_snapshot!(goto("
8391create table x(a int, b int);
8392create table y(c int, d int);
8393merge into x
8394 using y on true
8395 when matched then do nothing
8396 returning
8397 with (old as before, new as after)
8398 before.a, after$0.a;
8399"
8400 ), @r"
8401 ╭▸
8402 8 │ with (old as before, new as after)
8403 │ ───── 2. destination
8404 9 │ before.a, after.a;
8405 ╰╴ ─ 1. source
8406 ");
8407 }
8408
8409 #[test]
8410 fn goto_merge_returning_with_aliases_after_column() {
8411 assert_snapshot!(goto("
8412create table x(a int, b int);
8413create table y(c int, d int);
8414merge into x
8415 using y on true
8416 when matched then do nothing
8417 returning
8418 with (old as before, new as after)
8419 before.a, after.a$0;
8420"
8421 ), @r"
8422 ╭▸
8423 2 │ create table x(a int, b int);
8424 │ ─ 2. destination
8425 ‡
8426 9 │ before.a, after.a;
8427 ╰╴ ─ 1. source
8428 ");
8429 }
8430
8431 #[test]
8432 fn goto_merge_when_not_matched_insert_values_qualified_column() {
8433 assert_snapshot!(goto("
8434create table inventory (
8435 product_id int,
8436 quantity int,
8437 updated_at timestamp
8438);
8439create table orders (
8440 id int,
8441 product_id int,
8442 qty int
8443);
8444merge into inventory as t
8445using orders as o
8446 on t.product_id = o.product_id
8447when matched then
8448 do nothing
8449when not matched then
8450 insert values (o$0.product_id, o.qty, now());
8451"
8452 ), @r"
8453 ╭▸
8454 13 │ using orders as o
8455 │ ─ 2. destination
8456 ‡
8457 18 │ insert values (o.product_id, o.qty, now());
8458 ╰╴ ─ 1. source
8459 ");
8460 }
8461
8462 #[test]
8463 fn goto_merge_when_not_matched_insert_values_qualified_column_field() {
8464 assert_snapshot!(goto("
8465create table inventory (
8466 product_id int,
8467 quantity int,
8468 updated_at timestamp
8469);
8470create table orders (
8471 id int,
8472 product_id int,
8473 qty int
8474);
8475merge into inventory as t
8476using orders as o
8477 on t.product_id = o.product_id
8478when matched then
8479 do nothing
8480when not matched then
8481 insert values (o.product_id$0, o.qty, now());
8482"
8483 ), @r"
8484 ╭▸
8485 9 │ product_id int,
8486 │ ────────── 2. destination
8487 ‡
8488 18 │ insert values (o.product_id, o.qty, now());
8489 ╰╴ ─ 1. source
8490 ");
8491 }
8492
8493 #[test]
8494 fn goto_merge_when_not_matched_insert_values_unqualified_column() {
8495 assert_snapshot!(goto("
8496create table inventory (
8497 product_id int,
8498 quantity int
8499);
8500create table orders (
8501 product_id int,
8502 qty int
8503);
8504merge into inventory as t
8505using orders as o
8506 on t.product_id = o.product_id
8507when not matched then
8508 insert values (product_id$0, qty);
8509"
8510 ), @r"
8511 ╭▸
8512 7 │ product_id int,
8513 │ ────────── 2. destination
8514 ‡
8515 14 │ insert values (product_id, qty);
8516 ╰╴ ─ 1. source
8517 ");
8518 }
8519
8520 #[test]
8521 fn goto_insert_returning_old_table() {
8522 assert_snapshot!(goto("
8523create table t(a int, b int);
8524insert into t values (1, 2), (3, 4)
8525returning old$0.a, new.b;
8526"
8527 ), @r"
8528 ╭▸
8529 2 │ create table t(a int, b int);
8530 │ ─ 2. destination
8531 3 │ insert into t values (1, 2), (3, 4)
8532 4 │ returning old.a, new.b;
8533 ╰╴ ─ 1. source
8534 ");
8535 }
8536
8537 #[test]
8538 fn goto_insert_returning_old_column() {
8539 assert_snapshot!(goto("
8540create table t(a int, b int);
8541insert into t values (1, 2), (3, 4)
8542returning old.a$0, new.b;
8543"
8544 ), @r"
8545 ╭▸
8546 2 │ create table t(a int, b int);
8547 │ ─ 2. destination
8548 3 │ insert into t values (1, 2), (3, 4)
8549 4 │ returning old.a, new.b;
8550 ╰╴ ─ 1. source
8551 ");
8552 }
8553
8554 #[test]
8555 fn goto_insert_returning_new_table() {
8556 assert_snapshot!(goto("
8557create table t(a int, b int);
8558insert into t values (1, 2), (3, 4)
8559returning old.a, new$0.b;
8560"
8561 ), @r"
8562 ╭▸
8563 2 │ create table t(a int, b int);
8564 │ ─ 2. destination
8565 3 │ insert into t values (1, 2), (3, 4)
8566 4 │ returning old.a, new.b;
8567 ╰╴ ─ 1. source
8568 ");
8569 }
8570
8571 #[test]
8572 fn goto_insert_returning_new_column() {
8573 assert_snapshot!(goto("
8574create table t(a int, b int);
8575insert into t values (1, 2), (3, 4)
8576returning old.a, new.b$0;
8577"
8578 ), @r"
8579 ╭▸
8580 2 │ create table t(a int, b int);
8581 │ ─ 2. destination
8582 3 │ insert into t values (1, 2), (3, 4)
8583 4 │ returning old.a, new.b;
8584 ╰╴ ─ 1. source
8585 ");
8586 }
8587
8588 #[test]
8589 fn goto_update_returning_old_table() {
8590 assert_snapshot!(goto("
8591create table t(a int, b int);
8592update t set a = 42
8593returning old$0.a, new.b;
8594"
8595 ), @r"
8596 ╭▸
8597 2 │ create table t(a int, b int);
8598 │ ─ 2. destination
8599 3 │ update t set a = 42
8600 4 │ returning old.a, new.b;
8601 ╰╴ ─ 1. source
8602 ");
8603 }
8604
8605 #[test]
8606 fn goto_update_returning_old_column() {
8607 assert_snapshot!(goto("
8608create table t(a int, b int);
8609update t set a = 42
8610returning old.a$0, new.b;
8611"
8612 ), @r"
8613 ╭▸
8614 2 │ create table t(a int, b int);
8615 │ ─ 2. destination
8616 3 │ update t set a = 42
8617 4 │ returning old.a, new.b;
8618 ╰╴ ─ 1. source
8619 ");
8620 }
8621
8622 #[test]
8623 fn goto_update_returning_new_table() {
8624 assert_snapshot!(goto("
8625create table t(a int, b int);
8626update t set a = 42
8627returning old.a, new$0.b;
8628"
8629 ), @r"
8630 ╭▸
8631 2 │ create table t(a int, b int);
8632 │ ─ 2. destination
8633 3 │ update t set a = 42
8634 4 │ returning old.a, new.b;
8635 ╰╴ ─ 1. source
8636 ");
8637 }
8638
8639 #[test]
8640 fn goto_update_returning_new_column() {
8641 assert_snapshot!(goto("
8642create table t(a int, b int);
8643update t set a = 42
8644returning old.a, new.b$0;
8645"
8646 ), @r"
8647 ╭▸
8648 2 │ create table t(a int, b int);
8649 │ ─ 2. destination
8650 3 │ update t set a = 42
8651 4 │ returning old.a, new.b;
8652 ╰╴ ─ 1. source
8653 ");
8654 }
8655
8656 #[test]
8657 fn goto_delete_returning_old_table() {
8658 assert_snapshot!(goto("
8659create table t(a int, b int);
8660delete from t
8661returning old$0.a, new.b;
8662"
8663 ), @r"
8664 ╭▸
8665 2 │ create table t(a int, b int);
8666 │ ─ 2. destination
8667 3 │ delete from t
8668 4 │ returning old.a, new.b;
8669 ╰╴ ─ 1. source
8670 ");
8671 }
8672
8673 #[test]
8674 fn goto_delete_returning_old_column() {
8675 assert_snapshot!(goto("
8676create table t(a int, b int);
8677delete from t
8678returning old.a$0, new.b;
8679"
8680 ), @r"
8681 ╭▸
8682 2 │ create table t(a int, b int);
8683 │ ─ 2. destination
8684 3 │ delete from t
8685 4 │ returning old.a, new.b;
8686 ╰╴ ─ 1. source
8687 ");
8688 }
8689
8690 #[test]
8691 fn goto_delete_returning_new_table() {
8692 assert_snapshot!(goto("
8693create table t(a int, b int);
8694delete from t
8695returning old.a, new$0.b;
8696"
8697 ), @r"
8698 ╭▸
8699 2 │ create table t(a int, b int);
8700 │ ─ 2. destination
8701 3 │ delete from t
8702 4 │ returning old.a, new.b;
8703 ╰╴ ─ 1. source
8704 ");
8705 }
8706
8707 #[test]
8708 fn goto_delete_returning_new_column() {
8709 assert_snapshot!(goto("
8710create table t(a int, b int);
8711delete from t
8712returning old.a, new.b$0;
8713"
8714 ), @r"
8715 ╭▸
8716 2 │ create table t(a int, b int);
8717 │ ─ 2. destination
8718 3 │ delete from t
8719 4 │ returning old.a, new.b;
8720 ╰╴ ─ 1. source
8721 ");
8722 }
8723
8724 #[test]
8725 fn goto_insert_as_old_alias() {
8726 assert_snapshot!(goto("
8727create table t(a int, b int);
8728insert into t as old values (1, 2)
8729returning old$0.a, new.a;
8730"
8731 ), @r"
8732 ╭▸
8733 3 │ insert into t as old values (1, 2)
8734 │ ─── 2. destination
8735 4 │ returning old.a, new.a;
8736 ╰╴ ─ 1. source
8737 ");
8738 }
8739
8740 #[test]
8741 fn goto_delete_as_old_alias() {
8742 assert_snapshot!(goto("
8743create table t(a int, b int);
8744delete from t as old
8745returning old$0.a, new.a;
8746"
8747 ), @r"
8748 ╭▸
8749 3 │ delete from t as old
8750 │ ─── 2. destination
8751 4 │ returning old.a, new.a;
8752 ╰╴ ─ 1. source
8753 ");
8754 }
8755
8756 #[test]
8757 fn goto_update_as_old_alias() {
8758 assert_snapshot!(goto("
8759create table t(a int, b int);
8760update t as old set a = 42
8761returning old$0.a, new.a;
8762"
8763 ), @r"
8764 ╭▸
8765 3 │ update t as old set a = 42
8766 │ ─── 2. destination
8767 4 │ returning old.a, new.a;
8768 ╰╴ ─ 1. source
8769 ");
8770 }
8771
8772 #[test]
8773 fn goto_merge_returning_cte_column_unqualified() {
8774 assert_snapshot!(goto("
8775create table t(a int, b int);
8776with u(x, y) as (
8777 select 1, 2
8778)
8779merge into t
8780 using u on true
8781when matched then
8782 do nothing
8783when not matched then
8784 do nothing
8785returning x$0, u.y;
8786"
8787 ), @r"
8788 ╭▸
8789 3 │ with u(x, y) as (
8790 │ ─ 2. destination
8791 ‡
8792 12 │ returning x, u.y;
8793 ╰╴ ─ 1. source
8794 ");
8795 }
8796
8797 #[test]
8798 fn goto_merge_returning_cte_column_qualified_table() {
8799 assert_snapshot!(goto("
8800create table t(a int, b int);
8801with u(x, y) as (
8802 select 1, 2
8803)
8804merge into t
8805 using u on true
8806when matched then
8807 do nothing
8808when not matched then
8809 do nothing
8810returning x, u$0.y;
8811"
8812 ), @r"
8813 ╭▸
8814 3 │ with u(x, y) as (
8815 │ ─ 2. destination
8816 ‡
8817 12 │ returning x, u.y;
8818 ╰╴ ─ 1. source
8819 ");
8820 }
8821
8822 #[test]
8823 fn goto_merge_returning_cte_column_qualified_column() {
8824 assert_snapshot!(goto("
8825create table t(a int, b int);
8826with u(x, y) as (
8827 select 1, 2
8828)
8829merge into t
8830 using u on true
8831when matched then
8832 do nothing
8833when not matched then
8834 do nothing
8835returning x, u.y$0;
8836"
8837 ), @r"
8838 ╭▸
8839 3 │ with u(x, y) as (
8840 │ ─ 2. destination
8841 ‡
8842 12 │ returning x, u.y;
8843 ╰╴ ─ 1. source
8844 ");
8845 }
8846
8847 #[test]
8848 fn goto_overlay_with_cte_column() {
8849 assert_snapshot!(goto("
8850with t as (
8851 select '1' a, '2' b, 3 start
8852)
8853select overlay(a placing b$0 from start) from t;
8854 "), @r"
8855 ╭▸
8856 3 │ select '1' a, '2' b, 3 start
8857 │ ─ 2. destination
8858 4 │ )
8859 5 │ select overlay(a placing b from start) from t;
8860 ╰╴ ─ 1. source
8861 ");
8862 }
8863
8864 #[test]
8865 fn goto_overlay_with_cte_column_first_arg() {
8866 assert_snapshot!(goto("
8867with t as (
8868 select '1' a, '2' b, 3 start
8869)
8870select overlay(a$0 placing b from start) from t;
8871 "), @r"
8872 ╭▸
8873 3 │ select '1' a, '2' b, 3 start
8874 │ ─ 2. destination
8875 4 │ )
8876 5 │ select overlay(a placing b from start) from t;
8877 ╰╴ ─ 1. source
8878 ");
8879 }
8880
8881 #[test]
8882 fn goto_overlay_with_cte_column_from_arg() {
8883 assert_snapshot!(goto("
8884with t as (
8885 select '1' a, '2' b, 3 start
8886)
8887select overlay(a placing b from start$0) from t;
8888 "), @r"
8889 ╭▸
8890 3 │ select '1' a, '2' b, 3 start
8891 │ ───── 2. destination
8892 4 │ )
8893 5 │ select overlay(a placing b from start) from t;
8894 ╰╴ ─ 1. source
8895 ");
8896 }
8897
8898 #[test]
8899 fn goto_named_arg_to_param() {
8900 assert_snapshot!(goto("
8901create function foo(bar_param int) returns int as 'select 1' language sql;
8902select foo(bar_param$0 := 5);
8903"), @r"
8904 ╭▸
8905 2 │ create function foo(bar_param int) returns int as 'select 1' language sql;
8906 │ ───────── 2. destination
8907 3 │ select foo(bar_param := 5);
8908 ╰╴ ─ 1. source
8909 ");
8910 }
8911
8912 #[test]
8913 fn goto_named_arg_schema_qualified() {
8914 assert_snapshot!(goto("
8915create schema s;
8916create function s.foo(my_param int) returns int as 'select 1' language sql;
8917select s.foo(my_param$0 := 10);
8918"), @r"
8919 ╭▸
8920 3 │ create function s.foo(my_param int) returns int as 'select 1' language sql;
8921 │ ──────── 2. destination
8922 4 │ select s.foo(my_param := 10);
8923 ╰╴ ─ 1. source
8924 ");
8925 }
8926
8927 #[test]
8928 fn goto_named_arg_multiple_params() {
8929 assert_snapshot!(goto("
8930create function foo(a int, b int, c int) returns int as 'select 1' language sql;
8931select foo(b$0 := 2, a := 1);
8932"), @r"
8933 ╭▸
8934 2 │ create function foo(a int, b int, c int) returns int as 'select 1' language sql;
8935 │ ─ 2. destination
8936 3 │ select foo(b := 2, a := 1);
8937 ╰╴ ─ 1. source
8938 ");
8939 }
8940
8941 #[test]
8942 fn goto_named_arg_procedure() {
8943 assert_snapshot!(goto("
8944create procedure proc(param_x int) as 'select 1' language sql;
8945call proc(param_x$0 := 42);
8946"), @r"
8947 ╭▸
8948 2 │ create procedure proc(param_x int) as 'select 1' language sql;
8949 │ ─────── 2. destination
8950 3 │ call proc(param_x := 42);
8951 ╰╴ ─ 1. source
8952 ");
8953 }
8954
8955 #[test]
8956 fn goto_named_arg_not_found_unnamed_param() {
8957 goto_not_found(
8958 "
8959create function foo(int) returns int as 'select 1' language sql;
8960select foo(bar$0 := 5);
8961",
8962 );
8963 }
8964
8965 #[test]
8966 fn goto_named_arg_not_found_wrong_name() {
8967 goto_not_found(
8968 "
8969create function foo(correct_param int) returns int as 'select 1' language sql;
8970select foo(wrong_param$0 := 5);
8971",
8972 );
8973 }
8974
8975 #[test]
8976 fn goto_operator_function_ref() {
8977 assert_snapshot!(goto("
8978create function pg_catalog.tsvector_concat(tsvector, tsvector) returns tsvector language internal;
8979create operator pg_catalog.|| (leftarg = tsvector, rightarg = tsvector, function = pg_catalog.tsvector_concat$0);
8980"), @r"
8981 ╭▸
8982 2 │ create function pg_catalog.tsvector_concat(tsvector, tsvector) returns tsvector language internal;
8983 │ ─────────────── 2. destination
8984 3 │ create operator pg_catalog.|| (leftarg = tsvector, rightarg = tsvector, function = pg_catalog.tsvector_concat);
8985 ╰╴ ─ 1. source
8986 ");
8987 }
8988
8989 #[test]
8990 fn goto_operator_procedure_ref() {
8991 assert_snapshot!(goto("
8992create function f(int, int) returns int language internal;
8993create operator ||| (leftarg = int, rightarg = int, procedure = f$0);
8994"), @r"
8995 ╭▸
8996 2 │ create function f(int, int) returns int language internal;
8997 │ ─ 2. destination
8998 3 │ create operator ||| (leftarg = int, rightarg = int, procedure = f);
8999 ╰╴ ─ 1. source
9000 ");
9001 }
9002
9003 #[test]
9004 fn goto_cte_window_partition_column_from_create_table_if_not_exists() {
9005 assert_snapshot!(goto("
9006create table t (
9007 id bigint primary key,
9008 group_col text not null,
9009 update_date date not null
9010);
9011
9012with row_number_added as (
9013 select
9014 *,
9015 row_number() over (
9016 partition by group_col$0
9017 order by update_date desc
9018 ) as rn
9019 from t
9020)
9021select * from row_number_added
9022"), @"
9023 ╭▸
9024 4 │ group_col text not null,
9025 │ ───────── 2. destination
9026 ‡
9027 12 │ partition by group_col
9028 ╰╴ ─ 1. source
9029 ");
9030 }
9031
9032 #[test]
9033 fn goto_cte_window_order_column_from_create_table_if_not_exists() {
9034 assert_snapshot!(goto("
9035create table t (
9036 id bigint primary key,
9037 group_col text not null,
9038 update_date date not null
9039);
9040
9041with row_number_added as (
9042 select
9043 *,
9044 row_number() over (
9045 partition by group_col
9046 order by update_date$0 desc
9047 ) as rn
9048 from t
9049)
9050select * from row_number_added
9051"), @"
9052 ╭▸
9053 5 │ update_date date not null
9054 │ ─────────── 2. destination
9055 ‡
9056 13 │ order by update_date desc
9057 ╰╴ ─ 1. source
9058 ");
9059 }
9060
9061 #[test]
9062 fn goto_cte_window_partition_function_call_from_create_table() {
9063 assert_snapshot!(goto("
9064create function length(text) returns int language internal;
9065
9066create table t (
9067 id bigint primary key,
9068 group_col text not null,
9069 update_date date not null
9070);
9071
9072with row_number_added as (
9073 select
9074 *,
9075 row_number() over (
9076 partition by length$0(group_col)
9077 order by update_date$0 desc
9078 ) as rn
9079 from t
9080)
9081select * from row_number_added
9082"), @"
9083 ╭▸
9084 2 │ create function length(text) returns int language internal;
9085 │ ────── 2. destination
9086 ‡
9087 14 │ partition by length(group_col)
9088 ╰╴ ─ 1. source
9089 ");
9090 }
9091
9092 #[test]
9093 fn goto_select_window_def_reuse() {
9094 assert_snapshot!(goto("
9095create table tbl (
9096 id bigint primary key,
9097 group_col text not null,
9098 update_date date not null,
9099 value text
9100);
9101select
9102 id,
9103 group_col,
9104 row_number() over w as rn,
9105 lag(value) over w$0 as prev_value
9106from tbl
9107window w as (
9108 partition by group_col
9109 order by update_date desc
9110);
9111"), @r"
9112 ╭▸
9113 12 │ lag(value) over w as prev_value
9114 │ ─ 1. source
9115 13 │ from tbl
9116 14 │ window w as (
9117 ╰╴ ─ 2. destination
9118 ");
9119 }
9120}