diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rs index 9051f412bde9b..48380982e54d5 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 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 4206d4c65ed87..f5d6619d52903 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;