Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion datafusion/expr-common/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Copy link
Member

Choose a reason for hiding this comment

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

nit: Maybe add a test case for Decimals too, since they are also numeric types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests for Decimals and Dictionary are added, thanks.

fn test_math_decimal_coercion_rule(
lhs_type: DataType,
rhs_type: DataType,
Expand Down
8 changes: 8 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down