kumarUjjawal opened a new issue, #27094: URL: https://github.com/apache/datafusion/issues/27094
### Describe the bug With the PostgreSQL dialect, `^` is the power operator. Unary minus binds more loosely than `^`, so `-1 ^ 0` is planned as `-(1 ^ 0)` and returns `-1`. In PostgreSQL, unary minus binds more tightly than `^` ([operator precedence table](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE)). PostgreSQL computes `(-1) ^ 0` and returns `1`. ### To Reproduce ```sql SET datafusion.sql_parser.dialect = 'PostgreSQL'; SELECT -1 ^ 0; SELECT -2 ^ 2; EXPLAIN SELECT -2 ^ 2; ``` On main (`c120619de`), `datafusion-cli` returns: ```text +------------------------------+ | (- power(Int64(1),Int64(0))) | +------------------------------+ | -1.0 | +------------------------------+ +------------------------------+ | (- power(Int64(2),Int64(2))) | +------------------------------+ | -4.0 | +------------------------------+ ``` The plan shows `(- power(Int64(2),Int64(2)))`, so the minus is applied after the power. ### Expected behavior The same results as PostgreSQL 17: ```text postgres=# SELECT -1 ^ 0, -2 ^ 2; ?column? | ?column? ----------+---------- 1 | 4 ``` DuckDB gives the same results (`1` and `4`). ### Additional context The cause is in `sqlparser` 0.63.0. `parse_prefix_inner` parses the operand of a unary `-` or `+` at `Precedence::MulDivModOp`: ```rust tok @ Token::Minus | tok @ Token::Plus => { ... expr: Box::new( self.parse_subexpr(self.dialect.prec_value(Precedence::MulDivModOp))?, ), ``` `PostgreSqlDialect` sets `CARET_PREC` (100) above `MUL_DIV_MOD_OP_PREC` (90). So `^` is consumed into the operand of the minus. The fix is probably to parse the operand of a unary minus above `CARET_PREC` in the PostgreSQL dialect. Related: #22461 (binary operator precedence against PostgreSQL) and apache/datafusion-sqlparser-rs#814 (`~` precedence). Neither covers unary minus and `^`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
