use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class GenericUDFBaseNumeric method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentException(opName + " requires two arguments.");
}
for (int i = 0; i < 2; i++) {
Category category = arguments[i].getCategory();
if (category != Category.PRIMITIVE) {
throw new UDFArgumentTypeException(i, "The " + GenericUDFUtils.getOrdinal(i + 1) + " argument of " + opName + " is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found");
}
}
// we have access to these values in the map/reduce tasks.
if (confLookupNeeded) {
CompatLevel compatLevel = HiveCompat.getCompatLevel(SessionState.get().getConf());
ansiSqlArithmetic = compatLevel.ordinal() > CompatLevel.HIVE_0_12.ordinal();
confLookupNeeded = false;
}
leftOI = (PrimitiveObjectInspector) arguments[0];
rightOI = (PrimitiveObjectInspector) arguments[1];
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(deriveResultTypeInfo());
converterLeft = ObjectInspectorConverters.getConverter(leftOI, resultOI);
converterRight = ObjectInspectorConverters.getConverter(rightOI, resultOI);
return resultOI;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class GenericUDFCastFormat method getOutputOI.
private PrimitiveObjectInspector getOutputOI(ObjectInspector[] arguments) throws UDFArgumentException {
int key = getConstantIntValue(arguments, 0);
if (!OUTPUT_TYPES.keySet().contains(key)) {
throw new UDFArgumentException("Cast...format can only convert to DATE, TIMESTAMP, STRING," + "VARCHAR, CHAR. Can't convert to HiveParser_IdentifiersParser constant with value " + key);
}
String typeString = OUTPUT_TYPES.get(key);
if (serdeConstants.VARCHAR_TYPE_NAME.equals(typeString) || serdeConstants.CHAR_TYPE_NAME.equals(typeString)) {
if (arguments.length < 4 || arguments[3] == null) {
throw new UDFArgumentException(typeString + " missing length argument");
}
typeString += "(" + getConstantIntValue(arguments, 3) + ")";
}
PrimitiveTypeInfo typeInfo = TypeInfoFactory.getPrimitiveTypeInfo(typeString);
return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException 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;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class GenericUDFCharacterLength method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 1) {
throw new UDFArgumentLengthException("CHARACTER_LENGTH requires 1 argument, got " + arguments.length);
}
if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentException("CHARACTER_LENGTH only takes primitive types, got " + argumentOI.getTypeName());
}
argumentOI = (PrimitiveObjectInspector) arguments[0];
stringConverter = new PrimitiveObjectInspectorConverter.StringConverter(argumentOI);
PrimitiveObjectInspector.PrimitiveCategory inputType = argumentOI.getPrimitiveCategory();
ObjectInspector outputOI = null;
switch(inputType) {
case CHAR:
case VARCHAR:
case STRING:
isInputString = true;
break;
case BINARY:
isInputString = false;
break;
default:
throw new UDFArgumentException(" CHARACTER_LENGTH() only takes STRING/CHAR/VARCHAR/BINARY types as first argument, got " + inputType);
}
outputOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
return outputOI;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class GenericUDFFromUnixTime method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
checkArgsSize(arguments, 1, 2);
for (int i = 0; i < arguments.length; i++) {
checkArgPrimitive(arguments, i);
}
PrimitiveObjectInspector arg0OI = (PrimitiveObjectInspector) arguments[0];
switch(arg0OI.getPrimitiveCategory()) {
case INT:
inputIntOI = (IntObjectInspector) arguments[0];
break;
case LONG:
inputLongOI = (LongObjectInspector) arguments[0];
break;
default:
throw new UDFArgumentException("The function from_unixtime takes only int/long types for first argument. Got Type:" + arg0OI.getPrimitiveCategory().name());
}
if (arguments.length == 2) {
checkArgGroups(arguments, 1, inputTypes, STRING_GROUP);
obtainStringConverter(arguments, 1, inputTypes, converters);
}
if (timeZone == null) {
timeZone = SessionState.get() == null ? new HiveConf().getLocalTimeZone() : SessionState.get().getConf().getLocalTimeZone();
FORMATTER.withZone(timeZone);
}
return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}
Aggregations