Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 110 additions & 11 deletions crates/squawk_ide/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ fn column_completions_from_clause(
}));
}
Some(resolve::TableSource::WithTable(with_table)) => {
let columns = resolve::collect_with_table_columns_with_types(&with_table);
let columns = resolve::collect_with_table_columns_with_types(
binder,
file.syntax(),
&with_table,
);
completions.extend(columns.into_iter().map(|(name, ty)| CompletionItem {
label: name.to_string(),
kind: CompletionItemKind::Column,
Expand Down Expand Up @@ -394,20 +398,42 @@ fn column_completions_from_clause(
}));
}
Some(resolve::TableSource::Alias(alias)) => {
if let Some(column_list) = alias.column_list() {
completions.extend(column_list.columns().filter_map(|column| {
let name = column.name()?;
Some(CompletionItem {
label: Name::from_node(&name).to_string(),
let alias_columns: Vec<Name> = alias
.column_list()
.into_iter()
.flat_map(|column_list| column_list.columns())
.filter_map(|column| column.name().map(|name| Name::from_node(&name)))
.collect();

let base_columns = alias_base_columns_with_types(binder, file, &alias);

for (idx, alias_column) in alias_columns.iter().enumerate() {
completions.push(CompletionItem {
label: alias_column.to_string(),
kind: CompletionItemKind::Column,
detail: base_columns.get(idx).and_then(|(_, ty)| ty.clone()),
insert_text: None,
insert_text_format: None,
trigger_completion_after_insert: false,
sort_text: Some(format!("{idx:04}")),
});
}

completions.extend(
base_columns
.into_iter()
.skip(alias_columns.len())
.enumerate()
.map(|(idx, (name, ty))| CompletionItem {
label: name.to_string(),
kind: CompletionItemKind::Column,
detail: None,
detail: ty,
insert_text: None,
insert_text_format: None,
trigger_completion_after_insert: false,
sort_text: None,
})
}));
}
sort_text: Some(format!("{:04}", idx + alias_columns.len())),
}),
);
}
Some(resolve::TableSource::ParenSelect(paren_select)) => {
let columns = resolve::collect_paren_select_columns_with_types(
Expand All @@ -431,6 +457,59 @@ fn column_completions_from_clause(
completions
}

fn alias_base_columns_with_types(
binder: &binder::Binder,
file: &ast::SourceFile,
alias: &ast::Alias,
) -> Vec<(Name, Option<String>)> {
let Some(from_item) = alias.syntax().ancestors().find_map(ast::FromItem::cast) else {
return vec![];
};
let Some(table_ptr) = resolve::table_ptr_from_from_item(binder, &from_item) else {
return vec![];
};

let table_node = table_ptr.to_node(file.syntax());

match resolve::find_table_source(&table_node) {
Some(resolve::TableSource::CreateTable(create_table)) => {
resolve::collect_table_columns(binder, file.syntax(), &create_table)
.into_iter()
.filter_map(|column| {
let name = column.name()?;
let detail = column.ty().map(|t| t.syntax().text().to_string());
Some((Name::from_node(&name), detail))
})
.collect()
}
Some(resolve::TableSource::WithTable(with_table)) => {
resolve::collect_with_table_columns_with_types(binder, file.syntax(), &with_table)
.into_iter()
.map(|(name, ty)| (name, ty.map(|t| t.to_string())))
.collect()
}
Some(resolve::TableSource::CreateView(create_view)) => {
resolve::collect_view_columns_with_types(&create_view)
.into_iter()
.map(|(name, ty)| (name, ty.map(|t| t.to_string())))
.collect()
}
Some(resolve::TableSource::CreateMaterializedView(create_materialized_view)) => {
resolve::collect_materialized_view_columns_with_types(&create_materialized_view)
.into_iter()
.map(|(name, ty)| (name, ty.map(|t| t.to_string())))
.collect()
}
Some(resolve::TableSource::ParenSelect(paren_select)) => {
resolve::collect_paren_select_columns_with_types(binder, file.syntax(), &paren_select)
.into_iter()
.map(|(name, ty)| (name, ty.map(|t| t.to_string())))
.collect()
}
Some(resolve::TableSource::Alias(_)) | None => vec![],
}
}

fn schema_completions(binder: &binder::Binder) -> Vec<CompletionItem> {
let builtin_schemas = [
"public",
Expand Down Expand Up @@ -1098,6 +1177,26 @@ select $0 from t;
");
}

#[test]
fn completion_after_select_with_cte_alias_column_list() {
assert_snapshot!(completions("
with t as (select 1 a, 2 b, 3 c)
select $0 from t as u(x, y);
"), @r"
label | kind | detail
--------------------+----------+---------
x | Column | integer
y | Column | integer
c | Column | integer
* | Operator |
public | Schema |
pg_catalog | Schema |
pg_temp | Schema |
pg_toast | Schema |
information_schema | Schema |
");
}

#[test]
fn completion_values_cte() {
assert_snapshot!(completions("
Expand Down
106 changes: 106 additions & 0 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,84 @@ select * from t, json_to_recordset(t.x$0) as r(a int, b int);
"#);
}

#[test]
fn goto_lateral_values_alias_in_subquery() {
assert_snapshot!(goto("
select u.n, x.val
from (values (1), (2)) u(n)
cross join lateral (select u$0.n * 10 as val) x;
"), @r"
╭▸
3 │ from (values (1), (2)) u(n)
│ ─ 2. destination
4 │ cross join lateral (select u.n * 10 as val) x;
╰╴ ─ 1. source
");
}

#[test]
fn goto_lateral_missing_not_found() {
// Query 1 ERROR at Line 3: : ERROR: invalid reference to FROM-clause entry for table "u"
// LINE 3: cross join (select u.n * 10 as val) x;
// ^
// DETAIL: There is an entry for table "u", but it cannot be referenced from this part of the query.
// HINT: To reference that table, you must mark this subquery with LATERAL.
goto_not_found(
"
select u.n, x.val
from (values (1), (2)) u(n)
cross join (select u$0.n * 10 as val) x;
",
);
}

#[test]
fn goto_lateral_deeply_nested_paren_expr_values_alias_in_subquery() {
assert_snapshot!(goto("
select u.n, x.val
from (values (1), (2)) u(n)
cross join lateral ((((select u$0.n * 10 as val)))) x;
"), @r"
╭▸
3 │ from (values (1), (2)) u(n)
│ ─ 2. destination
4 │ cross join lateral ((((select u.n * 10 as val)))) x;
╰╴ ─ 1. source
");
}

#[test]
fn goto_lateral_deeply_nested_paren_expr_values_alias_column() {
assert_snapshot!(goto("
select u.n, x.val$0
from (values (1), (2)) u(n)
cross join lateral ((((select u.n * 10 as val)))) x;
"), @r"
╭▸
2 │ select u.n, x.val
│ ─ 1. source
3 │ from (values (1), (2)) u(n)
4 │ cross join lateral ((((select u.n * 10 as val)))) x;
╰╴ ─── 2. destination
");
}

#[test]
fn goto_lateral_deeply_nested_paren_expr_missing_not_found() {
// Query 1 ERROR at Line 3: : ERROR: invalid reference to FROM-clause entry for table "u"
// LINE 3: cross join ((((select u.n * 10 as val)))) x;
// ^
// DETAIL: There is an entry for table "u", but it cannot be referenced from this part of the query.
// HINT: To reference that table, you must mark this subquery with LATERAL.
goto_not_found(
"
select u.n, x.val
from (values (1), (2)) u(n)
cross join ((((select u$0.n * 10 as val)))) x;
",
);
}

#[test]
fn goto_drop_sequence() {
assert_snapshot!(goto("
Expand Down Expand Up @@ -4822,6 +4900,20 @@ select b$0 from (values (1, 2)) t(a, b);
");
}

#[test]
fn goto_values_column_alias_list_nested_parens() {
assert_snapshot!(goto("
select n$0
from ((values (1), (2))) u(n);
"), @r"
╭▸
2 │ select n
│ ─ 1. source
3 │ from ((values (1), (2))) u(n);
╰╴ ─ 2. destination
");
}

#[test]
fn goto_table_expr_column() {
assert_snapshot!(goto("
Expand Down Expand Up @@ -4850,6 +4942,20 @@ select a$0 from (table x);
");
}

#[test]
fn goto_table_expr_cte_table() {
assert_snapshot!(goto("
with t as (select 1 a, 2 b)
select * from (table t$0);
"), @r"
╭▸
2 │ with t as (select 1 a, 2 b)
│ ─ 2. destination
3 │ select * from (table t);
╰╴ ─ 1. source
");
}

#[test]
fn goto_table_expr_partial_column_alias_list() {
assert_snapshot!(goto("
Expand Down
Loading
Loading