use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class GenericUDFToTimestampLocalTZ method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length < 1) {
throw new UDFArgumentLengthException("The function CAST as TIMESTAMP WITH LOCAL TIME ZONE requires at least one argument, got " + arguments.length);
}
try {
argumentOI = (PrimitiveObjectInspector) arguments[0];
switch(argumentOI.getPrimitiveCategory()) {
case CHAR:
case VARCHAR:
case STRING:
case DATE:
case TIMESTAMP:
case TIMESTAMPLOCALTZ:
break;
default:
throw new UDFArgumentException("CAST as TIMESTAMP WITH LOCAL TIME ZONE only allows" + "string/date/timestamp/timestamp with time zone types");
}
} catch (ClassCastException e) {
throw new UDFArgumentException("The function CAST as TIMESTAMP WITH LOCAL TIME ZONE takes only primitive types");
}
SettableTimestampLocalTZObjectInspector outputOI = (SettableTimestampLocalTZObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
converter = new TimestampLocalTZConverter(argumentOI, outputOI);
return outputOI;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class GenericUDFOPDTIMinus method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentException(opName + " requires two arguments.");
}
PrimitiveObjectInspector resultOI = null;
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");
}
}
inputOIs = new PrimitiveObjectInspector[] { (PrimitiveObjectInspector) arguments[0], (PrimitiveObjectInspector) arguments[1] };
PrimitiveObjectInspector leftOI = inputOIs[0];
PrimitiveObjectInspector rightOI = inputOIs[1];
// Date - Int = Date
if (checkArgs(PrimitiveCategory.INTERVAL_YEAR_MONTH, PrimitiveCategory.INTERVAL_YEAR_MONTH)) {
minusOpType = OperationType.INTERVALYM_MINUS_INTERVALYM;
intervalArg1Idx = 0;
intervalArg2Idx = 1;
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.intervalYearMonthTypeInfo);
} else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.INTERVAL_YEAR_MONTH)) {
minusOpType = OperationType.DATE_MINUS_INTERVALYM;
dtArg1Idx = 0;
intervalArg1Idx = 1;
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.dateTypeInfo);
} else if (checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.INTERVAL_YEAR_MONTH)) {
minusOpType = OperationType.TIMESTAMP_MINUS_INTERVALYM;
dtArg1Idx = 0;
intervalArg1Idx = 1;
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.timestampTypeInfo);
} else if (checkArgs(PrimitiveCategory.INTERVAL_DAY_TIME, PrimitiveCategory.INTERVAL_DAY_TIME)) {
minusOpType = OperationType.INTERVALDT_MINUS_INTERVALDT;
intervalArg1Idx = 0;
intervalArg2Idx = 1;
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.intervalDayTimeTypeInfo);
} else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.INTERVAL_DAY_TIME) || checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.INTERVAL_DAY_TIME)) {
minusOpType = OperationType.TIMESTAMP_MINUS_INTERVALDT;
dtArg1Idx = 0;
intervalArg1Idx = 1;
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.timestampTypeInfo);
dt1Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
} else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.DATE) || checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.TIMESTAMP) || checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.TIMESTAMP) || checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.DATE)) {
// Operands converted to timestamp, result as interval day-time
minusOpType = OperationType.TIMESTAMP_MINUS_TIMESTAMP;
dtArg1Idx = 0;
dtArg2Idx = 1;
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.intervalDayTimeTypeInfo);
dt1Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
dt2Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
} else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.INT)) {
minusOpType = OperationType.DATE_MINUS_INT;
intervalArg1Idx = 1;
dtArg1Idx = 0;
resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.dateTypeInfo);
dt1Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
} else {
// Unsupported types - error
List<TypeInfo> argTypeInfos = new ArrayList<TypeInfo>(2);
argTypeInfos.add(leftOI.getTypeInfo());
argTypeInfos.add(rightOI.getTypeInfo());
throw new NoMatchingMethodException(this.getClass(), argTypeInfos, null);
}
return resultOI;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class GenericUDFRound method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length < 1 || arguments.length > 2) {
throw new UDFArgumentLengthException("ROUND requires one or two argument, got " + arguments.length);
}
if (arguments[0].getCategory() != Category.PRIMITIVE) {
throw new UDFArgumentTypeException(0, "ROUND input only takes primitive types, got " + arguments[0].getTypeName());
}
inputOI = (PrimitiveObjectInspector) arguments[0];
if (arguments.length == 2) {
if (arguments[1].getCategory() != Category.PRIMITIVE) {
throw new UDFArgumentTypeException(1, "ROUND second argument only takes primitive types, got " + arguments[1].getTypeName());
}
scaleOI = (PrimitiveObjectInspector) arguments[1];
switch(scaleOI.getPrimitiveCategory()) {
case VOID:
break;
case BYTE:
if (scaleOI instanceof WritableConstantByteObjectInspector) {
scale = ((WritableConstantByteObjectInspector) scaleOI).getWritableConstantValue().get();
} else {
constantScale = false;
}
break;
case SHORT:
if (scaleOI instanceof WritableConstantShortObjectInspector) {
scale = ((WritableConstantShortObjectInspector) scaleOI).getWritableConstantValue().get();
} else {
constantScale = false;
}
break;
case INT:
if (scaleOI instanceof WritableConstantIntObjectInspector) {
scale = ((WritableConstantIntObjectInspector) scaleOI).getWritableConstantValue().get();
} else {
constantScale = false;
}
break;
case LONG:
if (scaleOI instanceof WritableConstantLongObjectInspector) {
long l = ((WritableConstantLongObjectInspector) scaleOI).getWritableConstantValue().get();
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new UDFArgumentException(getFuncName().toUpperCase() + " scale argument out of allowed range");
}
scale = (int) l;
} else {
constantScale = false;
}
break;
default:
throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " second argument only takes numeric type");
}
}
inputType = inputOI.getPrimitiveCategory();
ObjectInspector outputOI = null;
switch(inputType) {
case DECIMAL:
DecimalTypeInfo inputTypeInfo = (DecimalTypeInfo) inputOI.getTypeInfo();
DecimalTypeInfo typeInfo = getOutputTypeInfo(inputTypeInfo, scale);
outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
if (!constantScale) {
throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " scale argument for " + "decimal must be constant");
}
break;
case VOID:
case BYTE:
case SHORT:
case INT:
case LONG:
case FLOAT:
case DOUBLE:
outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputType);
break;
case STRING:
case VARCHAR:
case CHAR:
outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(PrimitiveCategory.DOUBLE);
converterFromString = ObjectInspectorConverters.getConverter(inputOI, outputOI);
break;
default:
throw new UDFArgumentTypeException(0, "Only numeric or string group data types are allowed for ROUND function. Got " + inputType.name());
}
return outputOI;
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class TestGenericUDFNextDay method testNextDayErrorArg1.
@Test
public void testNextDayErrorArg1() throws HiveException {
@SuppressWarnings("resource") GenericUDFNextDay udf = new GenericUDFNextDay();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableLongObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
ObjectInspector[] arguments = { valueOI0, valueOI1 };
try {
udf.initialize(arguments);
assertTrue("UDFArgumentException expected", false);
} catch (UDFArgumentException e) {
assertEquals("next_day only takes STRING_GROUP, DATE_GROUP, VOID_GROUP types as 1st argument, got LONG", e.getMessage());
}
}
use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.
the class TestGenericUDFNextDay method testNextDayErrorArg2.
@Test
public void testNextDayErrorArg2() throws HiveException {
@SuppressWarnings("resource") GenericUDFNextDay udf = new GenericUDFNextDay();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
ObjectInspector[] arguments = { valueOI0, valueOI1 };
try {
udf.initialize(arguments);
assertTrue("UDFArgumentException expected", false);
} catch (UDFArgumentException e) {
assertEquals("next_day only takes STRING_GROUP, VOID_GROUP types as 2nd argument, got INT", e.getMessage());
}
}
Aggregations