Skip to content

PostgreSQL: unary minus binds more loosely than ^, so -1 ^ 0 parses as -(1 ^ 0) #2819

Open

Description

@kumarUjjawal

Problem

In PostgreSqlDialect, a unary minus binds more loosely than ^. The parser reads -1 ^ 0 as -(1 ^ 0).

In PostgreSQL, unary minus binds more tightly than ^ (operator precedence table). PostgreSQL reads -1 ^ 0 as (-1) ^ 0.

Reproduction

At main (d1c1790e3):

use sqlparser::dialect::PostgreSqlDialect;
use sqlparser::parser::Parser;

let ast = Parser::parse_sql(&PostgreSqlDialect {}, "SELECT -1 ^ 0").unwrap();
println!("{ast:#?}");

The projection expression is (spans removed):

UnaryOp {
    op: Minus,
    expr: BinaryOp {
        left: Value(Number("1", false)),
        op: PGExp,
        right: Value(Number("0", false)),
    },
}

The expected expression is:

BinaryOp {
    left: UnaryOp { op: Minus, expr: Value(Number("1", false)) },
    op: PGExp,
    right: Value(Number("0", false)),
}

SELECT -2 ^ 2 and SELECT +2 ^ 2 parse the same way.

Effect on query results

The grouping changes the result:

Query PostgreSQL 17 DuckDB DataFusion (PostgreSQL dialect)
SELECT -1 ^ 0 1 1 -1
SELECT -2 ^ 2 4 4 -4

DataFusion tracks this in apache/datafusion#27094.

Cause

parse_prefix_inner parses the operand of a unary - or + at Precedence::MulDivModOp (src/parser/mod.rs#L1967-L1979):

tok @ Token::Minus | tok @ Token::Plus => {
    // ...
    Ok(Expr::UnaryOp {
        op,
        expr: Box::new(
            self.parse_subexpr(self.dialect.prec_value(Precedence::MulDivModOp))?,
        ),
    })
}

PostgreSqlDialect sets CARET_PREC (100) above MUL_DIV_MOD_OP_PREC (90) (src/dialect/postgresql.rs#L213-L214). So the operand of the minus also takes the ^ that follows.

For the PostgreSQL dialect, the operand of a unary - or + must be parsed at a precedence above CARET_PREC. In the PostgreSQL table, unary + and - are also above COLLATE and AT.

The generic dialect is not affected in this way: there, Caret is below MulDivModOp.

Related: #814 (precedence of ~).

Activity

  1. Surajsuthar commented on Nov 4, 2026

    @Surajsuthar

    @kumarUjjawal are you working on this ?

  2. kumarUjjawal commented on Nov 4, 2026

    @kumarUjjawal
    Author

    @kumarUjjawal are you working on this ?

    Nope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions