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
522 changes: 289 additions & 233 deletions crates/squawk_fmt/src/fmt.rs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions crates/squawk_fmt/tests/after/custom_operator.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/squawk_fmt/tests/tests.rs
input_file: crates/squawk_fmt/tests/before/custom_operator.sql
---
select 1 /*before*/ <<<< /*after*/ 1;
select ##### /*after*/ 1;
6 changes: 6 additions & 0 deletions crates/squawk_fmt/tests/after/select_expr.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ select
2 between symmetric 1 and 3,
-- bin expr
1 + 1,
1 /* before op */ + /* after op */ 1,
2 @@@ 2,
true and false,
ts at time zone 'UTC',
Expand All @@ -40,6 +41,7 @@ select
'foo' not similar to 'f%',
1 operator(+) 1,
1 operator(public.+) 1,
1 operator /* before paren */(/* before path */ public /* before dot */./* before op */ + /* after op */) /* after paren */ 1,
true or false,
(1, 2) overlaps (3, 4),
10 % 3,
Expand Down Expand Up @@ -88,6 +90,10 @@ select
x is not nfkd normalized,
-- prefix expr
@-@ 10,
operator(public.+) /* after op */ 1,
+1,
-1,
not true,
-- slice expr
c[:2][2:],
-- tuple expr
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_fmt/tests/before/custom_operator.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 1 /*before*/ <<<< /*after*/ 1;
select ##### /*after*/ 1;
10 changes: 8 additions & 2 deletions crates/squawk_fmt/tests/before/select_expr.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ select
2 between symmetric 1 and 3,
-- bin expr
1 + 1,
1 /* before op */ + /* after op */ 1,
2@@@2,
true and false,
ts at time zone 'UTC',
Expand All @@ -34,8 +35,9 @@ select
1 not in (1, 2),
'foo' not like 'f%',
'foo' not similar to 'f%',
1 operator(+) 1,
1 operator(public.+) 1,
1 OPERATOR ( + ) 1,
1 OPERATOR ( PUBLIC . + ) 1,
1 OPERATOR /* before paren */ ( /* before path */ PUBLIC /* before dot */ . /* before op */ + /* after op */ ) /* after paren */ 1,
true or false,
(1, 2) overlaps (3, 4),
10 % 3,
Expand Down Expand Up @@ -84,6 +86,10 @@ select
x is not nfkd normalized,
-- prefix expr
@-@ 10,
OPERATOR ( PUBLIC . + ) /* after op */ 1,
+ 1,
- 1,
not true,
-- slice expr
c[:2][2:],
-- tuple expr
Expand Down
20 changes: 20 additions & 0 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12642,6 +12642,26 @@ create operator class ops for type int using btree family fa$0m as operator 1 <;
");
}

#[test]
fn goto_operator_call_schema() {
assert_snapshot!(goto("
create schema s;
create operator s.+ (
leftarg = integer,
rightarg = integer,
function = pg_catalog.int4pl
);
select 1 operator(s$0.+) 2;
"), @"
╭▸
2 │ create schema s;
│ ─ 2. destination
8 │ select 1 operator(s.+) 2;
╰╴ ─ 1. source
");
}

#[test]
fn goto_create_operator_class_for_order_by_family() {
assert_snapshot!(goto("
Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5052,7 +5052,7 @@ fn opt_operator(p: &mut Parser<'_>) -> bool {
// >
// bar.>
// foo.bar.>
fn operator(p: &mut Parser<'_>) {
pub(crate) fn operator(p: &mut Parser<'_>) {
let m = p.start();
opt_path_name_ref(p);
if !opt_operator(p) {
Expand Down
20 changes: 2 additions & 18 deletions crates/squawk_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,24 +399,8 @@ impl<'t> Parser<'t> {
self.bump(SyntaxKind::OPERATOR_KW);
self.bump(SyntaxKind::L_PAREN);

// database.
if self.eat(SyntaxKind::IDENT) {
self.expect(SyntaxKind::DOT);
}
// schema.
if self.eat(SyntaxKind::IDENT) {
self.expect(SyntaxKind::DOT);
}

// +, -, etc.
match grammar::current_operator(self) {
Some(kind) => {
self.bump(kind);
}
None => {
self.error("expected operator");
}
}
// e.g. `+`, `pg_catalog.+`, `db.pg_catalog.+`
grammar::operator(self);

self.expect(SyntaxKind::R_PAREN);
m.complete(self, SyntaxKind::OPERATOR_CALL);
Expand Down
7 changes: 7 additions & 0 deletions crates/squawk_parser/tests/data/ok/schemas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ set foo to default;
-- binary
select 3 operator(pg_catalog.+) 4;

-- parses by PG and then reports an error about cross DB refs not allowed
select 2 operator(database_name.pg_catalog.+) 2;

select 3 operator(+) 4;

select 1 operator(a.&&) 2;
Expand All @@ -76,5 +79,9 @@ select operator(-) 4;

select operator(a.b.-) 4;

-- keyword schema names
select 1 operator(domain.+) 2;
select 1 operator(u&"schema" uescape '!'.+) 2;


set catalog 'foo';
153 changes: 135 additions & 18 deletions crates/squawk_parser/tests/snapshots/tests__schemas_ok.snap
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,53 @@ SOURCE_FILE
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
IDENT "pg_catalog"
DOT "."
PLUS "+"
OP
PATH_REF
PATH_REF
PATH_SEGMENT_REF
IDENT "pg_catalog"
DOT "."
PATH_SEGMENT_REF
PLUS "+"
R_PAREN ")"
WHITESPACE " "
LITERAL
INT_NUMBER "4"
SEMICOLON ";"
WHITESPACE "\n\n"
COMMENT "-- parses by PG and then reports an error about cross DB refs not allowed"
WHITESPACE "\n"
SELECT
SELECT_CLAUSE
SELECT_KW "select"
WHITESPACE " "
TARGET_LIST
TARGET
BIN_EXPR
LITERAL
INT_NUMBER "2"
WHITESPACE " "
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
OP
PATH_REF
PATH_REF
PATH_REF
PATH_SEGMENT_REF
IDENT "database_name"
DOT "."
PATH_SEGMENT_REF
IDENT "pg_catalog"
DOT "."
PATH_SEGMENT_REF
PLUS "+"
R_PAREN ")"
WHITESPACE " "
LITERAL
INT_NUMBER "2"
SEMICOLON ";"
WHITESPACE "\n\n"
SELECT
SELECT_CLAUSE
SELECT_KW "select"
Expand All @@ -662,7 +700,8 @@ SOURCE_FILE
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
PLUS "+"
OP
PLUS "+"
R_PAREN ")"
WHITESPACE " "
LITERAL
Expand All @@ -682,11 +721,16 @@ SOURCE_FILE
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
IDENT "a"
DOT "."
CUSTOM_OP
AMP "&"
AMP "&"
OP
PATH_REF
PATH_REF
PATH_SEGMENT_REF
IDENT "a"
DOT "."
PATH_SEGMENT_REF
CUSTOM_OP
AMP "&"
AMP "&"
R_PAREN ")"
WHITESPACE " "
LITERAL
Expand All @@ -705,9 +749,14 @@ SOURCE_FILE
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
IDENT "pg_catalog"
DOT "."
MINUS "-"
OP
PATH_REF
PATH_REF
PATH_SEGMENT_REF
IDENT "pg_catalog"
DOT "."
PATH_SEGMENT_REF
MINUS "-"
R_PAREN ")"
WHITESPACE " "
LITERAL
Expand All @@ -724,7 +773,8 @@ SOURCE_FILE
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
MINUS "-"
OP
MINUS "-"
R_PAREN ")"
WHITESPACE " "
LITERAL
Expand All @@ -741,16 +791,83 @@ SOURCE_FILE
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
IDENT "a"
DOT "."
IDENT "b"
DOT "."
MINUS "-"
OP
PATH_REF
PATH_REF
PATH_REF
PATH_SEGMENT_REF
IDENT "a"
DOT "."
PATH_SEGMENT_REF
IDENT "b"
DOT "."
PATH_SEGMENT_REF
MINUS "-"
R_PAREN ")"
WHITESPACE " "
LITERAL
INT_NUMBER "4"
SEMICOLON ";"
WHITESPACE "\n\n"
COMMENT "-- keyword schema names"
WHITESPACE "\n"
SELECT
SELECT_CLAUSE
SELECT_KW "select"
WHITESPACE " "
TARGET_LIST
TARGET
BIN_EXPR
LITERAL
INT_NUMBER "1"
WHITESPACE " "
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
OP
PATH_REF
PATH_REF
PATH_SEGMENT_REF
DOMAIN_KW "domain"
DOT "."
PATH_SEGMENT_REF
PLUS "+"
R_PAREN ")"
WHITESPACE " "
LITERAL
INT_NUMBER "2"
SEMICOLON ";"
WHITESPACE "\n"
SELECT
SELECT_CLAUSE
SELECT_KW "select"
WHITESPACE " "
TARGET_LIST
TARGET
BIN_EXPR
LITERAL
INT_NUMBER "1"
WHITESPACE " "
OPERATOR_CALL
OPERATOR_KW "operator"
L_PAREN "("
OP
PATH_REF
PATH_REF
PATH_SEGMENT_REF
IDENT "u&\"schema\""
WHITESPACE " "
UESCAPE_KW "uescape"
WHITESPACE " "
STRING "'!'"
DOT "."
PATH_SEGMENT_REF
PLUS "+"
R_PAREN ")"
WHITESPACE " "
LITERAL
INT_NUMBER "2"
SEMICOLON ";"
WHITESPACE "\n\n\n"
SET
SET_KW "set"
Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use squawk_parser::SyntaxKind;
pub use self::node_ext::normalize_name_node;
pub use self::{
generated::tokens::*,
node_ext::{BinOp, LitKind, PostfixOp},
node_ext::{BinOp, LitKind, PostfixOp, PrefixOp},
nodes::*,
traits::{HasCreateTable, HasWithClause, NameLike},
};
Expand Down
8 changes: 0 additions & 8 deletions crates/squawk_syntax/src/ast/generated/nodes.rs

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

2 changes: 1 addition & 1 deletion crates/squawk_syntax/src/postgresql.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ IsNotNormalized =
'is' UnicodeNormalForm? 'not' 'normalized'

OperatorCall =
'operator' '(' (PathRef '.')? Op ')'
'operator' '(' Op ')'

ColonEq =
':' '='
Expand Down
Loading