public static String escapeSqlLike(String key) {
    StringBuilder sb = new StringBuilder(key.length());
    for (char c: key.toCharArray()) {
      switch(c) {
      case '_':
      case '%':
      case sqlEscapeChar:
        sb.append(sqlEscapeChar);
        // fall through
      default:
        sb.append(c);
        break;
      }
    }
    return sb.toString();
  }
Escape the '_', '%', as well as the escape characters inside the string key.
@param key the string that will be used for the SQL LIKE operator.
@return a string with escaped '_' and '%'.
ql/src/java/org/apache/hadoop/hive/ql/exec/Utilities.java