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