Skip to content
Open
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
33 changes: 32 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,36 @@ where
DisplaySeparated { slice, sep: ", " }
}

/// Returns true when the prefix operator `op` written directly before `operand`
/// would be tokenized as a single operator rather than as two.
fn lexes_as_one_operator(op: &UnaryOperator, operand: &Expr) -> bool {
let Expr::UnaryOp { op: leading, .. } = operand else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested unary operator also reaches the output through a lower-precedence parent.

I suggest you use a display-loop fuzzer to catch these errors during your next review iteration, and consider also using a criterion benchmark to ensure the performance do not fall.

return false;
};
match op {
UnaryOperator::Minus
| UnaryOperator::BitwiseNot
| UnaryOperator::PGSquareRoot
| UnaryOperator::PGCubeRoot => !matches!(
leading,
UnaryOperator::Not | UnaryOperator::PGPostfixFactorial
),
UnaryOperator::BangNot => {
matches!(leading, UnaryOperator::BangNot | UnaryOperator::BitwiseNot)
}
UnaryOperator::PGAbs => matches!(
leading,
UnaryOperator::AtDashAt
| UnaryOperator::DoubleAt
| UnaryOperator::Minus
| UnaryOperator::PGAbs
| UnaryOperator::QuestionDash
| UnaryOperator::QuestionPipe
),
_ => false,
}
}

/// Writes the given statements to the formatter, each ending with
/// a semicolon and space separated.
fn format_statement_list(f: &mut fmt::Formatter, statements: &[Statement]) -> fmt::Result {
Expand Down Expand Up @@ -1963,7 +1993,8 @@ impl fmt::Display for Expr {
| UnaryOperator::DoubleAt
| UnaryOperator::QuestionDash
| UnaryOperator::QuestionPipe
) {
) || lexes_as_one_operator(op, expr)
{
write!(f, "{op} {expr}")
} else {
write!(f, "{op}{expr}")
Expand Down
16 changes: 16 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19984,3 +19984,19 @@ fn parse_function_arg_call_chain_no_exponential_blowup() {
rx.recv_timeout(Duration::from_secs(5))
.expect("parser should reject this quickly, not loop exponentially");
}

#[test]
fn parse_nested_unary_ops() {
all_dialects().verified_stmt("SELECT - -1");
all_dialects().verified_stmt("SELECT ~ ~1");
all_dialects().verified_stmt("SELECT NOT NOT a");
all_dialects().one_statement_parses_to("SELECT ~ ~ 1", "SELECT ~ ~1");
}

#[test]
fn parse_adjacent_unary_ops_that_do_not_combine() {
all_dialects().verified_stmt("SELECT ++a");
all_dialects().verified_stmt("SELECT +~a");
all_dialects().verified_stmt("SELECT -NOT a");
all_dialects().verified_stmt("SELECT ~NOT a");
}
34 changes: 34 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9684,6 +9684,40 @@ fn parse_right_deep_join_chain() {
pg().verified_stmt("SELECT * FROM t0 NATURAL JOIN t1 INNER JOIN t2 ON true");
}

#[test]
fn parse_nested_pg_unary_ops() {
let select = pg().verified_only_select("SELECT @ @1");
assert_eq!(
SelectItem::UnnamedExpr(Expr::UnaryOp {
op: UnaryOperator::PGAbs,
expr: Box::new(Expr::UnaryOp {
op: UnaryOperator::PGAbs,
expr: Box::new(Expr::value(number("1"))),
}),
}),
select.projection[0]
);

let select = pg().verified_only_select("SELECT @@ 1");
assert_eq!(
SelectItem::UnnamedExpr(Expr::UnaryOp {
op: UnaryOperator::DoubleAt,
expr: Box::new(Expr::value(number("1"))),
}),
select.projection[0]
);

pg().verified_stmt("SELECT |/ |/1");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an example of why you should pin the lower-precedence operand case, as before the source change it fails with left: "SELECT ~ -a * 2", right: "SELECT ~-a * 2".

Suggested change
pg().verified_stmt("SELECT |/ |/1");
pg().verified_stmt("SELECT |/ |/1");
pg().verified_stmt("SELECT ~ -a * 2");

}

#[test]
fn parse_adjacent_pg_unary_ops_that_do_not_combine() {
pg().verified_stmt("SELECT +@a");
pg().verified_stmt("SELECT !!~a");
pg().verified_stmt("SELECT @~a");
pg().verified_stmt("SELECT !!|/a");
}

#[test]
fn parse_quoted_function_argument_name() {
let statement = pg_and_generic().verified_stmt(
Expand Down
Loading