In VolcanoRuleCall.matchRecurse(), when ascending (child operand is matched, looking for parent operand match), we want to check that the candidate parent relNode indeed has the previously matched relNode as a child with the right ordinal. However, some candidate parent can have less number of inputs than the parent operand, and thus we hit IndexOutOfBound when trying to grab the correct child for checking.
In the added unit test that repro the bug, we have a union with two inputs of class PhysLeafRel. The rule however, matches a union with three inputs, with the third child operand matching for PhysLeafRel.class. When a child relNode gets matched to the third child operand, we go up trying to see whether the union relNode can match the parent. Trying to access the union's third input hits the IndexOutOfBound error because the union only has two inputs.