findepi commented on code in PR #17521: URL: https://github.com/apache/datafusion/pull/17521#discussion_r2341196453
########## datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs: ########## @@ -204,7 +204,13 @@ fn find_most_restrictive_predicate( if let Some(scalar) = scalar_value { if let Some(current_best) = best_value { - let comparison = scalar.try_cmp(current_best)?; + // Only compare if the scalar values have compatible types + // If they don't have compatible types, we can't determine which is more restrictive + let Ok(comparison) = scalar.try_cmp(current_best) else { + // Can't compare - types are incompatible, so we can't simplify Review Comment: good idea, please do this function is used inside `fn simplify_column_predicates(predicates: Vec<Expr>) -> Result<Vec<Expr>>` returning `Ok(None)` is always _correct_ from correctness perspective. i.e. we won't simplify something that we could simplify. this also leads as to a test we could add ``` SELECT ... FROM ... WHERE a < 5 AND CAST(a AS varchar) < 'abc' AND a < 6 ``` this should be simplified to `a < 5 AND CAST(a AS varchar)`. I.e. the `a ? const` comparisons can be simplified using `find_most_restrictive_predicate`. The `f(a) ? const` comparisons cannot. current code in main fails (bad) current code in PR does not fail (better), but the root cause (comparing unrelated values) is not fixed (bad) probably a better fix would be to delete the line here https://github.com/apache/datafusion/blob/77ee675d70545b709b2a13f76c939c519c18493e/datafusion/optimizer/src/simplify_expressions/simplify_predicates.rs#L248-L249 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org