This bug occurs when there'a rule matching a Union operator with an unordered operand:
`operand(Union.class, unordered(operand(RelTypeB.class, any())))`,
with an plan tree that the operand to match is not the first input to the union:
The expected behavior is that this plan tree should fire the match, because `unordered` means matching any child of the union operator.
The bug is that the tree is not matched as expected, either matching in descending order (rule is triggered from LogicalUnion) or in ascending order (rule is triggered by adding another node equivalent to RelTypeB).
See the test cases in https://github.com/apache/calcite/pull/784
The cause of this issue is that VolcanoRuleCall doesn't handle `unordered` child operand at all. It uses `operand.ordinalInParent` to check if the matched RelNode matches the operand's ordinal(position) in parent's inputs. The value of `ordinalInParent` is always `0` in this case, requires the matched RelNode to also be the first input.
However, that only makes sense when child policy is `some`, which strictly requires to match in order. For child policy `unordered`, it should match regardless of the position of RelNode in the inputs.
This bug can be fixed with changes to function`VolcanoRuleCall#matchRecurse`: