Support type coercion for subquery
Is your feature request related to a problem or challenge? Please describe what you are trying to do. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] (This section helps Arrow developers understand the context and why for this feature, in addition to the what)
When I do https://github.com/apache/arrow-datafusion/pull/3396, the type coercion should be first do before other optimization.
But for the recursion subquery, we can't do the type coercion before generated the subquery sub expr or plan.
like in this test correlated_recursive_scalar_subquery
If I move the type coercion to the first rule of the optimizer, i will get the changes
Sort: #customer.c_custkey ASC NULLS LAST
Projection: #customer.c_custkey
Filter: CAST(#customer.c_acctbal AS Decimal128(25, 2)) < #__sq_2.__value
Inner Join: #customer.c_custkey = #__sq_2.o_custkey
TableScan: customer projection=[c_custkey, c_acctbal]
Projection: #orders.o_custkey, #SUM(orders.o_totalprice) AS __value, alias=__sq_2
Aggregate: groupBy=[[#orders.o_custkey]], aggr=[[SUM(#orders.o_totalprice)]]
Filter: CAST(#orders.o_totalprice AS Decimal128(25, 2)) < #__sq_1.__value
Inner Join: #orders.o_orderkey = #__sq_1.l_orderkey
TableScan: orders projection=[o_orderkey, o_custkey, o_totalprice]
Projection: #lineitem.l_orderkey, #SUM(lineitem.l_extendedprice) AS price AS __value, alias=__sq_1
Aggregate: groupBy=[[#lineitem.l_orderkey]], aggr=[[SUM(#lineitem.l_extendedprice)]]
TableScan: lineitem projection=[l_orderkey, l_extendedprice]
to
Sort: #customer.c_custkey ASC NULLS LAST
Projection: #customer.c_custkey
Filter: CAST(#customer.c_acctbal AS Decimal128(25, 2)) < #__sq_2.__value
Inner Join: #customer.c_custkey = #__sq_2.o_custkey
TableScan: customer projection=[c_custkey, c_acctbal]
Projection: #orders.o_custkey, #SUM(orders.o_totalprice) AS __value, alias=__sq_2
Aggregate: groupBy=[[#orders.o_custkey]], aggr=[[SUM(#orders.o_totalprice)]]
Filter: #orders.o_totalprice < #__sq_1.__value
Inner Join: #orders.o_orderkey = #__sq_1.l_orderkey
TableScan: orders projection=[o_orderkey, o_custkey, o_totalprice]
Projection: #lineitem.l_orderkey, #SUM(lineitem.l_extendedprice) AS price AS __value, alias=__sq_1
Aggregate: groupBy=[[#lineitem.l_orderkey]], aggr=[[SUM(#lineitem.l_extendedprice)]]
TableScan: lineitem projection=[l_orderkey, l_extendedprice]
Can't get the type coercion result of Filter: #orders.o_totalprice < #__sq_1.__value
Describe the solution you'd like A clear and concise description of what you want to happen.
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
Additional context Add any other context or screenshots about the feature request here.