Search in sources :

Example 46 with Date

use of org.apache.hadoop.hive.common.type.Date in project hive by apache.

the class GenericUDFAddMonths method evaluate.

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    Integer numMonthV;
    if (isNumMonthsConst) {
        numMonthV = numMonthsConst;
    } else {
        numMonthV = getIntValue(arguments, 1, tsConverters);
    }
    if (numMonthV == null) {
        return null;
    }
    int numMonthInt = numMonthV.intValue();
    // the function should support both short date and full timestamp format
    // time part of the timestamp should not be skipped
    Timestamp ts = getTimestampValue(arguments, 0, tsConverters);
    if (ts != null) {
        addMonth(ts, numMonthInt);
    } else {
        Date date = getDateValue(arguments, 0, dtConverters);
        if (date != null) {
            addMonth(date, numMonthInt);
        } else {
            return null;
        }
    }
    String res = formatter.format(calendar.getTime());
    output.set(res);
    return output;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp) Date(org.apache.hadoop.hive.common.type.Date)

Example 47 with Date

use of org.apache.hadoop.hive.common.type.Date in project hive by apache.

the class UDFYear method evaluate.

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    switch(inputTypes[0]) {
        case INTERVAL_YEAR_MONTH:
            HiveIntervalYearMonth intervalYearMonth = getIntervalYearMonthValue(arguments, 0, inputTypes, converters);
            if (intervalYearMonth == null) {
                return null;
            }
            output.set(intervalYearMonth.getYears());
            break;
        case STRING:
        case CHAR:
        case VARCHAR:
        case DATE:
        case TIMESTAMP:
        case TIMESTAMPLOCALTZ:
        case VOID:
            Date date = getDateValue(arguments, 0, converters);
            if (date == null) {
                return null;
            }
            calendar.setTimeInMillis(date.toEpochMilli());
            output.set(calendar.get(Calendar.YEAR));
    }
    return output;
}
Also used : HiveIntervalYearMonth(org.apache.hadoop.hive.common.type.HiveIntervalYearMonth) VectorUDFYearDate(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFYearDate) Date(org.apache.hadoop.hive.common.type.Date)

Example 48 with Date

use of org.apache.hadoop.hive.common.type.Date in project hive by apache.

the class GenericUDFMonthsBetween method evaluate.

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    // the function should support both short date and full timestamp format
    // time part of the timestamp should not be skipped
    Timestamp date1 = getTimestampValue(arguments, 0, tsConverters);
    if (date1 == null) {
        Date date = getDateValue(arguments, 0, dtConverters);
        if (date == null) {
            return null;
        }
        date1 = Timestamp.ofEpochMilli(date.toEpochMilli());
    }
    Timestamp date2 = getTimestampValue(arguments, 1, tsConverters);
    if (date2 == null) {
        Date date = getDateValue(arguments, 1, dtConverters);
        if (date == null) {
            return null;
        }
        date2 = Timestamp.ofEpochMilli(date.toEpochMilli());
    }
    cal1.setTimeInMillis(date1.toEpochMilli());
    cal2.setTimeInMillis(date2.toEpochMilli());
    // skip day/time part if both dates are end of the month
    // or the same day of the month
    int monDiffInt = (cal1.get(YEAR) - cal2.get(YEAR)) * 12 + (cal1.get(MONTH) - cal2.get(MONTH));
    if (cal1.get(DATE) == cal2.get(DATE) || (cal1.get(DATE) == cal1.getActualMaximum(DATE) && cal2.get(DATE) == cal2.getActualMaximum(DATE))) {
        output.set(monDiffInt);
        return output;
    }
    int sec1 = getDayPartInSec(cal1);
    int sec2 = getDayPartInSec(cal2);
    // 1 sec is 0.000000373 months (1/2678400). 1 month is 31 days.
    // there should be no adjustments for leap seconds
    double monBtwDbl = monDiffInt + (sec1 - sec2) / 2678400D;
    if (isRoundOffNeeded) {
        // Round a double to 8 decimal places.
        monBtwDbl = BigDecimal.valueOf(monBtwDbl).setScale(8, ROUND_HALF_UP).doubleValue();
    }
    output.set(monBtwDbl);
    return output;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp) Date(org.apache.hadoop.hive.common.type.Date)

Example 49 with Date

use of org.apache.hadoop.hive.common.type.Date in project hive by apache.

the class CastStringToDateWithFormat method evaluate.

@Override
protected void evaluate(LongColumnVector outputColVector, BytesColumnVector inV, int i) {
    String dateString = new String(inV.vector[i], inV.start[i], inV.length[i], StandardCharsets.UTF_8);
    Date date = formatter.parseDate(dateString.replaceAll("\u0000", ""));
    if (date != null) {
        outputColVector.vector[i] = DateWritableV2.dateToDays(date);
    } else {
        super.setNull(outputColVector, i);
    }
}
Also used : Date(org.apache.hadoop.hive.common.type.Date)

Example 50 with Date

use of org.apache.hadoop.hive.common.type.Date in project hive by apache.

the class CastStringToDate method evaluate.

protected void evaluate(LongColumnVector outputColVector, BytesColumnVector inV, int i) {
    String dateString = new String(inV.vector[i], inV.start[i], inV.length[i], StandardCharsets.UTF_8);
    Date hDate = DateParser.parseDate(dateString);
    if (hDate != null) {
        outputColVector.vector[i] = DateWritableV2.dateToDays(hDate);
        return;
    }
    setNull(outputColVector, i);
}
Also used : Date(org.apache.hadoop.hive.common.type.Date)

Aggregations

Date (org.apache.hadoop.hive.common.type.Date)71 Timestamp (org.apache.hadoop.hive.common.type.Timestamp)26 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)21 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)18 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)18 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)17 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)15 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)14 Text (org.apache.hadoop.io.Text)14 Test (org.junit.Test)14 HiveIntervalYearMonth (org.apache.hadoop.hive.common.type.HiveIntervalYearMonth)13 BytesWritable (org.apache.hadoop.io.BytesWritable)12 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)11 TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)11 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)11 List (java.util.List)10 LongWritable (org.apache.hadoop.io.LongWritable)10 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)9 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)9 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)9