public Tuple exec()

in src/main/java/org/apache/datasketches/pig/quantiles/GetQuantilesFromStringsSketch.java [44:79]


  public Tuple exec(final Tuple input) throws IOException {
    if (input.size() < 2) {
      throw new IllegalArgumentException("expected two or more inputs: sketch and list of fractions");
    }

    if (!(input.get(0) instanceof DataByteArray)) {
      throw new IllegalArgumentException(
          "expected a DataByteArray as a sketch, got " + input.get(0).getClass().getSimpleName());
    }
    final DataByteArray dba = (DataByteArray) input.get(0);
    final ItemsSketch<String> sketch = ItemsSketch.getInstance(
        Memory.wrap(dba.get()), Comparator.naturalOrder(), new ArrayOfStringsSerDe());
    if (sketch.isEmpty()) { return null; }

    if (input.size() == 2) {
      final Object arg = input.get(1);
      if (arg instanceof Integer) { // number of evenly spaced intervals
        return TupleFactory.getInstance().newTuple(Arrays.asList(sketch.getQuantiles((int) arg)));
      } else if (arg instanceof Double) { // just one fraction
        return TupleFactory.getInstance().newTuple(Arrays.asList(sketch.getQuantile((double) arg)));
      } else {
        throw new IllegalArgumentException("expected a double value as a fraction or an integer value"
            + " as a number of evenly spaced intervals, got " + arg.getClass().getSimpleName());
      }
    }
    // more than one number - must be double fractions
    final double[] fractions = new double[input.size() - 1];
    for (int i = 1; i < input.size(); i++) {
      if (!(input.get(i) instanceof Double)) {
        throw new IllegalArgumentException(
            "expected a double value as a fraction, got " + input.get(i).getClass().getSimpleName());
      }
      fractions[i - 1] = (double) input.get(i);
    }
    return TupleFactory.getInstance().newTuple(Arrays.asList(sketch.getQuantiles(fractions)));
  }