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
4 changes: 3 additions & 1 deletion crates/squawk_fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ fn build_expr<'a>(expr: ast::Expr) -> Doc<'a> {
doc = doc.append(Doc::space()).append(Doc::text("not"));
}
doc = doc.append(Doc::space()).append(Doc::text("between"));
if between_expr.symmetric_token().is_some() {
if between_expr.asymmetric_token().is_some() {
doc = doc.append(Doc::space()).append(Doc::text("asymmetric"));
} else if between_expr.symmetric_token().is_some() {
doc = doc.append(Doc::space()).append(Doc::text("symmetric"));
}
doc.append(Doc::space())
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_fmt/tests/after/select_expr.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ select
-- between expr
2 between 1 and 3,
2 not between 1 and 3,
2 between asymmetric 1 and 3,
2 between symmetric 1 and 3,
-- bin expr
1 + 1,
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_fmt/tests/before/select_expr.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ select
-- between expr
2 between 1 and 3,
2 not between 1 and 3,
2 between asymmetric 1 and 3,
2 between symmetric 1 and 3,
-- bin expr
1 + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(super) fn rewrite_create_table_as_as_select_into(
}

// TODO: support more
let ast::SelectVariant::Select(select) = create_table_as.query()? else {
let ast::SelectVariant::Select(select) = create_table_as.query()?.select_variant()? else {
return None;
};

Expand Down
34 changes: 24 additions & 10 deletions crates/squawk_ide/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ fn resolved_to_column_ptrs(
columns_from_create_table_impl(db, file, &parent_table, &mut cols, depth);
cols
}
ResolvedTableName::TableAs(create_table_as) => {
select_columns_with_types(db, file, &create_table_as.query())
.into_iter()
.map(|(name, _ty)| (name, None))
.collect()
}
ResolvedTableName::TableAs(create_table_as) => select_columns_with_types(
db,
file,
&create_table_as
.query()
.and_then(|query| query.select_variant()),
)
.into_iter()
.map(|(name, _ty)| (name, None))
.collect(),
ResolvedTableName::SelectInto(select_into) => {
select_into_columns_with_types(db, file, &select_into)
.into_iter()
Expand Down Expand Up @@ -194,9 +198,13 @@ fn resolved_to_columns_with_types(
ResolvedTableName::Table(parent_table) => {
table_columns_impl(db, file, &parent_table, depth)
}
ResolvedTableName::TableAs(create_table_as) => {
select_columns_with_types(db, file, &create_table_as.query())
}
ResolvedTableName::TableAs(create_table_as) => select_columns_with_types(
db,
file,
&create_table_as
.query()
.and_then(|query| query.select_variant()),
),
ResolvedTableName::SelectInto(select_into) => {
select_into_columns_with_types(db, file, &select_into)
}
Expand All @@ -212,7 +220,13 @@ pub(crate) fn create_table_as_columns_with_types(
create_table_as: &ast::CreateTableAs,
) -> Vec<(Name, Option<Type>)> {
for file in list_files(db, file) {
let columns = select_columns_with_types(db, file, &create_table_as.query());
let columns = select_columns_with_types(
db,
file,
&create_table_as
.query()
.and_then(|query| query.select_variant()),
);
if !columns.is_empty() {
return columns;
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/expand_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const DELIMITED_LIST_KINDS: &[SyntaxKind] = &[
SyntaxKind::PARTITION_ITEM_LIST,
SyntaxKind::PARTITION_LIST,
SyntaxKind::PATH_LIST,
SyntaxKind::REINDEX_OPTION_LIST,
SyntaxKind::RETURNING_OPTION_LIST,
SyntaxKind::REVOKE_COMMAND_LIST,
SyntaxKind::ROLE_REF_LIST,
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/folding_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
| SyntaxKind::PARTITION_ITEM_LIST
| SyntaxKind::PARTITION_LIST
| SyntaxKind::PATH_LIST
| SyntaxKind::REINDEX_OPTION_LIST
| SyntaxKind::RETURNING_OPTION_LIST
| SyntaxKind::REVOKE_COMMAND_LIST
| SyntaxKind::ROLE_OPTION_LIST
Expand Down
5 changes: 3 additions & 2 deletions crates/squawk_ide/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,7 @@ fn find_column_in_create_table_as_with_skip(
) -> Option<SmallVec<[Location; 1]>> {
let file = create_table_as.file_id;
let create_table_as = create_table_as.value;
let query = create_table_as.query()?;
let query = create_table_as.query()?.select_variant()?;

if let ast::SelectVariant::Values(values) = &query {
return resolve_values_column_after_index(file, values, column_name, skip_column_count);
Expand Down Expand Up @@ -2435,7 +2435,8 @@ fn count_columns_for_table_name(
.ancestors()
.find_map(ast::CreateTableAs::cast)
{
let select = ast_nav::select_from_variant(create_table_as.query()?)?;
let select =
ast_nav::select_from_variant(create_table_as.query()?.select_variant()?)?;
if let Some(target_list) = select.select_clause().and_then(|c| c.target_list()) {
return Some(target_list.targets().count());
}
Expand Down
11 changes: 10 additions & 1 deletion crates/squawk_linter/src/rules/require_concurrent_reindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ fn concurrently_fix(reindex: &ast::Reindex) -> Option<Fix> {
Some(Fix::new("Add `concurrently`", vec![edit]))
}

fn has_concurrently(reindex: &ast::Reindex) -> bool {
reindex.concurrently_token().is_some()
|| reindex.reindex_option_list().is_some_and(|option_list| {
option_list
.reindex_options()
.any(|option| option.concurrently_token().is_some())
})
}

pub(crate) fn require_concurrent_reindex(ctx: &mut Linter, parse: &Parse<SourceFile>) {
for stmt in parse.tree().stmts() {
if let ast::Stmt::Reindex(reindex) = stmt {
// REINDEX SYSTEM does not support CONCURRENTLY
if reindex.system_token().is_some() {
continue;
}
if reindex.concurrently_token().is_none() {
if !has_concurrently(&reindex) {
let fix = concurrently_fix(&reindex);
ctx.report(
Violation::for_node(
Expand Down
3 changes: 3 additions & 0 deletions crates/squawk_parser/src/generated/syntax_kind.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 64 additions & 34 deletions crates/squawk_parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ fn between_expr(p: &mut Parser<'_>) -> CompletedMarker {
let m = p.start();
p.eat(NOT_KW);
p.expect(BETWEEN_KW);
p.eat(SYMMETRIC_KW);
let _ = p.eat(SYMMETRIC_KW) || p.eat(ASYMMETRIC_KW);
b_expr(p);
p.expect(AND_KW);
b_expr(p);
Expand Down Expand Up @@ -8117,9 +8117,15 @@ fn path_name_ref_list(p: &mut Parser<'_>) {

fn path_list(p: &mut Parser<'_>) {
let m = p.start();
p.expect(L_PAREN);
path_name_ref_list(p);
p.expect(R_PAREN);
delimited(
p,
L_PAREN,
R_PAREN,
COMMA,
|| "unexpected comma".to_string(),
NAME_REF_FIRST,
|p| opt_path_name_ref(p).is_some(),
);
m.complete(p, PATH_LIST);
}

Expand Down Expand Up @@ -8951,33 +8957,39 @@ fn alter_vertex_edge_table(p: &mut Parser<'_>) {
}

fn drop_edge_tables(p: &mut Parser<'_>) {
assert!(p.at(DROP_KW));
assert!(p.at(DROP_KW) && p.nth_at_ts(1, EDGE));
let m = p.start();
p.bump(DROP_KW);
p.bump_any(); // EDGE/RELATIONSHIP
p.expect(TABLES_KW);
p.expect(L_PAREN);
path_name_ref(p);
while p.eat(COMMA) {
path_name_ref(p);
}
p.expect(R_PAREN);
delimited(
p,
L_PAREN,
R_PAREN,
COMMA,
|| "unexpected comma".to_string(),
NAME_REF_FIRST,
|p| opt_path_name_ref(p).is_some(),
);
opt_cascade_or_restrict(p);
m.complete(p, DROP_EDGE_TABLES);
}

fn drop_vertex_tables(p: &mut Parser<'_>) {
assert!(p.at(DROP_KW));
assert!(p.at(DROP_KW) && p.nth_at_ts(1, VERTEX));
let m = p.start();
p.bump(DROP_KW);
p.bump_any(); // VERTEX/NODE
p.expect(TABLES_KW);
p.expect(L_PAREN);
path_name_ref(p);
while p.eat(COMMA) {
path_name_ref(p);
}
p.expect(R_PAREN);
delimited(
p,
L_PAREN,
R_PAREN,
COMMA,
|| "unexpected comma".to_string(),
NAME_REF_FIRST,
|p| opt_path_name_ref(p).is_some(),
);
opt_cascade_or_restrict(p);
m.complete(p, DROP_VERTEX_TABLES);
}
Expand Down Expand Up @@ -10090,8 +10102,11 @@ fn opt_except_table_clause(p: &mut Parser<'_>) {
|| "unexpected comma".to_string(),
RELATION_NAME_FIRST.union(TokenSet::new(&[TABLE_KW])),
|p| {
let m = p.start();
p.eat(TABLE_KW);
opt_relation_name(p).is_some()
let result = opt_relation_name(p).is_some();
m.complete(p, EXCEPT_TABLE_NAME);
result
},
);
m.complete(p, EXCEPT_TABLE_CLAUSE);
Expand Down Expand Up @@ -10290,13 +10305,19 @@ fn create_statistics(p: &mut Parser<'_>) -> CompletedMarker {
}

fn opt_paren_name_ref_list(p: &mut Parser<'_>) -> bool {
if p.eat(L_PAREN) {
name_ref_list(p);
p.expect(R_PAREN);
true
} else {
false
if !p.at(L_PAREN) {
return false;
}
delimited(
p,
L_PAREN,
R_PAREN,
COMMA,
|| "unexpected comma".to_string(),
NAME_REF_FIRST,
|p| name_ref(p).is_some(),
);
true
}

// CREATE SUBSCRIPTION subscription_name
Expand Down Expand Up @@ -12324,7 +12345,8 @@ const REINDEX_OPTION_FIRST: TokenSet = TokenSet::new(&[CONCURRENTLY_KW, VERBOSE_
// TABLESPACE new_tablespace
// VERBOSE [ boolean ]
fn opt_reindex_option(p: &mut Parser<'_>) -> bool {
match p.current() {
let m = p.start();
let parsed = match p.current() {
CONCURRENTLY_KW | VERBOSE_KW => {
p.bump_any();
opt_bool_literal(p);
Expand All @@ -12336,7 +12358,13 @@ fn opt_reindex_option(p: &mut Parser<'_>) -> bool {
true
}
_ => false,
};
if parsed {
m.complete(p, REINDEX_OPTION);
} else {
m.abandon(p);
}
parsed
}

// REINDEX [ ( option [, ...] ) ] { INDEX | TABLE | SCHEMA } [ CONCURRENTLY ] name
Expand All @@ -12346,6 +12374,7 @@ fn reindex(p: &mut Parser<'_>) -> CompletedMarker {
let m = p.start();
p.bump(REINDEX_KW);
if p.at(L_PAREN) {
let options = p.start();
delimited(
p,
L_PAREN,
Expand All @@ -12355,6 +12384,7 @@ fn reindex(p: &mut Parser<'_>) -> CompletedMarker {
REINDEX_OPTION_FIRST,
opt_reindex_option,
);
options.complete(p, REINDEX_OPTION_LIST);
}
let name_required = match p.current() {
// { INDEX | TABLE | SCHEMA }
Expand Down Expand Up @@ -12689,38 +12719,38 @@ fn opt_direction(p: &mut Parser<'_>) -> bool {
RELATIVE_KW => {
let m = p.start();
p.bump(RELATIVE_KW);
if opt_numeric_literal(p).is_none() {
if b_expr(p).is_none() {
p.error("expected count")
}
m.complete(p, RELATIVE);
}
ABSOLUTE_KW => {
let m = p.start();
p.bump(ABSOLUTE_KW);
if opt_numeric_literal(p).is_none() {
if b_expr(p).is_none() {
p.error("expected count")
}
m.complete(p, ABSOLUTE);
}
FORWARD_KW => {
let m = p.start();
p.bump(FORWARD_KW);
if !p.eat(ALL_KW) {
let _ = opt_numeric_literal(p);
if !p.eat(ALL_KW) && p.at_ts(EXPR_FIRST) {
let _ = b_expr(p);
}
m.complete(p, FORWARD);
}
BACKWARD_KW => {
let m = p.start();
p.bump(BACKWARD_KW);
if !p.eat(ALL_KW) {
let _ = opt_numeric_literal(p);
if !p.eat(ALL_KW) && p.at_ts(EXPR_FIRST) {
let _ = b_expr(p);
}
m.complete(p, BACKWARD);
}
// count
_ if p.at_ts(NUMERIC_FIRST) => {
if opt_numeric_literal(p).is_none() {
_ if p.at_ts(NUMERIC_FIRST) || p.at(MINUS) || p.at(PLUS) => {
if b_expr(p).is_none() {
p.error("expected count")
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_parser/tests/data/ok/alter_publication.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ alter publication p owner to current_user;
-- rename
alter publication p rename to q;

alter publication p set all tables except (table t, u, table k);
3 changes: 3 additions & 0 deletions crates/squawk_parser/tests/data/ok/create_publication.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ create publication p

-- multiple tables
create publication pub for table chats, users;

create publication p for all tables except (table only t);
create publication p for all tables except (table t, u, table k);
2 changes: 1 addition & 1 deletion crates/squawk_parser/tests/data/ok/deallocate.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- pg_docs
deallocate prepare all;

deallocate all;

deallocate prepare foo;
deallocate foo;

Loading
Loading