squawk_ide/
goto_definition.rs

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