@Override
    public <T> T get(int columnIndex, Class<T> type) {
      if (current[columnIndex] == null) {
        return null;
      }
      if (type.isInstance(current[columnIndex])) {
        return (T) current[columnIndex];
      } else {
        if (current[columnIndex] instanceof Number) {
          if (type == Long.class)
              return type.cast(((Number) current[columnIndex]).longValue());
          if (type == Integer.class)
              return type.cast(((Number) current[columnIndex]).intValue());
          if (type == Short.class)
              return type.cast(((Number) current[columnIndex]).shortValue());
          if (type == Byte.class)
              return type.cast(((Number) current[columnIndex]).byteValue());
          if (type == String.class)
            return (T) String.valueOf(current[columnIndex]);
        }
        // RowSet can never return the HiveDecimal instances created on Hive side, nor its BigDecimal representation.
        // Instead, it gets converted into String object in ColumnBasedSet.addRow()...
        if (type == BigDecimal.class &&
            serdeConstants.DECIMAL_TYPE_NAME.equalsIgnoreCase(metadata(handle).columnTypeName(columnIndex))) {
          return (T) new BigDecimal((String) current[columnIndex]);
        }
        throw new ClassCastException(current[columnIndex].getClass() + " cannot be casted to " + type);
      }
    }
Executing HiveQL from HPL/SQL directly, without JDBC or Thrift.
service/src/java/org/apache/hive/service/cli/operation/hplsql/HplSqlQueryExecutor.java