diff --git a/crates/datafusion/src/read/predicate.rs b/crates/datafusion/src/read/predicate.rs index 6c26b43..21a4ad2 100644 --- a/crates/datafusion/src/read/predicate.rs +++ b/crates/datafusion/src/read/predicate.rs @@ -45,20 +45,10 @@ impl Converted { pub fn convert_filters_to_predicate(filters: &[Expr]) -> Option { filters .iter() - .filter_map(convert_filter_to_predicate) + .filter_map(|expr| predicate(expr).map(Converted::into_predicate)) .reduce(Predicate::and) } -fn convert_filter_to_predicate(expr: &Expr) -> Option { - // A bare column as a whole filter is a boolean column check: `col = true`. - // TODO: Also convert it as an AND or OR operand, as `NOT col` already is. - // `col = true` is equivalent to a boolean column, so that is sound. - match column(expr) { - Some(column) => Some(column.equal_to(Datum::bool(true))), - None => predicate(expr).map(Converted::into_predicate), - } -} - /// Converts a filter expression, or returns `None` if no part of it can be /// pushed down. fn predicate(expr: &Expr) -> Option { @@ -74,8 +64,13 @@ fn predicate(expr: &Expr) -> Option { right, }) => return Some(or(predicate(left)?, predicate(right)?)), Expr::BinaryExpr(binary) => comparison(binary)?, + // A bare column in filter position is a boolean column check: `col = true`. + // Null matches neither `col` nor `col = true`, so this is exact. + Expr::Column(column) => Reference::new(column.name()).equal_to(Datum::bool(true)), Expr::Not(inner) => match column(inner) { - // NOT of a bare boolean column: `col = false` + // Convert `NOT col` directly to `col = false` instead of `!(col = true)`: + // Iceberg rewrites `!(col = true)` to `col != true`, which metrics pruning + // cannot use to skip files, whereas `col = false` can. Some(column) => column.equal_to(Datum::bool(false)), None => match predicate(inner)? { Converted::Exact(p) => !p, @@ -509,9 +504,18 @@ mod tests { fn test_predicate_conversion_with_boolean_column() { let b = || Reference::new("b"); let foo = || Reference::new("foo"); - - // A bare boolean column is a filter only on its own. A top-level AND is - // split into separate filters, so `b` here is on its own. + // Pass a single unsplit filter so top-level `AND` exercises `predicate`'s + // `Operator::And` branch rather than `split_conjunction`. + let df_schema = create_test_schema(); + let convert_single = |sql: &str| { + let expr = SessionContext::new() + .parse_sql_expr(sql, &df_schema) + .unwrap(); + convert_filters_to_predicate(&[expr]) + }; + + // Bare boolean columns convert to `col = true` (and `NOT col` to `col = false`) + // everywhere in a filter expression, as `Converted::Exact` (issue 145). assert_eq!( convert_to_iceberg_predicate("b"), Some(b().equal_to(Datum::bool(true))) @@ -521,24 +525,55 @@ mod tests { Some(b().equal_to(Datum::bool(false))) ); assert_eq!( - convert_to_iceberg_predicate("b and foo > 1"), + convert_single("b and foo > 1"), Some( b().equal_to(Datum::bool(true)) .and(foo().greater_than(Datum::long(1))) ) ); - - // As an operand of OR or of a nested AND, it isn't converted. - assert_eq!(convert_to_iceberg_predicate("b or foo > 1"), None); + assert_eq!( + convert_to_iceberg_predicate("b or foo > 1"), + Some( + b().equal_to(Datum::bool(true)) + .or(foo().greater_than(Datum::long(1))) + ) + ); assert_eq!( convert_to_iceberg_predicate("(b and foo > 1) or foo < 0"), Some( - foo() - .greater_than(Datum::long(1)) + b().equal_to(Datum::bool(true)) + .and(foo().greater_than(Datum::long(1))) .or(foo().less_than(Datum::long(0))) ) ); - assert_eq!(convert_to_iceberg_predicate("not (b and foo > 1)"), None); + assert_eq!( + convert_to_iceberg_predicate("not (b and foo > 1)"), + Some( + !(b() + .equal_to(Datum::bool(true)) + .and(foo().greater_than(Datum::long(1)))) + ) + ); + assert_eq!( + convert_to_iceberg_predicate("not (b or foo > 1)"), + Some( + !(b() + .equal_to(Datum::bool(true)) + .or(foo().greater_than(Datum::long(1)))) + ) + ); + + // Soundness with an unconvertible sibling: AND keeps the convertible side as + // `Implied`, while OR and NOT of an `Implied` conjunction are dropped. + assert_eq!( + convert_single("b and length(bar) = 1"), + Some(b().equal_to(Datum::bool(true))) + ); + assert_eq!( + convert_to_iceberg_predicate("not (b and length(bar) = 1)"), + None + ); + assert_eq!(convert_to_iceberg_predicate("b or length(bar) = 1"), None); } #[test] diff --git a/crates/sqllogictest/testdata/slts/df_test/boolean_predicate_pushdown.slt b/crates/sqllogictest/testdata/slts/df_test/boolean_predicate_pushdown.slt index 205be33..37163ec 100644 --- a/crates/sqllogictest/testdata/slts/df_test/boolean_predicate_pushdown.slt +++ b/crates/sqllogictest/testdata/slts/df_test/boolean_predicate_pushdown.slt @@ -103,6 +103,46 @@ SELECT * FROM default.default.test_boolean_table WHERE is_active = true OR id = 3 true Premium member 5 true Verified account +# Verify bare boolean column as OR operand is pushed down (issue 145) +query TT +EXPLAIN SELECT * FROM default.default.test_boolean_table WHERE is_active OR id = 6 +---- +logical_plan +01)Filter: default.default.test_boolean_table.is_active OR default.default.test_boolean_table.id = Int32(6) +02)--TableScan: default.default.test_boolean_table projection=[id, is_active, description], partial_filters=[default.default.test_boolean_table.is_active OR default.default.test_boolean_table.id = Int32(6)] +physical_plan +01)FilterExec: is_active@1 OR id@0 = 6 +02)--CooperativeExec +03)----DataSourceExec: format=iceberg, projection=[id, is_active, description], provider=lazy, predicate=(is_active = true) OR (id = 6) + +# Row 6 has is_active = NULL and id = 6, so NULL OR true = true +query ITT rowsort +SELECT * FROM default.default.test_boolean_table WHERE is_active OR id = 6 +---- +1 true Active user +3 true Premium member +5 true Verified account +6 NULL Pending verification + +# Verify bare boolean column in a nested AND inside OR is pushed down (issue 145) +query TT +EXPLAIN SELECT * FROM default.default.test_boolean_table WHERE (is_active AND id > 2) OR id = 2 +---- +logical_plan +01)Filter: default.default.test_boolean_table.is_active AND default.default.test_boolean_table.id > Int32(2) OR default.default.test_boolean_table.id = Int32(2) +02)--TableScan: default.default.test_boolean_table projection=[id, is_active, description], partial_filters=[default.default.test_boolean_table.is_active AND default.default.test_boolean_table.id > Int32(2) OR default.default.test_boolean_table.id = Int32(2)] +physical_plan +01)FilterExec: is_active@1 AND id@0 > 2 OR id@0 = 2 +02)--CooperativeExec +03)----DataSourceExec: format=iceberg, projection=[id, is_active, description], provider=lazy, predicate=((is_active = true) AND (id > 2)) OR (id = 2) + +query ITT rowsort +SELECT * FROM default.default.test_boolean_table WHERE (is_active AND id > 2) OR id = 2 +---- +2 false Inactive user +3 true Premium member +5 true Verified account + # Test IS NULL on boolean column query ITT SELECT * FROM default.default.test_boolean_table WHERE is_active IS NULL