Skip to content

Commit 9c92005

Browse files
authored
linter: fix autofix for int & smallint with array types (#710)
1 parent 4cff2c0 commit 9c92005

File tree

6 files changed

+70
-4
lines changed

6 files changed

+70
-4
lines changed

PLAN.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,28 @@ sum(expression interval) -> interval
854854
sum(expression numeric) -> numeric
855855
```
856856

857+
unnset
858+
859+
```sql
860+
select * from unnest(ARRAY[1,2], ARRAY['foo','bar','baz']);
861+
-- ^$ hover
862+
```
863+
864+
```sql
865+
-- Expands multiple arrays (possibly of different data types) into a set of rows. If the arrays are not all the same length then the shorter ones are padded with NULLs. This form is only allowed in a query's FROM clause;
866+
unnest(anyarray, anyarray [, ... ] ) -> setof anyelement, anyelement [, ... ]
867+
```
868+
869+
```sql
870+
select * from unnest(ARRAY[1,2]);
871+
-- ^$ hover
872+
```
873+
874+
```sql
875+
-- Expands an array into a set of rows. The array's elements are read out in storage order.
876+
unnest(anyarray) -> setof anyelement
877+
```
878+
857879
#### Column Number
858880

859881
another example:
@@ -872,6 +894,8 @@ type: string
872894

873895
#### Star
874896

897+
case expr
898+
875899
```sql
876900
select * from (select case
877901
-- ^$ hover
@@ -886,6 +910,17 @@ select * from (select case
886910
("case": boolean)
887911
```
888912

913+
unnest
914+
915+
```sql
916+
select * from unnest(ARRAY[1,2], ARRAY['foo','bar','baz']);
917+
-- ^$ hover
918+
```
919+
920+
```
921+
("unnest": integer, "unnest": text)
922+
```
923+
889924
### Semantic Syntax Highlighting
890925

891926
https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide

crates/squawk_linter/src/rules/prefer_bigint_over_int.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn int_to_bigint_replacement(int_type: &str) -> &'static str {
3333
fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {
3434
let type_name = ty.syntax().first_token()?;
3535
let i64 = int_to_bigint_replacement(type_name.text());
36-
let edit = Edit::replace(ty.syntax().text_range(), i64);
36+
let edit = Edit::replace(type_name.text_range(), i64);
3737
Some(Fix::new(
3838
format!("Replace with a 64-bit integer type: `{i64}`"),
3939
vec![edit],
@@ -105,6 +105,15 @@ mod test {
105105
assert_snapshot!(fix("create table users (id serial primary key, score int not null);"), @"create table users (id bigserial primary key, score bigint not null);");
106106
}
107107

108+
#[test]
109+
fn fix_array_types() {
110+
assert_snapshot!(fix("create table users (ids int[]);"), @"create table users (ids bigint[]);");
111+
assert_snapshot!(fix("create table users (ids integer[]);"), @"create table users (ids bigint[]);");
112+
assert_snapshot!(fix("create table users (ids int4[]);"), @"create table users (ids int8[]);");
113+
assert_snapshot!(fix("create table users (ids serial[]);"), @"create table users (ids bigserial[]);");
114+
assert_snapshot!(fix("create table users (ids serial4[]);"), @"create table users (ids serial8[]);");
115+
}
116+
108117
#[test]
109118
fn err() {
110119
let sql = r#"

crates/squawk_linter/src/rules/prefer_bigint_over_smallint.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn smallint_to_bigint(smallint_type: &str) -> &'static str {
3232
fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {
3333
let type_name = ty.syntax().first_token()?;
3434
let i64 = smallint_to_bigint(type_name.text());
35-
let edit = Edit::replace(ty.syntax().text_range(), i64);
35+
let edit = Edit::replace(type_name.text_range(), i64);
3636
Some(Fix::new(
3737
format!("Replace with a 64-bit integer type: `{i64}`"),
3838
vec![edit],
@@ -101,6 +101,14 @@ mod test {
101101
assert_snapshot!(fix("create table users (id smallserial primary key, score smallint not null);"), @"create table users (id bigserial primary key, score bigint not null);");
102102
}
103103

104+
#[test]
105+
fn fix_array_types() {
106+
assert_snapshot!(fix("create table users (ids smallint[]);"), @"create table users (ids bigint[]);");
107+
assert_snapshot!(fix("create table users (ids int2[]);"), @"create table users (ids int8[]);");
108+
assert_snapshot!(fix("create table users (ids smallserial[]);"), @"create table users (ids bigserial[]);");
109+
assert_snapshot!(fix("create table users (ids serial2[]);"), @"create table users (ids serial8[]);");
110+
}
111+
104112
#[test]
105113
fn err() {
106114
let sql = r#"

crates/squawk_parser/src/grammar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2436,7 +2436,7 @@ fn expr_bp(p: &mut Parser<'_>, bp: u8, r: &Restrictions) -> Option<CompletedMark
24362436
let _ = expr_bp(p, op_bp, r);
24372437
lhs = m.complete(
24382438
p,
2439-
if matches!(op, COLON_COLON | AS_KW) {
2439+
if matches!(op, COLON_COLON) {
24402440
CAST_EXPR
24412441
} else if matches!(op, FAT_ARROW | COLON_EQ) {
24422442
NAMED_ARG

crates/squawk_syntax/src/ast/generated/nodes.rs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/squawk_syntax/src/postgresql.ungram

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ CallExpr =
7070
Expr ArgList
7171

7272
CastExpr =
73-
Expr (ColonColon? | 'as') Type
73+
'cast' '(' Expr 'as' Type ')'
74+
| Expr ColonColon Type
75+
| Expr Type
7476

7577
ArrayExpr =
7678
'array' '[' (Expr (',' Expr)*) ']'

0 commit comments

Comments
 (0)