fix ^ evaluates as bitwise XOR instead of exponentiation#22314
fix ^ evaluates as bitwise XOR instead of exponentiation#22314xiedeyantu wants to merge 3 commits into
Conversation
| } | ||
|
|
||
| let RawBinaryExpr { op, left, right } = binary_expr; | ||
| if op == BinaryOperator::PGExp { |
There was a problem hiding this comment.
Shouldn't it be handled somewhere else?
Do we neef to update documentation / tests, also covering bitwise XOR?
There was a problem hiding this comment.
Ah I see it correctly uses # in the tests, so the previous behavior was probably an oversight.
I think we should move the change to self.parse_sql_binary_op(&op) though?
There was a problem hiding this comment.
I've moved the method to binary_op.rs because BinaryOperator::PGExp doesn't have an operator chain like "#", so I used this approach to solve the problem. I also tried implementing "^" in a similar way to "#", but it required a lot of modifications and ultimately still relies on the "power" function. Would you accept this modification?
|
Thank you for opening this pull request! Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch). Details |
Which issue does this PR close?
^evaluates as bitwise XOR instead of exponentiation #22252.Rationale for this change
In PostgreSQL, the
^operator represents exponentiation, but DataFusion was interpreting it as bitwise XOR. This caused PostgreSQL-dialect queries such asSELECT 2 ^ 3;to return1instead of8.This change aligns DataFusion's PostgreSQL SQL semantics with PostgreSQL for this operator while preserving existing non-PostgreSQL behavior.
What changes are included in this PR?
BinaryOperator::PGExpduring SQL expression lowering.^expressions to the built-inpower(left, right)scalar function instead of treating them as bitwise XOR.^behavior unchanged.#.SELECT 2 ^ 3;under the PostgreSQL parser dialect.Are these changes tested?
Yes.
Added a regression test in scalar.slt to verify that PostgreSQL-dialect
^is evaluated as exponentiation.Validated with:
cargo test -p datafusion-sqllogictest --test sqllogictests scalarAre there any user-facing changes?
Yes.
For the PostgreSQL SQL parser dialect,
^now behaves as exponentiation instead of bitwise XOR, matching PostgreSQL semantics. Generic-dialect behavior is unchanged, and PostgreSQL bitwise XOR remains available through#.