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