fix ^ evaluates as bitwise XOR instead of exponentiation#22314
Open
xiedeyantu wants to merge 4 commits into
Open
fix ^ evaluates as bitwise XOR instead of exponentiation#22314xiedeyantu wants to merge 4 commits into
xiedeyantu wants to merge 4 commits into
Conversation
Dandandan
reviewed
May 18, 2026
| } | ||
|
|
||
| let RawBinaryExpr { op, left, right } = binary_expr; | ||
| if op == BinaryOperator::PGExp { |
Contributor
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?
Contributor
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?
Member
Author
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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#.