public HiveChar getHiveChar() { // Copy string value as-is return new HiveChar(value.toString(), -1); }
@Override public Object initValue(Object ignored) { return ((SettableHiveCharObjectInspector) this.objectInspector) .create(new HiveChar(StringUtils.EMPTY, -1)); } }.init(fieldObjInspector);
public HiveCharConverter(PrimitiveObjectInspector inputOI, SettableHiveCharObjectInspector outputOI) { this.inputOI = inputOI; this.outputOI = outputOI; hc = outputOI.create(new HiveChar("",-1)); }
private HiveChar getPrimitiveWithParams(HiveChar val) { HiveChar hc = new HiveChar(val, getMaxLength()); return hc; }
public Object create(HiveChar value) { HiveChar hc = new HiveChar(value, getMaxLength()); return hc; }
public Object set(Object o, String value) { return new HiveChar(value, getMaxLength()); }
@Override public Object initValue(Object ignored) { return ((SettableHiveCharObjectInspector) this.objectInspector) .create(new HiveChar(StringUtils.EMPTY, -1)); } }.init(fieldObjInspector);
public Object set(Object o, HiveChar value) { if (BaseCharUtils.doesPrimitiveMatchTypeParams(value, (CharTypeInfo) typeInfo)) { return value; } else { return new HiveChar(value, getMaxLength()); } }
public HiveCharWritable getPrimitiveWritableObject(Object o) { if (o == null) { return null; } HiveChar var; if (o instanceof String) { var = new HiveChar((String) o, getMaxLength()); } else { var = (HiveChar) o; } return getWritableWithParams(var); }
private static HiveCharWritable charW(String value, int length) { return new HiveCharWritable(new HiveChar(value, length)); }
@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)); } } }
private HiveChar getPrimitiveWithParams(HiveCharWritable val) { HiveChar hv = new HiveChar(); hv.setValue(val.getHiveChar(), getMaxLength()); return hv; }
public HiveChar getPrimitiveJavaObject(Object o) { if (o == null) { return null; } HiveChar value; if (o instanceof String) { value = new HiveChar((String) o, getMaxLength()); } else { value = (HiveChar) o; } if (BaseCharUtils.doesPrimitiveMatchTypeParams(value, (CharTypeInfo) typeInfo)) { return value; } // value needs to be converted to match type params return getPrimitiveWithParams(value); }
@Override public HiveChar getPrimitiveJavaObject(Object o) { if (o == null) { return null; } HiveChar ret = ((LazyHiveChar) o).getWritableObject().getHiveChar(); if (!BaseCharUtils.doesPrimitiveMatchTypeParams( ret, (CharTypeInfo)typeInfo)) { HiveChar newValue = new HiveChar(ret, ((CharTypeInfo)typeInfo).getLength()); return newValue; } return ret; }
public static HiveChar getRandHiveChar(Random r, CharTypeInfo charTypeInfo, boolean isUnicodeOk) { final int maxLength = 1 + r.nextInt(charTypeInfo.getLength()); final String randomString = getRandString(r, 100, isUnicodeOk); return new HiveChar(randomString, maxLength); }
private static HCatRecord getHCat13TypesRecord() { List<Object> rec_hcat13types = new ArrayList<Object>(5); rec_hcat13types.add(HiveDecimal.create(new BigDecimal("123.45")));//prec 5, scale 2 rec_hcat13types.add(new HiveChar("hive_char", 10)); rec_hcat13types.add(new HiveVarchar("hive_varchar", 20)); rec_hcat13types.add(Date.valueOf("2014-01-06")); rec_hcat13types.add(Timestamp.ofEpochMilli(System.currentTimeMillis())); return new DefaultHCatRecord(rec_hcat13types); } private static HCatRecord getHCat13TypesComplexRecord() {
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; }
@Test public void testChar() throws HiveException { GenericUDFOPNegative udf = new GenericUDFOPNegative(); HiveChar vc = new HiveChar("32300.004747", 12); HiveCharWritable input = new HiveCharWritable(vc); CharTypeInfo inputTypeInfo = TypeInfoFactory.getCharTypeInfo(12); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputTypeInfo), }; DeferredObject[] args = { new DeferredJavaObject(input) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.doubleTypeInfo, oi.getTypeInfo()); DoubleWritable res = (DoubleWritable) udf.evaluate(args); Assert.assertEquals(new Double(-32300.004747), new Double(res.get())); }
@Test @Concurrent(count=4) @Repeating(repetition=100) public void testStringLength() { HiveChar hc = new HiveChar(); hc.setValue("0123456789", 5); assertEquals("01234", hc.toString()); hc.setValue("0123456789", 10); assertEquals("0123456789", hc.toString()); hc.setValue("0123456789", 15); assertEquals("0123456789 ", hc.toString()); }