[SPARK-39841][SQL] simplify conflict binary comparison
What changes were proposed in this pull request?
Currently, We do not handle conflicting binary comparison expressions, Let's say have a query, it filter a < 0 and a > 1, this condition should be a FalseLiteral, we can fold ahead of time to avoid unnecessary calculations.
Have a idea, we can capture a and conjunction and both it's left and right are BinaryComparisonExpression, we optimize this predicate as following step:
- Normal and left and right comparison expression, to keep Attribute is left side and literal is right side.
- If both comparison expression left child is semantic equal and right child is foldable, we compute a range for column predicate comparison .
- Then update this column comparison predicate by this range.
Before this pr:

After this pr:

Why are the changes needed?
Fold ahead to avoid unnecessary calculations & improve performance
Does this PR introduce any user-facing change?
No, improve only
How was this patch tested?
Add new tests
Can one of the admins verify this patch?
@cloud-fan could you please take a look when you have a time, thanks
can you briefly explain your idea? Do you keep a range for each column and update the range when seeing a comparison? Then use the range to update the column comparison predicate?
can you briefly explain your idea? Do you keep a range for each column and update the range when seeing a comparison? Then use the range to update the column comparison predicate?
Yea, if capture a and conjunction and both it's left and right are BinaryComparisonExpression, we optimize this predicate as following step:
- Normal
andleft and right comparison expression, to keepAttributeis left side andliteralis right side. - If both comparison expression left child is semantic equal and right child is foldable, we compute a range for comparison left side.
- Then update this column comparison predicate by this range.
cc @gengliangwang
friendly ping @gengliangwang @cloud-fan , could you please take a look when you have a time?
cc @sigmod as well
friendly ping @gengliangwang @cloud-fan @sigmod, Sorry for late reply, i was busy last week, i fixed comments you left, please take a look again when you have a time~
@Yikf Is there any mainstream database that supports this optimization?
@Yikf Is there any mainstream database that supports this optimization?
I found a similar optimization in Trino; as follow: