Skip to content

Allow comparison between boolean and int values #16798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions datafusion/expr-common/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ pub fn comparison_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<D
.or_else(|| binary_coercion(lhs_type, rhs_type))
.or_else(|| struct_coercion(lhs_type, rhs_type))
.or_else(|| map_coercion(lhs_type, rhs_type))
.or_else(|| boolean_coercion(lhs_type, rhs_type))
}

/// Similar to [`comparison_coercion`] but prefers numeric if compares with
Expand Down Expand Up @@ -1052,6 +1053,20 @@ fn map_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
}
}

/// Coercion rules for boolean types: If at least one argument is
/// a boolean type and both arguments can be coerced into a boolean type, coerce
/// to boolean type.
fn boolean_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
use arrow::datatypes::DataType::*;
match (lhs_type, rhs_type) {
(Boolean, Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64)
| (Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64, Boolean) => {
Some(Boolean)
}
_ => None,
}
}

/// Returns the output type of applying mathematics operations such as
/// `+` to arguments of `lhs_type` and `rhs_type`.
fn mathematics_numerical_coercion(
Expand Down Expand Up @@ -2510,6 +2525,32 @@ mod tests {
DataType::List(Arc::clone(&inner_field))
);

// boolean
let int_types = vec![
DataType::Int8,
DataType::Int16,
DataType::Int32,
DataType::Int64,
DataType::UInt8,
DataType::UInt16,
DataType::UInt32,
DataType::UInt64,
];
for int_type in int_types {
test_coercion_binary_rule!(
DataType::Boolean,
int_type,
Operator::Eq,
DataType::Boolean
);
test_coercion_binary_rule!(
int_type,
DataType::Boolean,
Operator::Eq,
DataType::Boolean
);
}

// Negative test: inner_timestamp_field and inner_field are not compatible because their inner types are not compatible
let inner_timestamp_field = Arc::new(Field::new_list_field(
DataType::Timestamp(TimeUnit::Microsecond, None),
Expand Down
18 changes: 16 additions & 2 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1823,11 +1823,25 @@ mod test {
"
)?;

let empty = empty_with_type(DataType::Int64);
let empty = empty_with_type(DataType::Float64);
let plan = LogicalPlan::Projection(Projection::try_new(vec![expr], empty)?);
assert_type_coercion_error(
plan,
"Cannot infer common argument type for comparison operation Int64 IS DISTINCT FROM Boolean"
"Cannot infer common argument type for comparison operation Float64 IS DISTINCT FROM Boolean"
)?;

// cast to integer
let expr = col("a").is_true();
let empty = empty_with_type(DataType::Int64);
let plan =
LogicalPlan::Projection(Projection::try_new(vec![expr.clone()], empty)?);

assert_analyzed_plan_eq!(
plan,
@r"
Projection: CAST(a AS Boolean) IS TRUE
EmptyRelation
"
)?;

// is not true
Expand Down
6 changes: 3 additions & 3 deletions datafusion/physical-expr/src/expressions/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,19 +1085,19 @@ mod tests {

#[test]
fn case_test_incompatible() -> Result<()> {
// 1 then is int64
// 1 then is float64
// 2 then is boolean
let batch = case_test_batch()?;
let schema = batch.schema();

// CASE WHEN a = 'foo' THEN 123 WHEN a = 'bar' THEN true END
// CASE WHEN a = 'foo' THEN 1.23 WHEN a = 'bar' THEN true END
let when1 = binary(
col("a", &schema)?,
Operator::Eq,
lit("foo"),
&batch.schema(),
)?;
let then1 = lit(123i32);
let then1 = lit(1.23f64);
let when2 = binary(
col("a", &schema)?,
Operator::Eq,
Expand Down
4 changes: 3 additions & 1 deletion datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1536,8 +1536,10 @@ SELECT not(true), not(false)
----
false true

query error type_coercion\ncaused by\nError during planning: Cannot infer common argument type for comparison operation Int64 IS DISTINCT FROM Boolean
query BB
SELECT not(1), not(0)
----
false true

query ?B
SELECT null, not(null)
Expand Down