public HiveCharWritable(HiveChar hc) { set(hc); }
public HiveCharWritable(HiveCharWritable hcw) { set(hcw); }
public void setValue(LazyHiveChar copy) { data.set(copy.data, maxLength); }
public void set(String val) { set(val, -1); }
@Override public Object set(Object o, HiveChar value) { if (value == null) { return null; } HiveCharWritable writable = (HiveCharWritable) o; writable.set(value, getMaxLength()); return o; }
@Override public Object set(Object o, String value) { if (value == null) { return null; } HiveCharWritable writable = (HiveCharWritable) o; writable.set(value, getMaxLength()); return o; }
public void set(HiveCharWritable val, int maxLength) { set(val.getHiveChar(), maxLength); }
public void set(HiveChar val, int len) { set(val.getValue(), len); }
public void set(HiveChar val) { set(val.getValue(), -1); }
private HiveCharWritable getWritableWithParams(HiveCharWritable val) { HiveCharWritable newValue = new HiveCharWritable(); newValue.set(val, getMaxLength()); return newValue; }
@Override public Object create(HiveChar value) { HiveCharWritable ret; ret = new HiveCharWritable(); ret.set(value, getMaxLength()); return ret; }
private HiveCharWritable getWritableWithParams(HiveChar val) { HiveCharWritable hcw = new HiveCharWritable(); hcw.set(val, getMaxLength()); return hcw; }
public void enforceMaxLength(int maxLength) { if (getCharacterLength()!=maxLength) set(getHiveChar(), maxLength); }
public Object setReturnValue(String val) throws UDFArgumentException { if (val == null) { return null; } switch (type) { case STRING: ((Text)returnValue).set(val); return returnValue; case CHAR: ((HiveCharWritable) returnValue).set(val); return returnValue; case VARCHAR: ((HiveVarcharWritable)returnValue).set(val); return returnValue; default: throw new UDFArgumentException("Bad return type " + type); } }
@Override public void init(ByteArrayRef bytes, int start, int length) { if (oi.isEscaped()) { Text textData = data.getTextValue(); // This is doing a lot of copying here, this could be improved by enforcing length // at the same time as escaping rather than as separate steps. LazyUtils.copyAndEscapeStringDataToText(bytes.getData(), start, length, oi.getEscapeChar(),textData); data.set(textData.toString(), maxLength); isNull = false; } else { String byteData = null; try { byteData = Text.decode(bytes.getData(), start, length); data.set(byteData, maxLength); isNull = false; } catch (CharacterCodingException e) { isNull = true; LOG.debug("Data not in the HiveChar data type range so converted to null.", e); } } }
public Object copyObject(Object o) { if (o == null) { return null; } if (o instanceof Text) { String str = ((Text)o).toString(); HiveCharWritable hcw = new HiveCharWritable(); hcw.set(str, ((CharTypeInfo)typeInfo).getLength()); return hcw; } HiveCharWritable writable = (HiveCharWritable) o; if (doesWritableMatchTypeParams((HiveCharWritable) o)) { return new HiveCharWritable(writable); } return getWritableWithParams(writable); }
@Override public Object getTransformedWritable(DeferredObject object) throws HiveException { HiveChar value = columnType.getPrimitiveJavaObject(object.get()); if(value != null) { String transformedValue = transformer.transform(value.getValue()); if(transformedValue != null) { writable.set(transformedValue); return writable; } } return null; } }
@Override public Object getTransformedWritable(DeferredObject object) throws HiveException { HiveChar value = columnType.getPrimitiveJavaObject(object.get()); if(value != null) { String transformedValue = transformer.transform(value.getValue()); if(transformedValue != null) { writable.set(transformedValue); return writable; } } return null; } }
@Test public void testCharFormat() throws HiveException { GenericUDFPrintf udf = new GenericUDFPrintf(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getCharTypeInfo(10)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getVarcharTypeInfo(7)) }; HiveCharWritable formatChar = new HiveCharWritable(); formatChar.set("arg1=%s"); HiveVarcharWritable argVarchar = new HiveVarcharWritable(); argVarchar.set("world"); DeferredObject[] args = { new DeferredJavaObject(formatChar), new DeferredJavaObject(argVarchar) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(PrimitiveObjectInspectorFactory.writableStringObjectInspector, oi); Text res = (Text) udf.evaluate(args); Assert.assertEquals("arg1=world", res.toString()); }
@Test public void testVarcharFormat() throws HiveException { GenericUDFPrintf udf = new GenericUDFPrintf(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getVarcharTypeInfo(7)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getCharTypeInfo(5)) }; HiveCharWritable argChar = new HiveCharWritable(); argChar.set("hello"); HiveVarcharWritable formatVarchar = new HiveVarcharWritable(); formatVarchar.set("arg1=%s"); DeferredObject[] args = { new DeferredJavaObject(formatVarchar), new DeferredJavaObject(argChar) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(PrimitiveObjectInspectorFactory.writableStringObjectInspector, oi); Text res = (Text) udf.evaluate(args); Assert.assertEquals("arg1=hello", res.toString()); }