|
26 | 26 | //! encompasses the former, resulting in fewer checks during query execution. |
27 | 27 |
|
28 | 28 | use datafusion_common::{Column, Result, ScalarValue}; |
29 | | -use datafusion_expr::{BinaryExpr, Cast, Expr, Operator}; |
| 29 | +use datafusion_expr::{BinaryExpr, Expr, Operator}; |
30 | 30 | use std::collections::BTreeMap; |
31 | 31 |
|
32 | 32 | /// Simplifies a list of predicates by removing redundancies. |
@@ -239,8 +239,99 @@ fn find_most_restrictive_predicate( |
239 | 239 | fn extract_column_from_expr(expr: &Expr) -> Option<Column> { |
240 | 240 | match expr { |
241 | 241 | Expr::Column(col) => Some(col.clone()), |
242 | | - // Handle cases where the column might be wrapped in a cast or other operation |
243 | | - Expr::Cast(Cast { expr, .. }) => extract_column_from_expr(expr), |
244 | 242 | _ => None, |
245 | 243 | } |
246 | 244 | } |
| 245 | + |
| 246 | +#[cfg(test)] |
| 247 | +mod tests { |
| 248 | + use super::*; |
| 249 | + use arrow::datatypes::DataType; |
| 250 | + use datafusion_expr::{cast, col, lit}; |
| 251 | + |
| 252 | + #[test] |
| 253 | + fn test_simplify_predicates_with_cast() { |
| 254 | + // Test that predicates on cast expressions are not grouped with predicates on the raw column |
| 255 | + // a < 5 AND CAST(a AS varchar) < 'abc' AND a < 6 |
| 256 | + // Should simplify to: |
| 257 | + // a < 5 AND CAST(a AS varchar) < 'abc' |
| 258 | + |
| 259 | + let predicates = vec![ |
| 260 | + col("a").lt(lit(5i32)), |
| 261 | + cast(col("a"), DataType::Utf8).lt(lit("abc")), |
| 262 | + col("a").lt(lit(6i32)), |
| 263 | + ]; |
| 264 | + |
| 265 | + let result = simplify_predicates(predicates).unwrap(); |
| 266 | + |
| 267 | + // Should have 2 predicates: a < 5 and CAST(a AS varchar) < 'abc' |
| 268 | + assert_eq!(result.len(), 2); |
| 269 | + |
| 270 | + // Check that the cast predicate is preserved |
| 271 | + let has_cast_predicate = result.iter().any(|p| { |
| 272 | + matches!(p, Expr::BinaryExpr(BinaryExpr { |
| 273 | + left, |
| 274 | + op: Operator::Lt, |
| 275 | + right |
| 276 | + }) if matches!(left.as_ref(), Expr::Cast(_)) && right == &Box::new(lit("abc"))) |
| 277 | + }); |
| 278 | + assert!(has_cast_predicate, "Cast predicate should be preserved"); |
| 279 | + |
| 280 | + // Check that we have the more restrictive column predicate (a < 5) |
| 281 | + let has_column_predicate = result.iter().any(|p| { |
| 282 | + matches!(p, Expr::BinaryExpr(BinaryExpr { |
| 283 | + left, |
| 284 | + op: Operator::Lt, |
| 285 | + right |
| 286 | + }) if left == &Box::new(col("a")) && right == &Box::new(lit(5i32))) |
| 287 | + }); |
| 288 | + assert!(has_column_predicate, "Should have a < 5 predicate"); |
| 289 | + } |
| 290 | + |
| 291 | + #[test] |
| 292 | + fn test_extract_column_ignores_cast() { |
| 293 | + // Test that extract_column_from_expr does not extract columns from cast expressions |
| 294 | + let cast_expr = cast(col("a"), DataType::Utf8); |
| 295 | + assert_eq!(extract_column_from_expr(&cast_expr), None); |
| 296 | + |
| 297 | + // Test that it still extracts from direct column references |
| 298 | + let col_expr = col("a"); |
| 299 | + assert_eq!(extract_column_from_expr(&col_expr), Some(Column::from("a"))); |
| 300 | + } |
| 301 | + |
| 302 | + #[test] |
| 303 | + fn test_simplify_predicates_direct_columns_only() { |
| 304 | + // Test that only predicates on direct columns are simplified together |
| 305 | + let predicates = vec![ |
| 306 | + col("a").lt(lit(5i32)), |
| 307 | + col("a").lt(lit(3i32)), |
| 308 | + col("b").gt(lit(10i32)), |
| 309 | + col("b").gt(lit(20i32)), |
| 310 | + ]; |
| 311 | + |
| 312 | + let result = simplify_predicates(predicates).unwrap(); |
| 313 | + |
| 314 | + // Should have 2 predicates: a < 3 and b > 20 (most restrictive for each column) |
| 315 | + assert_eq!(result.len(), 2); |
| 316 | + |
| 317 | + // Check for a < 3 |
| 318 | + let has_a_predicate = result.iter().any(|p| { |
| 319 | + matches!(p, Expr::BinaryExpr(BinaryExpr { |
| 320 | + left, |
| 321 | + op: Operator::Lt, |
| 322 | + right |
| 323 | + }) if left == &Box::new(col("a")) && right == &Box::new(lit(3i32))) |
| 324 | + }); |
| 325 | + assert!(has_a_predicate, "Should have a < 3 predicate"); |
| 326 | + |
| 327 | + // Check for b > 20 |
| 328 | + let has_b_predicate = result.iter().any(|p| { |
| 329 | + matches!(p, Expr::BinaryExpr(BinaryExpr { |
| 330 | + left, |
| 331 | + op: Operator::Gt, |
| 332 | + right |
| 333 | + }) if left == &Box::new(col("b")) && right == &Box::new(lit(20i32))) |
| 334 | + }); |
| 335 | + assert!(has_b_predicate, "Should have b > 20 predicate"); |
| 336 | + } |
| 337 | +} |
0 commit comments