Skip to content

Commit 02317f6

Browse files
committed
ide: goto def nested table/values
1 parent 4083bc3 commit 02317f6

File tree

2 files changed

+190
-11
lines changed

2 files changed

+190
-11
lines changed

crates/squawk_ide/src/goto_definition.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3316,6 +3316,138 @@ select b$0 from x t(a, b);
33163316
");
33173317
}
33183318

3319+
#[test]
3320+
fn goto_values_column_alias_list() {
3321+
assert_snapshot!(goto("
3322+
select c$0 from (values (1)) t(c);
3323+
"), @r"
3324+
╭▸
3325+
2 │ select c from (values (1)) t(c);
3326+
╰╴ ─ 1. source ─ 2. destination
3327+
");
3328+
}
3329+
3330+
#[test]
3331+
fn goto_values_column_alias_list_qualified() {
3332+
assert_snapshot!(goto("
3333+
select t.c$0 from (values (1)) t(c);
3334+
"), @r"
3335+
╭▸
3336+
2 │ select t.c from (values (1)) t(c);
3337+
╰╴ ─ 1. source ─ 2. destination
3338+
");
3339+
}
3340+
3341+
#[test]
3342+
fn goto_values_column_alias_list_multiple() {
3343+
assert_snapshot!(goto("
3344+
select b$0 from (values (1, 2)) t(a, b);
3345+
"), @r"
3346+
╭▸
3347+
2 │ select b from (values (1, 2)) t(a, b);
3348+
╰╴ ─ 1. source ─ 2. destination
3349+
");
3350+
}
3351+
3352+
#[test]
3353+
fn goto_table_expr_column() {
3354+
assert_snapshot!(goto("
3355+
create table t(a int, b int);
3356+
select a$0 from (table t);
3357+
"), @r"
3358+
╭▸
3359+
2 │ create table t(a int, b int);
3360+
│ ─ 2. destination
3361+
3 │ select a from (table t);
3362+
╰╴ ─ 1. source
3363+
");
3364+
}
3365+
3366+
#[test]
3367+
fn goto_table_expr_column_with_cte() {
3368+
assert_snapshot!(goto("
3369+
with x as (select 1 a)
3370+
select a$0 from (table x);
3371+
"), @r"
3372+
╭▸
3373+
2 │ with x as (select 1 a)
3374+
│ ─ 2. destination
3375+
3 │ select a from (table x);
3376+
╰╴ ─ 1. source
3377+
");
3378+
}
3379+
3380+
#[test]
3381+
fn goto_table_expr_partial_column_alias_list() {
3382+
assert_snapshot!(goto("
3383+
with t as (select 1 a, 2 b)
3384+
select c, b$0 from (table t) u(c);
3385+
"), @r"
3386+
╭▸
3387+
2 │ with t as (select 1 a, 2 b)
3388+
│ ─ 2. destination
3389+
3 │ select c, b from (table t) u(c);
3390+
╰╴ ─ 1. source
3391+
");
3392+
}
3393+
3394+
#[test]
3395+
fn goto_subquery_partial_column_alias_list() {
3396+
assert_snapshot!(goto("
3397+
select x, b$0 from (select 1 a, 2 b) t(x);
3398+
"), @r"
3399+
╭▸
3400+
2 │ select x, b from (select 1 a, 2 b) t(x);
3401+
╰╴ ─ 1. source ─ 2. destination
3402+
");
3403+
}
3404+
3405+
#[test]
3406+
fn goto_table_expr_values_cte_partial_alias() {
3407+
assert_snapshot!(goto("
3408+
with t as (values (1, 2), (3, 4))
3409+
select column2$0 from (table t) u(a);
3410+
"), @r"
3411+
╭▸
3412+
2 │ with t as (values (1, 2), (3, 4))
3413+
│ ─ 2. destination
3414+
3 │ select column2 from (table t) u(a);
3415+
╰╴ ─ 1. source
3416+
");
3417+
}
3418+
3419+
#[test]
3420+
fn goto_cte_with_table_expr() {
3421+
assert_snapshot!(goto("
3422+
create table t(a int, b int);
3423+
with u as (table t)
3424+
select a$0 from u;
3425+
"), @r"
3426+
╭▸
3427+
2 │ create table t(a int, b int);
3428+
│ ─ 2. destination
3429+
3 │ with u as (table t)
3430+
4 │ select a from u;
3431+
╰╴ ─ 1. source
3432+
");
3433+
}
3434+
3435+
#[test]
3436+
fn goto_cte_with_table_expr_nested() {
3437+
assert_snapshot!(goto("
3438+
with t as (select 1 a, 2 b),
3439+
u as (table t)
3440+
select b$0 from u;
3441+
"), @r"
3442+
╭▸
3443+
2 │ with t as (select 1 a, 2 b),
3444+
│ ─ 2. destination
3445+
3 │ u as (table t)
3446+
4 │ select b from u;
3447+
╰╴ ─ 1. source
3448+
");
3449+
}
3450+
33193451
#[test]
33203452
fn goto_insert_table() {
33213453
assert_snapshot!(goto("

crates/squawk_ide/src/resolve.rs

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ fn resolve_select_qualified_column_ptr(
857857
&& Name::from_node(&alias_name) == column_table_name
858858
{
859859
if let Some(paren_select) = from_item.paren_select() {
860-
return resolve_subquery_column(
860+
return resolve_subquery_column_ptr(
861861
binder,
862862
root,
863863
&paren_select,
@@ -1031,7 +1031,7 @@ fn resolve_from_item_column_ptr(
10311031
let column_name = Name::from_node(column_name_ref);
10321032
if let Some(paren_select) = from_item.paren_select() {
10331033
let alias = from_item.alias();
1034-
return resolve_subquery_column(
1034+
return resolve_subquery_column_ptr(
10351035
binder,
10361036
root,
10371037
&paren_select,
@@ -1790,6 +1790,28 @@ fn resolve_cte_column(
17901790
continue;
17911791
}
17921792

1793+
if let ast::WithQuery::Table(table) = query {
1794+
let relation_name = table.relation_name()?;
1795+
let path = relation_name.path()?;
1796+
let (table_name, schema) = extract_table_schema_from_path(&path)?;
1797+
1798+
if schema.is_none()
1799+
&& let Some(nested_cte_column) =
1800+
resolve_cte_column(binder, root, name_ref, &table_name, column_name)
1801+
{
1802+
return Some(nested_cte_column);
1803+
}
1804+
1805+
return resolve_column_from_table_or_view(
1806+
binder,
1807+
root,
1808+
name_ref,
1809+
&table_name,
1810+
&schema,
1811+
column_name,
1812+
);
1813+
}
1814+
17931815
if let Some(column) =
17941816
column_in_with_query(&query, binder, root, column_name, column_list_len)
17951817
{
@@ -1927,7 +1949,7 @@ fn column_in_with_query(
19271949
None
19281950
}
19291951

1930-
fn resolve_subquery_column(
1952+
fn resolve_subquery_column_ptr(
19311953
binder: &Binder,
19321954
root: &SyntaxNode,
19331955
paren_select: &ast::ParenSelect,
@@ -1936,12 +1958,6 @@ fn resolve_subquery_column(
19361958
alias: Option<&ast::Alias>,
19371959
) -> Option<SyntaxNodePtr> {
19381960
let select_variant = paren_select.select()?;
1939-
let ast::SelectVariant::Select(subquery_select) = select_variant else {
1940-
return None;
1941-
};
1942-
1943-
let select_clause = subquery_select.select_clause()?;
1944-
let target_list = select_clause.target_list()?;
19451961

19461962
if let Some(alias) = alias
19471963
&& let Some(column_list) = alias.column_list()
@@ -1953,9 +1969,40 @@ fn resolve_subquery_column(
19531969
return Some(SyntaxNodePtr::new(col_name.syntax()));
19541970
}
19551971
}
1956-
return None;
1972+
if matches!(select_variant, ast::SelectVariant::Values(_)) {
1973+
return None;
1974+
}
1975+
}
1976+
1977+
if let ast::SelectVariant::Table(table) = select_variant {
1978+
let relation_name = table.relation_name()?;
1979+
let path = relation_name.path()?;
1980+
let (table_name, schema) = extract_table_schema_from_path(&path)?;
1981+
1982+
if schema.is_none()
1983+
&& let Some(cte_column_ptr) =
1984+
resolve_cte_column(binder, root, name_ref, &table_name, column_name)
1985+
{
1986+
return Some(cte_column_ptr);
1987+
}
1988+
1989+
return resolve_column_from_table_or_view(
1990+
binder,
1991+
root,
1992+
name_ref,
1993+
&table_name,
1994+
&schema,
1995+
column_name,
1996+
);
19571997
}
19581998

1999+
let ast::SelectVariant::Select(subquery_select) = select_variant else {
2000+
return None;
2001+
};
2002+
2003+
let select_clause = subquery_select.select_clause()?;
2004+
let target_list = select_clause.target_list()?;
2005+
19592006
for target in target_list.targets() {
19602007
if let Some((col_name, node)) = ColumnName::from_target(target.clone()) {
19612008
if let Some(col_name_str) = col_name.to_string()
@@ -2547,7 +2594,7 @@ fn resolve_column_from_paren_expr(
25472594
&& let Some(paren_select) = from_item.paren_select()
25482595
{
25492596
let alias = from_item.alias();
2550-
return resolve_subquery_column(
2597+
return resolve_subquery_column_ptr(
25512598
binder,
25522599
root,
25532600
&paren_select,

0 commit comments

Comments
 (0)