Skip to content

Apache DataFusion SQL Query Engine

fix: Add integer check for bitwise coercion (#20241)

Acfboye4e8598be476
diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rs
index 97ec54b298..4daa8a7a7f 100644
--- a/datafusion/expr-common/src/type_coercion/binary.rs
+++ b/datafusion/expr-common/src/type_coercion/binary.rs
@@ -470,7 +470,9 @@ fn bitwise_coercion(left_type: &DataType, right_type: &DataType) -> Option<DataT
return None;
}
- if left_type == right_type {
+ let is_integer_dictionary =
+ matches!(left_type, Dictionary(_, value_type) if value_type.is_integer());
+ if left_type == right_type && (left_type.is_integer() || is_integer_dictionary) {
return Some(left_type.clone());
}
diff --git a/datafusion/expr-common/src/type_coercion/binary/tests/arithmetic.rs b/datafusion/expr-common/src/type_coercion/binary/tests/arithmetic.rs
index bb9d44953b..eb5622fedb 100644
--- a/datafusion/expr-common/src/type_coercion/binary/tests/arithmetic.rs
+++ b/datafusion/expr-common/src/type_coercion/binary/tests/arithmetic.rs
@@ -228,6 +228,53 @@ fn test_type_coercion_arithmetic() -> Result<()> {
Ok(())
}
+#[test]
+fn test_bitwise_coercion_non_integer_types() -> Result<()> {
+ let err = BinaryTypeCoercer::new(
+ &DataType::Float32,
+ &Operator::BitwiseAnd,
+ &DataType::Float32,
+ )
+ .get_input_types()
+ .unwrap_err()
+ .to_string();
+ assert_contains!(
+ &err,
+ "Cannot infer common type for bitwise operation Float32 & Float32"
+ );
+
+ let err = BinaryTypeCoercer::new(
+ &DataType::Float32,
+ &Operator::BitwiseAnd,
+ &DataType::Float64,
+ )
+ .get_input_types()
+ .unwrap_err()
+ .to_string();
+ assert_contains!(
+ &err,
+ "Cannot infer common type for bitwise operation Float32 & Float64"
+ );
+
+ let err = BinaryTypeCoercer::new(
+ &DataType::Decimal128(10, 2),
+ &Operator::BitwiseAnd,
+ &DataType::Decimal128(10, 2),
+ )
+ .get_input_types()
+ .unwrap_err()
+ .to_string();
+ assert_contains!(
+ &err,
+ "Cannot infer common type for bitwise operation Decimal128(10, 2) & Decimal128(10, 2)"
+ );
+
+ let dict_int8 = DataType::Dictionary(DataType::Int8.into(), DataType::Int8.into());
+ test_coercion_binary_rule!(dict_int8, dict_int8, Operator::BitwiseAnd, dict_int8);
+
+ Ok(())
+}
+
fn test_math_decimal_coercion_rule(
lhs_type: DataType,
rhs_type: DataType,
diff --git a/datafusion/sqllogictest/test_files/scalar.slt b/datafusion/sqllogictest/test_files/scalar.slt
index 4206d4c65e..f5d6619d52 100644
--- a/datafusion/sqllogictest/test_files/scalar.slt
+++ b/datafusion/sqllogictest/test_files/scalar.slt
@@ -1311,6 +1311,14 @@ select a << b, c << d, e << f from signed_integers;
33554432 123 10485760
NULL NULL NULL
+## bitwise operations should reject non-integer types
+
+query error DataFusion error: Error during planning: Cannot infer common type for bitwise operation Float32 & Float32
+select arrow_cast(1, 'Float32') & arrow_cast(2, 'Float32');
+
+query error DataFusion error: Error during planning: Cannot infer common type for bitwise operation Date32 & Date32
+select arrow_cast(1, 'Date32') & arrow_cast(2, 'Date32');
+
statement ok
drop table unsigned_integers;