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_check_constraint_column() {
1407 assert_snapshot!(goto("
1408create table t (
1409 b int check (b > 10),
1410 c int check (c$0 > 10) no inherit
1411);
1412"), @r"
1413 ╭▸
1414 4 │ c int check (c > 10) no inherit
1415 │ ┬ ─ 1. source
1416 │ │
1417 ╰╴ 2. destination
1418 ");
1419 }
1420
1421 #[test]
1422 fn goto_generated_column() {
1423 assert_snapshot!(goto("
1424create table t (
1425 a int,
1426 b int generated always as (
1427 a$0 * 2
1428 ) stored
1429);
1430"), @r"
1431 ╭▸
1432 3 │ a int,
1433 │ ─ 2. destination
1434 4 │ b int generated always as (
1435 5 │ a * 2
1436 ╰╴ ─ 1. source
1437 ");
1438 }
1439
1440 #[test]
1441 fn goto_table_check_constraint_column() {
1442 assert_snapshot!(goto("
1443create table t (
1444 a int,
1445 b text,
1446 check (a$0 > b)
1447);
1448"), @r"
1449 ╭▸
1450 3 │ a int,
1451 │ ─ 2. destination
1452 4 │ b text,
1453 5 │ check (a > b)
1454 ╰╴ ─ 1. source
1455 ");
1456 }
1457
1458 #[test]
1459 fn goto_table_unique_constraint_column() {
1460 assert_snapshot!(goto("
1461create table t (
1462 a int,
1463 b text,
1464 unique (a$0)
1465);
1466"), @r"
1467 ╭▸
1468 3 │ a int,
1469 │ ─ 2. destination
1470 4 │ b text,
1471 5 │ unique (a)
1472 ╰╴ ─ 1. source
1473 ");
1474 }
1475
1476 #[test]
1477 fn goto_table_primary_key_constraint_column() {
1478 assert_snapshot!(goto("
1479create table t (
1480 id bigint generated always as identity,
1481 inserted_at timestamptz not null default now(),
1482 primary key (id, inserted_at$0)
1483);
1484"), @r"
1485 ╭▸
1486 4 │ inserted_at timestamptz not null default now(),
1487 │ ─────────── 2. destination
1488 5 │ primary key (id, inserted_at)
1489 ╰╴ ─ 1. source
1490 ");
1491 }
1492
1493 #[test]
1494 fn goto_table_not_null_constraint_column() {
1495 assert_snapshot!(goto("
1496create table t (
1497 id integer,
1498 name text,
1499 not null name$0
1500);
1501"), @r"
1502 ╭▸
1503 4 │ name text,
1504 │ ──── 2. destination
1505 5 │ not null name
1506 ╰╴ ─ 1. source
1507 ");
1508 }
1509
1510 #[test]
1511 fn goto_table_exclude_constraint_column() {
1512 assert_snapshot!(goto("
1513create table circles (
1514 c circle,
1515 exclude using gist (c$0 with &&)
1516);
1517"), @r"
1518 ╭▸
1519 3 │ c circle,
1520 │ ─ 2. destination
1521 4 │ exclude using gist (c with &&)
1522 ╰╴ ─ 1. source
1523 ");
1524 }
1525
1526 #[test]
1527 fn goto_table_exclude_constraint_include_column() {
1528 assert_snapshot!(goto("
1529create table t (
1530 a int,
1531 b text,
1532 exclude using btree ( a with > )
1533 include (a$0, b)
1534);
1535"), @r"
1536 ╭▸
1537 3 │ a int,
1538 │ ─ 2. destination
1539 ‡
1540 6 │ include (a, b)
1541 ╰╴ ─ 1. source
1542 ");
1543 }
1544
1545 #[test]
1546 fn goto_table_exclude_constraint_where_column() {
1547 assert_snapshot!(goto("
1548create table t (
1549 a int,
1550 b text,
1551 exclude using btree ( a with > )
1552 where ( a$0 > 10 and b like '%foo' )
1553);
1554"), @r"
1555 ╭▸
1556 3 │ a int,
1557 │ ─ 2. destination
1558 ‡
1559 6 │ where ( a > 10 and b like '%foo' )
1560 ╰╴ ─ 1. source
1561 ");
1562 }
1563
1564 #[test]
1565 fn goto_table_partition_by_column() {
1566 assert_snapshot!(goto("
1567create table t (
1568 id bigint generated always as identity,
1569 inserted_at timestamptz not null default now()
1570) partition by range (inserted_at$0);
1571"), @r"
1572 ╭▸
1573 4 │ inserted_at timestamptz not null default now()
1574 │ ─────────── 2. destination
1575 5 │ ) partition by range (inserted_at);
1576 ╰╴ ─ 1. source
1577 ");
1578 }
1579
1580 #[test]
1581 fn goto_table_partition_of_table() {
1582 assert_snapshot!(goto("
1583create table t ();
1584create table t_2026_01_02 partition of t$0
1585 for values from ('2026-01-02') to ('2026-01-03');
1586"), @r"
1587 ╭▸
1588 2 │ create table t ();
1589 │ ─ 2. destination
1590 3 │ create table t_2026_01_02 partition of t
1591 ╰╴ ─ 1. source
1592 ");
1593 }
1594
1595 #[test]
1596 fn goto_table_partition_of_cycle() {
1597 goto_not_found(
1598 "
1599create table part1 partition of part2
1600 for values from ('2026-01-02') to ('2026-01-03');
1601create table part2 partition of part1
1602 for values from ('2026-01-02') to ('2026-01-03');
1603select a$0 from part2;
1604",
1605 );
1606 }
1607
1608 #[test]
1609 fn goto_partition_table_column() {
1610 assert_snapshot!(goto("
1611create table part (
1612 a int,
1613 inserted_at timestamptz not null default now()
1614) partition by range (inserted_at);
1615create table part_2026_01_02 partition of part
1616 for values from ('2026-01-02') to ('2026-01-03');
1617select a$0 from part_2026_01_02;
1618"), @r"
1619 ╭▸
1620 3 │ a int,
1621 │ ─ 2. destination
1622 ‡
1623 8 │ select a from part_2026_01_02;
1624 ╰╴ ─ 1. source
1625 ");
1626 }
1627
1628 #[test]
1629 fn goto_alter_index_attach_partition() {
1630 assert_snapshot!(goto("
1631create table t (
1632 inserted_at timestamptz not null default now()
1633) partition by range (inserted_at);
1634create table part partition of t
1635 for values from ('2026-01-02') to ('2026-01-03');
1636alter index t attach partition part$0;
1637"), @r"
1638 ╭▸
1639 5 │ create table part partition of t
1640 │ ──── 2. destination
1641 6 │ for values from ('2026-01-02') to ('2026-01-03');
1642 7 │ alter index t attach partition part;
1643 ╰╴ ─ 1. source
1644 ");
1645 }
1646
1647 #[test]
1648 fn goto_create_table_like_clause() {
1649 assert_snapshot!(goto("
1650create table large_data_table(a text);
1651create table t (
1652 a text,
1653 like large_data_table$0,
1654 b integer
1655);
1656"), @r"
1657 ╭▸
1658 2 │ create table large_data_table(a text);
1659 │ ──────────────── 2. destination
1660 ‡
1661 5 │ like large_data_table,
1662 ╰╴ ─ 1. source
1663 ");
1664 }
1665
1666 #[test]
1667 fn goto_create_table_inherits() {
1668 assert_snapshot!(goto("
1669create table bar(a int);
1670create table t (a int)
1671inherits (foo.bar, bar$0, buzz);
1672"), @r"
1673 ╭▸
1674 2 │ create table bar(a int);
1675 │ ─── 2. destination
1676 3 │ create table t (a int)
1677 4 │ inherits (foo.bar, bar, buzz);
1678 ╰╴ ─ 1. source
1679 ");
1680 }
1681
1682 #[test]
1683 fn goto_create_table_like_clause_columns() {
1684 assert_snapshot!(goto("
1685create table t(a int, b int);
1686create table u(like t, c int);
1687select a$0, c from u;
1688"), @r"
1689 ╭▸
1690 2 │ create table t(a int, b int);
1691 │ ─ 2. destination
1692 3 │ create table u(like t, c int);
1693 4 │ select a, c from u;
1694 ╰╴ ─ 1. source
1695 ");
1696 }
1697
1698 #[test]
1699 fn goto_create_table_like_clause_local_column() {
1700 assert_snapshot!(goto("
1701create table t(a int, b int);
1702create table u(like t, c int);
1703select a, c$0 from u;
1704"), @r"
1705 ╭▸
1706 3 │ create table u(like t, c int);
1707 │ ─ 2. destination
1708 4 │ select a, c from u;
1709 ╰╴ ─ 1. source
1710 ");
1711 }
1712
1713 #[test]
1714 fn goto_create_table_like_clause_multi() {
1715 assert_snapshot!(goto("
1716create table t(a int, b int);
1717create table u(x int, y int);
1718create table k(like t, like u, c int);
1719select y$0 from k;
1720"), @r"
1721 ╭▸
1722 3 │ create table u(x int, y int);
1723 │ ─ 2. destination
1724 4 │ create table k(like t, like u, c int);
1725 5 │ select y from k;
1726 ╰╴ ─ 1. source
1727 ");
1728 }
1729
1730 #[test]
1731 fn goto_create_table_inherits_column() {
1732 assert_snapshot!(goto("
1733create table t (
1734 a int, b text
1735);
1736create table u (
1737 c int
1738) inherits (t);
1739select a$0 from u;
1740"), @r"
1741 ╭▸
1742 3 │ a int, b text
1743 │ ─ 2. destination
1744 ‡
1745 8 │ select a from u;
1746 ╰╴ ─ 1. source
1747 ");
1748 }
1749
1750 #[test]
1751 fn goto_create_table_inherits_local_column() {
1752 assert_snapshot!(goto("
1753create table t (
1754 a int, b text
1755);
1756create table u (
1757 c int
1758) inherits (t);
1759select c$0 from u;
1760"), @r"
1761 ╭▸
1762 6 │ c int
1763 │ ─ 2. destination
1764 7 │ ) inherits (t);
1765 8 │ select c from u;
1766 ╰╴ ─ 1. source
1767 ");
1768 }
1769
1770 #[test]
1771 fn goto_create_table_inherits_multiple_parents() {
1772 assert_snapshot!(goto("
1773create table t1 (
1774 a int
1775);
1776create table t2 (
1777 b text
1778);
1779create table u (
1780 c int
1781) inherits (t1, t2);
1782select b$0 from u;
1783"), @r"
1784 ╭▸
1785 6 │ b text
1786 │ ─ 2. destination
1787 ‡
1788 11 │ select b from u;
1789 ╰╴ ─ 1. source
1790 ");
1791 }
1792
1793 #[test]
1794 fn goto_create_foreign_table_inherits_column() {
1795 assert_snapshot!(goto("
1796create server myserver foreign data wrapper postgres_fdw;
1797create table t (
1798 a int, b text
1799);
1800create foreign table u (
1801 c int
1802) inherits (t) server myserver;
1803select a$0 from u;
1804"), @r"
1805 ╭▸
1806 4 │ a int, b text
1807 │ ─ 2. destination
1808 ‡
1809 9 │ select a from u;
1810 ╰╴ ─ 1. source
1811 ");
1812 }
1813
1814 #[test]
1815 fn goto_drop_temp_table_shadows_public() {
1816 assert_snapshot!(goto("
1818create table t();
1819create temp table t();
1820drop table t$0;
1821"), @r"
1822 ╭▸
1823 2 │ create table t();
1824 │ ─ 2. destination
1825 3 │ create temp table t();
1826 4 │ drop table t;
1827 ╰╴ ─ 1. source
1828 ");
1829 }
1830
1831 #[test]
1832 fn goto_drop_public_table_when_temp_exists() {
1833 assert_snapshot!(goto("
1835create table t();
1836create temp table t();
1837drop table public.t$0;
1838"), @r"
1839 ╭▸
1840 2 │ create table t();
1841 │ ─ 2. destination
1842 3 │ create temp table t();
1843 4 │ drop table public.t;
1844 ╰╴ ─ 1. source
1845 ");
1846 }
1847
1848 #[test]
1849 fn goto_drop_table_defined_after() {
1850 assert_snapshot!(goto("
1851drop table t$0;
1852create table t();
1853"), @r"
1854 ╭▸
1855 2 │ drop table t;
1856 │ ─ 1. source
1857 3 │ create table t();
1858 ╰╴ ─ 2. destination
1859 ");
1860 }
1861
1862 #[test]
1863 fn goto_drop_type() {
1864 assert_snapshot!(goto("
1865create type t as enum ('a', 'b');
1866drop type t$0;
1867"), @r"
1868 ╭▸
1869 2 │ create type t as enum ('a', 'b');
1870 │ ─ 2. destination
1871 3 │ drop type t;
1872 ╰╴ ─ 1. source
1873 ");
1874 }
1875
1876 #[test]
1877 fn goto_drop_type_with_schema() {
1878 assert_snapshot!(goto("
1879create type public.t as enum ('a', 'b');
1880drop type t$0;
1881"), @r"
1882 ╭▸
1883 2 │ create type public.t as enum ('a', 'b');
1884 │ ─ 2. destination
1885 3 │ drop type t;
1886 ╰╴ ─ 1. source
1887 ");
1888
1889 assert_snapshot!(goto("
1890create type foo.t as enum ('a', 'b');
1891drop type foo.t$0;
1892"), @r"
1893 ╭▸
1894 2 │ create type foo.t as enum ('a', 'b');
1895 │ ─ 2. destination
1896 3 │ drop type foo.t;
1897 ╰╴ ─ 1. source
1898 ");
1899
1900 goto_not_found(
1901 "
1902create type t as enum ('a', 'b');
1903drop type foo.t$0;
1904",
1905 );
1906 }
1907
1908 #[test]
1909 fn goto_drop_type_defined_after() {
1910 assert_snapshot!(goto("
1911drop type t$0;
1912create type t as enum ('a', 'b');
1913"), @r"
1914 ╭▸
1915 2 │ drop type t;
1916 │ ─ 1. source
1917 3 │ create type t as enum ('a', 'b');
1918 ╰╴ ─ 2. destination
1919 ");
1920 }
1921
1922 #[test]
1923 fn goto_drop_type_composite() {
1924 assert_snapshot!(goto("
1925create type person as (name text, age int);
1926drop type person$0;
1927"), @r"
1928 ╭▸
1929 2 │ create type person as (name text, age int);
1930 │ ────── 2. destination
1931 3 │ drop type person;
1932 ╰╴ ─ 1. source
1933 ");
1934 }
1935
1936 #[test]
1937 fn goto_create_table_type_reference() {
1938 assert_snapshot!(goto("
1939create type person_info as (name text, email text);
1940create table user(id int, member person_info$0);
1941"), @r"
1942 ╭▸
1943 2 │ create type person_info as (name text, email text);
1944 │ ─────────── 2. destination
1945 3 │ create table user(id int, member person_info);
1946 ╰╴ ─ 1. source
1947 ");
1948 }
1949
1950 #[test]
1951 fn goto_function_param_table_type() {
1952 assert_snapshot!(goto("
1953create table t(a int, b int);
1954create function b(t$0) returns int as 'select 1' language sql;
1955"), @r"
1956 ╭▸
1957 2 │ create table t(a int, b int);
1958 │ ─ 2. destination
1959 3 │ create function b(t) returns int as 'select 1' language sql;
1960 ╰╴ ─ 1. source
1961 ");
1962 }
1963
1964 #[test]
1965 fn goto_function_param_time_type() {
1966 assert_snapshot!(goto("
1967create type timestamp;
1968create function f(timestamp$0 without time zone) returns text language internal;
1969"), @r"
1970 ╭▸
1971 2 │ create type timestamp;
1972 │ ───────── 2. destination
1973 3 │ create function f(timestamp without time zone) returns text language internal;
1974 ╰╴ ─ 1. source
1975 ");
1976 }
1977
1978 #[test]
1979 fn goto_function_param_time_type_no_timezone() {
1980 assert_snapshot!(goto("
1981create type time;
1982create function f(time$0) returns text language internal;
1983"), @r"
1984 ╭▸
19852 │ create type time;
1986 │ ──── 2. destination
19873 │ create function f(time) returns text language internal;
1988 ╰╴ ─ 1. source
1989");
1990 }
1991
1992 #[test]
1993 fn goto_create_table_type_reference_enum() {
1994 assert_snapshot!(goto("
1995create type mood as enum ('sad', 'ok', 'happy');
1996create table users(id int, mood mood$0);
1997"), @r"
1998 ╭▸
1999 2 │ create type mood as enum ('sad', 'ok', 'happy');
2000 │ ──── 2. destination
2001 3 │ create table users(id int, mood mood);
2002 ╰╴ ─ 1. source
2003 ");
2004 }
2005
2006 #[test]
2007 fn goto_create_table_type_reference_range() {
2008 assert_snapshot!(goto("
2009create type int4_range as range (subtype = int4);
2010create table metrics(id int, span int4_range$0);
2011"), @r"
2012 ╭▸
2013 2 │ create type int4_range as range (subtype = int4);
2014 │ ────────── 2. destination
2015 3 │ create table metrics(id int, span int4_range);
2016 ╰╴ ─ 1. source
2017 ");
2018 }
2019
2020 #[test]
2021 fn goto_create_table_type_reference_input_output() {
2022 assert_snapshot!(goto("
2023create type myint (input = myintin, output = myintout, like = int4);
2024create table data(id int, value myint$0);
2025"), @r"
2026 ╭▸
2027 2 │ create type myint (input = myintin, output = myintout, like = int4);
2028 │ ───── 2. destination
2029 3 │ create table data(id int, value myint);
2030 ╰╴ ─ 1. source
2031 ");
2032 }
2033
2034 #[test]
2035 fn goto_composite_type_field() {
2036 assert_snapshot!(goto("
2037create type person_info as (name text, email text);
2038create table user(id int, member person_info);
2039select (member).name$0 from user;
2040"), @r"
2041 ╭▸
2042 2 │ create type person_info as (name text, email text);
2043 │ ──── 2. destination
2044 3 │ create table user(id int, member person_info);
2045 4 │ select (member).name from user;
2046 ╰╴ ─ 1. source
2047 ");
2048 }
2049
2050 #[test]
2051 fn goto_drop_type_range() {
2052 assert_snapshot!(goto("
2053create type int4_range as range (subtype = int4);
2054drop type int4_range$0;
2055"), @r"
2056 ╭▸
2057 2 │ create type int4_range as range (subtype = int4);
2058 │ ────────── 2. destination
2059 3 │ drop type int4_range;
2060 ╰╴ ─ 1. source
2061 ");
2062 }
2063
2064 #[test]
2065 fn goto_drop_domain() {
2066 assert_snapshot!(goto("
2067create domain posint as integer check (value > 0);
2068drop domain posint$0;
2069"), @r"
2070 ╭▸
2071 2 │ create domain posint as integer check (value > 0);
2072 │ ────── 2. destination
2073 3 │ drop domain posint;
2074 ╰╴ ─ 1. source
2075 ");
2076 }
2077
2078 #[test]
2079 fn goto_cast_to_domain() {
2080 assert_snapshot!(goto("
2081create domain posint as integer check (value > 0);
2082select 1::posint$0;
2083"), @r"
2084 ╭▸
2085 2 │ create domain posint as integer check (value > 0);
2086 │ ────── 2. destination
2087 3 │ select 1::posint;
2088 ╰╴ ─ 1. source
2089 ");
2090 }
2091
2092 #[test]
2093 fn goto_drop_type_domain() {
2094 assert_snapshot!(goto("
2095create domain posint as integer check (value > 0);
2096drop type posint$0;
2097"), @r"
2098 ╭▸
2099 2 │ create domain posint as integer check (value > 0);
2100 │ ────── 2. destination
2101 3 │ drop type posint;
2102 ╰╴ ─ 1. source
2103 ");
2104 }
2105
2106 #[test]
2107 fn goto_drop_view() {
2108 assert_snapshot!(goto("
2109create view v as select 1;
2110drop view v$0;
2111"), @r"
2112 ╭▸
2113 2 │ create view v as select 1;
2114 │ ─ 2. destination
2115 3 │ drop view v;
2116 ╰╴ ─ 1. source
2117 ");
2118 }
2119
2120 #[test]
2121 fn goto_drop_materialized_view() {
2122 assert_snapshot!(goto("
2123create materialized view v as select 1;
2124drop materialized view v$0;
2125"), @r"
2126 ╭▸
2127 2 │ create materialized view v as select 1;
2128 │ ─ 2. destination
2129 3 │ drop materialized view v;
2130 ╰╴ ─ 1. source
2131 ");
2132 }
2133
2134 #[test]
2135 fn goto_drop_view_with_schema() {
2136 assert_snapshot!(goto("
2137create view public.v as select 1;
2138drop view v$0;
2139"), @r"
2140 ╭▸
2141 2 │ create view public.v as select 1;
2142 │ ─ 2. destination
2143 3 │ drop view v;
2144 ╰╴ ─ 1. source
2145 ");
2146
2147 assert_snapshot!(goto("
2148create view foo.v as select 1;
2149drop view foo.v$0;
2150"), @r"
2151 ╭▸
2152 2 │ create view foo.v as select 1;
2153 │ ─ 2. destination
2154 3 │ drop view foo.v;
2155 ╰╴ ─ 1. source
2156 ");
2157
2158 goto_not_found(
2159 "
2160create view v as select 1;
2161drop view foo.v$0;
2162",
2163 );
2164 }
2165
2166 #[test]
2167 fn goto_drop_temp_view() {
2168 assert_snapshot!(goto("
2169create temp view v as select 1;
2170drop view v$0;
2171"), @r"
2172 ╭▸
2173 2 │ create temp view v as select 1;
2174 │ ─ 2. destination
2175 3 │ drop view v;
2176 ╰╴ ─ 1. source
2177 ");
2178 }
2179
2180 #[test]
2181 fn goto_select_from_view() {
2182 assert_snapshot!(goto("
2183create view v as select 1;
2184select * from v$0;
2185"), @r"
2186 ╭▸
2187 2 │ create view v as select 1;
2188 │ ─ 2. destination
2189 3 │ select * from v;
2190 ╰╴ ─ 1. source
2191 ");
2192 }
2193
2194 #[test]
2195 fn goto_select_from_materialized_view() {
2196 assert_snapshot!(goto("
2197create materialized view v as select 1;
2198select * from v$0;
2199"), @r"
2200 ╭▸
2201 2 │ create materialized view v as select 1;
2202 │ ─ 2. destination
2203 3 │ select * from v;
2204 ╰╴ ─ 1. source
2205 ");
2206 }
2207
2208 #[test]
2209 fn goto_select_from_view_with_schema() {
2210 assert_snapshot!(goto("
2211create view public.v as select 1;
2212select * from public.v$0;
2213"), @r"
2214 ╭▸
2215 2 │ create view public.v as select 1;
2216 │ ─ 2. destination
2217 3 │ select * from public.v;
2218 ╰╴ ─ 1. source
2219 ");
2220 }
2221
2222 #[test]
2223 fn goto_view_column() {
2224 assert_snapshot!(goto("
2225create view v as select 1 as a;
2226select a$0 from v;
2227"), @r"
2228 ╭▸
2229 2 │ create view v as select 1 as a;
2230 │ ─ 2. destination
2231 3 │ select a from v;
2232 ╰╴ ─ 1. source
2233 ");
2234 }
2235
2236 #[test]
2237 fn goto_view_column_qualified() {
2238 assert_snapshot!(goto("
2239create view v as select 1 as a;
2240select v.a$0 from v;
2241"), @r"
2242 ╭▸
2243 2 │ create view v as select 1 as a;
2244 │ ─ 2. destination
2245 3 │ select v.a from v;
2246 ╰╴ ─ 1. source
2247 ");
2248 }
2249
2250 #[test]
2251 fn goto_create_table_as_column() {
2252 assert_snapshot!(goto("
2253create table t as select 1 a;
2254select a$0 from t;
2255"), @r"
2256 ╭▸
2257 2 │ create table t as select 1 a;
2258 │ ─ 2. destination
2259 3 │ select a from t;
2260 ╰╴ ─ 1. source
2261 ");
2262 }
2263
2264 #[test]
2265 fn goto_select_from_create_table_as() {
2266 assert_snapshot!(goto("
2267create table t as select 1 a;
2268select a from t$0;
2269"), @r"
2270 ╭▸
2271 2 │ create table t as select 1 a;
2272 │ ─ 2. destination
2273 3 │ select a from t;
2274 ╰╴ ─ 1. source
2275 ");
2276 }
2277
2278 #[test]
2279 fn goto_view_with_explicit_column_list() {
2280 assert_snapshot!(goto("
2281create view v(col1) as select 1;
2282select * from v$0;
2283"), @r"
2284 ╭▸
2285 2 │ create view v(col1) as select 1;
2286 │ ─ 2. destination
2287 3 │ select * from v;
2288 ╰╴ ─ 1. source
2289 ");
2290 }
2291
2292 #[test]
2293 fn goto_view_column_with_explicit_column_list() {
2294 assert_snapshot!(goto("
2295 create view v(col1) as select 1;
2296 select col1$0 from v;
2297 "), @r"
2298 ╭▸
2299 2 │ create view v(col1) as select 1;
2300 │ ──── 2. destination
2301 3 │ select col1 from v;
2302 ╰╴ ─ 1. source
2303 ");
2304 }
2305
2306 #[test]
2307 fn goto_view_column_with_schema() {
2308 assert_snapshot!(goto("
2309create view public.v as select 1 as a;
2310select a$0 from public.v;
2311"), @r"
2312 ╭▸
2313 2 │ create view public.v as select 1 as a;
2314 │ ─ 2. destination
2315 3 │ select a from public.v;
2316 ╰╴ ─ 1. source
2317 ");
2318 }
2319
2320 #[test]
2321 fn goto_view_multiple_columns() {
2322 assert_snapshot!(goto("
2323create view v as select 1 as a, 2 as b;
2324select b$0 from v;
2325"), @r"
2326 ╭▸
2327 2 │ create view v as select 1 as a, 2 as b;
2328 │ ─ 2. destination
2329 3 │ select b from v;
2330 ╰╴ ─ 1. source
2331 ");
2332 }
2333
2334 #[test]
2335 fn goto_view_column_from_table() {
2336 assert_snapshot!(goto("
2337create table t(x int, y int);
2338create view v as select x, y from t;
2339select x$0 from v;
2340"), @r"
2341 ╭▸
2342 3 │ create view v as select x, y from t;
2343 │ ─ 2. destination
2344 4 │ select x from v;
2345 ╰╴ ─ 1. source
2346 ");
2347 }
2348
2349 #[test]
2350 fn goto_view_column_with_table_preference() {
2351 assert_snapshot!(goto("
2352create table v(a int);
2353create view vw as select 1 as a;
2354select a$0 from v;
2355"), @r"
2356 ╭▸
2357 2 │ create table v(a int);
2358 │ ─ 2. destination
2359 3 │ create view vw as select 1 as a;
2360 4 │ select a from v;
2361 ╰╴ ─ 1. source
2362 ");
2363 }
2364
2365 #[test]
2366 fn goto_cast_operator() {
2367 assert_snapshot!(goto("
2368create type foo as enum ('a', 'b');
2369select x::foo$0;
2370"), @r"
2371 ╭▸
2372 2 │ create type foo as enum ('a', 'b');
2373 │ ─── 2. destination
2374 3 │ select x::foo;
2375 ╰╴ ─ 1. source
2376 ");
2377 }
2378
2379 #[test]
2380 fn goto_cast_function() {
2381 assert_snapshot!(goto("
2382create type bar as enum ('x', 'y');
2383select cast(x as bar$0);
2384"), @r"
2385 ╭▸
2386 2 │ create type bar as enum ('x', 'y');
2387 │ ─── 2. destination
2388 3 │ select cast(x as bar);
2389 ╰╴ ─ 1. source
2390 ");
2391 }
2392
2393 #[test]
2394 fn goto_cast_with_schema() {
2395 assert_snapshot!(goto("
2396create type public.baz as enum ('m', 'n');
2397select x::public.baz$0;
2398"), @r"
2399 ╭▸
2400 2 │ create type public.baz as enum ('m', 'n');
2401 │ ─── 2. destination
2402 3 │ select x::public.baz;
2403 ╰╴ ─ 1. source
2404 ");
2405 }
2406
2407 #[test]
2408 fn goto_cast_timestamp_without_time_zone() {
2409 assert_snapshot!(goto("
2410create type pg_catalog.timestamp;
2411select ''::timestamp without$0 time zone;
2412"), @r"
2413 ╭▸
2414 2 │ create type pg_catalog.timestamp;
2415 │ ───────── 2. destination
2416 3 │ select ''::timestamp without time zone;
2417 ╰╴ ─ 1. source
2418 ");
2419 }
2420
2421 #[test]
2422 fn goto_cast_timestamp_with_time_zone() {
2423 assert_snapshot!(goto("
2424create type pg_catalog.timestamptz;
2425select ''::timestamp with$0 time zone;
2426"), @r"
2427 ╭▸
2428 2 │ create type pg_catalog.timestamptz;
2429 │ ─────────── 2. destination
2430 3 │ select ''::timestamp with time zone;
2431 ╰╴ ─ 1. source
2432 ");
2433 }
2434
2435 #[test]
2436 fn goto_cast_multirange_type_from_range() {
2437 assert_snapshot!(goto("
2438create type floatrange as range (
2439 subtype = float8,
2440 subtype_diff = float8mi
2441);
2442select '{[1.234, 5.678]}'::floatmultirange$0;
2443"), @r"
2444 ╭▸
2445 2 │ create type floatrange as range (
2446 │ ────────── 2. destination
2447 ‡
2448 6 │ select '{[1.234, 5.678]}'::floatmultirange;
2449 ╰╴ ─ 1. source
2450 ");
2451 }
2452
2453 #[test]
2454 fn goto_cast_multirange_special_type_name_string() {
2455 assert_snapshot!(goto("
2456create type floatrange as range (
2457 subtype = float8,
2458 subtype_diff = float8mi,
2459 multirange_type_name = 'floatmulirangething'
2460);
2461select '{[1.234, 5.678]}'::floatmulirangething$0;
2462"), @r"
2463 ╭▸
2464 2 │ create type floatrange as range (
2465 │ ────────── 2. destination
2466 ‡
2467 7 │ select '{[1.234, 5.678]}'::floatmulirangething;
2468 ╰╴ ─ 1. source
2469 ");
2470 }
2471
2472 #[test]
2473 fn goto_cast_multirange_special_type_name_ident() {
2474 assert_snapshot!(goto("
2475create type floatrange as range (
2476 subtype = float8,
2477 subtype_diff = float8mi,
2478 multirange_type_name = floatrangemutirange
2479);
2480select '{[1.234, 5.678]}'::floatrangemutirange$0;
2481"), @r"
2482 ╭▸
2483 2 │ create type floatrange as range (
2484 │ ────────── 2. destination
2485 ‡
2486 7 │ select '{[1.234, 5.678]}'::floatrangemutirange;
2487 ╰╴ ─ 1. source
2488 ");
2489 }
2490
2491 #[test]
2492 fn goto_cast_multirange_edge_case_type_from_range() {
2493 assert_snapshot!(goto("
2495create type floatrangerange as range (
2496 subtype = float8,
2497 subtype_diff = float8mi
2498);
2499select '{[1.234, 5.678]}'::floatmultirangerange$0;
2500"), @r"
2501 ╭▸
2502 2 │ create type floatrangerange as range (
2503 │ ─────────────── 2. destination
2504 ‡
2505 6 │ select '{[1.234, 5.678]}'::floatmultirangerange;
2506 ╰╴ ─ 1. source
2507 ");
2508 }
2509
2510 #[test]
2511 fn goto_cast_bigint_falls_back_to_int8() {
2512 assert_snapshot!(goto("
2513create type pg_catalog.int8;
2514select 1::bigint$0;
2515"), @r"
2516 ╭▸
2517 2 │ create type pg_catalog.int8;
2518 │ ──── 2. destination
2519 3 │ select 1::bigint;
2520 ╰╴ ─ 1. source
2521 ");
2522 }
2523
2524 #[test]
2525 fn goto_cast_bigint_prefers_user_type() {
2526 assert_snapshot!(goto("
2527create type bigint;
2528create type pg_catalog.int8;
2529select 1::bigint$0;
2530"), @r"
2531 ╭▸
2532 2 │ create type bigint;
2533 │ ────── 2. destination
2534 3 │ create type pg_catalog.int8;
2535 4 │ select 1::bigint;
2536 ╰╴ ─ 1. source
2537 ");
2538 }
2539
2540 #[test]
2541 fn goto_cast_smallserial_falls_back_to_int2() {
2542 assert_snapshot!(goto("
2543create type pg_catalog.int2;
2544select 1::smallserial$0;
2545"), @r"
2546 ╭▸
2547 2 │ create type pg_catalog.int2;
2548 │ ──── 2. destination
2549 3 │ select 1::smallserial;
2550 ╰╴ ─ 1. source
2551 ");
2552 }
2553
2554 #[test]
2555 fn goto_cast_serial2_falls_back_to_int2() {
2556 assert_snapshot!(goto("
2557create type pg_catalog.int2;
2558select 1::serial2$0;
2559"), @r"
2560 ╭▸
2561 2 │ create type pg_catalog.int2;
2562 │ ──── 2. destination
2563 3 │ select 1::serial2;
2564 ╰╴ ─ 1. source
2565 ");
2566 }
2567
2568 #[test]
2569 fn goto_cast_serial_falls_back_to_int4() {
2570 assert_snapshot!(goto("
2571create type pg_catalog.int4;
2572select 1::serial$0;
2573"), @r"
2574 ╭▸
2575 2 │ create type pg_catalog.int4;
2576 │ ──── 2. destination
2577 3 │ select 1::serial;
2578 ╰╴ ─ 1. source
2579 ");
2580 }
2581
2582 #[test]
2583 fn goto_cast_serial4_falls_back_to_int4() {
2584 assert_snapshot!(goto("
2585create type pg_catalog.int4;
2586select 1::serial4$0;
2587"), @r"
2588 ╭▸
2589 2 │ create type pg_catalog.int4;
2590 │ ──── 2. destination
2591 3 │ select 1::serial4;
2592 ╰╴ ─ 1. source
2593 ");
2594 }
2595
2596 #[test]
2597 fn goto_cast_bigserial_falls_back_to_int8() {
2598 assert_snapshot!(goto("
2599create type pg_catalog.int8;
2600select 1::bigserial$0;
2601"), @r"
2602 ╭▸
2603 2 │ create type pg_catalog.int8;
2604 │ ──── 2. destination
2605 3 │ select 1::bigserial;
2606 ╰╴ ─ 1. source
2607 ");
2608 }
2609
2610 #[test]
2611 fn goto_cast_serial8_falls_back_to_int8() {
2612 assert_snapshot!(goto("
2613create type pg_catalog.int8;
2614select 1::serial8$0;
2615"), @r"
2616 ╭▸
2617 2 │ create type pg_catalog.int8;
2618 │ ──── 2. destination
2619 3 │ select 1::serial8;
2620 ╰╴ ─ 1. source
2621 ");
2622 }
2623
2624 #[test]
2625 fn goto_cast_int_falls_back_to_int4() {
2626 assert_snapshot!(goto("
2627create type pg_catalog.int4;
2628select 1::int$0;
2629"), @r"
2630 ╭▸
2631 2 │ create type pg_catalog.int4;
2632 │ ──── 2. destination
2633 3 │ select 1::int;
2634 ╰╴ ─ 1. source
2635 ");
2636 }
2637
2638 #[test]
2639 fn goto_cast_integer_falls_back_to_int4() {
2640 assert_snapshot!(goto("
2641create type pg_catalog.int4;
2642select 1::integer$0;
2643"), @r"
2644 ╭▸
2645 2 │ create type pg_catalog.int4;
2646 │ ──── 2. destination
2647 3 │ select 1::integer;
2648 ╰╴ ─ 1. source
2649 ");
2650 }
2651
2652 #[test]
2653 fn goto_cast_smallint_falls_back_to_int2() {
2654 assert_snapshot!(goto("
2655create type pg_catalog.int2;
2656select 1::smallint$0;
2657"), @r"
2658 ╭▸
2659 2 │ create type pg_catalog.int2;
2660 │ ──── 2. destination
2661 3 │ select 1::smallint;
2662 ╰╴ ─ 1. source
2663 ");
2664 }
2665
2666 #[test]
2667 fn goto_cast_double_precision_falls_back_to_float8() {
2668 assert_snapshot!(goto("
2669create type pg_catalog.float8;
2670select '1'::double precision[]$0;
2671"), @r"
2672 ╭▸
2673 2 │ create type pg_catalog.float8;
2674 │ ────── 2. destination
2675 3 │ select '1'::double precision[];
2676 ╰╴ ─ 1. source
2677 ");
2678 }
2679
2680 #[test]
2681 fn goto_cast_varchar_with_modifier() {
2682 assert_snapshot!(goto("
2683create type pg_catalog.varchar;
2684select '1'::varchar$0(1);
2685"), @r"
2686 ╭▸
2687 2 │ create type pg_catalog.varchar;
2688 │ ─────── 2. destination
2689 3 │ select '1'::varchar(1);
2690 ╰╴ ─ 1. source
2691 ");
2692 }
2693
2694 #[test]
2695 fn goto_cast_composite_type() {
2696 assert_snapshot!(goto("
2697create type person_info as (name varchar(50), age int);
2698select ('Alice', 30)::person_info$0;
2699"), @r"
2700 ╭▸
2701 2 │ create type person_info as (name varchar(50), age int);
2702 │ ─────────── 2. destination
2703 3 │ select ('Alice', 30)::person_info;
2704 ╰╴ ─ 1. source
2705 ");
2706 }
2707
2708 #[test]
2709 fn goto_cast_composite_type_in_cte() {
2710 assert_snapshot!(goto("
2711create type person_info as (name varchar(50), age int);
2712with team as (
2713 select 1 as id, ('Alice', 30)::person_info$0 as member
2714)
2715select * from team;
2716"), @r"
2717 ╭▸
2718 2 │ create type person_info as (name varchar(50), age int);
2719 │ ─────────── 2. destination
2720 3 │ with team as (
2721 4 │ select 1 as id, ('Alice', 30)::person_info as member
2722 ╰╴ ─ 1. source
2723 ");
2724 }
2725
2726 #[test]
2727 fn goto_composite_type_field_name() {
2728 assert_snapshot!(goto("
2729create type person_info as (name varchar(50), age int);
2730with team as (
2731 select 1 as id, ('Alice', 30)::person_info as member
2732)
2733select (member).name$0, (member).age from team;
2734"), @r"
2735 ╭▸
2736 2 │ create type person_info as (name varchar(50), age int);
2737 │ ──── 2. destination
2738 ‡
2739 6 │ select (member).name, (member).age from team;
2740 ╰╴ ─ 1. source
2741 ");
2742 }
2743
2744 #[test]
2745 fn goto_composite_type_field_in_where() {
2746 assert_snapshot!(goto("
2747create type person_info as (name varchar(50), age int);
2748with team as (
2749 select 1 as id, ('Alice', 30)::person_info as member
2750 union all
2751 select 2, ('Bob', 25)::person_info
2752)
2753select (member).name, (member).age
2754from team
2755where (member).age$0 >= 18;
2756"), @r"
2757 ╭▸
2758 2 │ create type person_info as (name varchar(50), age int);
2759 │ ─── 2. destination
2760 ‡
2761 10 │ where (member).age >= 18;
2762 ╰╴ ─ 1. source
2763 ");
2764 }
2765
2766 #[test]
2767 fn goto_composite_type_field_base() {
2768 assert_snapshot!(goto("
2769create type person_info as (name varchar(50), age int);
2770with team as (
2771 select 1 as id, ('Alice', 30)::person_info as member
2772)
2773select (member$0).age from team;
2774"), @r"
2775 ╭▸
2776 4 │ select 1 as id, ('Alice', 30)::person_info as member
2777 │ ────── 2. destination
2778 5 │ )
2779 6 │ select (member).age from team;
2780 ╰╴ ─ 1. source
2781 ");
2782 }
2783
2784 #[test]
2785 fn goto_composite_type_field_nested_parens() {
2786 assert_snapshot!(goto("
2787create type person_info as (name varchar(50), age int);
2788with team as (
2789 select 1 as id, ('Alice', 30)::person_info as member
2790)
2791select ((((member))).name$0) from team;
2792"), @r"
2793 ╭▸
2794 2 │ create type person_info as (name varchar(50), age int);
2795 │ ──── 2. destination
2796 ‡
2797 6 │ select ((((member))).name) from team;
2798 ╰╴ ─ 1. source
2799 ");
2800 }
2801
2802 #[test]
2803 fn begin_to_rollback() {
2804 assert_snapshot!(goto(
2805 "
2806begin$0;
2807select 1;
2808rollback;
2809commit;
2810",
2811 ), @r"
2812 ╭▸
2813 2 │ begin;
2814 │ ─ 1. source
2815 3 │ select 1;
2816 4 │ rollback;
2817 ╰╴──────── 2. destination
2818 ");
2819 }
2820
2821 #[test]
2822 fn commit_to_begin() {
2823 assert_snapshot!(goto(
2824 "
2825begin;
2826select 1;
2827commit$0;
2828",
2829 ), @r"
2830 ╭▸
2831 2 │ begin;
2832 │ ───── 2. destination
2833 3 │ select 1;
2834 4 │ commit;
2835 ╰╴ ─ 1. source
2836 ");
2837 }
2838
2839 #[test]
2840 fn begin_to_commit() {
2841 assert_snapshot!(goto(
2842 "
2843begin$0;
2844select 1;
2845commit;
2846",
2847 ), @r"
2848 ╭▸
2849 2 │ begin;
2850 │ ─ 1. source
2851 3 │ select 1;
2852 4 │ commit;
2853 ╰╴────── 2. destination
2854 ");
2855 }
2856
2857 #[test]
2858 fn commit_to_start_transaction() {
2859 assert_snapshot!(goto(
2860 "
2861start transaction;
2862select 1;
2863commit$0;
2864",
2865 ), @r"
2866 ╭▸
2867 2 │ start transaction;
2868 │ ───────────────── 2. destination
2869 3 │ select 1;
2870 4 │ commit;
2871 ╰╴ ─ 1. source
2872 ");
2873 }
2874
2875 #[test]
2876 fn start_transaction_to_commit() {
2877 assert_snapshot!(goto(
2878 "
2879start$0 transaction;
2880select 1;
2881commit;
2882",
2883 ), @r"
2884 ╭▸
2885 2 │ start transaction;
2886 │ ─ 1. source
2887 3 │ select 1;
2888 4 │ commit;
2889 ╰╴────── 2. destination
2890 ");
2891 }
2892
2893 #[test]
2894 fn goto_with_search_path() {
2895 assert_snapshot!(goto(r#"
2896set search_path to "foo", public;
2897create table foo.t();
2898drop table t$0;
2899"#), @r"
2900 ╭▸
2901 3 │ create table foo.t();
2902 │ ─ 2. destination
2903 4 │ drop table t;
2904 ╰╴ ─ 1. source
2905 ");
2906 }
2907
2908 #[test]
2909 fn goto_with_search_path_and_unspecified_table() {
2910 assert_snapshot!(goto(r#"
2911set search_path to foo,bar;
2912create table t();
2913drop table foo.t$0;
2914"#), @r"
2915 ╭▸
2916 3 │ create table t();
2917 │ ─ 2. destination
2918 4 │ drop table foo.t;
2919 ╰╴ ─ 1. source
2920 ");
2921 }
2922
2923 #[test]
2924 fn goto_column_not_in_cte_but_in_table() {
2925 goto_not_found(
2927 r"
2928create table t (c int);
2929with t as (select 1 a)
2930select c$0 from t;
2931",
2932 );
2933 }
2934
2935 #[test]
2936 fn goto_with_search_path_empty() {
2937 goto_not_found(
2938 r#"
2939set search_path = '';
2940create table t();
2941drop table t$0;
2942"#,
2943 );
2944 }
2945
2946 #[test]
2947 fn goto_with_search_path_like_variable() {
2948 goto_not_found(
2950 "
2951set bar.search_path to foo, public;
2952create table foo.t();
2953drop table t$0;
2954",
2955 )
2956 }
2957
2958 #[test]
2959 fn goto_with_search_path_second_schema() {
2960 assert_snapshot!(goto("
2961set search_path to foo, bar, public;
2962create table bar.t();
2963drop table t$0;
2964"), @r"
2965 ╭▸
2966 3 │ create table bar.t();
2967 │ ─ 2. destination
2968 4 │ drop table t;
2969 ╰╴ ─ 1. source
2970 ");
2971 }
2972
2973 #[test]
2974 fn goto_with_search_path_skips_first() {
2975 assert_snapshot!(goto("
2976set search_path to foo, bar, public;
2977create table foo.t();
2978create table bar.t();
2979drop table t$0;
2980"), @r"
2981 ╭▸
2982 3 │ create table foo.t();
2983 │ ─ 2. destination
2984 4 │ create table bar.t();
2985 5 │ drop table t;
2986 ╰╴ ─ 1. source
2987 ");
2988 }
2989
2990 #[test]
2991 fn goto_without_search_path_uses_default() {
2992 assert_snapshot!(goto("
2993create table foo.t();
2994create table public.t();
2995drop table t$0;
2996"), @r"
2997 ╭▸
2998 3 │ create table public.t();
2999 │ ─ 2. destination
3000 4 │ drop table t;
3001 ╰╴ ─ 1. source
3002 ");
3003 }
3004
3005 #[test]
3006 fn goto_with_set_schema() {
3007 assert_snapshot!(goto("
3008set schema 'myschema';
3009create table myschema.t();
3010drop table t$0;
3011"), @r"
3012 ╭▸
3013 3 │ create table myschema.t();
3014 │ ─ 2. destination
3015 4 │ drop table t;
3016 ╰╴ ─ 1. source
3017 ");
3018 }
3019
3020 #[test]
3021 fn goto_with_set_schema_ignores_other_schemas() {
3022 assert_snapshot!(goto("
3023set schema 'myschema';
3024create table public.t();
3025create table myschema.t();
3026drop table t$0;
3027"), @r"
3028 ╭▸
3029 4 │ create table myschema.t();
3030 │ ─ 2. destination
3031 5 │ drop table t;
3032 ╰╴ ─ 1. source
3033 ");
3034 }
3035
3036 #[test]
3037 fn goto_with_search_path_changed_twice() {
3038 assert_snapshot!(goto("
3039set search_path to foo;
3040create table foo.t();
3041set search_path to bar;
3042create table bar.t();
3043drop table t$0;
3044"), @r"
3045 ╭▸
3046 5 │ create table bar.t();
3047 │ ─ 2. destination
3048 6 │ drop table t;
3049 ╰╴ ─ 1. source
3050 ");
3051
3052 assert_snapshot!(goto("
3053set search_path to foo;
3054create table foo.t();
3055drop table t$0;
3056set search_path to bar;
3057create table bar.t();
3058drop table t;
3059"), @r"
3060 ╭▸
3061 3 │ create table foo.t();
3062 │ ─ 2. destination
3063 4 │ drop table t;
3064 ╰╴ ─ 1. source
3065 ");
3066 }
3067
3068 #[test]
3069 fn goto_with_empty_search_path() {
3070 goto_not_found(
3071 "
3072set search_path to '';
3073create table public.t();
3074drop table t$0;
3075",
3076 )
3077 }
3078
3079 #[test]
3080 fn goto_with_search_path_uppercase() {
3081 assert_snapshot!(goto("
3082SET SEARCH_PATH TO foo;
3083create table foo.t();
3084drop table t$0;
3085"), @r"
3086 ╭▸
3087 3 │ create table foo.t();
3088 │ ─ 2. destination
3089 4 │ drop table t;
3090 ╰╴ ─ 1. source
3091 ");
3092 }
3093
3094 #[test]
3095 fn goto_table_stmt() {
3096 assert_snapshot!(goto("
3097create table t();
3098table t$0;
3099"), @r"
3100 ╭▸
3101 2 │ create table t();
3102 │ ─ 2. destination
3103 3 │ table t;
3104 ╰╴ ─ 1. source
3105 ");
3106 }
3107
3108 #[test]
3109 fn goto_table_stmt_with_schema() {
3110 assert_snapshot!(goto("
3111create table public.t();
3112table public.t$0;
3113"), @r"
3114 ╭▸
3115 2 │ create table public.t();
3116 │ ─ 2. destination
3117 3 │ table public.t;
3118 ╰╴ ─ 1. source
3119 ");
3120 }
3121
3122 #[test]
3123 fn goto_table_stmt_with_search_path() {
3124 assert_snapshot!(goto("
3125set search_path to foo;
3126create table foo.t();
3127table t$0;
3128"), @r"
3129 ╭▸
3130 3 │ create table foo.t();
3131 │ ─ 2. destination
3132 4 │ table t;
3133 ╰╴ ─ 1. source
3134 ");
3135 }
3136
3137 #[test]
3138 fn goto_drop_index() {
3139 assert_snapshot!(goto("
3140create index idx_name on t(x);
3141drop index idx_name$0;
3142"), @r"
3143 ╭▸
3144 2 │ create index idx_name on t(x);
3145 │ ──────── 2. destination
3146 3 │ drop index idx_name;
3147 ╰╴ ─ 1. source
3148 ");
3149 }
3150
3151 #[test]
3152 fn goto_drop_index_with_schema() {
3153 assert_snapshot!(goto(r#"
3154set search_path to public;
3155create index idx_name on t(x);
3156drop index public.idx_name$0;
3157"#), @r"
3158 ╭▸
3159 3 │ create index idx_name on t(x);
3160 │ ──────── 2. destination
3161 4 │ drop index public.idx_name;
3162 ╰╴ ─ 1. source
3163 ");
3164 }
3165
3166 #[test]
3167 fn goto_drop_index_defined_after() {
3168 assert_snapshot!(goto("
3169drop index idx_name$0;
3170create index idx_name on t(x);
3171"), @r"
3172 ╭▸
3173 2 │ drop index idx_name;
3174 │ ─ 1. source
3175 3 │ create index idx_name on t(x);
3176 ╰╴ ──────── 2. destination
3177 ");
3178 }
3179
3180 #[test]
3181 fn goto_index_definition_returns_self() {
3182 assert_snapshot!(goto("
3183create index idx_name$0 on t(x);
3184"), @r"
3185 ╭▸
3186 2 │ create index idx_name on t(x);
3187 │ ┬──────┬
3188 │ │ │
3189 │ │ 1. source
3190 ╰╴ 2. destination
3191 ");
3192 }
3193
3194 #[test]
3195 fn goto_drop_index_with_search_path() {
3196 assert_snapshot!(goto(r#"
3197create index idx_name on t(x);
3198set search_path to bar;
3199create index idx_name on f(x);
3200set search_path to default;
3201drop index idx_name$0;
3202"#), @r"
3203 ╭▸
3204 2 │ create index idx_name on t(x);
3205 │ ──────── 2. destination
3206 ‡
3207 6 │ drop index idx_name;
3208 ╰╴ ─ 1. source
3209 ");
3210 }
3211
3212 #[test]
3213 fn goto_drop_index_multiple() {
3214 assert_snapshot!(goto("
3215create index idx1 on t(x);
3216create index idx2 on t(y);
3217drop index idx1, idx2$0;
3218"), @r"
3219 ╭▸
3220 3 │ create index idx2 on t(y);
3221 │ ──── 2. destination
3222 4 │ drop index idx1, idx2;
3223 ╰╴ ─ 1. source
3224 ");
3225 }
3226
3227 #[test]
3228 fn goto_create_index_table() {
3229 assert_snapshot!(goto("
3230create table users(id int);
3231create index idx_users on users$0(id);
3232"), @r"
3233 ╭▸
3234 2 │ create table users(id int);
3235 │ ───── 2. destination
3236 3 │ create index idx_users on users(id);
3237 ╰╴ ─ 1. source
3238 ");
3239 }
3240
3241 #[test]
3242 fn goto_create_index_table_with_schema() {
3243 assert_snapshot!(goto("
3244create table public.users(id int);
3245create index idx_users on public.users$0(id);
3246"), @r"
3247 ╭▸
3248 2 │ create table public.users(id int);
3249 │ ───── 2. destination
3250 3 │ create index idx_users on public.users(id);
3251 ╰╴ ─ 1. source
3252 ");
3253 }
3254
3255 #[test]
3256 fn goto_create_index_table_with_search_path() {
3257 assert_snapshot!(goto(r#"
3258set search_path to foo;
3259create table foo.users(id int);
3260create index idx_users on users$0(id);
3261"#), @r"
3262 ╭▸
3263 3 │ create table foo.users(id int);
3264 │ ───── 2. destination
3265 4 │ create index idx_users on users(id);
3266 ╰╴ ─ 1. source
3267 ");
3268 }
3269
3270 #[test]
3271 fn goto_create_index_temp_table() {
3272 assert_snapshot!(goto("
3273create temp table users(id int);
3274create index idx_users on users$0(id);
3275"), @r"
3276 ╭▸
3277 2 │ create temp table users(id int);
3278 │ ───── 2. destination
3279 3 │ create index idx_users on users(id);
3280 ╰╴ ─ 1. source
3281 ");
3282 }
3283
3284 #[test]
3285 fn goto_create_index_column() {
3286 assert_snapshot!(goto("
3287create table users(id int, email text);
3288create index idx_email on users(email$0);
3289"), @r"
3290 ╭▸
3291 2 │ create table users(id int, email text);
3292 │ ───── 2. destination
3293 3 │ create index idx_email on users(email);
3294 ╰╴ ─ 1. source
3295 ");
3296 }
3297
3298 #[test]
3299 fn goto_create_index_first_column() {
3300 assert_snapshot!(goto("
3301create table users(id int, email text);
3302create index idx_id on users(id$0);
3303"), @r"
3304 ╭▸
3305 2 │ create table users(id int, email text);
3306 │ ── 2. destination
3307 3 │ create index idx_id on users(id);
3308 ╰╴ ─ 1. source
3309 ");
3310 }
3311
3312 #[test]
3313 fn goto_create_index_multiple_columns() {
3314 assert_snapshot!(goto("
3315create table users(id int, email text, name text);
3316create index idx_users on users(id, email$0, name);
3317"), @r"
3318 ╭▸
3319 2 │ create table users(id int, email text, name text);
3320 │ ───── 2. destination
3321 3 │ create index idx_users on users(id, email, name);
3322 ╰╴ ─ 1. source
3323 ");
3324 }
3325
3326 #[test]
3327 fn goto_create_index_column_with_schema() {
3328 assert_snapshot!(goto("
3329create table public.users(id int, email text);
3330create index idx_email on public.users(email$0);
3331"), @r"
3332 ╭▸
3333 2 │ create table public.users(id int, email text);
3334 │ ───── 2. destination
3335 3 │ create index idx_email on public.users(email);
3336 ╰╴ ─ 1. source
3337 ");
3338 }
3339
3340 #[test]
3341 fn goto_create_index_column_temp_table() {
3342 assert_snapshot!(goto("
3343create temp table users(id int, email text);
3344create index idx_email on users(email$0);
3345"), @r"
3346 ╭▸
3347 2 │ create temp table users(id int, email text);
3348 │ ───── 2. destination
3349 3 │ create index idx_email on users(email);
3350 ╰╴ ─ 1. source
3351 ");
3352 }
3353
3354 #[test]
3355 fn goto_drop_function() {
3356 assert_snapshot!(goto("
3357create function foo() returns int as $$ select 1 $$ language sql;
3358drop function foo$0();
3359"), @r"
3360 ╭▸
3361 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3362 │ ─── 2. destination
3363 3 │ drop function foo();
3364 ╰╴ ─ 1. source
3365 ");
3366 }
3367
3368 #[test]
3369 fn goto_drop_function_with_schema() {
3370 assert_snapshot!(goto("
3371set search_path to public;
3372create function foo() returns int as $$ select 1 $$ language sql;
3373drop function public.foo$0();
3374"), @r"
3375 ╭▸
3376 3 │ create function foo() returns int as $$ select 1 $$ language sql;
3377 │ ─── 2. destination
3378 4 │ drop function public.foo();
3379 ╰╴ ─ 1. source
3380 ");
3381 }
3382
3383 #[test]
3384 fn goto_drop_function_defined_after() {
3385 assert_snapshot!(goto("
3386drop function foo$0();
3387create function foo() returns int as $$ select 1 $$ language sql;
3388"), @r"
3389 ╭▸
3390 2 │ drop function foo();
3391 │ ─ 1. source
3392 3 │ create function foo() returns int as $$ select 1 $$ language sql;
3393 ╰╴ ─── 2. destination
3394 ");
3395 }
3396
3397 #[test]
3398 fn goto_function_definition_returns_self() {
3399 assert_snapshot!(goto("
3400create function foo$0() returns int as $$ select 1 $$ language sql;
3401"), @r"
3402 ╭▸
3403 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3404 │ ┬─┬
3405 │ │ │
3406 │ │ 1. source
3407 ╰╴ 2. destination
3408 ");
3409 }
3410
3411 #[test]
3412 fn goto_drop_function_with_search_path() {
3413 assert_snapshot!(goto("
3414create function foo() returns int as $$ select 1 $$ language sql;
3415set search_path to bar;
3416create function foo() returns int as $$ select 1 $$ language sql;
3417set search_path to default;
3418drop function foo$0();
3419"), @r"
3420 ╭▸
3421 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3422 │ ─── 2. destination
3423 ‡
3424 6 │ drop function foo();
3425 ╰╴ ─ 1. source
3426 ");
3427 }
3428
3429 #[test]
3430 fn goto_drop_function_multiple() {
3431 assert_snapshot!(goto("
3432create function foo() returns int as $$ select 1 $$ language sql;
3433create function bar() returns int as $$ select 1 $$ language sql;
3434drop function foo(), bar$0();
3435"), @r"
3436 ╭▸
3437 3 │ create function bar() returns int as $$ select 1 $$ language sql;
3438 │ ─── 2. destination
3439 4 │ drop function foo(), bar();
3440 ╰╴ ─ 1. source
3441 ");
3442 }
3443
3444 #[test]
3445 fn goto_drop_function_overloaded() {
3446 assert_snapshot!(goto("
3447create function add(complex) returns complex as $$ select null $$ language sql;
3448create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3449drop function add$0(complex);
3450"), @r"
3451 ╭▸
3452 2 │ create function add(complex) returns complex as $$ select null $$ language sql;
3453 │ ─── 2. destination
3454 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3455 4 │ drop function add(complex);
3456 ╰╴ ─ 1. source
3457 ");
3458 }
3459
3460 #[test]
3461 fn goto_drop_function_second_overload() {
3462 assert_snapshot!(goto("
3463create function add(complex) returns complex as $$ select null $$ language sql;
3464create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3465drop function add$0(bigint);
3466"), @r"
3467 ╭▸
3468 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
3469 │ ─── 2. destination
3470 4 │ drop function add(bigint);
3471 ╰╴ ─ 1. source
3472 ");
3473 }
3474
3475 #[test]
3476 fn goto_select_function_call() {
3477 assert_snapshot!(goto("
3478create function foo() returns int as $$ select 1 $$ language sql;
3479select foo$0();
3480"), @r"
3481 ╭▸
3482 2 │ create function foo() returns int as $$ select 1 $$ language sql;
3483 │ ─── 2. destination
3484 3 │ select foo();
3485 ╰╴ ─ 1. source
3486 ");
3487 }
3488
3489 #[test]
3490 fn goto_select_aggregate_call() {
3491 assert_snapshot!(goto("
3492create aggregate foo(int) (
3493 sfunc = int4pl,
3494 stype = int,
3495 initcond = '0'
3496);
3497
3498select foo$0(1);
3499"), @r"
3500 ╭▸
3501 2 │ create aggregate foo(int) (
3502 │ ─── 2. destination
3503 ‡
3504 8 │ select foo(1);
3505 ╰╴ ─ 1. source
3506 ");
3507 }
3508
3509 #[test]
3510 fn goto_default_constraint_function_call() {
3511 assert_snapshot!(goto("
3512create function f() returns int as 'select 1' language sql;
3513create table t(
3514 a int default f$0()
3515);
3516"), @r"
3517 ╭▸
3518 2 │ create function f() returns int as 'select 1' language sql;
3519 │ ─ 2. destination
3520 3 │ create table t(
3521 4 │ a int default f()
3522 ╰╴ ─ 1. source
3523 ");
3524 }
3525
3526 #[test]
3527 fn goto_select_function_call_with_schema() {
3528 assert_snapshot!(goto("
3529create function public.foo() returns int as $$ select 1 $$ language sql;
3530select public.foo$0();
3531"), @r"
3532 ╭▸
3533 2 │ create function public.foo() returns int as $$ select 1 $$ language sql;
3534 │ ─── 2. destination
3535 3 │ select public.foo();
3536 ╰╴ ─ 1. source
3537 ");
3538 }
3539
3540 #[test]
3541 fn goto_select_function_call_with_search_path() {
3542 assert_snapshot!(goto("
3543set search_path to myschema;
3544create function foo() returns int as $$ select 1 $$ language sql;
3545select myschema.foo$0();
3546"), @r"
3547 ╭▸
3548 3 │ create function foo() returns int as $$ select 1 $$ language sql;
3549 │ ─── 2. destination
3550 4 │ select myschema.foo();
3551 ╰╴ ─ 1. source
3552 ");
3553 }
3554
3555 #[test]
3556 fn goto_function_call_style_column_access() {
3557 assert_snapshot!(goto("
3558create table t(a int, b int);
3559select a$0(t) from t;
3560"), @r"
3561 ╭▸
3562 2 │ create table t(a int, b int);
3563 │ ─ 2. destination
3564 3 │ select a(t) from t;
3565 ╰╴ ─ 1. source
3566 ");
3567 }
3568
3569 #[test]
3570 fn goto_function_call_style_column_access_with_function_precedence() {
3571 assert_snapshot!(goto("
3572create table t(a int, b int);
3573create function b(t) returns int as 'select 1' LANGUAGE sql;
3574select b$0(t) from t;
3575"), @r"
3576 ╭▸
3577 3 │ create function b(t) returns int as 'select 1' LANGUAGE sql;
3578 │ ─ 2. destination
3579 4 │ select b(t) from t;
3580 ╰╴ ─ 1. source
3581 ");
3582 }
3583
3584 #[test]
3585 fn goto_function_call_style_column_access_table_arg() {
3586 assert_snapshot!(goto("
3587create table t(a int, b int);
3588select a(t$0) from t;
3589"), @r"
3590 ╭▸
3591 2 │ create table t(a int, b int);
3592 │ ─ 2. destination
3593 3 │ select a(t) from t;
3594 ╰╴ ─ 1. source
3595 ");
3596 }
3597
3598 #[test]
3599 fn goto_function_call_style_column_access_table_arg_with_function() {
3600 assert_snapshot!(goto("
3601create table t(a int, b int);
3602create function b(t) returns int as 'select 1' LANGUAGE sql;
3603select b(t$0) from t;
3604"), @r"
3605 ╭▸
3606 2 │ create table t(a int, b int);
3607 │ ─ 2. destination
3608 3 │ create function b(t) returns int as 'select 1' LANGUAGE sql;
3609 4 │ select b(t) from t;
3610 ╰╴ ─ 1. source
3611 ");
3612 }
3613
3614 #[test]
3615 fn goto_function_call_multiple_args_not_column_access() {
3616 goto_not_found(
3617 "
3618create table t(a int, b int);
3619select a$0(t, 1) from t;
3620",
3621 );
3622 }
3623
3624 #[test]
3625 fn goto_function_call_nested() {
3626 assert_snapshot!(goto("
3627create function f() returns int8
3628 as 'select 1'
3629 language sql;
3630select format('foo%d', f$0());
3631"), @r"
3632 ╭▸
3633 2 │ create function f() returns int8
3634 │ ─ 2. destination
3635 ‡
3636 5 │ select format('foo%d', f());
3637 ╰╴ ─ 1. source
3638 ");
3639 }
3640
3641 #[test]
3642 fn goto_field_style_function_call() {
3643 assert_snapshot!(goto("
3644create table t(a int);
3645create function b(t) returns int as 'select 1' language sql;
3646select t.b$0 from t;
3647"), @r"
3648 ╭▸
3649 3 │ create function b(t) returns int as 'select 1' language sql;
3650 │ ─ 2. destination
3651 4 │ select t.b from t;
3652 ╰╴ ─ 1. source
3653 ");
3654 }
3655
3656 #[test]
3657 fn goto_field_style_function_call_column_precedence() {
3658 assert_snapshot!(goto("
3659create table t(a int, b int);
3660create function b(t) returns int as 'select 1' language sql;
3661select t.b$0 from t;
3662"), @r"
3663 ╭▸
3664 2 │ create table t(a int, b int);
3665 │ ─ 2. destination
3666 3 │ create function b(t) returns int as 'select 1' language sql;
3667 4 │ select t.b from t;
3668 ╰╴ ─ 1. source
3669 ");
3670 }
3671
3672 #[test]
3673 fn goto_field_style_function_call_table_ref() {
3674 assert_snapshot!(goto("
3675create table t(a int);
3676create function b(t) returns int as 'select 1' language sql;
3677select t$0.b from t;
3678"), @r"
3679 ╭▸
3680 2 │ create table t(a int);
3681 │ ─ 2. destination
3682 3 │ create function b(t) returns int as 'select 1' language sql;
3683 4 │ select t.b from t;
3684 ╰╴ ─ 1. source
3685 ");
3686 }
3687
3688 #[test]
3689 fn goto_function_call_style_in_where() {
3690 assert_snapshot!(goto("
3691create table t(a int, b int);
3692select * from t where a$0(t) > 0;
3693"), @r"
3694 ╭▸
3695 2 │ create table t(a int, b int);
3696 │ ─ 2. destination
3697 3 │ select * from t where a(t) > 0;
3698 ╰╴ ─ 1. source
3699 ");
3700 }
3701
3702 #[test]
3703 fn goto_function_call_style_in_where_function_precedence() {
3704 assert_snapshot!(goto("
3705create table t(a int, b int);
3706create function b(t) returns int as 'select 1' language sql;
3707select * from t where b$0(t) > 0;
3708"), @r"
3709 ╭▸
3710 3 │ create function b(t) returns int as 'select 1' language sql;
3711 │ ─ 2. destination
3712 4 │ select * from t where b(t) > 0;
3713 ╰╴ ─ 1. source
3714 ");
3715 }
3716
3717 #[test]
3718 fn goto_field_style_function_call_in_where() {
3719 assert_snapshot!(goto("
3720create table t(a int);
3721create function b(t) returns int as 'select 1' language sql;
3722select * from t where t.b$0 > 0;
3723"), @r"
3724 ╭▸
3725 3 │ create function b(t) returns int as 'select 1' language sql;
3726 │ ─ 2. destination
3727 4 │ select * from t where t.b > 0;
3728 ╰╴ ─ 1. source
3729 ");
3730 }
3731
3732 #[test]
3733 fn goto_field_style_in_where_column_precedence() {
3734 assert_snapshot!(goto("
3735create table t(a int, b int);
3736create function b(t) returns int as 'select 1' language sql;
3737select * from t where t.b$0 > 0;
3738"), @r"
3739 ╭▸
3740 2 │ create table t(a int, b int);
3741 │ ─ 2. destination
3742 3 │ create function b(t) returns int as 'select 1' language sql;
3743 4 │ select * from t where t.b > 0;
3744 ╰╴ ─ 1. source
3745 ");
3746 }
3747
3748 #[test]
3749 fn goto_function_call_style_table_arg_in_where() {
3750 assert_snapshot!(goto("
3751create table t(a int);
3752select * from t where a(t$0) > 2;
3753"), @r"
3754 ╭▸
3755 2 │ create table t(a int);
3756 │ ─ 2. destination
3757 3 │ select * from t where a(t) > 2;
3758 ╰╴ ─ 1. source
3759 ");
3760 }
3761
3762 #[test]
3763 fn goto_qualified_table_ref_in_where() {
3764 assert_snapshot!(goto("
3765create table t(a int);
3766create function b(t) returns int as 'select 1' language sql;
3767select * from t where t$0.b > 2;
3768"), @r"
3769 ╭▸
3770 2 │ create table t(a int);
3771 │ ─ 2. destination
3772 3 │ create function b(t) returns int as 'select 1' language sql;
3773 4 │ select * from t where t.b > 2;
3774 ╰╴ ─ 1. source
3775 ");
3776 }
3777
3778 #[test]
3779 fn goto_function_call_style_in_order_by() {
3780 assert_snapshot!(goto("
3781create table t(a int, b int);
3782create function b(t) returns int as 'select 1' language sql;
3783select * from t order by b$0(t);
3784"), @r"
3785 ╭▸
3786 3 │ create function b(t) returns int as 'select 1' language sql;
3787 │ ─ 2. destination
3788 4 │ select * from t order by b(t);
3789 ╰╴ ─ 1. source
3790 ");
3791 }
3792
3793 #[test]
3794 fn goto_field_style_in_order_by() {
3795 assert_snapshot!(goto("
3796create table t(a int);
3797create function b(t) returns int as 'select 1' language sql;
3798select * from t order by t.b$0;
3799"), @r"
3800 ╭▸
3801 3 │ create function b(t) returns int as 'select 1' language sql;
3802 │ ─ 2. destination
3803 4 │ select * from t order by t.b;
3804 ╰╴ ─ 1. source
3805 ");
3806 }
3807
3808 #[test]
3809 fn goto_function_call_style_in_group_by() {
3810 assert_snapshot!(goto("
3811create table t(a int, b int);
3812select * from t group by a$0(t);
3813"), @r"
3814 ╭▸
3815 2 │ create table t(a int, b int);
3816 │ ─ 2. destination
3817 3 │ select * from t group by a(t);
3818 ╰╴ ─ 1. source
3819 ");
3820 }
3821
3822 #[test]
3823 fn goto_field_style_in_group_by() {
3824 assert_snapshot!(goto("
3825create table t(a int);
3826create function b(t) returns int as 'select 1' language sql;
3827select * from t group by t.b$0;
3828"), @r"
3829 ╭▸
3830 3 │ create function b(t) returns int as 'select 1' language sql;
3831 │ ─ 2. destination
3832 4 │ select * from t group by t.b;
3833 ╰╴ ─ 1. source
3834 ");
3835 }
3836
3837 #[test]
3838 fn goto_cte_table() {
3839 assert_snapshot!(goto("
3840with x as (select 1 as a)
3841select a from x$0;
3842"), @r"
3843 ╭▸
3844 2 │ with x as (select 1 as a)
3845 │ ─ 2. destination
3846 3 │ select a from x;
3847 ╰╴ ─ 1. source
3848 ");
3849 }
3850
3851 #[test]
3852 fn goto_cte_column() {
3853 assert_snapshot!(goto("
3854with x as (select 1 as a)
3855select a$0 from x;
3856"), @r"
3857 ╭▸
3858 2 │ with x as (select 1 as a)
3859 │ ─ 2. destination
3860 3 │ select a from x;
3861 ╰╴ ─ 1. source
3862 ");
3863 }
3864
3865 #[test]
3866 fn goto_cte_qualified_column_prefers_cte_over_table() {
3867 assert_snapshot!(goto("
3868create table u(id int, b int);
3869with u as (select 1 id, 2 b)
3870select u.id$0 from u;
3871"), @r"
3872 ╭▸
3873 3 │ with u as (select 1 id, 2 b)
3874 │ ── 2. destination
3875 4 │ select u.id from u;
3876 ╰╴ ─ 1. source
3877 ");
3878 }
3879
3880 #[test]
3881 fn goto_subquery_qualified_column() {
3882 assert_snapshot!(goto("
3883select t.a$0 from (select 1 a) t;
3884"), @r"
3885 ╭▸
3886 2 │ select t.a from (select 1 a) t;
3887 ╰╴ ─ 1. source ─ 2. destination
3888 ");
3889 }
3890
3891 #[test]
3892 fn goto_cte_multiple_columns() {
3893 assert_snapshot!(goto("
3894with x as (select 1 as a, 2 as b)
3895select b$0 from x;
3896"), @r"
3897 ╭▸
3898 2 │ with x as (select 1 as a, 2 as b)
3899 │ ─ 2. destination
3900 3 │ select b from x;
3901 ╰╴ ─ 1. source
3902 ");
3903 }
3904
3905 #[test]
3906 fn goto_cte_nested() {
3907 assert_snapshot!(goto("
3908with x as (select 1 as a),
3909 y as (select a from x)
3910select a$0 from y;
3911"), @r"
3912 ╭▸
3913 3 │ y as (select a from x)
3914 │ ─ 2. destination
3915 4 │ select a from y;
3916 ╰╴ ─ 1. source
3917 ");
3918 }
3919
3920 #[test]
3921 fn goto_cte_unnamed_column() {
3922 assert_snapshot!(goto(r#"
3923with x as (select 1)
3924select "?column?"$0 from x;
3925"#), @r#"
3926 ╭▸
3927 2 │ with x as (select 1)
3928 │ ─ 2. destination
3929 3 │ select "?column?" from x;
3930 ╰╴ ─ 1. source
3931 "#);
3932 }
3933
3934 #[test]
3935 fn goto_cte_star_expansion() {
3936 assert_snapshot!(goto("
3937with t as (select 1 a),
3938 y as (select * from t)
3939select a$0 from y;
3940"), @r"
3941 ╭▸
3942 2 │ with t as (select 1 a),
3943 │ ─ 2. destination
3944 3 │ y as (select * from t)
3945 4 │ select a from y;
3946 ╰╴ ─ 1. source
3947 ");
3948 }
3949
3950 #[test]
3951 fn goto_cte_qualified_star_join_column() {
3952 assert_snapshot!(goto("
3953create table u(id int, b int);
3954create table t(id int, a int);
3955
3956with k as (
3957 select u.* from t join u on a = b
3958)
3959select b$0 from k;
3960"), @r"
3961 ╭▸
3962 2 │ create table u(id int, b int);
3963 │ ─ 2. destination
3964 ‡
3965 8 │ select b from k;
3966 ╰╴ ─ 1. source
3967 ");
3968 }
3969
3970 #[test]
3971 fn goto_cte_qualified_star_join_column_with_partial_column_list() {
3972 assert_snapshot!(goto("
3973with
3974 u as (
3975 select 1 id, 2 b
3976 ),
3977 t as (
3978 select 1 id, 2 a
3979 ),
3980 k(x) as (
3981 select u.* from t join u on a = b
3982 )
3983select b$0 from k;
3984"), @r"
3985 ╭▸
3986 4 │ select 1 id, 2 b
3987 │ ─ 2. destination
3988 ‡
3989 12 │ select b from k;
3990 ╰╴ ─ 1. source
3991 ");
3992 }
3993
3994 #[test]
3995 fn goto_cte_reference_inside_cte() {
3996 assert_snapshot!(goto("
3997with t as (select 1 a),
3998 y as (select a$0 from t)
3999select a from y;
4000"), @r"
4001 ╭▸
4002 2 │ with t as (select 1 a),
4003 │ ─ 2. destination
4004 3 │ y as (select a from t)
4005 ╰╴ ─ 1. source
4006 ");
4007 }
4008
4009 #[test]
4010 fn goto_cte_with_column_list() {
4011 assert_snapshot!(goto("
4012with t(a) as (select 1)
4013select a$0 from t;
4014"), @r"
4015 ╭▸
4016 2 │ with t(a) as (select 1)
4017 │ ─ 2. destination
4018 3 │ select a from t;
4019 ╰╴ ─ 1. source
4020 ");
4021 }
4022
4023 #[test]
4024 fn goto_cte_with_partial_column_list() {
4025 assert_snapshot!(goto("
4026with t(x) as (select 1 as a, 2 as b)
4027select b$0 from t;
4028"), @r"
4029 ╭▸
4030 2 │ with t(x) as (select 1 as a, 2 as b)
4031 │ ─ 2. destination
4032 3 │ select b from t;
4033 ╰╴ ─ 1. source
4034 ");
4035 }
4036
4037 #[test]
4038 fn goto_cte_with_partial_column_list_renamed() {
4039 assert_snapshot!(goto("
4040with t(x) as (select 1 as a, 2 as b)
4041select x$0 from t;
4042"), @r"
4043 ╭▸
4044 2 │ with t(x) as (select 1 as a, 2 as b)
4045 │ ─ 2. destination
4046 3 │ select x from t;
4047 ╰╴ ─ 1. source
4048 ");
4049 }
4050
4051 #[test]
4052 fn goto_cte_insert_returning_star_column() {
4053 assert_snapshot!(goto("
4054create table t(a int, b int);
4055with inserted as (
4056 insert into t values (1, 2), (3, 4)
4057 returning *
4058)
4059select a$0 from inserted;
4060"), @r"
4061 ╭▸
4062 2 │ create table t(a int, b int);
4063 │ ─ 2. destination
4064 ‡
4065 7 │ select a from inserted;
4066 ╰╴ ─ 1. source
4067 ");
4068 }
4069
4070 #[test]
4071 fn goto_cte_delete_returning_star_column() {
4072 assert_snapshot!(goto("
4073create table t(a int, b int);
4074with deleted as (
4075 delete from t
4076 returning *
4077)
4078select a$0 from deleted;
4079"), @r"
4080 ╭▸
4081 2 │ create table t(a int, b int);
4082 │ ─ 2. destination
4083 ‡
4084 7 │ select a from deleted;
4085 ╰╴ ─ 1. source
4086 ");
4087 }
4088
4089 #[test]
4090 fn goto_cte_update_returning_star_column() {
4091 assert_snapshot!(goto("
4092create table t(a int, b int);
4093with updated as (
4094 update t set a = 42
4095 returning *
4096)
4097select a$0 from updated;
4098"), @r"
4099 ╭▸
4100 2 │ create table t(a int, b int);
4101 │ ─ 2. destination
4102 ‡
4103 7 │ select a from updated;
4104 ╰╴ ─ 1. source
4105 ");
4106 }
4107
4108 #[test]
4109 fn goto_cte_update_returning_column_list_overwrites_column() {
4110 goto_not_found(
4111 "
4112create table t(a int, b int);
4113with updated(c) as (
4114 update t set a = 10
4115 returning a
4116)
4117select a$0 from updated;
4118",
4119 );
4120 }
4121
4122 #[test]
4123 fn goto_cte_column_list_overwrites_column() {
4124 goto_not_found(
4125 "
4126with t(x) as (select 1 as a)
4127select a$0 from t;
4128",
4129 );
4130 }
4131
4132 #[test]
4133 fn goto_cte_shadows_table() {
4134 assert_snapshot!(goto("
4135create table t(a int);
4136with t as (select a$0 from t)
4137select a from t;
4138"), @r"
4139 ╭▸
4140 2 │ create table t(a int);
4141 │ ─ 2. destination
4142 3 │ with t as (select a from t)
4143 ╰╴ ─ 1. source
4144 ");
4145 }
4146
4147 #[test]
4148 fn goto_subquery_column() {
4149 assert_snapshot!(goto("
4150select a$0 from (select 1 a);
4151"), @r"
4152 ╭▸
4153 2 │ select a from (select 1 a);
4154 ╰╴ ─ 1. source ─ 2. destination
4155 ");
4156 }
4157
4158 #[test]
4159 fn goto_subquery_column_with_as() {
4160 assert_snapshot!(goto("
4161select a$0 from (select 1 as a);
4162"), @r"
4163 ╭▸
4164 2 │ select a from (select 1 as a);
4165 ╰╴ ─ 1. source ─ 2. destination
4166 ");
4167 }
4168
4169 #[test]
4170 fn goto_subquery_column_multiple_columns() {
4171 assert_snapshot!(goto("
4172select b$0 from (select 1 a, 2 b);
4173"), @r"
4174 ╭▸
4175 2 │ select b from (select 1 a, 2 b);
4176 ╰╴ ─ 1. source ─ 2. destination
4177 ");
4178 }
4179
4180 #[test]
4181 fn goto_subquery_column_nested_parens() {
4182 assert_snapshot!(goto("
4183select a$0 from ((select 1 a));
4184"), @r"
4185 ╭▸
4186 2 │ select a from ((select 1 a));
4187 ╰╴ ─ 1. source ─ 2. destination
4188 ");
4189 }
4190
4191 #[test]
4192 fn goto_subquery_column_star_table() {
4193 assert_snapshot!(goto("
4194create table foo.t(a int);
4195select a$0 from (select * from foo.t);
4196"), @r"
4197 ╭▸
4198 2 │ create table foo.t(a int);
4199 │ ─ 2. destination
4200 3 │ select a from (select * from foo.t);
4201 ╰╴ ─ 1. source
4202 ");
4203 }
4204
4205 #[test]
4206 fn goto_subquery_column_qualified_star_join() {
4207 assert_snapshot!(goto("
4208create table t(a int);
4209create table u(b int);
4210select b$0 from (select u.* from t join u on a = b);
4211"), @r"
4212 ╭▸
4213 3 │ create table u(b int);
4214 │ ─ 2. destination
4215 4 │ select b from (select u.* from t join u on a = b);
4216 ╰╴ ─ 1. source
4217 ");
4218 }
4219
4220 #[test]
4221 fn goto_subquery_column_qualified_star_join_not_found() {
4222 goto_not_found(
4223 "
4224create table t(a int);
4225create table u(b int);
4226select a$0 from (select u.* from t join u on a = b);
4227",
4228 );
4229 }
4230
4231 #[test]
4232 fn goto_subquery_column_alias_list() {
4233 assert_snapshot!(goto("
4234select c$0, t.c from (select 1) t(c);
4235"), @r"
4236 ╭▸
4237 2 │ select c, t.c from (select 1) t(c);
4238 ╰╴ ─ 1. source ─ 2. destination
4239 ");
4240 }
4241
4242 #[test]
4243 fn goto_subquery_column_alias_list_qualified() {
4244 assert_snapshot!(goto("
4245select t.c$0 from (select 1) t(c);
4246"), @r"
4247 ╭▸
4248 2 │ select t.c from (select 1) t(c);
4249 ╰╴ ─ 1. source ─ 2. destination
4250 ");
4251 }
4252
4253 #[test]
4254 fn goto_subquery_column_alias_list_multiple() {
4255 assert_snapshot!(goto("
4256select b$0 from (select 1, 2) t(a, b);
4257"), @r"
4258 ╭▸
4259 2 │ select b from (select 1, 2) t(a, b);
4260 ╰╴ ─ 1. source ─ 2. destination
4261 ");
4262 }
4263
4264 #[test]
4265 fn goto_cte_column_alias_list() {
4266 assert_snapshot!(goto("
4267with x as (select 1)
4268select c$0 from x t(c);
4269"), @r"
4270 ╭▸
4271 3 │ select c from x t(c);
4272 │ ┬ ─ 2. destination
4273 │ │
4274 ╰╴ 1. source
4275 ");
4276 }
4277
4278 #[test]
4279 fn goto_cte_column_alias_list_qualified() {
4280 assert_snapshot!(goto("
4281with x as (select 1)
4282select t.c$0 from x t(c);
4283"), @r"
4284 ╭▸
4285 3 │ select t.c from x t(c);
4286 │ ┬ ─ 2. destination
4287 │ │
4288 ╰╴ 1. source
4289 ");
4290 }
4291
4292 #[test]
4293 fn goto_cte_column_alias_list_multiple() {
4294 assert_snapshot!(goto("
4295with x as (select 1, 2)
4296select b$0 from x t(a, b);
4297"), @r"
4298 ╭▸
4299 3 │ select b from x t(a, b);
4300 ╰╴ ─ 1. source ─ 2. destination
4301 ");
4302 }
4303
4304 #[test]
4305 fn goto_values_column_alias_list() {
4306 assert_snapshot!(goto("
4307select c$0 from (values (1)) t(c);
4308"), @r"
4309 ╭▸
4310 2 │ select c from (values (1)) t(c);
4311 ╰╴ ─ 1. source ─ 2. destination
4312 ");
4313 }
4314
4315 #[test]
4316 fn goto_values_column_alias_list_qualified() {
4317 assert_snapshot!(goto("
4318select t.c$0 from (values (1)) t(c);
4319"), @r"
4320 ╭▸
4321 2 │ select t.c from (values (1)) t(c);
4322 ╰╴ ─ 1. source ─ 2. destination
4323 ");
4324 }
4325
4326 #[test]
4327 fn goto_values_column_alias_list_multiple() {
4328 assert_snapshot!(goto("
4329select b$0 from (values (1, 2)) t(a, b);
4330"), @r"
4331 ╭▸
4332 2 │ select b from (values (1, 2)) t(a, b);
4333 ╰╴ ─ 1. source ─ 2. destination
4334 ");
4335 }
4336
4337 #[test]
4338 fn goto_table_expr_column() {
4339 assert_snapshot!(goto("
4340create table t(a int, b int);
4341select a$0 from (table t);
4342"), @r"
4343 ╭▸
4344 2 │ create table t(a int, b int);
4345 │ ─ 2. destination
4346 3 │ select a from (table t);
4347 ╰╴ ─ 1. source
4348 ");
4349 }
4350
4351 #[test]
4352 fn goto_table_expr_column_with_cte() {
4353 assert_snapshot!(goto("
4354with x as (select 1 a)
4355select a$0 from (table x);
4356"), @r"
4357 ╭▸
4358 2 │ with x as (select 1 a)
4359 │ ─ 2. destination
4360 3 │ select a from (table x);
4361 ╰╴ ─ 1. source
4362 ");
4363 }
4364
4365 #[test]
4366 fn goto_table_expr_partial_column_alias_list() {
4367 assert_snapshot!(goto("
4368with t as (select 1 a, 2 b)
4369select c, b$0 from (table t) u(c);
4370"), @r"
4371 ╭▸
4372 2 │ with t as (select 1 a, 2 b)
4373 │ ─ 2. destination
4374 3 │ select c, b from (table t) u(c);
4375 ╰╴ ─ 1. source
4376 ");
4377 }
4378
4379 #[test]
4380 fn goto_subquery_partial_column_alias_list() {
4381 assert_snapshot!(goto("
4382select x, b$0 from (select 1 a, 2 b) t(x);
4383"), @r"
4384 ╭▸
4385 2 │ select x, b from (select 1 a, 2 b) t(x);
4386 ╰╴ ─ 1. source ─ 2. destination
4387 ");
4388 }
4389
4390 #[test]
4391 fn goto_table_expr_values_cte_partial_alias() {
4392 assert_snapshot!(goto("
4393with t as (values (1, 2), (3, 4))
4394select column2$0 from (table t) u(a);
4395"), @r"
4396 ╭▸
4397 2 │ with t as (values (1, 2), (3, 4))
4398 │ ─ 2. destination
4399 3 │ select column2 from (table t) u(a);
4400 ╰╴ ─ 1. source
4401 ");
4402 }
4403
4404 #[test]
4405 fn goto_cte_with_table_expr() {
4406 assert_snapshot!(goto("
4407create table t(a int, b int);
4408with u as (table t)
4409select a$0 from u;
4410"), @r"
4411 ╭▸
4412 2 │ create table t(a int, b int);
4413 │ ─ 2. destination
4414 3 │ with u as (table t)
4415 4 │ select a from u;
4416 ╰╴ ─ 1. source
4417 ");
4418 }
4419
4420 #[test]
4421 fn goto_cte_with_table_expr_nested() {
4422 assert_snapshot!(goto("
4423with t as (select 1 a, 2 b),
4424 u as (table t)
4425select b$0 from u;
4426"), @r"
4427 ╭▸
4428 2 │ with t as (select 1 a, 2 b),
4429 │ ─ 2. destination
4430 3 │ u as (table t)
4431 4 │ select b from u;
4432 ╰╴ ─ 1. source
4433 ");
4434 }
4435
4436 #[test]
4437 fn goto_insert_table() {
4438 assert_snapshot!(goto("
4439create table users(id int, email text);
4440insert into users$0(id, email) values (1, 'test@example.com');
4441"), @r"
4442 ╭▸
4443 2 │ create table users(id int, email text);
4444 │ ───── 2. destination
4445 3 │ insert into users(id, email) values (1, 'test@example.com');
4446 ╰╴ ─ 1. source
4447 ");
4448 }
4449
4450 #[test]
4451 fn goto_insert_table_with_schema() {
4452 assert_snapshot!(goto("
4453create table public.users(id int, email text);
4454insert into public.users$0(id, email) values (1, 'test@example.com');
4455"), @r"
4456 ╭▸
4457 2 │ create table public.users(id int, email text);
4458 │ ───── 2. destination
4459 3 │ insert into public.users(id, email) values (1, 'test@example.com');
4460 ╰╴ ─ 1. source
4461 ");
4462 }
4463
4464 #[test]
4465 fn goto_insert_column() {
4466 assert_snapshot!(goto("
4467create table users(id int, email text);
4468insert into users(id$0, email) values (1, 'test@example.com');
4469"), @r"
4470 ╭▸
4471 2 │ create table users(id int, email text);
4472 │ ── 2. destination
4473 3 │ insert into users(id, email) values (1, 'test@example.com');
4474 ╰╴ ─ 1. source
4475 ");
4476 }
4477
4478 #[test]
4479 fn goto_insert_column_second() {
4480 assert_snapshot!(goto("
4481create table users(id int, email text);
4482insert into users(id, email$0) values (1, 'test@example.com');
4483"), @r"
4484 ╭▸
4485 2 │ create table users(id int, email text);
4486 │ ───── 2. destination
4487 3 │ insert into users(id, email) values (1, 'test@example.com');
4488 ╰╴ ─ 1. source
4489 ");
4490 }
4491
4492 #[test]
4493 fn goto_insert_column_with_schema() {
4494 assert_snapshot!(goto("
4495create table public.users(id int, email text);
4496insert into public.users(email$0) values ('test@example.com');
4497"), @r"
4498 ╭▸
4499 2 │ create table public.users(id int, email text);
4500 │ ───── 2. destination
4501 3 │ insert into public.users(email) values ('test@example.com');
4502 ╰╴ ─ 1. source
4503 ");
4504 }
4505
4506 #[test]
4507 fn goto_insert_table_with_search_path() {
4508 assert_snapshot!(goto("
4509set search_path to foo;
4510create table foo.users(id int, email text);
4511insert into users$0(id, email) values (1, 'test@example.com');
4512"), @r"
4513 ╭▸
4514 3 │ create table foo.users(id int, email text);
4515 │ ───── 2. destination
4516 4 │ insert into users(id, email) values (1, 'test@example.com');
4517 ╰╴ ─ 1. source
4518 ");
4519 }
4520
4521 #[test]
4522 fn goto_insert_column_with_search_path() {
4523 assert_snapshot!(goto("
4524set search_path to myschema;
4525create table myschema.users(id int, email text, name text);
4526insert into users(email$0, name) values ('test@example.com', 'Test');
4527"), @r"
4528 ╭▸
4529 3 │ create table myschema.users(id int, email text, name text);
4530 │ ───── 2. destination
4531 4 │ insert into users(email, name) values ('test@example.com', 'Test');
4532 ╰╴ ─ 1. source
4533 ");
4534 }
4535
4536 #[test]
4537 fn goto_delete_table() {
4538 assert_snapshot!(goto("
4539create table users(id int, email text);
4540delete from users$0 where id = 1;
4541"), @r"
4542 ╭▸
4543 2 │ create table users(id int, email text);
4544 │ ───── 2. destination
4545 3 │ delete from users where id = 1;
4546 ╰╴ ─ 1. source
4547 ");
4548 }
4549
4550 #[test]
4551 fn goto_delete_table_with_schema() {
4552 assert_snapshot!(goto("
4553create table public.users(id int, email text);
4554delete from public.users$0 where id = 1;
4555"), @r"
4556 ╭▸
4557 2 │ create table public.users(id int, email text);
4558 │ ───── 2. destination
4559 3 │ delete from public.users where id = 1;
4560 ╰╴ ─ 1. source
4561 ");
4562 }
4563
4564 #[test]
4565 fn goto_delete_table_with_search_path() {
4566 assert_snapshot!(goto("
4567set search_path to foo;
4568create table foo.users(id int, email text);
4569delete from users$0 where id = 1;
4570"), @r"
4571 ╭▸
4572 3 │ create table foo.users(id int, email text);
4573 │ ───── 2. destination
4574 4 │ delete from users where id = 1;
4575 ╰╴ ─ 1. source
4576 ");
4577 }
4578
4579 #[test]
4580 fn goto_delete_temp_table() {
4581 assert_snapshot!(goto("
4582create temp table users(id int, email text);
4583delete from users$0 where id = 1;
4584"), @r"
4585 ╭▸
4586 2 │ create temp table users(id int, email text);
4587 │ ───── 2. destination
4588 3 │ delete from users where id = 1;
4589 ╰╴ ─ 1. source
4590 ");
4591 }
4592
4593 #[test]
4594 fn goto_delete_where_column() {
4595 assert_snapshot!(goto("
4596create table users(id int, email text);
4597delete from users where id$0 = 1;
4598"), @r"
4599 ╭▸
4600 2 │ create table users(id int, email text);
4601 │ ── 2. destination
4602 3 │ delete from users where id = 1;
4603 ╰╴ ─ 1. source
4604 ");
4605 }
4606
4607 #[test]
4608 fn goto_delete_where_column_second() {
4609 assert_snapshot!(goto("
4610create table users(id int, email text);
4611delete from users where email$0 = 'test@example.com';
4612"), @r"
4613 ╭▸
4614 2 │ create table users(id int, email text);
4615 │ ───── 2. destination
4616 3 │ delete from users where email = 'test@example.com';
4617 ╰╴ ─ 1. source
4618 ");
4619 }
4620
4621 #[test]
4622 fn goto_delete_where_column_with_schema() {
4623 assert_snapshot!(goto("
4624create table public.users(id int, email text, name text);
4625delete from public.users where name$0 = 'Test';
4626"), @r"
4627 ╭▸
4628 2 │ create table public.users(id int, email text, name text);
4629 │ ──── 2. destination
4630 3 │ delete from public.users where name = 'Test';
4631 ╰╴ ─ 1. source
4632 ");
4633 }
4634
4635 #[test]
4636 fn goto_delete_where_column_with_search_path() {
4637 assert_snapshot!(goto("
4638set search_path to myschema;
4639create table myschema.users(id int, email text, active boolean);
4640delete from users where active$0 = true;
4641"), @r"
4642 ╭▸
4643 3 │ create table myschema.users(id int, email text, active boolean);
4644 │ ────── 2. destination
4645 4 │ delete from users where active = true;
4646 ╰╴ ─ 1. source
4647 ");
4648 }
4649
4650 #[test]
4651 fn goto_delete_where_multiple_columns() {
4652 assert_snapshot!(goto("
4653create table users(id int, email text, active boolean);
4654delete from users where id$0 = 1 and active = true;
4655"), @r"
4656 ╭▸
4657 2 │ create table users(id int, email text, active boolean);
4658 │ ── 2. destination
4659 3 │ delete from users where id = 1 and active = true;
4660 ╰╴ ─ 1. source
4661 ");
4662 }
4663
4664 #[test]
4665 fn goto_delete_using_table() {
4666 assert_snapshot!(goto("
4667create table t(id int, f_id int);
4668create table f(id int, name text);
4669delete from t using f$0 where f_id = f.id and f.name = 'foo';
4670"), @r"
4671 ╭▸
4672 3 │ create table f(id int, name text);
4673 │ ─ 2. destination
4674 4 │ delete from t using f where f_id = f.id and f.name = 'foo';
4675 ╰╴ ─ 1. source
4676 ");
4677 }
4678
4679 #[test]
4680 fn goto_delete_using_table_with_schema() {
4681 assert_snapshot!(goto("
4682create table t(id int, f_id int);
4683create table public.f(id int, name text);
4684delete from t using public.f$0 where f_id = f.id;
4685"), @r"
4686 ╭▸
4687 3 │ create table public.f(id int, name text);
4688 │ ─ 2. destination
4689 4 │ delete from t using public.f where f_id = f.id;
4690 ╰╴ ─ 1. source
4691 ");
4692 }
4693
4694 #[test]
4695 fn goto_delete_using_column_in_where() {
4696 assert_snapshot!(goto("
4697create table t(id int, f_id int);
4698create table f(id int, name text);
4699delete from t using f where f_id = f.id$0 and f.name = 'foo';
4700"), @r"
4701 ╭▸
4702 2 │ create table t(id int, f_id int);
4703 │ ── 2. destination
4704 3 │ create table f(id int, name text);
4705 4 │ delete from t using f where f_id = f.id and f.name = 'foo';
4706 ╰╴ ─ 1. source
4707 ");
4708 }
4709
4710 #[test]
4711 fn goto_select_from_table() {
4712 assert_snapshot!(goto("
4713create table users(id int, email text);
4714select * from users$0;
4715"), @r"
4716 ╭▸
4717 2 │ create table users(id int, email text);
4718 │ ───── 2. destination
4719 3 │ select * from users;
4720 ╰╴ ─ 1. source
4721 ");
4722 }
4723
4724 #[test]
4725 fn goto_select_from_table_with_schema() {
4726 assert_snapshot!(goto("
4727create table public.users(id int, email text);
4728select * from public.users$0;
4729"), @r"
4730 ╭▸
4731 2 │ create table public.users(id int, email text);
4732 │ ───── 2. destination
4733 3 │ select * from public.users;
4734 ╰╴ ─ 1. source
4735 ");
4736 }
4737
4738 #[test]
4739 fn goto_select_from_table_with_search_path() {
4740 assert_snapshot!(goto("
4741set search_path to foo;
4742create table foo.users(id int, email text);
4743select * from users$0;
4744"), @r"
4745 ╭▸
4746 3 │ create table foo.users(id int, email text);
4747 │ ───── 2. destination
4748 4 │ select * from users;
4749 ╰╴ ─ 1. source
4750 ");
4751 }
4752
4753 #[test]
4754 fn goto_select_from_temp_table() {
4755 assert_snapshot!(goto("
4756create temp table users(id int, email text);
4757select * from users$0;
4758"), @r"
4759 ╭▸
4760 2 │ create temp table users(id int, email text);
4761 │ ───── 2. destination
4762 3 │ select * from users;
4763 ╰╴ ─ 1. source
4764 ");
4765 }
4766
4767 #[test]
4768 fn goto_select_from_table_defined_after() {
4769 assert_snapshot!(goto("
4770select * from users$0;
4771create table users(id int, email text);
4772"), @r"
4773 ╭▸
4774 2 │ select * from users;
4775 │ ─ 1. source
4776 3 │ create table users(id int, email text);
4777 ╰╴ ───── 2. destination
4778 ");
4779 }
4780
4781 #[test]
4782 fn goto_select_column() {
4783 assert_snapshot!(goto("
4784create table users(id int, email text);
4785select id$0 from users;
4786"), @r"
4787 ╭▸
4788 2 │ create table users(id int, email text);
4789 │ ── 2. destination
4790 3 │ select id from users;
4791 ╰╴ ─ 1. source
4792 ");
4793 }
4794
4795 #[test]
4796 fn goto_select_column_second() {
4797 assert_snapshot!(goto("
4798create table users(id int, email text);
4799select id, email$0 from users;
4800"), @r"
4801 ╭▸
4802 2 │ create table users(id int, email text);
4803 │ ───── 2. destination
4804 3 │ select id, email from users;
4805 ╰╴ ─ 1. source
4806 ");
4807 }
4808
4809 #[test]
4810 fn goto_select_column_with_schema() {
4811 assert_snapshot!(goto("
4812create table public.users(id int, email text);
4813select email$0 from public.users;
4814"), @r"
4815 ╭▸
4816 2 │ create table public.users(id int, email text);
4817 │ ───── 2. destination
4818 3 │ select email from public.users;
4819 ╰╴ ─ 1. source
4820 ");
4821 }
4822
4823 #[test]
4824 fn goto_select_column_with_search_path() {
4825 assert_snapshot!(goto("
4826set search_path to foo;
4827create table foo.users(id int, email text);
4828select id$0 from users;
4829"), @r"
4830 ╭▸
4831 3 │ create table foo.users(id int, email text);
4832 │ ── 2. destination
4833 4 │ select id from users;
4834 ╰╴ ─ 1. source
4835 ");
4836 }
4837
4838 #[test]
4839 fn goto_select_table_as_column() {
4840 assert_snapshot!(goto("
4841create table t(x bigint, y bigint);
4842select t$0 from t;
4843"), @r"
4844 ╭▸
4845 2 │ create table t(x bigint, y bigint);
4846 │ ─ 2. destination
4847 3 │ select t from t;
4848 ╰╴ ─ 1. source
4849 ");
4850 }
4851
4852 #[test]
4853 fn goto_select_table_star_expansion() {
4854 assert_snapshot!(goto("
4855create table t(id int, a int);
4856select t$0.* from t;
4857"), @r"
4858 ╭▸
4859 2 │ create table t(id int, a int);
4860 │ ─ 2. destination
4861 3 │ select t.* from t;
4862 ╰╴ ─ 1. source
4863 ");
4864 }
4865
4866 #[test]
4867 fn goto_select_table_as_column_with_schema() {
4868 assert_snapshot!(goto("
4869create table public.t(x bigint, y bigint);
4870select t$0 from public.t;
4871"), @r"
4872 ╭▸
4873 2 │ create table public.t(x bigint, y bigint);
4874 │ ─ 2. destination
4875 3 │ select t from public.t;
4876 ╰╴ ─ 1. source
4877 ");
4878 }
4879
4880 #[test]
4881 fn goto_select_table_as_column_with_search_path() {
4882 assert_snapshot!(goto("
4883set search_path to foo;
4884create table foo.users(id int, email text);
4885select users$0 from users;
4886"), @r"
4887 ╭▸
4888 3 │ create table foo.users(id int, email text);
4889 │ ───── 2. destination
4890 4 │ select users from users;
4891 ╰╴ ─ 1. source
4892 ");
4893 }
4894
4895 #[test]
4896 fn goto_select_column_with_same_name_as_table() {
4897 assert_snapshot!(goto("
4898create table t(t int);
4899select t$0 from t;
4900"), @r"
4901 ╭▸
4902 2 │ create table t(t int);
4903 │ ─ 2. destination
4904 3 │ select t from t;
4905 ╰╴ ─ 1. source
4906 ");
4907 }
4908
4909 #[test]
4910 fn goto_drop_schema() {
4911 assert_snapshot!(goto("
4912create schema foo;
4913drop schema foo$0;
4914"), @r"
4915 ╭▸
4916 2 │ create schema foo;
4917 │ ─── 2. destination
4918 3 │ drop schema foo;
4919 ╰╴ ─ 1. source
4920 ");
4921 }
4922
4923 #[test]
4924 fn goto_create_schema_authorization() {
4925 assert_snapshot!(goto("
4926create schema authorization foo$0;
4927"), @r"
4928 ╭▸
4929 2 │ create schema authorization foo;
4930 │ ┬─┬
4931 │ │ │
4932 │ │ 1. source
4933 ╰╴ 2. destination
4934 ");
4935 }
4936
4937 #[test]
4938 fn goto_drop_schema_authorization() {
4939 assert_snapshot!(goto("
4940create schema authorization foo;
4941drop schema foo$0;
4942"), @r"
4943 ╭▸
4944 2 │ create schema authorization foo;
4945 │ ─── 2. destination
4946 3 │ drop schema foo;
4947 ╰╴ ─ 1. source
4948 ");
4949 }
4950
4951 #[test]
4952 fn goto_drop_schema_defined_after() {
4953 assert_snapshot!(goto("
4954drop schema foo$0;
4955create schema foo;
4956"), @r"
4957 ╭▸
4958 2 │ drop schema foo;
4959 │ ─ 1. source
4960 3 │ create schema foo;
4961 ╰╴ ─── 2. destination
4962 ");
4963 }
4964
4965 #[test]
4966 fn goto_schema_qualifier_in_table() {
4967 assert_snapshot!(goto("
4968create schema foo;
4969create table foo$0.t(a int);
4970"), @r"
4971 ╭▸
4972 2 │ create schema foo;
4973 │ ─── 2. destination
4974 3 │ create table foo.t(a int);
4975 ╰╴ ─ 1. source
4976 ");
4977 }
4978
4979 #[test]
4980 fn goto_schema_qualifier_in_drop_table() {
4981 assert_snapshot!(goto("
4982create schema foo;
4983create table foo.t(a int);
4984drop table foo$0.t;
4985"), @r"
4986 ╭▸
4987 2 │ create schema foo;
4988 │ ─── 2. destination
4989 3 │ create table foo.t(a int);
4990 4 │ drop table foo.t;
4991 ╰╴ ─ 1. source
4992 ");
4993 }
4994
4995 #[test]
4996 fn goto_schema_qualifier_multiple_schemas() {
4997 assert_snapshot!(goto("
4998create schema foo;
4999create schema bar;
5000create table bar$0.t(a int);
5001"), @r"
5002 ╭▸
5003 3 │ create schema bar;
5004 │ ─── 2. destination
5005 4 │ create table bar.t(a int);
5006 ╰╴ ─ 1. source
5007 ");
5008 }
5009
5010 #[test]
5011 fn goto_schema_qualifier_in_function_call() {
5012 assert_snapshot!(goto(r#"
5013create schema foo;
5014create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
5015select foo$0.bar();
5016"#), @r"
5017 ╭▸
5018 2 │ create schema foo;
5019 │ ─── 2. destination
5020 3 │ create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
5021 4 │ select foo.bar();
5022 ╰╴ ─ 1. source
5023 ");
5024 }
5025
5026 #[test]
5027 fn goto_schema_qualifier_in_function_call_from_clause() {
5028 assert_snapshot!(goto(r#"
5029create schema myschema;
5030create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
5031select * from myschema$0.get_data();
5032"#), @r"
5033 ╭▸
5034 2 │ create schema myschema;
5035 │ ──────── 2. destination
5036 3 │ create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
5037 4 │ select * from myschema.get_data();
5038 ╰╴ ─ 1. source
5039 ");
5040 }
5041
5042 #[test]
5043 fn goto_schema_qualifier_in_select_from() {
5044 assert_snapshot!(goto("
5045create schema foo;
5046create table foo.t(x int);
5047select x from foo$0.t;
5048"), @r"
5049 ╭▸
5050 2 │ create schema foo;
5051 │ ─── 2. destination
5052 3 │ create table foo.t(x int);
5053 4 │ select x from foo.t;
5054 ╰╴ ─ 1. source
5055 ");
5056 }
5057
5058 #[test]
5059 fn goto_qualified_column_table() {
5060 assert_snapshot!(goto("
5061create table t(a int);
5062select t$0.a from t;
5063"), @r"
5064 ╭▸
5065 2 │ create table t(a int);
5066 │ ─ 2. destination
5067 3 │ select t.a from t;
5068 ╰╴ ─ 1. source
5069 ");
5070 }
5071
5072 #[test]
5073 fn goto_qualified_column_column() {
5074 assert_snapshot!(goto("
5075create table t(a int);
5076select t.a$0 from t;
5077"), @r"
5078 ╭▸
5079 2 │ create table t(a int);
5080 │ ─ 2. destination
5081 3 │ select t.a from t;
5082 ╰╴ ─ 1. source
5083 ");
5084 }
5085
5086 #[test]
5087 fn goto_three_part_qualified_column_schema() {
5088 assert_snapshot!(goto("
5089create schema foo;
5090create table foo.t(a int);
5091select foo$0.t.a from t;
5092"), @r"
5093 ╭▸
5094 2 │ create schema foo;
5095 │ ─── 2. destination
5096 3 │ create table foo.t(a int);
5097 4 │ select foo.t.a from t;
5098 ╰╴ ─ 1. source
5099 ");
5100 }
5101
5102 #[test]
5103 fn goto_three_part_qualified_column_table() {
5104 assert_snapshot!(goto("
5105create schema foo;
5106create table foo.t(a int);
5107select foo.t$0.a from t;
5108"), @r"
5109 ╭▸
5110 3 │ create table foo.t(a int);
5111 │ ─ 2. destination
5112 4 │ select foo.t.a from t;
5113 ╰╴ ─ 1. source
5114 ");
5115 }
5116
5117 #[test]
5118 fn goto_three_part_qualified_column_column() {
5119 assert_snapshot!(goto("
5120create schema foo;
5121create table foo.t(a int);
5122select foo.t.a$0 from t;
5123"), @r"
5124 ╭▸
5125 3 │ create table foo.t(a int);
5126 │ ─ 2. destination
5127 4 │ select foo.t.a from t;
5128 ╰╴ ─ 1. source
5129 ");
5130 }
5131
5132 #[test]
5133 fn goto_cte_values_column1() {
5134 assert_snapshot!(goto("
5135with t as (
5136 values (1, 2), (3, 4)
5137)
5138select column1$0, column2 from t;
5139"), @r"
5140 ╭▸
5141 3 │ values (1, 2), (3, 4)
5142 │ ─ 2. destination
5143 4 │ )
5144 5 │ select column1, column2 from t;
5145 ╰╴ ─ 1. source
5146 ");
5147 }
5148
5149 #[test]
5150 fn goto_cte_values_column2() {
5151 assert_snapshot!(goto("
5152with t as (
5153 values (1, 2), (3, 4)
5154)
5155select column1, column2$0 from t;
5156"), @r"
5157 ╭▸
5158 3 │ values (1, 2), (3, 4)
5159 │ ─ 2. destination
5160 4 │ )
5161 5 │ select column1, column2 from t;
5162 ╰╴ ─ 1. source
5163 ");
5164 }
5165
5166 #[test]
5167 fn goto_cte_values_single_column() {
5168 assert_snapshot!(goto("
5169with t as (
5170 values (1), (2), (3)
5171)
5172select column1$0 from t;
5173"), @r"
5174 ╭▸
5175 3 │ values (1), (2), (3)
5176 │ ─ 2. destination
5177 4 │ )
5178 5 │ select column1 from t;
5179 ╰╴ ─ 1. source
5180 ");
5181 }
5182
5183 #[test]
5184 fn goto_cte_values_multiple_rows() {
5185 assert_snapshot!(goto("
5186with t as (
5187 values
5188 (1, 2, 3),
5189 (4, 5, 6),
5190 (7, 8, 9)
5191)
5192select column3$0 from t;
5193"), @r"
5194 ╭▸
5195 4 │ (1, 2, 3),
5196 │ ─ 2. destination
5197 ‡
5198 8 │ select column3 from t;
5199 ╰╴ ─ 1. source
5200 ");
5201 }
5202
5203 #[test]
5204 fn goto_cte_values_uppercase_column_names() {
5205 assert_snapshot!(goto("
5206with t as (
5207 values (1, 2), (3, 4)
5208)
5209select COLUMN1$0, COLUMN2 from t;
5210"), @r"
5211 ╭▸
5212 3 │ values (1, 2), (3, 4)
5213 │ ─ 2. destination
5214 4 │ )
5215 5 │ select COLUMN1, COLUMN2 from t;
5216 ╰╴ ─ 1. source
5217 ");
5218 }
5219
5220 #[test]
5221 fn goto_qualified_column_with_schema_in_from_table() {
5222 assert_snapshot!(goto("
5223create table foo.t(a int, b int);
5224select t$0.a from foo.t;
5225"), @r"
5226 ╭▸
5227 2 │ create table foo.t(a int, b int);
5228 │ ─ 2. destination
5229 3 │ select t.a from foo.t;
5230 ╰╴ ─ 1. source
5231 ");
5232 }
5233
5234 #[test]
5235 fn goto_qualified_column_with_schema_in_from_column() {
5236 assert_snapshot!(goto("
5237create table foo.t(a int, b int);
5238select t.a$0 from foo.t;
5239"), @r"
5240 ╭▸
5241 2 │ create table foo.t(a int, b int);
5242 │ ─ 2. destination
5243 3 │ select t.a from foo.t;
5244 ╰╴ ─ 1. source
5245 ");
5246 }
5247
5248 #[test]
5249 fn goto_cte_union_all_column() {
5250 assert_snapshot!(goto("
5251with t as (
5252 select 1 as a, 2 as b
5253 union all
5254 select 3, 4
5255)
5256select a$0, b from t;
5257"), @r"
5258 ╭▸
5259 3 │ select 1 as a, 2 as b
5260 │ ─ 2. destination
5261 ‡
5262 7 │ select a, b from t;
5263 ╰╴ ─ 1. source
5264 ");
5265 }
5266
5267 #[test]
5268 fn goto_cte_union_all_column_second() {
5269 assert_snapshot!(goto("
5270with t as (
5271 select 1 as a, 2 as b
5272 union all
5273 select 3, 4
5274)
5275select a, b$0 from t;
5276"), @r"
5277 ╭▸
5278 3 │ select 1 as a, 2 as b
5279 │ ─ 2. destination
5280 ‡
5281 7 │ select a, b from t;
5282 ╰╴ ─ 1. source
5283 ");
5284 }
5285
5286 #[test]
5287 fn goto_cte_union_column() {
5288 assert_snapshot!(goto("
5289with t as (
5290 select 1 as a, 2 as b
5291 union
5292 select 3, 4
5293)
5294select a$0 from t;
5295"), @r"
5296 ╭▸
5297 3 │ select 1 as a, 2 as b
5298 │ ─ 2. destination
5299 ‡
5300 7 │ select a from t;
5301 ╰╴ ─ 1. source
5302 ");
5303 }
5304
5305 #[test]
5306 fn goto_cte_insert_returning_column() {
5307 assert_snapshot!(goto("
5308create table t(a int, b int);
5309with inserted as (
5310 insert into t values (1, 2), (3, 4)
5311 returning a, b
5312)
5313select a$0 from inserted;
5314"), @r"
5315 ╭▸
5316 5 │ returning a, b
5317 │ ─ 2. destination
5318 6 │ )
5319 7 │ select a from inserted;
5320 ╰╴ ─ 1. source
5321 ");
5322 }
5323
5324 #[test]
5325 fn goto_cte_insert_returning_aliased_column() {
5326 assert_snapshot!(goto("
5327create table t(a int, b int);
5328with inserted as (
5329 insert into t values (1, 2), (3, 4)
5330 returning a as x
5331)
5332select x$0 from inserted;
5333"), @r"
5334 ╭▸
5335 5 │ returning a as x
5336 │ ─ 2. destination
5337 6 │ )
5338 7 │ select x from inserted;
5339 ╰╴ ─ 1. source
5340 ");
5341 }
5342
5343 #[test]
5344 fn goto_drop_aggregate() {
5345 assert_snapshot!(goto("
5346create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5347drop aggregate myavg$0(int);
5348"), @r"
5349 ╭▸
5350 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5351 │ ───── 2. destination
5352 3 │ drop aggregate myavg(int);
5353 ╰╴ ─ 1. source
5354 ");
5355 }
5356
5357 #[test]
5358 fn goto_drop_aggregate_with_schema() {
5359 assert_snapshot!(goto("
5360set search_path to public;
5361create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5362drop aggregate public.myavg$0(int);
5363"), @r"
5364 ╭▸
5365 3 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5366 │ ───── 2. destination
5367 4 │ drop aggregate public.myavg(int);
5368 ╰╴ ─ 1. source
5369 ");
5370 }
5371
5372 #[test]
5373 fn goto_drop_aggregate_defined_after() {
5374 assert_snapshot!(goto("
5375drop aggregate myavg$0(int);
5376create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5377"), @r"
5378 ╭▸
5379 2 │ drop aggregate myavg(int);
5380 │ ─ 1. source
5381 3 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5382 ╰╴ ───── 2. destination
5383 ");
5384 }
5385
5386 #[test]
5387 fn goto_aggregate_definition_returns_self() {
5388 assert_snapshot!(goto("
5389create aggregate myavg$0(int) (sfunc = int4_avg_accum, stype = _int8);
5390"), @r"
5391 ╭▸
5392 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5393 │ ┬───┬
5394 │ │ │
5395 │ │ 1. source
5396 ╰╴ 2. destination
5397 ");
5398 }
5399
5400 #[test]
5401 fn goto_drop_aggregate_with_search_path() {
5402 assert_snapshot!(goto("
5403create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5404set search_path to bar;
5405create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5406set search_path to default;
5407drop aggregate myavg$0(int);
5408"), @r"
5409 ╭▸
5410 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5411 │ ───── 2. destination
5412 ‡
5413 6 │ drop aggregate myavg(int);
5414 ╰╴ ─ 1. source
5415 ");
5416 }
5417
5418 #[test]
5419 fn goto_drop_aggregate_multiple() {
5420 assert_snapshot!(goto("
5421create aggregate avg1(int) (sfunc = int4_avg_accum, stype = _int8);
5422create aggregate avg2(int) (sfunc = int4_avg_accum, stype = _int8);
5423drop aggregate avg1(int), avg2$0(int);
5424"), @r"
5425 ╭▸
5426 3 │ create aggregate avg2(int) (sfunc = int4_avg_accum, stype = _int8);
5427 │ ──── 2. destination
5428 4 │ drop aggregate avg1(int), avg2(int);
5429 ╰╴ ─ 1. source
5430 ");
5431 }
5432
5433 #[test]
5434 fn goto_drop_aggregate_overloaded() {
5435 assert_snapshot!(goto("
5436create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
5437create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
5438drop aggregate sum$0(complex);
5439"), @r"
5440 ╭▸
5441 2 │ create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
5442 │ ─── 2. destination
5443 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
5444 4 │ drop aggregate sum(complex);
5445 ╰╴ ─ 1. source
5446 ");
5447 }
5448
5449 #[test]
5450 fn goto_drop_aggregate_second_overload() {
5451 assert_snapshot!(goto("
5452create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
5453create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
5454drop aggregate sum$0(bigint);
5455"), @r"
5456 ╭▸
5457 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
5458 │ ─── 2. destination
5459 4 │ drop aggregate sum(bigint);
5460 ╰╴ ─ 1. source
5461 ");
5462 }
5463
5464 #[test]
5465 fn goto_drop_routine_function() {
5466 assert_snapshot!(goto("
5467create function foo() returns int as $$ select 1 $$ language sql;
5468drop routine foo$0();
5469"), @r"
5470 ╭▸
5471 2 │ create function foo() returns int as $$ select 1 $$ language sql;
5472 │ ─── 2. destination
5473 3 │ drop routine foo();
5474 ╰╴ ─ 1. source
5475 ");
5476 }
5477
5478 #[test]
5479 fn goto_drop_routine_aggregate() {
5480 assert_snapshot!(goto("
5481create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5482drop routine myavg$0(int);
5483"), @r"
5484 ╭▸
5485 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
5486 │ ───── 2. destination
5487 3 │ drop routine myavg(int);
5488 ╰╴ ─ 1. source
5489 ");
5490 }
5491
5492 #[test]
5493 fn goto_drop_routine_with_schema() {
5494 assert_snapshot!(goto("
5495set search_path to public;
5496create function foo() returns int as $$ select 1 $$ language sql;
5497drop routine public.foo$0();
5498"), @r"
5499 ╭▸
5500 3 │ create function foo() returns int as $$ select 1 $$ language sql;
5501 │ ─── 2. destination
5502 4 │ drop routine public.foo();
5503 ╰╴ ─ 1. source
5504 ");
5505 }
5506
5507 #[test]
5508 fn goto_drop_routine_defined_after() {
5509 assert_snapshot!(goto("
5510drop routine foo$0();
5511create function foo() returns int as $$ select 1 $$ language sql;
5512"), @r"
5513 ╭▸
5514 2 │ drop routine foo();
5515 │ ─ 1. source
5516 3 │ create function foo() returns int as $$ select 1 $$ language sql;
5517 ╰╴ ─── 2. destination
5518 ");
5519 }
5520
5521 #[test]
5522 fn goto_drop_routine_with_search_path() {
5523 assert_snapshot!(goto("
5524create function foo() returns int as $$ select 1 $$ language sql;
5525set search_path to bar;
5526create function foo() returns int as $$ select 1 $$ language sql;
5527set search_path to default;
5528drop routine foo$0();
5529"), @r"
5530 ╭▸
5531 2 │ create function foo() returns int as $$ select 1 $$ language sql;
5532 │ ─── 2. destination
5533 ‡
5534 6 │ drop routine foo();
5535 ╰╴ ─ 1. source
5536 ");
5537 }
5538
5539 #[test]
5540 fn goto_drop_routine_overloaded() {
5541 assert_snapshot!(goto("
5542create function add(complex) returns complex as $$ select null $$ language sql;
5543create function add(bigint) returns bigint as $$ select 1 $$ language sql;
5544drop routine add$0(complex);
5545"), @r"
5546 ╭▸
5547 2 │ create function add(complex) returns complex as $$ select null $$ language sql;
5548 │ ─── 2. destination
5549 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
5550 4 │ drop routine add(complex);
5551 ╰╴ ─ 1. source
5552 ");
5553 }
5554
5555 #[test]
5556 fn goto_drop_routine_second_overload() {
5557 assert_snapshot!(goto("
5558create function add(complex) returns complex as $$ select null $$ language sql;
5559create function add(bigint) returns bigint as $$ select 1 $$ language sql;
5560drop routine add$0(bigint);
5561"), @r"
5562 ╭▸
5563 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
5564 │ ─── 2. destination
5565 4 │ drop routine add(bigint);
5566 ╰╴ ─ 1. source
5567 ");
5568 }
5569
5570 #[test]
5571 fn goto_drop_routine_aggregate_overloaded() {
5572 assert_snapshot!(goto("
5573create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
5574create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
5575drop routine sum$0(complex);
5576"), @r"
5577 ╭▸
5578 2 │ create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
5579 │ ─── 2. destination
5580 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
5581 4 │ drop routine sum(complex);
5582 ╰╴ ─ 1. source
5583 ");
5584 }
5585
5586 #[test]
5587 fn goto_drop_routine_multiple() {
5588 assert_snapshot!(goto("
5589create function foo() returns int as $$ select 1 $$ language sql;
5590create function bar() returns int as $$ select 1 $$ language sql;
5591drop routine foo(), bar$0();
5592"), @r"
5593 ╭▸
5594 3 │ create function bar() returns int as $$ select 1 $$ language sql;
5595 │ ─── 2. destination
5596 4 │ drop routine foo(), bar();
5597 ╰╴ ─ 1. source
5598 ");
5599 }
5600
5601 #[test]
5602 fn goto_drop_procedure() {
5603 assert_snapshot!(goto("
5604create procedure foo() language sql as $$ select 1 $$;
5605drop procedure foo$0();
5606"), @r"
5607 ╭▸
5608 2 │ create procedure foo() language sql as $$ select 1 $$;
5609 │ ─── 2. destination
5610 3 │ drop procedure foo();
5611 ╰╴ ─ 1. source
5612 ");
5613 }
5614
5615 #[test]
5616 fn goto_drop_procedure_with_schema() {
5617 assert_snapshot!(goto("
5618set search_path to public;
5619create procedure foo() language sql as $$ select 1 $$;
5620drop procedure public.foo$0();
5621"), @r"
5622 ╭▸
5623 3 │ create procedure foo() language sql as $$ select 1 $$;
5624 │ ─── 2. destination
5625 4 │ drop procedure public.foo();
5626 ╰╴ ─ 1. source
5627 ");
5628 }
5629
5630 #[test]
5631 fn goto_drop_procedure_defined_after() {
5632 assert_snapshot!(goto("
5633drop procedure foo$0();
5634create procedure foo() language sql as $$ select 1 $$;
5635"), @r"
5636 ╭▸
5637 2 │ drop procedure foo();
5638 │ ─ 1. source
5639 3 │ create procedure foo() language sql as $$ select 1 $$;
5640 ╰╴ ─── 2. destination
5641 ");
5642 }
5643
5644 #[test]
5645 fn goto_drop_procedure_with_search_path() {
5646 assert_snapshot!(goto("
5647create procedure foo() language sql as $$ select 1 $$;
5648set search_path to bar;
5649create procedure foo() language sql as $$ select 1 $$;
5650set search_path to default;
5651drop procedure foo$0();
5652"), @r"
5653 ╭▸
5654 2 │ create procedure foo() language sql as $$ select 1 $$;
5655 │ ─── 2. destination
5656 ‡
5657 6 │ drop procedure foo();
5658 ╰╴ ─ 1. source
5659 ");
5660 }
5661
5662 #[test]
5663 fn goto_drop_procedure_overloaded() {
5664 assert_snapshot!(goto("
5665create procedure add(complex) language sql as $$ select null $$;
5666create procedure add(bigint) language sql as $$ select 1 $$;
5667drop procedure add$0(complex);
5668"), @r"
5669 ╭▸
5670 2 │ create procedure add(complex) language sql as $$ select null $$;
5671 │ ─── 2. destination
5672 3 │ create procedure add(bigint) language sql as $$ select 1 $$;
5673 4 │ drop procedure add(complex);
5674 ╰╴ ─ 1. source
5675 ");
5676 }
5677
5678 #[test]
5679 fn goto_drop_procedure_second_overload() {
5680 assert_snapshot!(goto("
5681create procedure add(complex) language sql as $$ select null $$;
5682create procedure add(bigint) language sql as $$ select 1 $$;
5683drop procedure add$0(bigint);
5684"), @r"
5685 ╭▸
5686 3 │ create procedure add(bigint) language sql as $$ select 1 $$;
5687 │ ─── 2. destination
5688 4 │ drop procedure add(bigint);
5689 ╰╴ ─ 1. source
5690 ");
5691 }
5692
5693 #[test]
5694 fn goto_drop_procedure_multiple() {
5695 assert_snapshot!(goto("
5696create procedure foo() language sql as $$ select 1 $$;
5697create procedure bar() language sql as $$ select 1 $$;
5698drop procedure foo(), bar$0();
5699"), @r"
5700 ╭▸
5701 3 │ create procedure bar() language sql as $$ select 1 $$;
5702 │ ─── 2. destination
5703 4 │ drop procedure foo(), bar();
5704 ╰╴ ─ 1. source
5705 ");
5706 }
5707
5708 #[test]
5709 fn goto_procedure_definition_returns_self() {
5710 assert_snapshot!(goto("
5711create procedure foo$0() language sql as $$ select 1 $$;
5712"), @r"
5713 ╭▸
5714 2 │ create procedure foo() language sql as $$ select 1 $$;
5715 │ ┬─┬
5716 │ │ │
5717 │ │ 1. source
5718 ╰╴ 2. destination
5719 ");
5720 }
5721
5722 #[test]
5723 fn goto_call_procedure() {
5724 assert_snapshot!(goto("
5725create procedure foo() language sql as $$ select 1 $$;
5726call foo$0();
5727"), @r"
5728 ╭▸
5729 2 │ create procedure foo() language sql as $$ select 1 $$;
5730 │ ─── 2. destination
5731 3 │ call foo();
5732 ╰╴ ─ 1. source
5733 ");
5734 }
5735
5736 #[test]
5737 fn goto_call_procedure_with_schema() {
5738 assert_snapshot!(goto("
5739create procedure public.foo() language sql as $$ select 1 $$;
5740call public.foo$0();
5741"), @r"
5742 ╭▸
5743 2 │ create procedure public.foo() language sql as $$ select 1 $$;
5744 │ ─── 2. destination
5745 3 │ call public.foo();
5746 ╰╴ ─ 1. source
5747 ");
5748 }
5749
5750 #[test]
5751 fn goto_call_procedure_with_search_path() {
5752 assert_snapshot!(goto("
5753set search_path to myschema;
5754create procedure foo() language sql as $$ select 1 $$;
5755call myschema.foo$0();
5756"), @r"
5757 ╭▸
5758 3 │ create procedure foo() language sql as $$ select 1 $$;
5759 │ ─── 2. destination
5760 4 │ call myschema.foo();
5761 ╰╴ ─ 1. source
5762 ");
5763 }
5764
5765 #[test]
5766 fn goto_drop_routine_procedure() {
5767 assert_snapshot!(goto("
5768create procedure foo() language sql as $$ select 1 $$;
5769drop routine foo$0();
5770"), @r"
5771 ╭▸
5772 2 │ create procedure foo() language sql as $$ select 1 $$;
5773 │ ─── 2. destination
5774 3 │ drop routine foo();
5775 ╰╴ ─ 1. source
5776 ");
5777 }
5778
5779 #[test]
5780 fn goto_drop_routine_prefers_function_over_procedure() {
5781 assert_snapshot!(goto("
5782create function foo() returns int as $$ select 1 $$ language sql;
5783create procedure foo() language sql as $$ select 1 $$;
5784drop routine foo$0();
5785"), @r"
5786 ╭▸
5787 2 │ create function foo() returns int as $$ select 1 $$ language sql;
5788 │ ─── 2. destination
5789 3 │ create procedure foo() language sql as $$ select 1 $$;
5790 4 │ drop routine foo();
5791 ╰╴ ─ 1. source
5792 ");
5793 }
5794
5795 #[test]
5796 fn goto_drop_routine_prefers_aggregate_over_procedure() {
5797 assert_snapshot!(goto("
5798create aggregate foo(int) (sfunc = int4_avg_accum, stype = _int8);
5799create procedure foo(int) language sql as $$ select 1 $$;
5800drop routine foo$0(int);
5801"), @r"
5802 ╭▸
5803 2 │ create aggregate foo(int) (sfunc = int4_avg_accum, stype = _int8);
5804 │ ─── 2. destination
5805 3 │ create procedure foo(int) language sql as $$ select 1 $$;
5806 4 │ drop routine foo(int);
5807 ╰╴ ─ 1. source
5808 ");
5809 }
5810
5811 #[test]
5812 fn goto_table_alias_in_qualified_column() {
5813 assert_snapshot!(goto("
5814create table t(a int8, b text);
5815select f$0.a from t as f;
5816"), @r"
5817 ╭▸
5818 3 │ select f.a from t as f;
5819 ╰╴ ─ 1. source ─ 2. destination
5820 ");
5821 }
5822
5823 #[test]
5824 fn goto_column_through_table_alias() {
5825 assert_snapshot!(goto("
5826create table t(a int8, b text);
5827select f.a$0 from t as f;
5828"), @r"
5829 ╭▸
5830 2 │ create table t(a int8, b text);
5831 │ ─ 2. destination
5832 3 │ select f.a from t as f;
5833 ╰╴ ─ 1. source
5834 ");
5835 }
5836
5837 #[test]
5838 fn goto_cte_alias_renamed_column() {
5839 assert_snapshot!(goto("
5840with t as (select 1 a, 2 b)
5841select f.x$0 from t as f(x);
5842"), @r"
5843 ╭▸
5844 3 │ select f.x from t as f(x);
5845 ╰╴ ─ 1. source ─ 2. destination
5846 ");
5847 }
5848
5849 #[test]
5850 fn goto_cte_alias_unrenamed_column() {
5851 assert_snapshot!(goto("
5852with t as (select 1 a, 2 b)
5853select f.b$0 from t as f(x);
5854"), @r"
5855 ╭▸
5856 2 │ with t as (select 1 a, 2 b)
5857 │ ─ 2. destination
5858 3 │ select f.b from t as f(x);
5859 ╰╴ ─ 1. source
5860 ");
5861 }
5862
5863 #[test]
5864 fn goto_join_table() {
5865 assert_snapshot!(goto("
5866create table users(id int, email text);
5867create table messages(id int, user_id int, message text);
5868select * from users join messages$0 on users.id = messages.user_id;
5869"), @r"
5870 ╭▸
5871 3 │ create table messages(id int, user_id int, message text);
5872 │ ──────── 2. destination
5873 4 │ select * from users join messages on users.id = messages.user_id;
5874 ╰╴ ─ 1. source
5875 ");
5876 }
5877
5878 #[test]
5879 fn goto_join_qualified_column_from_joined_table() {
5880 assert_snapshot!(goto("
5881create table users(id int, email text);
5882create table messages(id int, user_id int, message text);
5883select messages.user_id$0 from users join messages on users.id = messages.user_id;
5884"), @r"
5885 ╭▸
5886 3 │ create table messages(id int, user_id int, message text);
5887 │ ─────── 2. destination
5888 4 │ select messages.user_id from users join messages on users.id = messages.user_id;
5889 ╰╴ ─ 1. source
5890 ");
5891 }
5892
5893 #[test]
5894 fn goto_join_qualified_column_from_base_table() {
5895 assert_snapshot!(goto("
5896create table users(id int, email text);
5897create table messages(id int, user_id int, message text);
5898select users.id$0 from users join messages on users.id = messages.user_id;
5899"), @r"
5900 ╭▸
5901 2 │ create table users(id int, email text);
5902 │ ── 2. destination
5903 3 │ create table messages(id int, user_id int, message text);
5904 4 │ select users.id from users join messages on users.id = messages.user_id;
5905 ╰╴ ─ 1. source
5906 ");
5907 }
5908
5909 #[test]
5910 fn goto_join_multiple_joins() {
5911 assert_snapshot!(goto("
5912create table users(id int, name text);
5913create table messages(id int, user_id int, message text);
5914create table comments(id int, message_id int, text text);
5915select comments.text$0 from users
5916 join messages on users.id = messages.user_id
5917 join comments on messages.id = comments.message_id;
5918"), @r"
5919 ╭▸
5920 4 │ create table comments(id int, message_id int, text text);
5921 │ ──── 2. destination
5922 5 │ select comments.text from users
5923 ╰╴ ─ 1. source
5924 ");
5925 }
5926
5927 #[test]
5928 fn goto_join_with_aliases() {
5929 assert_snapshot!(goto("
5930create table users(id int, name text);
5931create table messages(id int, user_id int, message text);
5932select m.message$0 from users as u join messages as m on u.id = m.user_id;
5933"), @r"
5934 ╭▸
5935 3 │ create table messages(id int, user_id int, message text);
5936 │ ─────── 2. destination
5937 4 │ select m.message from users as u join messages as m on u.id = m.user_id;
5938 ╰╴ ─ 1. source
5939 ");
5940 }
5941
5942 #[test]
5943 fn goto_join_unqualified_column() {
5944 assert_snapshot!(goto("
5945create table users(id int, email text);
5946create table messages(id int, user_id int, message text);
5947select message$0 from users join messages on users.id = messages.user_id;
5948"), @r"
5949 ╭▸
5950 3 │ create table messages(id int, user_id int, message text);
5951 │ ─────── 2. destination
5952 4 │ select message from users join messages on users.id = messages.user_id;
5953 ╰╴ ─ 1. source
5954 ");
5955 }
5956
5957 #[test]
5958 fn goto_join_with_many_tables() {
5959 assert_snapshot!(goto("
5960create table users(id int, email text);
5961create table messages(id int, user_id int, message text);
5962create table logins(id int, user_id int, at timestamptz);
5963create table posts(id int, user_id int, post text);
5964
5965select post$0
5966 from users
5967 join messages
5968 on users.id = messages.user_id
5969 join logins
5970 on users.id = logins.user_id
5971 join posts
5972 on users.id = posts.user_id
5973"), @r"
5974 ╭▸
5975 5 │ create table posts(id int, user_id int, post text);
5976 │ ──── 2. destination
5977 6 │
5978 7 │ select post
5979 ╰╴ ─ 1. source
5980 ");
5981 }
5982
5983 #[test]
5984 fn goto_join_with_schema() {
5985 assert_snapshot!(goto("
5986create schema foo;
5987create table foo.users(id int, email text);
5988create table foo.messages(id int, user_id int, message text);
5989select foo.messages.message$0 from foo.users join foo.messages on foo.users.id = foo.messages.user_id;
5990"), @r"
5991 ╭▸
5992 4 │ create table foo.messages(id int, user_id int, message text);
5993 │ ─────── 2. destination
5994 5 │ select foo.messages.message from foo.users join foo.messages on foo.users.id = foo.messages.user_id;
5995 ╰╴ ─ 1. source
5996 ");
5997 }
5998
5999 #[test]
6000 fn goto_join_left_join() {
6001 assert_snapshot!(goto("
6002create table users(id int, email text);
6003create table messages(id int, user_id int, message text);
6004select messages.message$0 from users left join messages on users.id = messages.user_id;
6005"), @r"
6006 ╭▸
6007 3 │ create table messages(id int, user_id int, message text);
6008 │ ─────── 2. destination
6009 4 │ select messages.message from users left join messages on users.id = messages.user_id;
6010 ╰╴ ─ 1. source
6011 ");
6012 }
6013
6014 #[test]
6015 fn goto_join_on_table_qualifier() {
6016 assert_snapshot!(goto("
6017create table t(a int);
6018create table u(a int);
6019select * from t join u on u$0.a = t.a;
6020"), @r"
6021 ╭▸
6022 3 │ create table u(a int);
6023 │ ─ 2. destination
6024 4 │ select * from t join u on u.a = t.a;
6025 ╰╴ ─ 1. source
6026 ");
6027 }
6028
6029 #[test]
6030 fn goto_join_on_column() {
6031 assert_snapshot!(goto("
6032create table t(a int);
6033create table u(a int);
6034select * from t join u on u.a$0 = t.a;
6035"), @r"
6036 ╭▸
6037 3 │ create table u(a int);
6038 │ ─ 2. destination
6039 4 │ select * from t join u on u.a = t.a;
6040 ╰╴ ─ 1. source
6041 ");
6042 }
6043
6044 #[test]
6045 fn goto_join_using_column() {
6046 assert_snapshot!(goto("
6047create table t(a int);
6048create table u(a int);
6049select * from t join u using (a$0);
6050"), @r"
6051 ╭▸
6052 2 │ create table t(a int);
6053 │ ─ 2. destination
6054 3 │ create table u(a int);
6055 │ ─ 3. destination
6056 4 │ select * from t join u using (a);
6057 ╰╴ ─ 1. source
6058 ");
6059 }
6060
6061 #[test]
6062 fn goto_insert_select_cte_column() {
6063 assert_snapshot!(goto("
6064create table users(id int, email text);
6065with new_data as (
6066 select 1 as id, 'test@example.com' as email
6067)
6068insert into users (id, email)
6069select id$0, email from new_data;
6070"), @r"
6071 ╭▸
6072 4 │ select 1 as id, 'test@example.com' as email
6073 │ ── 2. destination
6074 ‡
6075 7 │ select id, email from new_data;
6076 ╰╴ ─ 1. source
6077 ");
6078 }
6079
6080 #[test]
6081 fn goto_insert_select_cte_column_second() {
6082 assert_snapshot!(goto("
6083create table users(id int, email text);
6084with new_data as (
6085 select 1 as id, 'test@example.com' as email
6086)
6087insert into users (id, email)
6088select id, email$0 from new_data;
6089"), @r"
6090 ╭▸
6091 4 │ select 1 as id, 'test@example.com' as email
6092 │ ───── 2. destination
6093 ‡
6094 7 │ select id, email from new_data;
6095 ╰╴ ─ 1. source
6096 ");
6097 }
6098
6099 #[test]
6100 fn goto_insert_select_cte_table() {
6101 assert_snapshot!(goto("
6102create table users(id int, email text);
6103with new_data as (
6104 select 1 as id, 'test@example.com' as email
6105)
6106insert into users (id, email)
6107select id, email from new_data$0;
6108"), @r"
6109 ╭▸
6110 3 │ with new_data as (
6111 │ ──────── 2. destination
6112 ‡
6113 7 │ select id, email from new_data;
6114 ╰╴ ─ 1. source
6115 ");
6116 }
6117
6118 #[test]
6119 fn goto_delete_cte_column() {
6120 assert_snapshot!(goto("
6121create table users(id int, email text);
6122with old_data as (
6123 select 1 as id
6124)
6125delete from users where id in (select id$0 from old_data);
6126"), @r"
6127 ╭▸
6128 4 │ select 1 as id
6129 │ ── 2. destination
6130 5 │ )
6131 6 │ delete from users where id in (select id from old_data);
6132 ╰╴ ─ 1. source
6133 ");
6134 }
6135
6136 #[test]
6137 fn goto_update_table() {
6138 assert_snapshot!(goto("
6139create table users(id int, email text);
6140update users$0 set email = 'new@example.com';
6141"), @r"
6142 ╭▸
6143 2 │ create table users(id int, email text);
6144 │ ───── 2. destination
6145 3 │ update users set email = 'new@example.com';
6146 ╰╴ ─ 1. source
6147 ");
6148 }
6149
6150 #[test]
6151 fn goto_update_table_with_schema() {
6152 assert_snapshot!(goto("
6153create table public.users(id int, email text);
6154update public.users$0 set email = 'new@example.com';
6155"), @r"
6156 ╭▸
6157 2 │ create table public.users(id int, email text);
6158 │ ───── 2. destination
6159 3 │ update public.users set email = 'new@example.com';
6160 ╰╴ ─ 1. source
6161 ");
6162 }
6163
6164 #[test]
6165 fn goto_update_table_with_search_path() {
6166 assert_snapshot!(goto("
6167set search_path to foo;
6168create table foo.users(id int, email text);
6169update users$0 set email = 'new@example.com';
6170"), @r"
6171 ╭▸
6172 3 │ create table foo.users(id int, email text);
6173 │ ───── 2. destination
6174 4 │ update users set email = 'new@example.com';
6175 ╰╴ ─ 1. source
6176 ");
6177 }
6178
6179 #[test]
6180 fn goto_update_where_column() {
6181 assert_snapshot!(goto("
6182create table users(id int, email text);
6183update users set email = 'new@example.com' where id$0 = 1;
6184"), @r"
6185 ╭▸
6186 2 │ create table users(id int, email text);
6187 │ ── 2. destination
6188 3 │ update users set email = 'new@example.com' where id = 1;
6189 ╰╴ ─ 1. source
6190 ");
6191 }
6192
6193 #[test]
6194 fn goto_update_where_column_with_schema() {
6195 assert_snapshot!(goto("
6196create table public.users(id int, email text);
6197update public.users set email = 'new@example.com' where id$0 = 1;
6198"), @r"
6199 ╭▸
6200 2 │ create table public.users(id int, email text);
6201 │ ── 2. destination
6202 3 │ update public.users set email = 'new@example.com' where id = 1;
6203 ╰╴ ─ 1. source
6204 ");
6205 }
6206
6207 #[test]
6208 fn goto_update_where_column_with_search_path() {
6209 assert_snapshot!(goto("
6210set search_path to foo;
6211create table foo.users(id int, email text);
6212update users set email = 'new@example.com' where id$0 = 1;
6213"), @r"
6214 ╭▸
6215 3 │ create table foo.users(id int, email text);
6216 │ ── 2. destination
6217 4 │ update users set email = 'new@example.com' where id = 1;
6218 ╰╴ ─ 1. source
6219 ");
6220 }
6221
6222 #[test]
6223 fn goto_update_set_column() {
6224 assert_snapshot!(goto("
6225create table users(id int, email text);
6226update users set email$0 = 'new@example.com' where id = 1;
6227"), @r"
6228 ╭▸
6229 2 │ create table users(id int, email text);
6230 │ ───── 2. destination
6231 3 │ update users set email = 'new@example.com' where id = 1;
6232 ╰╴ ─ 1. source
6233 ");
6234 }
6235
6236 #[test]
6237 fn goto_update_set_column_with_schema() {
6238 assert_snapshot!(goto("
6239create table public.users(id int, email text);
6240update public.users set email$0 = 'new@example.com' where id = 1;
6241"), @r"
6242 ╭▸
6243 2 │ create table public.users(id int, email text);
6244 │ ───── 2. destination
6245 3 │ update public.users set email = 'new@example.com' where id = 1;
6246 ╰╴ ─ 1. source
6247 ");
6248 }
6249
6250 #[test]
6251 fn goto_update_set_column_with_search_path() {
6252 assert_snapshot!(goto("
6253set search_path to foo;
6254create table foo.users(id int, email text);
6255update users set email$0 = 'new@example.com' where id = 1;
6256"), @r"
6257 ╭▸
6258 3 │ create table foo.users(id int, email text);
6259 │ ───── 2. destination
6260 4 │ update users set email = 'new@example.com' where id = 1;
6261 ╰╴ ─ 1. source
6262 ");
6263 }
6264
6265 #[test]
6266 fn goto_update_from_table() {
6267 assert_snapshot!(goto("
6268create table users(id int, email text);
6269create table messages(id int, user_id int, email text);
6270update users set email = messages.email from messages$0 where users.id = messages.user_id;
6271"), @r"
6272 ╭▸
6273 3 │ create table messages(id int, user_id int, email text);
6274 │ ──────── 2. destination
6275 4 │ update users set email = messages.email from messages where users.id = messages.user_id;
6276 ╰╴ ─ 1. source
6277 ");
6278 }
6279
6280 #[test]
6281 fn goto_update_from_table_with_schema() {
6282 assert_snapshot!(goto("
6283create table users(id int, email text);
6284create table public.messages(id int, user_id int, email text);
6285update users set email = messages.email from public.messages$0 where users.id = messages.user_id;
6286"), @r"
6287 ╭▸
6288 3 │ create table public.messages(id int, user_id int, email text);
6289 │ ──────── 2. destination
6290 4 │ update users set email = messages.email from public.messages where users.id = messages.user_id;
6291 ╰╴ ─ 1. source
6292 ");
6293 }
6294
6295 #[test]
6296 fn goto_update_from_table_with_search_path() {
6297 assert_snapshot!(goto("
6298set search_path to foo;
6299create table users(id int, email text);
6300create table foo.messages(id int, user_id int, email text);
6301update users set email = messages.email from messages$0 where users.id = messages.user_id;
6302"), @r"
6303 ╭▸
6304 4 │ create table foo.messages(id int, user_id int, email text);
6305 │ ──────── 2. destination
6306 5 │ update users set email = messages.email from messages where users.id = messages.user_id;
6307 ╰╴ ─ 1. source
6308 ");
6309 }
6310
6311 #[test]
6312 fn goto_update_with_cte_table() {
6313 assert_snapshot!(goto("
6314create table users(id int, email text);
6315with new_data as (
6316 select 1 as id, 'new@example.com' as email
6317)
6318update users set email = new_data.email from new_data$0 where users.id = new_data.id;
6319"), @r"
6320 ╭▸
6321 3 │ with new_data as (
6322 │ ──────── 2. destination
6323 ‡
6324 6 │ update users set email = new_data.email from new_data where users.id = new_data.id;
6325 ╰╴ ─ 1. source
6326 ");
6327 }
6328
6329 #[test]
6330 fn goto_update_with_cte_column_in_set() {
6331 assert_snapshot!(goto("
6332create table users(id int, email text);
6333with new_data as (
6334 select 1 as id, 'new@example.com' as email
6335)
6336update users set email = new_data.email$0 from new_data where users.id = new_data.id;
6337"), @r"
6338 ╭▸
6339 4 │ select 1 as id, 'new@example.com' as email
6340 │ ───── 2. destination
6341 5 │ )
6342 6 │ update users set email = new_data.email from new_data where users.id = new_data.id;
6343 ╰╴ ─ 1. source
6344 ");
6345 }
6346
6347 #[test]
6348 fn goto_update_with_cte_column_in_where() {
6349 assert_snapshot!(goto("
6350create table users(id int, email text);
6351with new_data as (
6352 select 1 as id, 'new@example.com' as email
6353)
6354update users set email = new_data.email from new_data where new_data.id$0 = users.id;
6355"), @r"
6356 ╭▸
6357 4 │ select 1 as id, 'new@example.com' as email
6358 │ ── 2. destination
6359 5 │ )
6360 6 │ update users set email = new_data.email from new_data where new_data.id = users.id;
6361 ╰╴ ─ 1. source
6362 ");
6363 }
6364
6365 #[test]
6366 fn goto_update_with_cte_values() {
6367 assert_snapshot!(goto("
6368create table users(id int, email text);
6369with new_data as (
6370 values (1, 'new@example.com')
6371)
6372update users set email = new_data.column2$0 from new_data where users.id = new_data.column1;
6373"), @r"
6374 ╭▸
6375 4 │ values (1, 'new@example.com')
6376 │ ───────────────── 2. destination
6377 5 │ )
6378 6 │ update users set email = new_data.column2 from new_data where users.id = new_data.column1;
6379 ╰╴ ─ 1. source
6380 ");
6381 }
6382
6383 #[test]
6384 fn goto_truncate_table() {
6385 assert_snapshot!(goto("
6386create table t();
6387truncate table t$0;
6388"), @r"
6389 ╭▸
6390 2 │ create table t();
6391 │ ─ 2. destination
6392 3 │ truncate table t;
6393 ╰╴ ─ 1. source
6394 ");
6395 }
6396
6397 #[test]
6398 fn goto_truncate_table_without_table_keyword() {
6399 assert_snapshot!(goto("
6400create table t();
6401truncate t$0;
6402"), @r"
6403 ╭▸
6404 2 │ create table t();
6405 │ ─ 2. destination
6406 3 │ truncate t;
6407 ╰╴ ─ 1. source
6408 ");
6409 }
6410
6411 #[test]
6412 fn goto_truncate_multiple_tables() {
6413 assert_snapshot!(goto("
6414create table t1();
6415create table t2();
6416truncate t1, t2$0;
6417"), @r"
6418 ╭▸
6419 3 │ create table t2();
6420 │ ── 2. destination
6421 4 │ truncate t1, t2;
6422 ╰╴ ─ 1. source
6423 ");
6424 }
6425
6426 #[test]
6427 fn goto_lock_table() {
6428 assert_snapshot!(goto("
6429create table t();
6430lock table t$0;
6431"), @r"
6432 ╭▸
6433 2 │ create table t();
6434 │ ─ 2. destination
6435 3 │ lock table t;
6436 ╰╴ ─ 1. source
6437 ");
6438 }
6439
6440 #[test]
6441 fn goto_lock_table_without_table_keyword() {
6442 assert_snapshot!(goto("
6443create table t();
6444lock t$0;
6445"), @r"
6446 ╭▸
6447 2 │ create table t();
6448 │ ─ 2. destination
6449 3 │ lock t;
6450 ╰╴ ─ 1. source
6451 ");
6452 }
6453
6454 #[test]
6455 fn goto_lock_multiple_tables() {
6456 assert_snapshot!(goto("
6457create table t1();
6458create table t2();
6459lock t1, t2$0;
6460"), @r"
6461 ╭▸
6462 3 │ create table t2();
6463 │ ── 2. destination
6464 4 │ lock t1, t2;
6465 ╰╴ ─ 1. source
6466 ");
6467 }
6468
6469 #[test]
6470 fn goto_vacuum_table() {
6471 assert_snapshot!(goto("
6472create table users(id int, email text);
6473vacuum users$0;
6474"), @r"
6475 ╭▸
6476 2 │ create table users(id int, email text);
6477 │ ───── 2. destination
6478 3 │ vacuum users;
6479 ╰╴ ─ 1. source
6480 ");
6481 }
6482
6483 #[test]
6484 fn goto_vacuum_multiple_tables() {
6485 assert_snapshot!(goto("
6486create table t1();
6487create table t2();
6488vacuum t1, t2$0;
6489"), @r"
6490 ╭▸
6491 3 │ create table t2();
6492 │ ── 2. destination
6493 4 │ vacuum t1, t2;
6494 ╰╴ ─ 1. source
6495 ");
6496 }
6497
6498 #[test]
6499 fn goto_alter_table() {
6500 assert_snapshot!(goto("
6501create table users(id int, email text);
6502alter table users$0 alter email set not null;
6503"), @r"
6504 ╭▸
6505 2 │ create table users(id int, email text);
6506 │ ───── 2. destination
6507 3 │ alter table users alter email set not null;
6508 ╰╴ ─ 1. source
6509 ");
6510 }
6511
6512 #[test]
6513 fn goto_alter_table_column() {
6514 assert_snapshot!(goto("
6515create table users(id int, email text);
6516alter table users alter email$0 set not null;
6517"), @r"
6518 ╭▸
6519 2 │ create table users(id int, email text);
6520 │ ───── 2. destination
6521 3 │ alter table users alter email set not null;
6522 ╰╴ ─ 1. source
6523 ");
6524 }
6525
6526 #[test]
6527 fn goto_alter_table_column_with_column_keyword() {
6528 assert_snapshot!(goto("
6529create table users(id int, email text);
6530alter table users alter column email$0 set not null;
6531"), @r"
6532 ╭▸
6533 2 │ create table users(id int, email text);
6534 │ ───── 2. destination
6535 3 │ alter table users alter column email set not null;
6536 ╰╴ ─ 1. source
6537 ");
6538 }
6539
6540 #[test]
6541 fn goto_alter_table_add_column() {
6542 assert_snapshot!(goto("
6543create table users(id int);
6544alter table users$0 add column email text;
6545"), @r"
6546 ╭▸
6547 2 │ create table users(id int);
6548 │ ───── 2. destination
6549 3 │ alter table users add column email text;
6550 ╰╴ ─ 1. source
6551 ");
6552 }
6553
6554 #[test]
6555 fn goto_alter_table_drop_column() {
6556 assert_snapshot!(goto("
6557create table users(id int, email text);
6558alter table users drop column email$0;
6559"), @r"
6560 ╭▸
6561 2 │ create table users(id int, email text);
6562 │ ───── 2. destination
6563 3 │ alter table users drop column email;
6564 ╰╴ ─ 1. source
6565 ");
6566 }
6567
6568 #[test]
6569 fn goto_alter_table_drop_column_table_name() {
6570 assert_snapshot!(goto("
6571create table users(id int, email text);
6572alter table users$0 drop column email;
6573"), @r"
6574 ╭▸
6575 2 │ create table users(id int, email text);
6576 │ ───── 2. destination
6577 3 │ alter table users drop column email;
6578 ╰╴ ─ 1. source
6579 ");
6580 }
6581
6582 #[test]
6583 fn goto_comment_on_table() {
6584 assert_snapshot!(goto("
6585create table t(id int);
6586comment on table t$0 is '';
6587"), @r"
6588 ╭▸
6589 2 │ create table t(id int);
6590 │ ─ 2. destination
6591 3 │ comment on table t is '';
6592 ╰╴ ─ 1. source
6593 ");
6594 }
6595
6596 #[test]
6597 fn goto_comment_on_column() {
6598 assert_snapshot!(goto("
6599create table t(id int);
6600comment on column t.id$0 is '';
6601"), @r"
6602 ╭▸
6603 2 │ create table t(id int);
6604 │ ── 2. destination
6605 3 │ comment on column t.id is '';
6606 ╰╴ ─ 1. source
6607 ");
6608 }
6609
6610 #[test]
6611 fn goto_refresh_materialized_view() {
6612 assert_snapshot!(goto("
6613create materialized view mv as select 1;
6614refresh materialized view mv$0;
6615"), @r"
6616 ╭▸
6617 2 │ create materialized view mv as select 1;
6618 │ ── 2. destination
6619 3 │ refresh materialized view mv;
6620 ╰╴ ─ 1. source
6621 ");
6622 }
6623
6624 #[test]
6625 fn goto_refresh_materialized_view_concurrently() {
6626 assert_snapshot!(goto("
6627create materialized view mv as select 1;
6628refresh materialized view concurrently mv$0;
6629"), @r"
6630 ╭▸
6631 2 │ create materialized view mv as select 1;
6632 │ ── 2. destination
6633 3 │ refresh materialized view concurrently mv;
6634 ╰╴ ─ 1. source
6635 ");
6636 }
6637
6638 #[test]
6639 fn goto_reindex_table() {
6640 assert_snapshot!(goto("
6641create table users(id int);
6642reindex table users$0;
6643"), @r"
6644 ╭▸
6645 2 │ create table users(id int);
6646 │ ───── 2. destination
6647 3 │ reindex table users;
6648 ╰╴ ─ 1. source
6649 ");
6650 }
6651
6652 #[test]
6653 fn goto_reindex_index() {
6654 assert_snapshot!(goto("
6655create table t(c int);
6656create index idx on t(c);
6657reindex index idx$0;
6658"), @r"
6659 ╭▸
6660 3 │ create index idx on t(c);
6661 │ ─── 2. destination
6662 4 │ reindex index idx;
6663 ╰╴ ─ 1. source
6664 ");
6665 }
6666
6667 #[test]
6668 fn goto_select_exists_column() {
6669 assert_snapshot!(goto("
6670select exists$0 from (
6671 select exists(select 1)
6672);
6673"), @r"
6674 ╭▸
6675 2 │ select exists from (
6676 │ ─ 1. source
6677 3 │ select exists(select 1)
6678 ╰╴ ──────────────── 2. destination
6679 ");
6680 }
6681
6682 #[test]
6683 fn goto_reindex_schema() {
6684 assert_snapshot!(goto("
6685create schema app;
6686reindex schema app$0;
6687"), @r"
6688 ╭▸
6689 2 │ create schema app;
6690 │ ─── 2. destination
6691 3 │ reindex schema app;
6692 ╰╴ ─ 1. source
6693 ");
6694 }
6695
6696 #[test]
6697 fn goto_reindex_database() {
6698 assert_snapshot!(goto("
6699create database appdb;
6700reindex database appdb$0;
6701"), @r"
6702 ╭▸
6703 2 │ create database appdb;
6704 │ ───── 2. destination
6705 3 │ reindex database appdb;
6706 ╰╴ ─ 1. source
6707 ");
6708 }
6709
6710 #[test]
6711 fn goto_reindex_system() {
6712 assert_snapshot!(goto("
6713create database systemdb;
6714reindex system systemdb$0;
6715"), @r"
6716 ╭▸
6717 2 │ create database systemdb;
6718 │ ──────── 2. destination
6719 3 │ reindex system systemdb;
6720 ╰╴ ─ 1. source
6721 ");
6722 }
6723
6724 #[test]
6725 fn goto_merge_returning_aliased_column() {
6726 assert_snapshot!(goto(
6727 "
6728create table t(a int, b int);
6729with u(x, y) as (
6730 select 1, 2
6731),
6732merged as (
6733 merge into t
6734 using u
6735 on t.a = u.x
6736 when matched then
6737 do nothing
6738 when not matched then
6739 do nothing
6740 returning a as x, b as y
6741)
6742select x$0 from merged;
6743",
6744 ), @r"
6745 ╭▸
6746 14 │ returning a as x, b as y
6747 │ ─ 2. destination
6748 15 │ )
6749 16 │ select x from merged;
6750 ╰╴ ─ 1. source
6751 ");
6752 }
6753
6754 #[test]
6755 fn goto_cte_update_returning_column() {
6756 assert_snapshot!(goto("
6757create table t(a int, b int);
6758with updated(c) as (
6759 update t set a = 10
6760 returning a, b
6761)
6762select c, b$0 from updated;"
6763 ), @r"
6764 ╭▸
6765 5 │ returning a, b
6766 │ ─ 2. destination
6767 6 │ )
6768 7 │ select c, b from updated;
6769 ╰╴ ─ 1. source
6770 ");
6771 }
6772
6773 #[test]
6774 fn goto_update_returning_column_to_table_def() {
6775 assert_snapshot!(goto("
6776create table t(a int, b int);
6777with updated(c) as (
6778 update t set a = 10
6779 returning a, b$0
6780)
6781select c, b from updated;"
6782 ), @r"
6783 ╭▸
6784 2 │ create table t(a int, b int);
6785 │ ─ 2. destination
6786 ‡
6787 5 │ returning a, b
6788 ╰╴ ─ 1. source
6789 ");
6790 }
6791
6792 #[test]
6793 fn goto_insert_returning_column_to_table_def() {
6794 assert_snapshot!(goto("
6795create table t(a int, b int);
6796with inserted as (
6797 insert into t values (1, 2)
6798 returning a$0, b
6799)
6800select a, b from inserted;"
6801 ), @r"
6802 ╭▸
6803 2 │ create table t(a int, b int);
6804 │ ─ 2. destination
6805 ‡
6806 5 │ returning a, b
6807 ╰╴ ─ 1. source
6808 ");
6809 }
6810
6811 #[test]
6812 fn goto_delete_returning_column_to_table_def() {
6813 assert_snapshot!(goto("
6814create table t(a int, b int);
6815with deleted as (
6816 delete from t
6817 returning a, b$0
6818)
6819select a, b from deleted;"
6820 ), @r"
6821 ╭▸
6822 2 │ create table t(a int, b int);
6823 │ ─ 2. destination
6824 ‡
6825 5 │ returning a, b
6826 ╰╴ ─ 1. source
6827 ");
6828 }
6829
6830 #[test]
6831 fn goto_update_returning_qualified_star_table() {
6832 assert_snapshot!(goto("
6833create table t(a int, b int);
6834update t set a = 10
6835returning t$0.*;"
6836 ), @r"
6837 ╭▸
6838 2 │ create table t(a int, b int);
6839 │ ─ 2. destination
6840 3 │ update t set a = 10
6841 4 │ returning t.*;
6842 ╰╴ ─ 1. source
6843 ");
6844 }
6845
6846 #[test]
6847 fn goto_insert_returning_qualified_star_table() {
6848 assert_snapshot!(goto("
6849create table t(a int, b int);
6850insert into t values (1, 2)
6851returning t$0.*;"
6852 ), @r"
6853 ╭▸
6854 2 │ create table t(a int, b int);
6855 │ ─ 2. destination
6856 3 │ insert into t values (1, 2)
6857 4 │ returning t.*;
6858 ╰╴ ─ 1. source
6859 ");
6860 }
6861
6862 #[test]
6863 fn goto_delete_returning_qualified_star_table() {
6864 assert_snapshot!(goto("
6865create table t(a int, b int);
6866delete from t
6867returning t$0.*;"
6868 ), @r"
6869 ╭▸
6870 2 │ create table t(a int, b int);
6871 │ ─ 2. destination
6872 3 │ delete from t
6873 4 │ returning t.*;
6874 ╰╴ ─ 1. source
6875 ");
6876 }
6877
6878 #[test]
6879 fn goto_update_alias_in_set_clause() {
6880 assert_snapshot!(goto("
6881create table t(a int, b int);
6882update t as f set f$0.a = 10;"
6883 ), @r"
6884 ╭▸
6885 3 │ update t as f set f.a = 10;
6886 │ ┬ ─ 1. source
6887 │ │
6888 ╰╴ 2. destination
6889 ");
6890 }
6891
6892 #[test]
6893 fn goto_update_alias_in_where_clause() {
6894 assert_snapshot!(goto("
6895create table t(a int, b int);
6896update t as f set a = 10 where f$0.b = 5;"
6897 ), @r"
6898 ╭▸
6899 3 │ update t as f set a = 10 where f.b = 5;
6900 ╰╴ ─ 2. destination ─ 1. source
6901 ");
6902 }
6903
6904 #[test]
6905 fn goto_update_alias_in_from_clause() {
6906 assert_snapshot!(goto("
6907create table t(a int, b int);
6908create table u(c int);
6909update t as f set a = 10 from u where f$0.b = u.c;"
6910 ), @r"
6911 ╭▸
6912 4 │ update t as f set a = 10 from u where f.b = u.c;
6913 ╰╴ ─ 2. destination ─ 1. source
6914 ");
6915 }
6916
6917 #[test]
6918 fn goto_insert_alias_in_on_conflict() {
6919 assert_snapshot!(goto("
6920create table t(a int primary key, b int);
6921insert into t as f values (1, 2) on conflict (f$0.a) do nothing;"
6922 ), @r"
6923 ╭▸
6924 3 │ insert into t as f values (1, 2) on conflict (f.a) do nothing;
6925 ╰╴ ─ 2. destination ─ 1. source
6926 ");
6927 }
6928
6929 #[test]
6930 fn goto_insert_alias_in_returning() {
6931 assert_snapshot!(goto("
6932create table t(a int, b int);
6933insert into t as f values (1, 2) returning f$0.a;"
6934 ), @r"
6935 ╭▸
6936 3 │ insert into t as f values (1, 2) returning f.a;
6937 ╰╴ ─ 2. destination ─ 1. source
6938 ");
6939 }
6940
6941 #[test]
6942 fn goto_insert_alias_returning_column() {
6943 assert_snapshot!(goto("
6944create table t(a int, b int);
6945insert into t as f values (1, 2) returning f.a$0;"
6946 ), @r"
6947 ╭▸
6948 2 │ create table t(a int, b int);
6949 │ ─ 2. destination
6950 3 │ insert into t as f values (1, 2) returning f.a;
6951 ╰╴ ─ 1. source
6952 ");
6953 }
6954
6955 #[test]
6956 fn goto_delete_from_alias() {
6957 assert_snapshot!(goto("
6958create table t(a int, b int);
6959delete from t as f where f$0.a = 10;"
6960 ), @r"
6961 ╭▸
6962 3 │ delete from t as f where f.a = 10;
6963 │ ┬ ─ 1. source
6964 │ │
6965 ╰╴ 2. destination
6966 ");
6967 }
6968
6969 #[test]
6970 fn goto_delete_from_alias_column() {
6971 assert_snapshot!(goto("
6972create table t(a int, b int);
6973delete from t as f where f.a$0 = 10;"
6974 ), @r"
6975 ╭▸
6976 2 │ create table t(a int, b int);
6977 │ ─ 2. destination
6978 3 │ delete from t as f where f.a = 10;
6979 ╰╴ ─ 1. source
6980 ");
6981 }
6982
6983 #[test]
6984 fn goto_delete_from_alias_returning() {
6985 assert_snapshot!(goto("
6986create table t(a int, b int);
6987delete from t as f returning f$0.a"
6988 ), @r"
6989 ╭▸
6990 3 │ delete from t as f returning f.a
6991 │ ┬ ─ 1. source
6992 │ │
6993 ╰╴ 2. destination
6994 ");
6995
6996 assert_snapshot!(goto("
6997create table t(a int, b int);
6998delete from t as f returning f.a$0"
6999 ), @r"
7000 ╭▸
7001 2 │ create table t(a int, b int);
7002 │ ─ 2. destination
7003 3 │ delete from t as f returning f.a
7004 ╰╴ ─ 1. source
7005 ");
7006 }
7007
7008 #[test]
7009 fn goto_merge_alias_on_table() {
7010 assert_snapshot!(goto("
7011create table t(a int, b int);
7012create table u(a int, b int);
7013merge into t as f
7014 using u on u.a = f$0.a
7015 when matched then do nothing;
7016"
7017
7018 ), @r"
7019 ╭▸
7020 4 │ merge into t as f
7021 │ ─ 2. destination
7022 5 │ using u on u.a = f.a
7023 ╰╴ ─ 1. source
7024 ");
7025 }
7026
7027 #[test]
7028 fn goto_merge_alias_on_column() {
7029 assert_snapshot!(goto("
7030create table t(a int, b int);
7031create table u(a int, b int);
7032merge into t as f
7033 using u on u.a = f.a$0
7034 when matched then do nothing;
7035"
7036
7037 ), @r"
7038 ╭▸
7039 2 │ create table t(a int, b int);
7040 │ ─ 2. destination
7041 ‡
7042 5 │ using u on u.a = f.a
7043 ╰╴ ─ 1. source
7044 ");
7045 }
7046
7047 #[test]
7048 fn goto_merge_alias_returning() {
7049 assert_snapshot!(goto("
7050create table t(a int, b int);
7051create table u(a int, b int);
7052merge into t as f
7053 using u on u.a = f.a
7054 when matched then do nothing
7055 returning f$0.a;
7056"
7057
7058 ), @r"
7059 ╭▸
7060 4 │ merge into t as f
7061 │ ─ 2. destination
7062 ‡
7063 7 │ returning f.a;
7064 ╰╴ ─ 1. source
7065 ");
7066
7067 assert_snapshot!(goto("
7068create table t(a int, b int);
7069create table u(a int, b int);
7070merge into t as f
7071 using u on u.a = f.a
7072 when matched then do nothing
7073 returning f.a$0;
7074"
7075 ), @r"
7076 ╭▸
7077 2 │ create table t(a int, b int);
7078 │ ─ 2. destination
7079 ‡
7080 7 │ returning f.a;
7081 ╰╴ ─ 1. source
7082 ");
7083 }
7084
7085 #[test]
7086 fn goto_merge_using_table_in_when_clause() {
7087 assert_snapshot!(goto("
7088create table t(a int, b int);
7089create table u(a int, b int);
7090merge into t
7091 using u on true
7092 when matched and u$0.a = t.a
7093 then do nothing;
7094"
7095 ), @r"
7096 ╭▸
7097 3 │ create table u(a int, b int);
7098 │ ─ 2. destination
7099 ‡
7100 6 │ when matched and u.a = t.a
7101 ╰╴ ─ 1. source
7102 ");
7103 }
7104
7105 #[test]
7106 fn goto_merge_using_table_column_in_when_clause() {
7107 assert_snapshot!(goto("
7108create table t(a int, b int);
7109create table u(a int, b int);
7110merge into t
7111 using u on true
7112 when matched and u.a$0 = t.a
7113 then do nothing;
7114"
7115 ), @r"
7116 ╭▸
7117 3 │ create table u(a int, b int);
7118 │ ─ 2. destination
7119 ‡
7120 6 │ when matched and u.a = t.a
7121 ╰╴ ─ 1. source
7122 ");
7123 }
7124
7125 #[test]
7126 fn goto_merge_unqualified_column_target_table() {
7127 assert_snapshot!(goto("
7128create table x(a int, b int);
7129create table y(c int, d int);
7130merge into x
7131 using y
7132 on true
7133 when matched and a$0 = c
7134 then do nothing;
7135"
7136 ), @r"
7137 ╭▸
7138 2 │ create table x(a int, b int);
7139 │ ─ 2. destination
7140 ‡
7141 7 │ when matched and a = c
7142 ╰╴ ─ 1. source
7143 ");
7144 }
7145
7146 #[test]
7147 fn goto_merge_unqualified_column_source_table() {
7148 assert_snapshot!(goto("
7149create table x(a int, b int);
7150create table y(c int, d int);
7151merge into x
7152 using y
7153 on true
7154 when matched and a = c$0
7155 then do nothing;
7156"
7157 ), @r"
7158 ╭▸
7159 3 │ create table y(c int, d int);
7160 │ ─ 2. destination
7161 ‡
7162 7 │ when matched and a = c
7163 ╰╴ ─ 1. source
7164 ");
7165 }
7166
7167 #[test]
7168 fn goto_merge_into_table() {
7169 assert_snapshot!(goto("
7170create table x(a int, b int);
7171create table y(c int, d int);
7172merge into x$0
7173 using y
7174 on true
7175 when matched and a = c
7176 then do nothing;
7177"
7178 ), @r"
7179 ╭▸
7180 2 │ create table x(a int, b int);
7181 │ ─ 2. destination
7182 3 │ create table y(c int, d int);
7183 4 │ merge into x
7184 ╰╴ ─ 1. source
7185 ");
7186 }
7187
7188 #[test]
7189 fn goto_merge_using_clause_table() {
7190 assert_snapshot!(goto("
7191create table x(a int, b int);
7192create table y(c int, d int);
7193merge into x
7194 using y$0
7195 on true
7196 when matched and a = c
7197 then do nothing;
7198"
7199 ), @r"
7200 ╭▸
7201 3 │ create table y(c int, d int);
7202 │ ─ 2. destination
7203 4 │ merge into x
7204 5 │ using y
7205 ╰╴ ─ 1. source
7206 ");
7207 }
7208
7209 #[test]
7210 fn goto_merge_using_clause_alias() {
7211 assert_snapshot!(goto("
7212create table x(a int, b int);
7213create table y(c int, d int);
7214merge into x as g
7215 using y as k
7216 on true
7217 when matched and a = k.c$0
7218 then do nothing;
7219"
7220 ), @r"
7221 ╭▸
7222 3 │ create table y(c int, d int);
7223 │ ─ 2. destination
7224 ‡
7225 7 │ when matched and a = k.c
7226 ╰╴ ─ 1. source
7227 ");
7228 }
7229
7230 #[test]
7231 fn goto_merge_on_clause_unqualified_source_column() {
7232 assert_snapshot!(goto("
7233create table x(a int, b int);
7234create table y(c int, d int);
7235merge into x as g
7236 using y as k
7237 on g.a = c$0 and a = c
7238 when matched and g.a = k.c
7239 then do nothing;
7240"
7241 ), @r"
7242 ╭▸
7243 3 │ create table y(c int, d int);
7244 │ ─ 2. destination
7245 ‡
7246 6 │ on g.a = c and a = c
7247 ╰╴ ─ 1. source
7248 ");
7249 }
7250
7251 #[test]
7252 fn goto_merge_returning_old_table() {
7253 assert_snapshot!(goto("
7254create table x(a int, b int);
7255create table y(c int, d int);
7256merge into x as g
7257 using y as k
7258 on g.a = c and a = k.c
7259 when matched and g.a = k.c
7260 then do nothing
7261 returning old$0.a, new.a;
7262"
7263 ), @r"
7264 ╭▸
7265 2 │ create table x(a int, b int);
7266 │ ─ 2. destination
7267 ‡
7268 9 │ returning old.a, new.a;
7269 ╰╴ ─ 1. source
7270 ");
7271 }
7272
7273 #[test]
7274 fn goto_merge_returning_old_column() {
7275 assert_snapshot!(goto("
7276create table x(a int, b int);
7277create table y(c int, d int);
7278merge into x as g
7279 using y as k
7280 on g.a = c and a = k.c
7281 when matched and g.a = k.c
7282 then do nothing
7283 returning old.a$0, new.a;
7284"
7285 ), @r"
7286 ╭▸
7287 2 │ create table x(a int, b int);
7288 │ ─ 2. destination
7289 ‡
7290 9 │ returning old.a, new.a;
7291 ╰╴ ─ 1. source
7292 ");
7293 }
7294
7295 #[test]
7296 fn goto_merge_returning_new_table() {
7297 assert_snapshot!(goto("
7298create table x(a int, b int);
7299create table y(c int, d int);
7300merge into x as g
7301 using y as k
7302 on g.a = c and a = k.c
7303 when matched and g.a = k.c
7304 then do nothing
7305 returning old.a, new$0.a;
7306"
7307 ), @r"
7308 ╭▸
7309 2 │ create table x(a int, b int);
7310 │ ─ 2. destination
7311 ‡
7312 9 │ returning old.a, new.a;
7313 ╰╴ ─ 1. source
7314 ");
7315 }
7316
7317 #[test]
7318 fn goto_merge_returning_new_column() {
7319 assert_snapshot!(goto("
7320create table x(a int, b int);
7321create table y(c int, d int);
7322merge into x as g
7323 using y as k
7324 on g.a = c and a = k.c
7325 when matched and g.a = k.c
7326 then do nothing
7327 returning old.a, new.a$0;
7328"
7329 ), @r"
7330 ╭▸
7331 2 │ create table x(a int, b int);
7332 │ ─ 2. destination
7333 ‡
7334 9 │ returning old.a, new.a;
7335 ╰╴ ─ 1. source
7336 ");
7337 }
7338
7339 #[test]
7340 fn goto_merge_with_tables_named_old_new_old_table() {
7341 assert_snapshot!(goto("
7342create table old(a int, b int);
7343create table new(c int, d int);
7344merge into old
7345 using new
7346 on true
7347 when matched
7348 then do nothing
7349 returning old$0.a, new.d;
7350"
7351 ), @r"
7352 ╭▸
7353 2 │ create table old(a int, b int);
7354 │ ─── 2. destination
7355 ‡
7356 9 │ returning old.a, new.d;
7357 ╰╴ ─ 1. source
7358 ");
7359 }
7360
7361 #[test]
7362 fn goto_merge_with_tables_named_old_new_old_column() {
7363 assert_snapshot!(goto("
7364create table old(a int, b int);
7365create table new(c int, d int);
7366merge into old
7367 using new
7368 on true
7369 when matched
7370 then do nothing
7371 returning old.a$0, new.d;
7372"
7373 ), @r"
7374 ╭▸
7375 2 │ create table old(a int, b int);
7376 │ ─ 2. destination
7377 ‡
7378 9 │ returning old.a, new.d;
7379 ╰╴ ─ 1. source
7380 ");
7381 }
7382
7383 #[test]
7384 fn goto_merge_with_tables_named_old_new_new_table() {
7385 assert_snapshot!(goto("
7386create table old(a int, b int);
7387create table new(c int, d int);
7388merge into old
7389 using new
7390 on true
7391 when matched
7392 then do nothing
7393 returning old.a, new$0.d;
7394"
7395 ), @r"
7396 ╭▸
7397 3 │ create table new(c int, d int);
7398 │ ─── 2. destination
7399 ‡
7400 9 │ returning old.a, new.d;
7401 ╰╴ ─ 1. source
7402 ");
7403 }
7404
7405 #[test]
7406 fn goto_merge_with_tables_named_old_new_new_column() {
7407 assert_snapshot!(goto("
7408create table old(a int, b int);
7409create table new(c int, d int);
7410merge into old
7411 using new
7412 on true
7413 when matched
7414 then do nothing
7415 returning old.a, new.d$0;
7416"
7417 ), @r"
7418 ╭▸
7419 3 │ create table new(c int, d int);
7420 │ ─ 2. destination
7421 ‡
7422 9 │ returning old.a, new.d;
7423 ╰╴ ─ 1. source
7424 ");
7425 }
7426
7427 #[test]
7428 fn goto_merge_returning_with_aliases_before_table() {
7429 assert_snapshot!(goto("
7430create table x(a int, b int);
7431create table y(c int, d int);
7432merge into x
7433 using y on true
7434 when matched then do nothing
7435 returning
7436 with (old as before, new as after)
7437 before$0.a, after.a;
7438"
7439 ), @r"
7440 ╭▸
7441 8 │ with (old as before, new as after)
7442 │ ────── 2. destination
7443 9 │ before.a, after.a;
7444 ╰╴ ─ 1. source
7445 ");
7446 }
7447
7448 #[test]
7449 fn goto_merge_returning_with_aliases_before_column() {
7450 assert_snapshot!(goto("
7451create table x(a int, b int);
7452create table y(c int, d int);
7453merge into x
7454 using y on true
7455 when matched then do nothing
7456 returning
7457 with (old as before, new as after)
7458 before.a$0, after.a;
7459"
7460 ), @r"
7461 ╭▸
7462 2 │ create table x(a int, b int);
7463 │ ─ 2. destination
7464 ‡
7465 9 │ before.a, after.a;
7466 ╰╴ ─ 1. source
7467 ");
7468 }
7469
7470 #[test]
7471 fn goto_merge_returning_with_aliases_after_table() {
7472 assert_snapshot!(goto("
7473create table x(a int, b int);
7474create table y(c int, d int);
7475merge into x
7476 using y on true
7477 when matched then do nothing
7478 returning
7479 with (old as before, new as after)
7480 before.a, after$0.a;
7481"
7482 ), @r"
7483 ╭▸
7484 8 │ with (old as before, new as after)
7485 │ ───── 2. destination
7486 9 │ before.a, after.a;
7487 ╰╴ ─ 1. source
7488 ");
7489 }
7490
7491 #[test]
7492 fn goto_merge_returning_with_aliases_after_column() {
7493 assert_snapshot!(goto("
7494create table x(a int, b int);
7495create table y(c int, d int);
7496merge into x
7497 using y on true
7498 when matched then do nothing
7499 returning
7500 with (old as before, new as after)
7501 before.a, after.a$0;
7502"
7503 ), @r"
7504 ╭▸
7505 2 │ create table x(a int, b int);
7506 │ ─ 2. destination
7507 ‡
7508 9 │ before.a, after.a;
7509 ╰╴ ─ 1. source
7510 ");
7511 }
7512
7513 #[test]
7514 fn goto_merge_when_not_matched_insert_values_qualified_column() {
7515 assert_snapshot!(goto("
7516create table inventory (
7517 product_id int,
7518 quantity int,
7519 updated_at timestamp
7520);
7521create table orders (
7522 id int,
7523 product_id int,
7524 qty int
7525);
7526merge into inventory as t
7527using orders as o
7528 on t.product_id = o.product_id
7529when matched then
7530 do nothing
7531when not matched then
7532 insert values (o$0.product_id, o.qty, now());
7533"
7534 ), @r"
7535 ╭▸
7536 13 │ using orders as o
7537 │ ─ 2. destination
7538 ‡
7539 18 │ insert values (o.product_id, o.qty, now());
7540 ╰╴ ─ 1. source
7541 ");
7542 }
7543
7544 #[test]
7545 fn goto_merge_when_not_matched_insert_values_qualified_column_field() {
7546 assert_snapshot!(goto("
7547create table inventory (
7548 product_id int,
7549 quantity int,
7550 updated_at timestamp
7551);
7552create table orders (
7553 id int,
7554 product_id int,
7555 qty int
7556);
7557merge into inventory as t
7558using orders as o
7559 on t.product_id = o.product_id
7560when matched then
7561 do nothing
7562when not matched then
7563 insert values (o.product_id$0, o.qty, now());
7564"
7565 ), @r"
7566 ╭▸
7567 9 │ product_id int,
7568 │ ────────── 2. destination
7569 ‡
7570 18 │ insert values (o.product_id, o.qty, now());
7571 ╰╴ ─ 1. source
7572 ");
7573 }
7574
7575 #[test]
7576 fn goto_merge_when_not_matched_insert_values_unqualified_column() {
7577 assert_snapshot!(goto("
7578create table inventory (
7579 product_id int,
7580 quantity int
7581);
7582create table orders (
7583 product_id int,
7584 qty int
7585);
7586merge into inventory as t
7587using orders as o
7588 on t.product_id = o.product_id
7589when not matched then
7590 insert values (product_id$0, qty);
7591"
7592 ), @r"
7593 ╭▸
7594 7 │ product_id int,
7595 │ ────────── 2. destination
7596 ‡
7597 14 │ insert values (product_id, qty);
7598 ╰╴ ─ 1. source
7599 ");
7600 }
7601
7602 #[test]
7603 fn goto_insert_returning_old_table() {
7604 assert_snapshot!(goto("
7605create table t(a int, b int);
7606insert into t values (1, 2), (3, 4)
7607returning old$0.a, new.b;
7608"
7609 ), @r"
7610 ╭▸
7611 2 │ create table t(a int, b int);
7612 │ ─ 2. destination
7613 3 │ insert into t values (1, 2), (3, 4)
7614 4 │ returning old.a, new.b;
7615 ╰╴ ─ 1. source
7616 ");
7617 }
7618
7619 #[test]
7620 fn goto_insert_returning_old_column() {
7621 assert_snapshot!(goto("
7622create table t(a int, b int);
7623insert into t values (1, 2), (3, 4)
7624returning old.a$0, new.b;
7625"
7626 ), @r"
7627 ╭▸
7628 2 │ create table t(a int, b int);
7629 │ ─ 2. destination
7630 3 │ insert into t values (1, 2), (3, 4)
7631 4 │ returning old.a, new.b;
7632 ╰╴ ─ 1. source
7633 ");
7634 }
7635
7636 #[test]
7637 fn goto_insert_returning_new_table() {
7638 assert_snapshot!(goto("
7639create table t(a int, b int);
7640insert into t values (1, 2), (3, 4)
7641returning old.a, new$0.b;
7642"
7643 ), @r"
7644 ╭▸
7645 2 │ create table t(a int, b int);
7646 │ ─ 2. destination
7647 3 │ insert into t values (1, 2), (3, 4)
7648 4 │ returning old.a, new.b;
7649 ╰╴ ─ 1. source
7650 ");
7651 }
7652
7653 #[test]
7654 fn goto_insert_returning_new_column() {
7655 assert_snapshot!(goto("
7656create table t(a int, b int);
7657insert into t values (1, 2), (3, 4)
7658returning old.a, new.b$0;
7659"
7660 ), @r"
7661 ╭▸
7662 2 │ create table t(a int, b int);
7663 │ ─ 2. destination
7664 3 │ insert into t values (1, 2), (3, 4)
7665 4 │ returning old.a, new.b;
7666 ╰╴ ─ 1. source
7667 ");
7668 }
7669
7670 #[test]
7671 fn goto_update_returning_old_table() {
7672 assert_snapshot!(goto("
7673create table t(a int, b int);
7674update t set a = 42
7675returning old$0.a, new.b;
7676"
7677 ), @r"
7678 ╭▸
7679 2 │ create table t(a int, b int);
7680 │ ─ 2. destination
7681 3 │ update t set a = 42
7682 4 │ returning old.a, new.b;
7683 ╰╴ ─ 1. source
7684 ");
7685 }
7686
7687 #[test]
7688 fn goto_update_returning_old_column() {
7689 assert_snapshot!(goto("
7690create table t(a int, b int);
7691update t set a = 42
7692returning old.a$0, new.b;
7693"
7694 ), @r"
7695 ╭▸
7696 2 │ create table t(a int, b int);
7697 │ ─ 2. destination
7698 3 │ update t set a = 42
7699 4 │ returning old.a, new.b;
7700 ╰╴ ─ 1. source
7701 ");
7702 }
7703
7704 #[test]
7705 fn goto_update_returning_new_table() {
7706 assert_snapshot!(goto("
7707create table t(a int, b int);
7708update t set a = 42
7709returning old.a, new$0.b;
7710"
7711 ), @r"
7712 ╭▸
7713 2 │ create table t(a int, b int);
7714 │ ─ 2. destination
7715 3 │ update t set a = 42
7716 4 │ returning old.a, new.b;
7717 ╰╴ ─ 1. source
7718 ");
7719 }
7720
7721 #[test]
7722 fn goto_update_returning_new_column() {
7723 assert_snapshot!(goto("
7724create table t(a int, b int);
7725update t set a = 42
7726returning old.a, new.b$0;
7727"
7728 ), @r"
7729 ╭▸
7730 2 │ create table t(a int, b int);
7731 │ ─ 2. destination
7732 3 │ update t set a = 42
7733 4 │ returning old.a, new.b;
7734 ╰╴ ─ 1. source
7735 ");
7736 }
7737
7738 #[test]
7739 fn goto_delete_returning_old_table() {
7740 assert_snapshot!(goto("
7741create table t(a int, b int);
7742delete from t
7743returning old$0.a, new.b;
7744"
7745 ), @r"
7746 ╭▸
7747 2 │ create table t(a int, b int);
7748 │ ─ 2. destination
7749 3 │ delete from t
7750 4 │ returning old.a, new.b;
7751 ╰╴ ─ 1. source
7752 ");
7753 }
7754
7755 #[test]
7756 fn goto_delete_returning_old_column() {
7757 assert_snapshot!(goto("
7758create table t(a int, b int);
7759delete from t
7760returning old.a$0, new.b;
7761"
7762 ), @r"
7763 ╭▸
7764 2 │ create table t(a int, b int);
7765 │ ─ 2. destination
7766 3 │ delete from t
7767 4 │ returning old.a, new.b;
7768 ╰╴ ─ 1. source
7769 ");
7770 }
7771
7772 #[test]
7773 fn goto_delete_returning_new_table() {
7774 assert_snapshot!(goto("
7775create table t(a int, b int);
7776delete from t
7777returning old.a, new$0.b;
7778"
7779 ), @r"
7780 ╭▸
7781 2 │ create table t(a int, b int);
7782 │ ─ 2. destination
7783 3 │ delete from t
7784 4 │ returning old.a, new.b;
7785 ╰╴ ─ 1. source
7786 ");
7787 }
7788
7789 #[test]
7790 fn goto_delete_returning_new_column() {
7791 assert_snapshot!(goto("
7792create table t(a int, b int);
7793delete from t
7794returning old.a, new.b$0;
7795"
7796 ), @r"
7797 ╭▸
7798 2 │ create table t(a int, b int);
7799 │ ─ 2. destination
7800 3 │ delete from t
7801 4 │ returning old.a, new.b;
7802 ╰╴ ─ 1. source
7803 ");
7804 }
7805
7806 #[test]
7807 fn goto_insert_as_old_alias() {
7808 assert_snapshot!(goto("
7809create table t(a int, b int);
7810insert into t as old values (1, 2)
7811returning old$0.a, new.a;
7812"
7813 ), @r"
7814 ╭▸
7815 3 │ insert into t as old values (1, 2)
7816 │ ─── 2. destination
7817 4 │ returning old.a, new.a;
7818 ╰╴ ─ 1. source
7819 ");
7820 }
7821
7822 #[test]
7823 fn goto_delete_as_old_alias() {
7824 assert_snapshot!(goto("
7825create table t(a int, b int);
7826delete from t as old
7827returning old$0.a, new.a;
7828"
7829 ), @r"
7830 ╭▸
7831 3 │ delete from t as old
7832 │ ─── 2. destination
7833 4 │ returning old.a, new.a;
7834 ╰╴ ─ 1. source
7835 ");
7836 }
7837
7838 #[test]
7839 fn goto_update_as_old_alias() {
7840 assert_snapshot!(goto("
7841create table t(a int, b int);
7842update t as old set a = 42
7843returning old$0.a, new.a;
7844"
7845 ), @r"
7846 ╭▸
7847 3 │ update t as old set a = 42
7848 │ ─── 2. destination
7849 4 │ returning old.a, new.a;
7850 ╰╴ ─ 1. source
7851 ");
7852 }
7853
7854 #[test]
7855 fn goto_merge_returning_cte_column_unqualified() {
7856 assert_snapshot!(goto("
7857create table t(a int, b int);
7858with u(x, y) as (
7859 select 1, 2
7860)
7861merge into t
7862 using u on true
7863when matched then
7864 do nothing
7865when not matched then
7866 do nothing
7867returning x$0, u.y;
7868"
7869 ), @r"
7870 ╭▸
7871 3 │ with u(x, y) as (
7872 │ ─ 2. destination
7873 ‡
7874 12 │ returning x, u.y;
7875 ╰╴ ─ 1. source
7876 ");
7877 }
7878
7879 #[test]
7880 fn goto_merge_returning_cte_column_qualified_table() {
7881 assert_snapshot!(goto("
7882create table t(a int, b int);
7883with u(x, y) as (
7884 select 1, 2
7885)
7886merge into t
7887 using u on true
7888when matched then
7889 do nothing
7890when not matched then
7891 do nothing
7892returning x, u$0.y;
7893"
7894 ), @r"
7895 ╭▸
7896 3 │ with u(x, y) as (
7897 │ ─ 2. destination
7898 ‡
7899 12 │ returning x, u.y;
7900 ╰╴ ─ 1. source
7901 ");
7902 }
7903
7904 #[test]
7905 fn goto_merge_returning_cte_column_qualified_column() {
7906 assert_snapshot!(goto("
7907create table t(a int, b int);
7908with u(x, y) as (
7909 select 1, 2
7910)
7911merge into t
7912 using u on true
7913when matched then
7914 do nothing
7915when not matched then
7916 do nothing
7917returning x, u.y$0;
7918"
7919 ), @r"
7920 ╭▸
7921 3 │ with u(x, y) as (
7922 │ ─ 2. destination
7923 ‡
7924 12 │ returning x, u.y;
7925 ╰╴ ─ 1. source
7926 ");
7927 }
7928
7929 #[test]
7930 fn goto_overlay_with_cte_column() {
7931 assert_snapshot!(goto("
7932with t as (
7933 select '1' a, '2' b, 3 start
7934)
7935select overlay(a placing b$0 from start) from t;
7936 "), @r"
7937 ╭▸
7938 3 │ select '1' a, '2' b, 3 start
7939 │ ─ 2. destination
7940 4 │ )
7941 5 │ select overlay(a placing b from start) from t;
7942 ╰╴ ─ 1. source
7943 ");
7944 }
7945
7946 #[test]
7947 fn goto_overlay_with_cte_column_first_arg() {
7948 assert_snapshot!(goto("
7949with t as (
7950 select '1' a, '2' b, 3 start
7951)
7952select overlay(a$0 placing b from start) from t;
7953 "), @r"
7954 ╭▸
7955 3 │ select '1' a, '2' b, 3 start
7956 │ ─ 2. destination
7957 4 │ )
7958 5 │ select overlay(a placing b from start) from t;
7959 ╰╴ ─ 1. source
7960 ");
7961 }
7962
7963 #[test]
7964 fn goto_overlay_with_cte_column_from_arg() {
7965 assert_snapshot!(goto("
7966with t as (
7967 select '1' a, '2' b, 3 start
7968)
7969select overlay(a placing b from start$0) from t;
7970 "), @r"
7971 ╭▸
7972 3 │ select '1' a, '2' b, 3 start
7973 │ ───── 2. destination
7974 4 │ )
7975 5 │ select overlay(a placing b from start) from t;
7976 ╰╴ ─ 1. source
7977 ");
7978 }
7979
7980 #[test]
7981 fn goto_named_arg_to_param() {
7982 assert_snapshot!(goto("
7983create function foo(bar_param int) returns int as 'select 1' language sql;
7984select foo(bar_param$0 := 5);
7985"), @r"
7986 ╭▸
7987 2 │ create function foo(bar_param int) returns int as 'select 1' language sql;
7988 │ ───────── 2. destination
7989 3 │ select foo(bar_param := 5);
7990 ╰╴ ─ 1. source
7991 ");
7992 }
7993
7994 #[test]
7995 fn goto_named_arg_schema_qualified() {
7996 assert_snapshot!(goto("
7997create schema s;
7998create function s.foo(my_param int) returns int as 'select 1' language sql;
7999select s.foo(my_param$0 := 10);
8000"), @r"
8001 ╭▸
8002 3 │ create function s.foo(my_param int) returns int as 'select 1' language sql;
8003 │ ──────── 2. destination
8004 4 │ select s.foo(my_param := 10);
8005 ╰╴ ─ 1. source
8006 ");
8007 }
8008
8009 #[test]
8010 fn goto_named_arg_multiple_params() {
8011 assert_snapshot!(goto("
8012create function foo(a int, b int, c int) returns int as 'select 1' language sql;
8013select foo(b$0 := 2, a := 1);
8014"), @r"
8015 ╭▸
8016 2 │ create function foo(a int, b int, c int) returns int as 'select 1' language sql;
8017 │ ─ 2. destination
8018 3 │ select foo(b := 2, a := 1);
8019 ╰╴ ─ 1. source
8020 ");
8021 }
8022
8023 #[test]
8024 fn goto_named_arg_procedure() {
8025 assert_snapshot!(goto("
8026create procedure proc(param_x int) as 'select 1' language sql;
8027call proc(param_x$0 := 42);
8028"), @r"
8029 ╭▸
8030 2 │ create procedure proc(param_x int) as 'select 1' language sql;
8031 │ ─────── 2. destination
8032 3 │ call proc(param_x := 42);
8033 ╰╴ ─ 1. source
8034 ");
8035 }
8036
8037 #[test]
8038 fn goto_named_arg_not_found_unnamed_param() {
8039 goto_not_found(
8040 "
8041create function foo(int) returns int as 'select 1' language sql;
8042select foo(bar$0 := 5);
8043",
8044 );
8045 }
8046
8047 #[test]
8048 fn goto_named_arg_not_found_wrong_name() {
8049 goto_not_found(
8050 "
8051create function foo(correct_param int) returns int as 'select 1' language sql;
8052select foo(wrong_param$0 := 5);
8053",
8054 );
8055 }
8056
8057 #[test]
8058 fn goto_operator_function_ref() {
8059 assert_snapshot!(goto("
8060create function pg_catalog.tsvector_concat(tsvector, tsvector) returns tsvector language internal;
8061create operator pg_catalog.|| (leftarg = tsvector, rightarg = tsvector, function = pg_catalog.tsvector_concat$0);
8062"), @r"
8063 ╭▸
8064 2 │ create function pg_catalog.tsvector_concat(tsvector, tsvector) returns tsvector language internal;
8065 │ ─────────────── 2. destination
8066 3 │ create operator pg_catalog.|| (leftarg = tsvector, rightarg = tsvector, function = pg_catalog.tsvector_concat);
8067 ╰╴ ─ 1. source
8068 ");
8069 }
8070
8071 #[test]
8072 fn goto_operator_procedure_ref() {
8073 assert_snapshot!(goto("
8074create function f(int, int) returns int language internal;
8075create operator ||| (leftarg = int, rightarg = int, procedure = f$0);
8076"), @r"
8077 ╭▸
8078 2 │ create function f(int, int) returns int language internal;
8079 │ ─ 2. destination
8080 3 │ create operator ||| (leftarg = int, rightarg = int, procedure = f);
8081 ╰╴ ─ 1. source
8082 ");
8083 }
8084}