break; case DataType.DOUBLE: tuple.set(i, DataType.toDouble(object)); break; case DataType.FLOAT:
/** * Force a data object to a Double, if possible. Any numeric type * can be forced to a Double, as well * as CharArray, ByteArray, or Boolean. Complex types cannot be * forced to an Double. This isn't particularly efficient, so if you * already <b>know</b> that the object you have is a Double you * should just cast it. Unlike {@link #toDouble(Object, byte)} this * method will first determine the type of o and then do the cast. * Use {@link #toDouble(Object, byte)} if you already know the type. * @param o object to cast * @return The object as a Double. * @throws ExecException if the type can't be forced to an Double. */ public static Double toDouble(Object o) throws ExecException { return toDouble(o, findType(o)); }
/** * java level API * @param input expects a single numeric value * @return output returns a single numeric value, absolute value of the argument */ public Double exec(Tuple input) throws IOException { if (input == null || input.size() == 0 || input.get(0) == null) return null; Double d; try{ d = DataType.toDouble(input.get(0)); } catch (NumberFormatException nfe){ System.err.println("Failed to process input; error - " + nfe.getMessage()); return null; } catch (Exception e){ throw new IOException("Caught exception processing input row ", e); } return Math.abs(d); }
/** * java level API * @param input expects a single numeric value * @return output returns a single numeric value, * the closest long to the argument */ @Override public Long exec(Tuple input) throws IOException { if (input == null || input.size() == 0 || input.get(0) == null) return null; try{ Double d = DataType.toDouble(input.get(0)); return Math.round(d); } catch (NumberFormatException nfe){ System.err.println("Failed to process input; error - " + nfe.getMessage()); return null; } catch (Exception e){ throw new IOException("Caught exception processing input row ", e); } }
/** * java level API * @param input expects a numeric value to round, a number of digits to keep, and an optional rounding mode. * @return output returns a single numeric value, the number with only those digits retained */ @Override public Double exec(Tuple input) throws IOException { if (input == null || input.size() < 2) return null; try { Double num = DataType.toDouble(input.get(0)); Integer digits = DataType.toInteger(input.get(1)); RoundingMode mode = (input.size() >= 3) ? RoundingMode.valueOf(DataType.toInteger(input.get(2))) : RoundingMode.HALF_EVEN; if (num == null) return null; BigDecimal bdnum = BigDecimal.valueOf(num); bdnum = bdnum.setScale(digits, mode); return bdnum.doubleValue(); } catch (NumberFormatException nfe){ System.err.println("Failed to process input; error - " + nfe.getMessage()); return null; } catch (Exception e){ throw new IOException("Caught exception processing input row ", e); } }
public DataByteArray exec(Tuple input) throws IOException { if (input == null || input.size() != dimension) { throw new IllegalArgumentException("Tuple doesn't have " + dimension + " elements"); } else { Vector r = new DenseVector(dimension); for (int i = 0; i < dimension; i++) { final Object v = input.get(i); if (v != null) { r.set(i, DataType.toDouble(v)); } else { r.set(i, Double.NaN); } } return PigVector.toBytes(r); } }
private TypedObject getTypedObject(Object data, FieldDetail detail) throws ExecException { if (data == null) { return null; } byte type = detail.type; switch (type) { case DataType.BOOLEAN: return TypeSystem.asTypedObject(DataType.toBoolean(data, type)); case DataType.INTEGER: case DataType.LONG: return TypeSystem.asTypedObject(DataType.toLong(data, type)); case DataType.FLOAT: case DataType.DOUBLE: return TypeSystem.asTypedObject(DataType.toDouble(data, type)); case DataType.DATETIME: return TypeSystem.asTypedObject(new Timestamp(DataType.toDateTime(data, type).getMillis())); case DataType.BYTE: case DataType.BYTEARRAY: case DataType.CHARARRAY: return TypeSystem.asTypedObject(DataType.toString(data, type)); case DataType.BIGINTEGER: case DataType.BIGDECIMAL: return TypeSystem.asTypedObject(DataType.toBigDecimal(data, type)); default: //TUPLE, BAG, MAP, INTERNALMAP, GENERIC_WRITABLECOMPARABLE, ERROR, UNKNOWN, NULL and anything else return null; } }
return DataType.toLong(o); case DataType.DOUBLE: return DataType.toDouble(o); case DataType.FLOAT: return DataType.toFloat(o);
break; case DataType.DOUBLE: tuple.set(i, DataType.toDouble(object)); break; case DataType.FLOAT: