Skip to content

Commit 97fb235

Browse files
authored
PostgreSQL: Fix unary +/- precedence before exponentiation (#2829)
1 parent 5dd8f8d commit 97fb235

6 files changed

Lines changed: 91 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT -1 ^ 0;

‎src/dialect/mod.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ pub trait Dialect: Debug + Any {
11571157
Precedence::Period => 100,
11581158
Precedence::DoubleColon => 50,
11591159
Precedence::AtTz => 41,
1160-
Precedence::MulDivModOp => 40,
1160+
Precedence::MulDivModOp | Precedence::UnaryPlusMinus => 40,
11611161
Precedence::PlusMinus => 30,
11621162
Precedence::Xor => 24,
11631163
Precedence::Ampersand => 23,
@@ -2389,6 +2389,8 @@ pub enum Precedence {
23892389
DoubleColon,
23902390
/// Timezone operator (e.g. `AT TIME ZONE`).
23912391
AtTz,
2392+
/// Unary `+` and `-`, bounding their operand.
2393+
UnaryPlusMinus,
23922394
/// Multiplication / Division / Modulo operators (`*`, `/`, `%`).
23932395
MulDivModOp,
23942396
/// Addition / Subtraction (`+`, `-`).

‎src/dialect/postgresql.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub struct PostgreSqlDialect {}
209209
const PERIOD_PREC: u8 = 200;
210210
const DOUBLE_COLON_PREC: u8 = 140;
211211
const BRACKET_PREC: u8 = 130;
212+
const UNARY_PLUS_MINUS_PREC: u8 = 125;
212213
const COLLATE_PREC: u8 = 120;
213214
const AT_TZ_PREC: u8 = 110;
214215
const CARET_PREC: u8 = 100;
@@ -388,6 +389,7 @@ impl Dialect for PostgreSqlDialect {
388389
Precedence::UnaryNot => NOT_PREC,
389390
Precedence::And => AND_PREC,
390391
Precedence::Or => OR_PREC,
392+
Precedence::UnaryPlusMinus => UNARY_PLUS_MINUS_PREC,
391393
}
392394
}
393395

‎src/dialect/sqlite.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Dialect for SQLiteDialect {
128128
Precedence::Period => 100,
129129
Precedence::DoubleColon => 50,
130130
Precedence::AtTz => 41,
131-
Precedence::MulDivModOp => 40,
131+
Precedence::MulDivModOp | Precedence::UnaryPlusMinus => 40,
132132
Precedence::PlusMinus => 30,
133133
Precedence::Xor => 25,
134134
Precedence::Ampersand => 24,

‎src/parser/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ impl<'a> Parser<'a> {
20162016
Ok(Expr::UnaryOp {
20172017
op,
20182018
expr: Box::new(
2019-
self.parse_subexpr(self.dialect.prec_value(Precedence::MulDivModOp))?,
2019+
self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryPlusMinus))?,
20202020
),
20212021
})
20222022
}

‎tests/sqlparser_postgres.rs‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13316,6 +13316,51 @@ fn parse_alter_table_no_inherit() {
1331613316
}
1331713317
}
1331813318

13319+
#[test]
13320+
fn parse_pg_prefix_operator_before_multiplication() {
13321+
for sql in ["SELECT ~ 1 * 2", "SELECT |/ 25 * 4", "SELECT # 1 * 2"] {
13322+
let select = pg().verified_only_select(sql);
13323+
let Expr::UnaryOp { expr, .. } = expr_from_projection(&select.projection[0]) else {
13324+
panic!("Expected prefix operator expression in {sql}")
13325+
};
13326+
assert!(
13327+
matches!(
13328+
expr.as_ref(),
13329+
Expr::BinaryOp {
13330+
op: BinaryOperator::Multiply,
13331+
..
13332+
}
13333+
),
13334+
"{sql}"
13335+
);
13336+
}
13337+
}
13338+
13339+
#[test]
13340+
fn parse_unary_plus_minus_before_pg_exponentiation() {
13341+
for (sql, unary_op, operand, exponent) in [
13342+
("SELECT -1 ^ 0", UnaryOperator::Minus, "1", "0"),
13343+
("SELECT +2 ^ 2", UnaryOperator::Plus, "2", "2"),
13344+
] {
13345+
let select = pg().verified_only_select(sql);
13346+
let Expr::BinaryOp { left, op, right } = expr_from_projection(&select.projection[0]) else {
13347+
panic!("Expected exponentiation expression")
13348+
};
13349+
assert_eq!(*op, BinaryOperator::PGExp);
13350+
assert_eq!(
13351+
right.as_ref(),
13352+
&Expr::Value(number(exponent).with_empty_span())
13353+
);
13354+
assert_eq!(
13355+
left.as_ref(),
13356+
&Expr::UnaryOp {
13357+
op: unary_op,
13358+
expr: Box::new(Expr::Value(number(operand).with_empty_span())),
13359+
}
13360+
);
13361+
}
13362+
}
13363+
1331913364
#[test]
1332013365
fn parse_create_function_cost() {
1332113366
let stmt = pg_and_generic()
@@ -13465,6 +13510,44 @@ fn parse_create_function_rows() {
1346513510
}
1346613511
}
1346713512

13513+
#[test]
13514+
fn parse_unary_minus_before_pg_at_time_zone() {
13515+
let select = pg().verified_only_select("SELECT -ts AT TIME ZONE 'UTC'");
13516+
let Expr::AtTimeZone { timestamp, .. } = expr_from_projection(&select.projection[0]) else {
13517+
panic!("Expected AT TIME ZONE expression")
13518+
};
13519+
assert_eq!(
13520+
timestamp.as_ref(),
13521+
&Expr::UnaryOp {
13522+
op: UnaryOperator::Minus,
13523+
expr: Box::new(Expr::Identifier(Ident::new("ts"))),
13524+
}
13525+
);
13526+
}
13527+
13528+
#[test]
13529+
fn parse_generic_unary_minus_before_caret() {
13530+
let statements = Parser::parse_sql(&GenericDialect {}, "SELECT -1 ^ 0").unwrap();
13531+
let Statement::Query(query) = &statements[0] else {
13532+
panic!("Expected query")
13533+
};
13534+
let SetExpr::Select(select) = query.body.as_ref() else {
13535+
panic!("Expected select")
13536+
};
13537+
let Expr::BinaryOp { left, op, right } = expr_from_projection(&select.projection[0]) else {
13538+
panic!("Expected bitwise expression")
13539+
};
13540+
assert_eq!(*op, BinaryOperator::BitwiseXor);
13541+
assert_eq!(
13542+
left.as_ref(),
13543+
&Expr::UnaryOp {
13544+
op: UnaryOperator::Minus,
13545+
expr: Box::new(Expr::Value(number("1").with_empty_span())),
13546+
}
13547+
);
13548+
assert_eq!(right.as_ref(), &Expr::Value(number("0").with_empty_span()));
13549+
}
13550+
1346813551
#[test]
1346913552
fn parse_create_function_support() {
1347013553
let stmt = pg_and_generic().verified_stmt(

0 commit comments

Comments
 (0)