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 {
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 @@ -19679,3 +19679,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 @@ -9663,3 +9663,37 @@ fn parse_right_deep_join_chain() {
// NATURAL JOIN followed by a constrained join must stay left-associative.
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");
}

#[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");
}
Loading