(Tuple input)
| 324 | * @see org.apache.pig.EvalFunc#exec(org.apache.pig.data.Tuple) |
| 325 | */ |
| 326 | @Override |
| 327 | public Tuple exec(Tuple input) throws IOException { |
| 328 | if (input == null || input.size() < 1) { |
| 329 | return null; |
| 330 | } |
| 331 | try { |
| 332 | DataBag bagOfIntermediates = (DataBag) input.get(0); |
| 333 | Iterator<Tuple> intermediateIterator = bagOfIntermediates.iterator(); |
| 334 | if (!intermediateIterator.hasNext()) { |
| 335 | return null; |
| 336 | } |
| 337 | Tuple peekTuple = intermediateIterator.next(); |
| 338 | if (peekTuple == null || peekTuple.size() < 3 ) return null; |
| 339 | int n = (Integer) peekTuple.get(0); |
| 340 | int fieldNum = (Integer) peekTuple.get(1); |
| 341 | DataBag inputBag = (DataBag) peekTuple.get(2); |
| 342 | boolean allInputBagsNull = true; |
| 343 | |
| 344 | PriorityQueue<Tuple> store = new PriorityQueue<Tuple>(n + 1, |
| 345 | new TupleComparator(fieldNum, sortDesc)); |
| 346 | |
| 347 | if (inputBag != null) { |
| 348 | allInputBagsNull = false; |
| 349 | updateTop(store, n, inputBag); |
| 350 | } |
| 351 | |
| 352 | while (intermediateIterator.hasNext()) { |
| 353 | Tuple t = intermediateIterator.next(); |
| 354 | if (t == null || t.size() < 3 ) continue; |
| 355 | inputBag = (DataBag) t.get(2); |
| 356 | if (inputBag != null) { |
| 357 | allInputBagsNull = false; |
| 358 | updateTop(store, n, inputBag); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | Tuple retTuple = mTupleFactory.newTuple(3); |
| 363 | retTuple.set(0, n); |
| 364 | retTuple.set(1,fieldNum); |
| 365 | DataBag outputBag = null; |
| 366 | if (!allInputBagsNull) { |
| 367 | outputBag = mBagFactory.newDefaultBag(); |
| 368 | for (Tuple t : store) { |
| 369 | outputBag.add(t); |
| 370 | } |
| 371 | } |
| 372 | retTuple.set(2, outputBag); |
| 373 | if (log.isDebugEnabled()) { |
| 374 | if (randomizer.nextInt(1000) == 1) log.debug("outputting "+retTuple.toDelimitedString("\t")); |
| 375 | } |
| 376 | return retTuple; |
| 377 | } catch (ExecException e) { |
| 378 | throw new RuntimeException("ExecException executing function: ", e); |
| 379 | } catch (Exception e) { |
| 380 | throw new RuntimeException("General Exception executing function: ", e); |
| 381 | } |
| 382 | } |
| 383 |
nothing calls this directly
no test coverage detected