Search in sources :

Example 1 with HiveSqlDateTimeFormatter

use of org.apache.hadoop.hive.common.format.datetime.HiveSqlDateTimeFormatter in project hive by apache.

the class GenericUDFCastFormat method initialize.

/**
 * @param arguments
 *  0. const int, value of a HiveParser_IdentifiersParser constant which represents a TOK_[TYPE]
 *  1. expression to convert
 *  2. constant string, format pattern
 *  3. (optional) constant int, output char/varchar length
 */
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 3 && arguments.length != 4) {
        throw new UDFArgumentException("Function cast_format requires 3 or 4 arguments (int, expression, StringLiteral" + "[, var/char length]), got " + arguments.length);
    }
    outputOI = getOutputOI(arguments);
    try {
        inputOI = (PrimitiveObjectInspector) arguments[1];
    } catch (ClassCastException e) {
        throw new UDFArgumentException("Function CAST...as ... FORMAT ...takes only primitive types");
    }
    PrimitiveObjectInspectorUtils.PrimitiveGrouping inputPG = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(inputOI.getPrimitiveCategory());
    PrimitiveObjectInspectorUtils.PrimitiveGrouping outputPG = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(outputOI.getPrimitiveCategory());
    if (inputOI.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.TIMESTAMPLOCALTZ) {
        throw new UDFArgumentException("Timestamp with local time zone not yet supported for cast ... format function");
    }
    if (!(inputPG == PrimitiveObjectInspectorUtils.PrimitiveGrouping.STRING_GROUP && outputPG == PrimitiveObjectInspectorUtils.PrimitiveGrouping.DATE_GROUP || inputPG == PrimitiveObjectInspectorUtils.PrimitiveGrouping.DATE_GROUP && outputPG == PrimitiveObjectInspectorUtils.PrimitiveGrouping.STRING_GROUP || inputPG == PrimitiveObjectInspectorUtils.PrimitiveGrouping.VOID_GROUP)) {
        throw new UDFArgumentException("Function CAST...as ... FORMAT ... only converts datetime objects to string types" + " and string or void objects to datetime types. Type of object provided: " + outputOI.getPrimitiveCategory() + " in primitive grouping " + inputPG + ", type provided: " + inputOI.getPrimitiveCategory() + " in primitive grouping " + outputPG);
    }
    boolean forParsing = (outputPG == PrimitiveObjectInspectorUtils.PrimitiveGrouping.DATE_GROUP);
    formatter = new HiveSqlDateTimeFormatter(getConstantStringValue(arguments, 2), forParsing);
    return outputOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) HiveSqlDateTimeFormatter(org.apache.hadoop.hive.common.format.datetime.HiveSqlDateTimeFormatter) PrimitiveObjectInspectorUtils(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils)

Aggregations

HiveSqlDateTimeFormatter (org.apache.hadoop.hive.common.format.datetime.HiveSqlDateTimeFormatter)1 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)1 PrimitiveObjectInspectorUtils (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils)1