Skip to main content

squawk_ide/
goto_definition.rs

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