@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length < 2) { throw new UDFArgumentLengthException( "grouping() requires at least 2 argument, got " + arguments.length); } if (arguments[0].getCategory() != Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "The first argument to grouping() must be primitive"); } PrimitiveObjectInspector arg1OI = (PrimitiveObjectInspector) arguments[0]; // INT can happen in cases where grouping() is used without grouping sets, in all other cases it should be LONG. if (!(arg1OI.getPrimitiveCategory() == PrimitiveCategory.INT || arg1OI.getPrimitiveCategory() == PrimitiveCategory.LONG)) { throw new UDFArgumentTypeException(0, "The first argument to grouping() must be an int/long. Got: " + arg1OI.getPrimitiveCategory()); } groupingIdOI = arg1OI; indices = new int[arguments.length - 1]; for (int i = 1; i < arguments.length; i++) { PrimitiveObjectInspector arg2OI = (PrimitiveObjectInspector) arguments[i]; if (!(arg2OI instanceof ConstantObjectInspector)) { throw new UDFArgumentTypeException(i, "Must be a constant. Got: " + arg2OI.getClass().getSimpleName()); } indices[i - 1] = PrimitiveObjectInspectorUtils .getInt(((ConstantObjectInspector) arguments[i]).getWritableConstantValue(), arg2OI); } return PrimitiveObjectInspectorFactory.writableLongObjectInspector; }
case INT: case LONG: longValue = PrimitiveObjectInspectorUtils.getLong(val, poi); break; case FLOAT: case DOUBLE: doubleValue = PrimitiveObjectInspectorUtils.getDouble(val, poi); break; case STRING: case CHAR: case VARCHAR: bytesVal = PrimitiveObjectInspectorUtils.getString(val, poi).getBytes(); setBytesValue(bytesVal); break; case BINARY: bytesVal = PrimitiveObjectInspectorUtils.getBinary(val, poi).copyBytes(); setBytesValue(bytesVal); break; case DECIMAL: decimalValue = PrimitiveObjectInspectorUtils.getHiveDecimal(val, poi); break; case DATE: longValue = DateWritableV2.dateToDays(PrimitiveObjectInspectorUtils.getDate(val, poi)); case TIMESTAMP: timestampValue = PrimitiveObjectInspectorUtils.getTimestamp(val, poi).toSqlTimestamp(); break; case INTERVAL_YEAR_MONTH: longValue = PrimitiveObjectInspectorUtils.getHiveIntervalYearMonth(val, poi).getTotalMonths(); break;
@Override public Object convert(Object input) { if (input == null) { return null; } try { return outputOI.set(r, PrimitiveObjectInspectorUtils.getFloat(input, inputOI)); } catch (NumberFormatException e) { return null; } } }
/** * Compare 2 Primitive Objects with their Object Inspector, conversions * allowed. Note that NULL does not equal to NULL according to SQL standard. */ public static boolean comparePrimitiveObjectsWithConversion(Object o1, PrimitiveObjectInspector oi1, Object o2, PrimitiveObjectInspector oi2) { if (o1 == null || o2 == null) { return false; } if (oi1.getPrimitiveCategory() == oi2.getPrimitiveCategory()) { return comparePrimitiveObjects(o1, oi1, o2, oi2); } // If not equal, convert all to double and compare try { return getDouble(o1, oi1) == getDouble(o2, oi2); } catch (NumberFormatException e) { return false; } }
switch (oi.getPrimitiveCategory()) { case VOID: break; case STRING: StringObjectInspector soi = (StringObjectInspector) oi; String s = soi.getPrimitiveJavaObject(o).trim(); try { if (s.length() == DATE_LENGTH) { result = Date.valueOf(s); } else { Timestamp ts = getTimestampFromString(s); if (ts != null) { result = Date.ofEpochMilli(ts.toEpochMilli()); case CHAR: case VARCHAR: { String val = getString(o, oi).trim(); try { if (val.length() == DATE_LENGTH) { result = Date.valueOf(val); } else { Timestamp ts = getTimestampFromString(val); if (ts != null) { result = Date.ofEpochMilli(ts.toEpochMilli()); default: throw new RuntimeException("Cannot convert to Date from: " + oi.getTypeName());
break; case STRING: result = HiveDecimal.create(((StringObjectInspector) oi).getPrimitiveJavaObject(o)); break; case CHAR: case VARCHAR: result = HiveDecimal.create(getString(o, oi)); break; case TIMESTAMP: default: throw new RuntimeException("Hive 2 Internal error: unsupported conversion from type: " + oi.getTypeName());
case BOOLEAN: ((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = (PrimitiveObjectInspectorUtils.getBoolean( object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]) ? 1 : 0); break; case BYTE: ((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getByte( object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]); break; case SHORT: ((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getShort( object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]); break; case INT: ((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getInt( object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]); break; case LONG: ((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getLong( object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]); break; PrimitiveObjectInspectorUtils.getTimestamp( object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]); if (timestamp == null) {
switch (oi.getPrimitiveCategory()) { case VOID: result = 0; break; case BOOLEAN: result = (((BooleanObjectInspector) oi).get(o) ? 1 : 0); break; case BYTE: case CHAR: case VARCHAR: { result = Long.parseLong(getString(o, oi)); break; if (!dec.isLong()) { throw new NumberFormatException(); result = dec.longValue(); default: throw new RuntimeException("Hive 2 Internal error: unsupported conversion from type: " + oi.getTypeName());
switch (oi.getPrimitiveCategory()) { case VOID: { result = 0; result = (((BooleanObjectInspector) oi).get(o) ? 1 : 0); break; if (soi.preferWritable()) { Text t = soi.getPrimitiveWritableObject(o); result = LazyInteger.parseInt(t.getBytes(), 0, t.getLength()); } else { String s = soi.getPrimitiveJavaObject(o); result = Integer.parseInt(s); result = Integer.parseInt(getString(o, oi)); break; if (!dec.isInt()) { throw new NumberFormatException(); result = dec.intValue(); default: { throw new RuntimeException("Hive 2 Internal error: unsupported conversion from type: " + oi.getTypeName());
return null; switch (oi.getPrimitiveCategory()) { case VOID: { return null; String s = soi.getPrimitiveJavaObject(o).trim(); return TimestampTZUtil.parseOrNull(trimNanoTimestamp(s), timeZone); String s = getString(o, oi).trim(); return TimestampTZUtil.parseOrNull(trimNanoTimestamp(s), timeZone); " from: " + oi.getTypeName());
public Writable serialize(Object obj, ObjectInspector objInspector) throws Exception { if (objInspector.getCategory() != ObjectInspector.Category.STRUCT) { throw new SerDeException(getClass().toString() + " can only serialize struct types, but we got: " + objInspector.getTypeName()); } else { PrimitiveObjectInspector primitive = (PrimitiveObjectInspector) inspector; timestamp = PrimitiveObjectInspectorUtils.getTimestamp(value, primitive).toEpochMilli();
@Override public Object convert(Object input) { if (input == null) { return null; } switch (inputOI.getPrimitiveCategory()) { case BOOLEAN: return outputOI.set(hc, ((BooleanObjectInspector) inputOI).get(input) ? new HiveChar("TRUE", -1) : new HiveChar("FALSE", -1)); default: return outputOI.set(hc, PrimitiveObjectInspectorUtils.getHiveChar(input, inputOI)); } } }
switch (oi.getPrimitiveCategory()) { case VOID: result = 0; break; case BOOLEAN: result = (((BooleanObjectInspector) oi).get(o) ? 1 : 0); break; case BYTE: case STRING: StringObjectInspector soi = (StringObjectInspector) oi; String s = soi.getPrimitiveJavaObject(o); result = Double.parseDouble(s); break; case CHAR: case VARCHAR: result = Double.parseDouble(getString(o, oi)); break; case TIMESTAMP: case DECIMAL: result = ((HiveDecimalObjectInspector) oi) .getPrimitiveJavaObject(o).doubleValue(); break; case DATE: // unsupported conversion default: throw new RuntimeException("Hive 2 Internal error: unsupported conversion from type: " + oi.getTypeName());
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { if (arguments[0].get() == null) { return null; String separator = PrimitiveObjectInspectorUtils.getString( arguments[0].get(), (PrimitiveObjectInspector)argumentOIs[0]); if (arguments[i].get() != null) { if (first) { first = false; sb.append(separator); if (argumentOIs[i].getCategory().equals(Category.LIST)) { Object strArray = arguments[i].get(); ListObjectInspector strArrayOI = (ListObjectInspector) argumentOIs[i]; sb.append(PrimitiveObjectInspectorUtils.getString( arguments[i].get(), (PrimitiveObjectInspector)argumentOIs[i]));
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { if (arguments[0] == null || arguments[0].get() == null) { return null; return Double.valueOf(((DoubleObjectInspector)argumentOI).get(input)); case STRING: return PrimitiveObjectInspectorUtils.getString(input, (StringObjectInspector)argumentOI); case BINARY: return PrimitiveObjectInspectorUtils.getBinary(input, (BinaryObjectInspector) argumentOI).getBytes(); case VARCHAR: if (outputType == PrimitiveCategory.CHAR) { HiveVarchar hiveVarchar = PrimitiveObjectInspectorUtils.getHiveVarchar(input, (HiveVarcharObjectInspector) argumentOI); return new HiveChar(hiveVarchar.getValue(), HiveChar.MAX_CHAR_LENGTH); } else { return PrimitiveObjectInspectorUtils.getHiveVarchar(input, (HiveVarcharObjectInspector)argumentOI); return PrimitiveObjectInspectorUtils.getHiveChar(input, (HiveCharObjectInspector) argumentOI); case DATE: return PrimitiveObjectInspectorUtils.getDate(input, (DateObjectInspector) argumentOI); case TIMESTAMP: return PrimitiveObjectInspectorUtils.getTimestamp(input, (TimestampObjectInspector) argumentOI); case DECIMAL: PrimitiveObjectInspectorUtils.getHiveDecimal(input, (HiveDecimalObjectInspector) argumentOI); return new HiveVarchar(decimalValue.toString(), HiveVarchar.MAX_VARCHAR_LENGTH);
if(quantilesOI.getCategory() == ObjectInspector.Category.LIST) { StandardListObjectInspector sloi = (StandardListObjectInspector) quantilesOI; for(int i = 0; i < nquantiles; i++) { myagg.quantiles[i] = PrimitiveObjectInspectorUtils.getDouble( sloi.getListElement(parameters[1], i), (PrimitiveObjectInspector) sloi.getListElementObjectInspector()); myagg.quantiles[0] = PrimitiveObjectInspectorUtils.getDouble(parameters[1], (PrimitiveObjectInspector) quantilesOI); throw new HiveException(getClass().getSimpleName() + " requires percentile values to " + "lie strictly between 0 and 1, but you supplied " + myagg.quantiles[i]); if(parameters.length == 3 && nbinsOI != null) { myagg.histogram.allocate(PrimitiveObjectInspectorUtils.getInt(parameters[2], nbinsOI)); } else { double v = PrimitiveObjectInspectorUtils.getDouble(parameters[0], inputOI); myagg.histogram.add(v);
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { Object value = bytesOI.getPrimitiveJavaObject(arguments[0].get()); if (value == null) { return null; } ByteBuffer wrappedBytes = ByteBuffer.wrap((byte[])value); CharBuffer decoded; if (decoder != null){ try { decoded = decoder.decode(wrappedBytes); } catch (CharacterCodingException e) { throw new HiveException(e); } } else { String charSetName = PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI); decoded = Charset.forName(charSetName).decode(wrappedBytes); } return decoded.toString(); }
/** * Get the byte value out of a primitive object. Note that * NullPointerException will be thrown if o is null. Note that * NumberFormatException will be thrown if o is not a valid number. */ public static byte getByte(Object o, PrimitiveObjectInspector oi) { byte result; switch (oi.getPrimitiveCategory()) { case DECIMAL: { HiveDecimal dec = ((HiveDecimalObjectInspector) oi) .getPrimitiveJavaObject(o); if (!dec.isByte()) { throw new NumberFormatException(); } result = dec.byteValue(); } break; default: // For all other data types, use int conversion. At some point, we should have all // conversions make sure the value fits. return (byte) getInt(o, oi); } return result; }
/** * Get the short value out of a primitive object. Note that * NullPointerException will be thrown if o is null. Note that * NumberFormatException will be thrown if o is not a valid number. */ public static short getShort(Object o, PrimitiveObjectInspector oi) { short result; switch (oi.getPrimitiveCategory()) { case DECIMAL: { HiveDecimal dec = ((HiveDecimalObjectInspector) oi) .getPrimitiveJavaObject(o); if (!dec.isShort()) { throw new NumberFormatException(); } result = dec.shortValue(); } break; default: // For all other data types, use int conversion. At some point, we should have all // conversions make sure the value fits. return (short) getInt(o, oi); } return result; }
public static HiveChar getHiveChar(Object o, PrimitiveObjectInspector oi) { if (o == null) { return null; } HiveChar result = null; switch (oi.getPrimitiveCategory()) { case CHAR: result = ((HiveCharObjectInspector) oi).getPrimitiveJavaObject(o); break; default: // No char length available, copy whole string value here. result = new HiveChar(); result.setValue(getString(o, oi)); break; } return result; }