Nested Loop Join produces wrong result's if there are multiple batches on the right side. It builds an ExapandableHyperContainer to hold all the right side of batches. Then for each record on left side input evaluates the condition with all records on right side and emit the output if condition is satisfied. The main loop inside populateOutgoingBatch call's doEval with correct indexes to evaluate records on both the sides. In generated code of doEval for some reason there is a right shift of 16 done on the rightBatchIndex (sample shared below).
When the actual loop is processing second batch, inside eval method the index with right shift becomes 0 and it ends up evaluating condition w.r.t first right batch again. So if there is more than one batch (upto 65535) on right side doEval will always consider first batch for condition evaluation. But the output data will be based on correct batch so there will be issues like OutOfBound and WrongData. Cases can be:
Let's say: rightBatchIndex: index of right batch to consider, rightRecordIndexWithinBatch: index of record in right batch at rightBatchIndex
1) First right batch comes with zero data and with OK_NEW_SCHEMA (let's say because of filter in the operator tree). Next Right batch has > 0 data. So when we call doEval for second batch(rightBatchIndex = 1) and first record in it (i.e. rightRecordIndexWithinBatch = 0), actual evaluation will happen using first batch (since rightBatchIndex >>> 16 = 0). On accessing record at rightRecordIndexWithinBatch in first batch it will throw IndexOutofBoundException since the first batch has no records.
2) Let's say there are 2 batches on right side. Also let's say first batch contains 3 records (with id_right=1/2/3) and 2nd batch also contain 3 records (with id_right=10/20/30). Also let's say there is 1 batch on left side with 3 records (with id_left=1/2/3). Then in this case the NestedLoopJoin (with equality condition) will end up producing 6 records instead of 3. It produces first 3 records based on match between left records and match in first right batch records. But while 2nd right batch it will evaluate id_left=id_right based on first batch instead and will again find matches and will produce another 3 records. Example:
Left Batch Data:
Right Batch Data: