Search in sources :

Example 56 with DateWritable

use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.

the class GenericUDFCurrentDate method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 0) {
        throw new UDFArgumentLengthException("The function CURRENT_DATE does not take any arguments, but found " + arguments.length);
    }
    if (currentDate == null) {
        Date dateVal = Date.valueOf(SessionState.get().getQueryCurrentTimestamp().toString().substring(0, 10));
        currentDate = new DateWritable(dateVal);
    }
    return PrimitiveObjectInspectorFactory.writableDateObjectInspector;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) Date(java.sql.Date)

Example 57 with DateWritable

use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.

the class GenericUDFCurrentDate method copyToNewInstance.

@Override
public void copyToNewInstance(Object newInstance) throws UDFArgumentException {
    super.copyToNewInstance(newInstance);
    // Need to preserve currentDate
    GenericUDFCurrentDate other = (GenericUDFCurrentDate) newInstance;
    if (this.currentDate != null) {
        other.currentDate = new DateWritable(this.currentDate);
    }
}
Also used : DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable)

Example 58 with DateWritable

use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.

the class GenericUDFDateDiff method checkArguments.

private Converter checkArguments(ObjectInspector[] arguments, int i) throws UDFArgumentException {
    if (arguments[i].getCategory() != ObjectInspector.Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but " + arguments[i].getTypeName() + " is passed. as first arguments");
    }
    PrimitiveCategory inputType = ((PrimitiveObjectInspector) arguments[i]).getPrimitiveCategory();
    Converter converter;
    switch(inputType) {
        case STRING:
        case VARCHAR:
        case CHAR:
            converter = ObjectInspectorConverters.getConverter((PrimitiveObjectInspector) arguments[i], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
            break;
        case TIMESTAMP:
            converter = new TimestampConverter((PrimitiveObjectInspector) arguments[i], PrimitiveObjectInspectorFactory.writableTimestampObjectInspector);
            break;
        case DATE:
            converter = ObjectInspectorConverters.getConverter((PrimitiveObjectInspector) arguments[i], PrimitiveObjectInspectorFactory.writableDateObjectInspector);
            break;
        default:
            throw new UDFArgumentException(" DATEDIFF() only takes STRING/TIMESTAMP/DATEWRITABLE types as " + (i + 1) + "-th argument, got " + inputType);
    }
    return converter;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) TimestampConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter) TimestampConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter) Converter(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Example 59 with DateWritable

use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.

the class TestGenericUDFDate method testTimestampToDate.

public void testTimestampToDate() throws HiveException {
    GenericUDFDate udf = new GenericUDFDate();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableTimestampObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    DeferredObject valueObj = new DeferredJavaObject(new TimestampWritable(new Timestamp(109, 06, 30, 4, 17, 52, 0)));
    DeferredObject[] args = { valueObj };
    DateWritable output = (DateWritable) udf.evaluate(args);
    assertEquals("to_date() test for TIMESTAMP failed ", "2009-07-30", output.toString());
    // Try with null args
    DeferredObject[] nullArgs = { new DeferredJavaObject(null) };
    output = (DateWritable) udf.evaluate(nullArgs);
    assertNull("to_date() with null TIMESTAMP", output);
}
Also used : 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) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) GenericUDFDate(org.apache.hadoop.hive.ql.udf.generic.GenericUDFDate) Timestamp(java.sql.Timestamp)

Example 60 with DateWritable

use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.

the class TestGenericUDFDate method testVoidToDate.

public void testVoidToDate() throws HiveException {
    GenericUDFDate udf = new GenericUDFDate();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableVoidObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    DeferredObject[] args = { new DeferredJavaObject(null) };
    DateWritable output = (DateWritable) udf.evaluate(args);
    // Try with null VOID
    assertNull("to_date() with null DATE ", output);
    // Try with erroneously generated VOID
    DeferredObject[] junkArgs = { new DeferredJavaObject(new Text("2015-11-22")) };
    try {
        udf.evaluate(junkArgs);
        fail("to_date() test with VOID non-null failed");
    } catch (UDFArgumentException udfae) {
        assertEquals("TO_DATE() received non-null object of VOID type", udfae.getMessage());
    }
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) 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) GenericUDFDate(org.apache.hadoop.hive.ql.udf.generic.GenericUDFDate) Text(org.apache.hadoop.io.Text)

Aggregations

DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)77 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)36 Text (org.apache.hadoop.io.Text)36 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)35 Date (java.sql.Date)31 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)29 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)29 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)25 IntWritable (org.apache.hadoop.io.IntWritable)25 Timestamp (java.sql.Timestamp)24 BytesWritable (org.apache.hadoop.io.BytesWritable)24 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)22 LongWritable (org.apache.hadoop.io.LongWritable)22 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)20 BooleanWritable (org.apache.hadoop.io.BooleanWritable)20 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)19 HiveIntervalDayTimeWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable)19 FloatWritable (org.apache.hadoop.io.FloatWritable)19 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)18 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)18