@Override public Tuple next() throws IOException { Tuple aTuple; Tuple outTuple = new VTuple(outColumnNum); if (isClosed) { return null; } if (cachedData.size() == 0) { rescan(); } if (!scanNode.hasQual()) { if (currentRow < cachedData.size()) { aTuple = cachedData.get(currentRow++); projector.eval(aTuple, outTuple); outTuple.setOffset(aTuple.getOffset()); return outTuple; } return null; } else { while (currentRow < cachedData.size()) { aTuple = cachedData.get(currentRow++); if (qual.eval(aTuple).isTrue()) { projector.eval(aTuple, outTuple); return outTuple; } } return null; } }
public MergeJoinExec( TaskAttemptContext context, JoinNode plan, PhysicalExec outer, PhysicalExec inner, SortSpec[] outerSortKey, SortSpec[] innerSortKey) { super(context, plan.getInSchema(), plan.getOutSchema(), outer, inner); Preconditions.checkArgument( plan.hasJoinQual(), "Sort-merge join is only used for the equi-join, " + "but there is no join condition"); this.joinNode = plan; this.joinQual = plan.getJoinQual(); this.qualCtx = this.joinQual.newContext(); this.outerTupleSlots = new ArrayList<Tuple>(INITIAL_TUPLE_SLOT); this.innerTupleSlots = new ArrayList<Tuple>(INITIAL_TUPLE_SLOT); SortSpec[][] sortSpecs = new SortSpec[2][]; sortSpecs[0] = outerSortKey; sortSpecs[1] = innerSortKey; this.joincomparator = new JoinTupleComparator(outer.getSchema(), inner.getSchema(), sortSpecs); this.tupleComparator = PlannerUtil.getComparatorsFromJoinQual( plan.getJoinQual(), outer.getSchema(), inner.getSchema()); this.outerIterator = outerTupleSlots.iterator(); this.innerIterator = innerTupleSlots.iterator(); // for projection this.projector = new Projector(inSchema, outSchema, plan.getTargets()); this.evalContexts = projector.renew(); // for join frameTuple = new FrameTuple(); outTuple = new VTuple(outSchema.getColumnNum()); }
public Tuple next() throws IOException { Tuple previous; for (; ; ) { if (!outerIterator.hasNext() && !innerIterator.hasNext()) { if (end) { return null; } if (outerTuple == null) { outerTuple = leftChild.next(); } if (innerTuple == null) { innerTuple = rightChild.next(); } outerTupleSlots.clear(); innerTupleSlots.clear(); int cmp; while ((cmp = joincomparator.compare(outerTuple, innerTuple)) != 0) { if (cmp > 0) { innerTuple = rightChild.next(); } else if (cmp < 0) { outerTuple = leftChild.next(); } if (innerTuple == null || outerTuple == null) { return null; } } try { previous = outerTuple.clone(); do { outerTupleSlots.add(outerTuple.clone()); outerTuple = leftChild.next(); if (outerTuple == null) { end = true; break; } } while (tupleComparator[0].compare(previous, outerTuple) == 0); outerIterator = outerTupleSlots.iterator(); outerNext = outerIterator.next(); previous = innerTuple.clone(); do { innerTupleSlots.add(innerTuple.clone()); innerTuple = rightChild.next(); if (innerTuple == null) { end = true; break; } } while (tupleComparator[1].compare(previous, innerTuple) == 0); innerIterator = innerTupleSlots.iterator(); } catch (CloneNotSupportedException e) { } } if (!innerIterator.hasNext()) { outerNext = outerIterator.next(); innerIterator = innerTupleSlots.iterator(); } frameTuple.set(outerNext, innerIterator.next()); joinQual.eval(qualCtx, inSchema, frameTuple); if (joinQual.terminate(qualCtx).asBool()) { projector.eval(evalContexts, frameTuple); projector.terminate(evalContexts, outTuple); return outTuple; } } }