alex-natzka commented on issue #3635:
URL: 
https://github.com/apache/arrow-datafusion/issues/3635#issuecomment-1276121968

   Looks like `CommonSubexprEliminate` creates the second projection which is 
initially _before the filter node_, but then the filter gets pushed down, c.f. 
the ordering of optimization rules here: 
https://github.com/apache/arrow-datafusion/blob/61c38b7114e802f9f289bf5364a031395f5799a6/datafusion/optimizer/src/optimizer.rs#L153-L162
   
   I just tried and I get a more sensible execution plan in the test 
`multiple_or_predicates` in 
https://github.com/apache/arrow-datafusion/blob/d72eb9a1c4c18bcabbf941541a9c1defa83a592c/datafusion/core/tests/sql/predicates.rs#L433,
 if I re-order the optimization rules and place `CommonSubexprEliminate` 
_after_ `FilterPushdown`.
   
   Changing the order of optimization rules here 
https://github.com/apache/arrow-datafusion/blob/61c38b7114e802f9f289bf5364a031395f5799a6/datafusion/optimizer/src/optimizer.rs#L138
 like this 
   
![image](https://user-images.githubusercontent.com/88324834/195346489-e78405eb-7153-48d8-afb6-a4f8d68a2442.png)
   
   yields the following test output:
   ```
   [
       "Explain [plan_type:Utf8, plan:Utf8]",
       "  Projection: lineitem.l_partkey [l_partkey:Int64]",
       "    Filter: part.p_brand = Utf8(\"Brand#12\") AND lineitem.l_quantity 
>= Decimal128(Some(100),15,2) AND lineitem.l_quantity <= 
Decimal128(Some(1100),15,2) AND CAST(part.p_size AS Int64) BETWEEN Int64(1) AND 
Int64(5) OR part.p_brand = Utf8(\"Brand#23\") AND lineitem.l_quantity >= 
Decimal128(Some(1000),15,2) AND lineitem.l_quantity <= 
Decimal128(Some(2000),15,2) AND CAST(part.p_size AS Int64) BETWEEN Int64(1) AND 
Int64(10) OR part.p_brand = Utf8(\"Brand#34\") AND lineitem.l_quantity >= 
Decimal128(Some(2000),15,2) AND lineitem.l_quantity <= 
Decimal128(Some(3000),15,2) AND CAST(part.p_size AS Int64) BETWEEN Int64(1) AND 
Int64(15) [l_partkey:Int64, l_quantity:Decimal128(15, 2), p_partkey:Int64, 
p_brand:Utf8, p_size:Int32]",
       "      Inner Join: lineitem.l_partkey = part.p_partkey [l_partkey:Int64, 
l_quantity:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
       "        TableScan: lineitem projection=[l_partkey, l_quantity] 
[l_partkey:Int64, l_quantity:Decimal128(15, 2)]",
       "        TableScan: part projection=[p_partkey, p_brand, p_size] 
[p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
   ]
   actual:
   
   [
       "Explain [plan_type:Utf8, plan:Utf8]",
       "  Projection: lineitem.l_partkey [l_partkey:Int64]",
       "    Filter: part.p_brand = Utf8(\"Brand#12\") AND lineitem.l_quantity 
>= Decimal128(Some(100),15,2) AND lineitem.l_quantity <= 
Decimal128(Some(1100),15,2) AND part.p_size <= Int32(5) OR part.p_brand = 
Utf8(\"Brand#23\") AND lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND 
lineitem.l_quantity <= Decimal128(Some(2000),15,2) AND part.p_size <= Int32(10) 
OR part.p_brand = Utf8(\"Brand#34\") AND lineitem.l_quantity >= 
Decimal128(Some(2000),15,2) AND lineitem.l_quantity <= 
Decimal128(Some(3000),15,2) AND part.p_size <= Int32(15) [l_partkey:Int64, 
l_quantity:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
       "      Inner Join: lineitem.l_partkey = part.p_partkey [l_partkey:Int64, 
l_quantity:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
       "        TableScan: lineitem projection=[l_partkey, l_quantity] 
[l_partkey:Int64, l_quantity:Decimal128(15, 2)]",
       "        Filter: part.p_size >= Int32(1) [p_partkey:Int64, p_brand:Utf8, 
p_size:Int32]",
       "          TableScan: part projection=[p_partkey, p_brand, p_size], 
partial_filters=[part.p_size >= Int32(1)] [p_partkey:Int64, p_brand:Utf8, 
p_size:Int32]",
   ]
   ```
   
   which looks better to me.


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to