Skip to content

fix ^ evaluates as bitwise XOR instead of exponentiation#22314

Open
xiedeyantu wants to merge 4 commits into
apache:mainfrom
xiedeyantu:cifang
Open

fix ^ evaluates as bitwise XOR instead of exponentiation#22314
xiedeyantu wants to merge 4 commits into
apache:mainfrom
xiedeyantu:cifang

Conversation

@xiedeyantu
Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

In PostgreSQL, the ^ operator represents exponentiation, but DataFusion was interpreting it as bitwise XOR. This caused PostgreSQL-dialect queries such as SELECT 2 ^ 3; to return 1 instead of 8.

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?

  • Added PostgreSQL-specific handling for BinaryOperator::PGExp during SQL expression lowering.
  • Lowered PostgreSQL ^ expressions to the built-in power(left, right) scalar function instead of treating them as bitwise XOR.
  • Kept existing generic-dialect ^ behavior unchanged.
  • Kept PostgreSQL bitwise XOR behavior unchanged via #.
  • Added a sqllogictest regression covering 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 scalar

Are 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 #.

@github-actions github-actions Bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels May 17, 2026
@Dandandan Dandandan added the api change Changes the API exposed to users of the crate label May 18, 2026
Comment thread datafusion/sql/src/expr/mod.rs Outdated
}

let RawBinaryExpr { op, left, right } = binary_expr;
if op == BinaryOperator::PGExp {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be handled somewhere else?

Do we neef to update documentation / tests, also covering bitwise XOR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@github-actions github-actions Bot added the auto detected api change Auto detected API change label May 18, 2026
@github-actions github-actions Bot removed the auto detected api change Auto detected API change label May 18, 2026
@xiedeyantu xiedeyantu requested a review from Dandandan May 19, 2026 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api change Changes the API exposed to users of the crate sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL compatibility: ^ evaluates as bitwise XOR instead of exponentiation

2 participants