@Override
  public Object evaluate(DeferredObject[] arguments) throws HiveException {
    if (arguments[0].get() == null || arguments[1].get() == null
        || (isPosConst && posConst == null)
        || (isOccurrenceConst && occurrenceConst == null)) {
      return null;
    }
    int pos = 1;
    if (isPosConst) {
      pos = posConst;
    } else if (arguments.length > 2) {
      IntWritable posWritable = (IntWritable) converters[2].convert(arguments[2].get());
      if (posWritable == null) {
        return null;
      }
      pos = posWritable.get();
      if (pos == 0) {
        intWritable.set(0);
        return intWritable;
      }
    }
    int occurrence = 1;
    if (isOccurrenceConst) {
      occurrence = occurrenceConst;
    } else if (arguments.length > 3) {
      IntWritable occurrenceWritable = (IntWritable) converters[3]
          .convert(arguments[3].get());
      if (occurrenceWritable == null) {
        return null;
      }
      occurrence = occurrenceWritable.get();
      if (occurrence <= 0) {
        // The illegal occurrence is not a const value so we can't fail the query at
        // semantic analysis. Just returns NULL for this row.
        return null;
      }
    }

    Text text = (Text) converters[0].convert(arguments[0].get());
    Text subtext = (Text) converters[1].convert(arguments[1].get());
    String textString = text.toString();
    String subtextString = subtext.toString();

    int matchPos = -1;
    int matchNum;
    if (pos > 0) {
      int beg = pos - 1;
      for (matchNum = 0; matchNum < occurrence && beg < textString.length(); matchNum++) {
        matchPos = textString.indexOf(subtextString, beg);
        if (matchPos == -1) break;
        beg = matchPos + 1;
      }
    } else {
      int beg = textString.length() + pos;
      for (matchNum = 0; matchNum < occurrence && beg >= 0; matchNum++) {
        matchPos = textString.lastIndexOf(subtextString, beg);
        if (matchPos == -1) break;
        beg = matchPos - 1;
      }
    }
    if (matchNum < occurrence) {
      matchPos = -1;
    }

    intWritable.set(matchPos + 1);
    return intWritable;
  }
Generic UDF for string function <code>INSTR(str,substr[,pos[,occurrence]])</code>.
This extends the function from MySQL
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_instr
and mimics the function from Oracle
https://docs.oracle.com/database/121/SQLRF/functions089.htm#SQLRF00651

<pre>
usage:
INSTR(str, substr[, pos[, occurrence]])
</pre>
<p>
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFInstr.java