Skip to main content

squawk_ide/
goto_definition.rs

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