Search in sources :

Example 11 with PrimitiveObjectInspectorFactory.writableDateObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector in project hive by apache.

the class GenericUDFToDate method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 1) {
        throw new UDFArgumentLengthException("The function CAST as DATE requires at least one argument, got " + arguments.length);
    }
    try {
        argumentOI = (PrimitiveObjectInspector) arguments[0];
        PrimitiveCategory pc = argumentOI.getPrimitiveCategory();
        PrimitiveGrouping pg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pc);
        switch(pg) {
            case DATE_GROUP:
            case STRING_GROUP:
            case VOID_GROUP:
                break;
            default:
                throw new UDFArgumentException("CAST as DATE only allows date,string, or timestamp types");
        }
    } catch (ClassCastException e) {
        throw new UDFArgumentException("The function CAST as DATE takes only primitive types");
    }
    dc = new DateConverter(argumentOI, PrimitiveObjectInspectorFactory.writableDateObjectInspector);
    return PrimitiveObjectInspectorFactory.writableDateObjectInspector;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) DateConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.DateConverter) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) PrimitiveGrouping(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping)

Example 12 with PrimitiveObjectInspectorFactory.writableDateObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector in project hive by apache.

the class TestGenericUDFSortArray method testSortStruct.

@Test
public void testSortStruct() throws HiveException {
    ObjectInspector[] inputOIs = { ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getStandardStructObjectInspector(asList("f1", "f2", "f3", "f4"), asList(PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableDoubleObjectInspector, PrimitiveObjectInspectorFactory.writableDateObjectInspector, ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableIntObjectInspector)))) };
    udf.initialize(inputOIs);
    Object i1 = asList(new Text("a"), new DoubleWritable(3.1415), new DateWritable(new Date(2015, 5, 26)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(4)));
    Object i2 = asList(new Text("b"), new DoubleWritable(3.14), new DateWritable(new Date(2015, 5, 26)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(4)));
    Object i3 = asList(new Text("a"), new DoubleWritable(3.1415), new DateWritable(new Date(2015, 5, 25)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(5)));
    Object i4 = asList(new Text("a"), new DoubleWritable(3.1415), new DateWritable(new Date(2015, 5, 25)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(4)));
    runAndVerify(asList(i1, i2, i3, i4), asList(i4, i3, i1, i2));
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) Text(org.apache.hadoop.io.Text) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) Date(java.sql.Date) IntWritable(org.apache.hadoop.io.IntWritable) Test(org.junit.Test)

Example 13 with PrimitiveObjectInspectorFactory.writableDateObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector in project hive by apache.

the class TestGenericUDFOPPlus method testIntervalYearMonthPlusDate.

@Test
public void testIntervalYearMonthPlusDate() throws Exception {
    GenericUDFOPPlus udf = new GenericUDFOPPlus();
    HiveIntervalYearMonthWritable left = new HiveIntervalYearMonthWritable(HiveIntervalYearMonth.valueOf("2-8"));
    DateWritable right = new DateWritable(Date.valueOf("2001-06-15"));
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableHiveIntervalYearMonthObjectInspector, PrimitiveObjectInspectorFactory.writableDateObjectInspector };
    DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(TypeInfoFactory.dateTypeInfo, oi.getTypeInfo());
    DateWritable res = (DateWritable) udf.evaluate(args);
    Assert.assertEquals(Date.valueOf("2004-02-15"), res.get());
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) HiveIntervalYearMonthWritable(org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable) Test(org.junit.Test)

Example 14 with PrimitiveObjectInspectorFactory.writableDateObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector in project hive by apache.

the class TestGenericUDFQuarter method testQuarterDt.

public void testQuarterDt() throws HiveException {
    GenericUDFQuarter udf = new GenericUDFQuarter();
    ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
    ObjectInspector[] arguments = { valueOI0 };
    udf.initialize(arguments);
    // positive Unix time
    runAndVerifyDt("2014-01-01", 1, udf);
    runAndVerifyDt("2014-02-10", 1, udf);
    runAndVerifyDt("2014-03-31", 1, udf);
    runAndVerifyDt("2014-04-02", 2, udf);
    runAndVerifyDt("2014-05-28", 2, udf);
    runAndVerifyDt("2016-06-03", 2, udf);
    runAndVerifyDt("2016-07-28", 3, udf);
    runAndVerifyDt("2016-08-29", 3, udf);
    runAndVerifyDt("2016-09-29", 3, udf);
    runAndVerifyDt("2016-10-29", 4, udf);
    runAndVerifyDt("2016-11-29", 4, udf);
    runAndVerifyDt("2016-12-31", 4, udf);
    // negative Unix time
    runAndVerifyDt("1966-01-01", 1, udf);
    runAndVerifyDt("1966-03-31", 1, udf);
    runAndVerifyDt("1966-04-01", 2, udf);
    runAndVerifyDt("1966-12-31", 4, udf);
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)

Example 15 with PrimitiveObjectInspectorFactory.writableDateObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector in project hive by apache.

the class TestGenericUDFToUnixTimestamp method testDate.

public void testDate() throws HiveException {
    GenericUDFToUnixTimeStamp udf = new GenericUDFToUnixTimeStamp();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    Date date = Date.valueOf("1970-01-01");
    runAndVerify(udf, new DateWritable(date), new LongWritable(date.getTime() / 1000));
    // test null values
    runAndVerify(udf, null, null);
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) LongWritable(org.apache.hadoop.io.LongWritable) Date(java.sql.Date)

Aggregations

ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)32 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)22 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)19 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)19 Date (java.sql.Date)13 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)13 Test (org.junit.Test)9 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)5 Text (org.apache.hadoop.io.Text)5 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)4 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)4 TimestampConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter)4 GenericUDFDateAdd (org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateAdd)3 GenericUDFDateSub (org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateSub)3 HiveIntervalDayTimeWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable)3 HiveIntervalYearMonthWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable)3 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)3 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)3 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)2 IntWritable (org.apache.hadoop.io.IntWritable)2