xngin
xngin copied to clipboard
Enhance derived unfold rule to handle expressions other than column ref
This rule does not support unfold derived table as inner side of outer join if the output contains expressions other than columns.
The reason is that outer join may change the output of inner side, and it's not safe to unfold and replace expressions in parent tree with derived table's output directly.
MySQL's uses ItemRef
to implements its "derived merge" logic. Additional care will be taken in join operator to make sure non-matched rows only result in NULL.
We can take this approach, to add a InnerRef
variant in Expr
, then logical optimization is free to unfold outer join's children.
When bottom up traversing the parent tree, we do normal replace.
Once outer join encountered and the unfold is from the inner side, we can wrap original expressions(other than single column) with InnerRef
and store them into join operator. Join operator will evaluate them with care of outer join semantics.