use of org.apache.hadoop.hive.common.type.HiveVarchar in project hive by apache.
the class TestGenericUDFOPPositive method testVarchar.
@Test
public void testVarchar() throws HiveException {
GenericUDFOPPositive udf = new GenericUDFOPPositive();
HiveVarchar vc = new HiveVarchar("32300.004747", 12);
HiveVarcharWritable input = new HiveVarcharWritable(vc);
VarcharTypeInfo inputTypeInfo = TypeInfoFactory.getVarcharTypeInfo(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(32300.004747, res.get(), EPSILON);
}
use of org.apache.hadoop.hive.common.type.HiveVarchar in project hive by apache.
the class TestGenericUDFSortArrayByField method testSortPrimitiveTupleTwoFieldOrderDESC.
@Test
public void testSortPrimitiveTupleTwoFieldOrderDESC() throws HiveException {
List<ObjectInspector> tuple = new ArrayList<ObjectInspector>();
tuple.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
tuple.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
ObjectInspector[] inputOIs = { ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getStandardStructObjectInspector(asList("Company", "Department"), tuple)), PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector };
udf.initialize(inputOIs);
Object i1 = asList(new Text("Linkedin"), new Text("HR"));
Object i2 = asList(new Text("Linkedin"), new Text("IT"));
Object i3 = asList(new Text("Linkedin"), new Text("Finance"));
Object i4 = asList(new Text("Facebook"), new Text("IT"));
Object i5 = asList(new Text("Facebook"), new Text("Finance"));
Object i6 = asList(new Text("Facebook"), new Text("HR"));
Object i7 = asList(new Text("Google"), new Text("Logistics"));
Object i8 = asList(new Text("Google"), new Text("Finance"));
Object i9 = asList(new Text("Google"), new Text("HR"));
HiveVarchar vc = new HiveVarchar();
vc.setValue("Department");
GenericUDF.DeferredJavaObject[] argas = { new GenericUDF.DeferredJavaObject(asList(i1, i2, i3, i4, i5, i6, i7, i8, i9)), new GenericUDF.DeferredJavaObject(new Text("Company")), new GenericUDF.DeferredJavaObject(new HiveVarcharWritable(vc)), new GenericUDF.DeferredJavaObject(new Text("DESC")) };
runAndVerify(argas, asList(i2, i1, i3, i7, i9, i8, i4, i6, i5));
}
use of org.apache.hadoop.hive.common.type.HiveVarchar in project hive by apache.
the class MyTestPrimitiveClass method getRandHiveVarchar.
public static HiveVarchar getRandHiveVarchar(Random r, ExtraTypeInfo extraTypeInfo) {
int maxLength = 10 + r.nextInt(60);
extraTypeInfo.hiveVarcharMaxLength = maxLength;
String randomString = getRandString(r, "abcdefghijklmnopqrstuvwxyz", 100);
HiveVarchar hiveVarchar = new HiveVarchar(randomString, maxLength);
return hiveVarchar;
}
use of org.apache.hadoop.hive.common.type.HiveVarchar in project hive by apache.
the class MyTestClass method nonRandomFill.
public void nonRandomFill(int idx) {
myByte = (Byte) getNonRandValue(nrByte, idx);
myShort = (Short) getNonRandValue(nrShort, idx);
myInt = (Integer) getNonRandValue(nrInt, idx);
myLong = (Long) getNonRandValue(nrLong, idx);
myFloat = (Float) getNonRandValue(nrFloat, idx);
myDouble = (Double) getNonRandValue(nrDouble, idx);
myString = (String) getNonRandValue(nrString, idx);
myHiveChar = new HiveChar(myString, myString.length());
myHiveVarchar = new HiveVarchar(myString, myString.length());
myDecimal = (HiveDecimal) getNonRandValue(nrDecimal, idx);
myDate = (Date) getNonRandValue(nrDate, idx);
myIntervalYearMonth = (HiveIntervalYearMonth) getNonRandValue(nrIntervalYearMonth, idx);
myIntervalDayTime = (HiveIntervalDayTime) getNonRandValue(nrIntervalDayTime, idx);
myStruct = null;
myList = null;
}
use of org.apache.hadoop.hive.common.type.HiveVarchar in project hive by apache.
the class TestPrimitiveObjectInspectorUtils method testgetTimestampWithMillisecondsInt.
@Test
public void testgetTimestampWithMillisecondsInt() {
DateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
PrimitiveObjectInspector voidOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.VOID);
assertEquals(null, PrimitiveObjectInspectorUtils.getTimestamp(new Object(), voidOI));
PrimitiveObjectInspector booleanOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.BOOLEAN);
assertEquals("1970-01-01 00:00:00.001", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(true, booleanOI).toSqlTimestamp()));
assertEquals("1970-01-01 00:00:00.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(false, booleanOI).toSqlTimestamp()));
PrimitiveObjectInspector byteOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.BYTE);
assertEquals("1970-01-01 00:00:00.001", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((byte) 1, byteOI).toSqlTimestamp()));
assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((byte) -1, byteOI).toSqlTimestamp()));
PrimitiveObjectInspector shortOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.SHORT);
assertEquals("1970-01-01 00:00:00.001", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((short) 1, shortOI).toSqlTimestamp()));
assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((short) -1, shortOI).toSqlTimestamp()));
PrimitiveObjectInspector intOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.INT);
assertEquals("1970-01-17 11:22:01.282", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((int) 1423321282, intOI).toSqlTimestamp()));
assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((int) -1, intOI).toSqlTimestamp()));
PrimitiveObjectInspector longOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.LONG);
assertEquals("1970-01-17 11:22:01.282", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(1423321282L, longOI).toSqlTimestamp()));
assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(-1L, longOI).toSqlTimestamp()));
// Float loses some precisions
PrimitiveObjectInspector floatOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.FLOAT);
assertEquals("2015-02-07 15:02:24.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(1423321282.123f, floatOI).toSqlTimestamp()));
assertEquals("1969-12-31 23:59:58.876", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(-1.123f, floatOI).toSqlTimestamp()));
PrimitiveObjectInspector doubleOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.DOUBLE);
assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((double) 1423321282.123, doubleOI).toSqlTimestamp()));
assertEquals("1969-12-31 23:59:58.877", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((double) -1.123, doubleOI).toSqlTimestamp()));
PrimitiveObjectInspector decimalOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.DECIMAL);
assertEquals("2015-02-07 15:01:22.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(HiveDecimal.create(1423321282L), decimalOI).toSqlTimestamp()));
assertEquals("1969-12-31 23:59:59.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(HiveDecimal.create(-1), decimalOI).toSqlTimestamp()));
PrimitiveObjectInspector stringOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp("2015-02-07 15:01:22.123", stringOI).toSqlTimestamp()));
PrimitiveObjectInspector charOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.CHAR);
assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(new HiveChar("2015-02-07 15:01:22.123", 30), charOI).toSqlTimestamp()));
PrimitiveObjectInspector varcharOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.VARCHAR);
assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(new HiveVarchar("2015-02-07 15:01:22.123", 30), varcharOI).toSqlTimestamp()));
PrimitiveObjectInspector dateOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.DATE);
assertEquals("2015-02-07 00:00:00.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(Date.ofEpochMilli(1423321282123L), dateOI).toSqlTimestamp()));
PrimitiveObjectInspector timestampOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.TIMESTAMP);
assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(Timestamp.ofEpochMilli(1423321282123L), timestampOI).toSqlTimestamp()));
}
Aggregations