Skip to content

Apache DataFusion SQL Query Engine

feat: Add support for PostgreSQL bitwise XOR operator (#7256)

Jonah Gao72ab4c89d35b
diff --git a/datafusion/core/tests/sqllogictests/test_files/scalar.slt b/datafusion/core/tests/sqllogictests/test_files/scalar.slt
index 80f5bd6c9d..1f6d926aea 100644
--- a/datafusion/core/tests/sqllogictests/test_files/scalar.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/scalar.slt
@@ -1032,6 +1032,49 @@ select a ^ b, c ^ d, e ^ f from signed_integers;
-998 -133 -16
NULL NULL NULL
+# bitwise xor with other operators
+query II rowsort
+select 2 * c - 1 ^ 856 + d + 3, d ^ 7 >> 4 from signed_integers;
+----
+-3328 128
+-822 64
+686 -16
+NULL -32
+
+statement ok
+set datafusion.sql_parser.dialect = postgresql;
+
+# postgresql bitwise xor with column and scalar
+query I rowsort
+select c # 856 from signed_integers;
+----
+-138
+-367
+803
+NULL
+
+# postgresql bitwise xor with columns
+query III rowsort
+select a # b, c # d, e # f from signed_integers;
+----
+-10003 -3026 -10
+-101 -1591 -10
+-998 -133 -16
+NULL NULL NULL
+
+# postgresql bitwise xor with other operators
+query II rowsort
+select 2 * c - 1 # 856 + d + 3, d # 7 >> 4 from signed_integers;
+----
+-3328 128
+-822 64
+686 -16
+NULL -32
+
+statement ok
+set datafusion.sql_parser.dialect = generic;
+
+
## bitwise right shift
# bitwise right shift with column and scalar
diff --git a/datafusion/expr/src/operator.rs b/datafusion/expr/src/operator.rs
index b94f8f15db..112e29082d 100644
--- a/datafusion/expr/src/operator.rs
+++ b/datafusion/expr/src/operator.rs
@@ -69,7 +69,7 @@ pub enum Operator {
BitwiseAnd,
/// Bitwise or, like `|`
BitwiseOr,
- /// Bitwise xor, like `#`
+ /// Bitwise xor, such as `^` in MySQL or `#` in PostgreSQL
BitwiseXor,
/// Bitwise right, like `>>`
BitwiseShiftRight,
@@ -253,7 +253,7 @@ impl fmt::Display for Operator {
Operator::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
Operator::BitwiseAnd => "&",
Operator::BitwiseOr => "|",
- Operator::BitwiseXor => "#",
+ Operator::BitwiseXor => "BIT_XOR",
Operator::BitwiseShiftRight => ">>",
Operator::BitwiseShiftLeft => "<<",
Operator::StringConcat => "||",
@@ -443,7 +443,7 @@ mod tests {
// BitXor
assert_eq!(
format!("{}", lit(1u32) ^ lit(2u32)),
- "UInt32(1) # UInt32(2)"
+ "UInt32(1) BIT_XOR UInt32(2)"
);
// Shl
assert_eq!(
diff --git a/datafusion/sql/src/expr/binary_op.rs b/datafusion/sql/src/expr/binary_op.rs
index c5d2238ac0..74b820ece7 100644
--- a/datafusion/sql/src/expr/binary_op.rs
+++ b/datafusion/sql/src/expr/binary_op.rs
@@ -43,6 +43,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
BinaryOperator::BitwiseAnd => Ok(Operator::BitwiseAnd),
BinaryOperator::BitwiseOr => Ok(Operator::BitwiseOr),
BinaryOperator::BitwiseXor => Ok(Operator::BitwiseXor),
+ BinaryOperator::PGBitwiseXor => Ok(Operator::BitwiseXor),
BinaryOperator::PGBitwiseShiftRight => Ok(Operator::BitwiseShiftRight),
BinaryOperator::PGBitwiseShiftLeft => Ok(Operator::BitwiseShiftLeft),
BinaryOperator::StringConcat => Ok(Operator::StringConcat),