hive 函数
date_add ()
用法
select weekofyear('2022-01-01 12:20:20',2)
返回值
2022-01-03
函数含义
对输入的yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd' 进行天数相加
源码地址
org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateAdd
源代码
@Description(name = "date_add",
value = "_FUNC_(start_date, num_days) - Returns the date that is num_days after start_date.",
extended = "start_date is a string in the format 'yyyy-MM-dd HH:mm:ss' or"
+ " 'yyyy-MM-dd'. num_days is a number. The time part of start_date is "
+ "ignored.\n"
+ "Example:\n "
+ " > SELECT _FUNC_('2009-07-30', 1) FROM src LIMIT 1;\n"
+ " '2009-07-31'")
@VectorizedExpressions({VectorUDFDateAddColScalar.class, VectorUDFDateAddScalarCol.class, VectorUDFDateAddColCol.class})
public class GenericUDFDateAdd extends GenericUDF {
// hvie Date解析器 把 String => Date
private transient final DateParser dateParser = new DateParser();
private transient final Date dateVal = new Date();
private transient Converter dateConverter;
private transient Converter daysConverter;
private transient PrimitiveCategory inputType1;
private transient PrimitiveCategory inputType2;
// DateWritableV2 相当于 java.sql.Date 的可写。日期格式为 YYYY-MM-DD
private final DateWritableV2 output = new DateWritableV2();
protected int signModifier = 1; // 1 for addition, -1 for subtraction
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
// 这里是自己写的的判断 可以更换成 checkArgsSize
if (arguments.length != 2) {
throw new UDFArgumentLengthException(
"date_add() requires 2 argument, got " + arguments.length);
}
// 验证是否是hive 原始数据类型 可以使用 checkArgPrimitive 替代
if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentTypeException(0,
"Only primitive type arguments are accepted but "
+ arguments[0].getTypeName() + " is passed. as first arguments");
}
if (arguments[1].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentTypeException(1,
"Only primitive type arguments are accepted but "
+ arguments[1].getTypeName() + " is passed. as second arguments");
}
inputType1 = ((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory();
ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
// 对输入的类型进行校验与转换
switch (inputType1) {
case STRING:
case VARCHAR:
case CHAR:
inputType1 = PrimitiveCategory.STRING;
dateConverter = ObjectInspectorConverters.getConverter(
(PrimitiveObjectInspector) arguments[0],
PrimitiveObjectInspectorFactory.writableStringObjectInspector);
break;
case TIMESTAMP:
dateConverter = new TimestampConverter((PrimitiveObjectInspector) arguments[0],
PrimitiveObjectInspectorFactory.writableTimestampObjectInspector);
break;
case DATE:
dateConverter = ObjectInspectorConverters.getConverter(
(PrimitiveObjectInspector) arguments[0],
PrimitiveObjectInspectorFactory.writableDateObjectInspector);
break;
default:
throw new UDFArgumentException(
" DATE_ADD() only takes STRING/TIMESTAMP/DATEWRITABLE types as first argument, got "
+ inputType1);
}
inputType2 = ((PrimitiveObjectInspector) arguments[1]).getPrimitiveCategory();
switch (inputType2) {
case BYTE:
daysConverter = ObjectInspectorConverters.getConverter(
(PrimitiveObjectInspector) arguments[1],
PrimitiveObjectInspectorFactory.writableByteObjectInspector);
break;
case SHORT:
daysConverter = ObjectInspectorConverters.getConverter(
(PrimitiveObjectInspector) arguments[1],
PrimitiveObjectInspectorFactory.writableShortObjectInspector);
break;
case INT:
daysConverter = ObjectInspectorConverters.getConverter(
(PrimitiveObjectInspector) arguments[1],
PrimitiveObjectInspectorFactory.writableIntObjectInspector);
break;
default:
throw new UDFArgumentException(
" DATE_ADD() only takes TINYINT/SMALLINT/INT types as second argument, got " + inputType2);
}
return outputOI;
}
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
if (arguments[0].get() == null) {
return null;
}
Object daysWritableObject = daysConverter.convert(arguments[1].get());
if (daysWritableObject == null) {
return null;
}
int toBeAdded;
// 对输入的第二个 需要加减的参数进行类型转换
if (daysWritableObject instanceof ByteWritable) {
toBeAdded = ((ByteWritable) daysWritableObject).get();
} else if (daysWritableObject instanceof ShortWritable) {
toBeAdded = ((ShortWritable) daysWritableObject).get();
} else if (daysWritableObject instanceof IntWritable) {
toBeAdded = ((IntWritable) daysWritableObject).get();
} else {
return null;
}
// Convert the first param into a DateWritableV2 value
// 不同的类型处理的方式不一样
switch (inputType1) {
case STRING:
String dateString = dateConverter.convert(arguments[0].get()).toString();
if (dateParser.parseDate(dateString, dateVal)) {
output.set(dateVal);
} else {
return null;
}
break;
case TIMESTAMP:
Timestamp ts = ((TimestampWritableV2) dateConverter.convert(arguments[0].get()))
.getTimestamp();
output.set(DateWritableV2.millisToDays(ts.toEpochMilli()));
break;
case DATE:
DateWritableV2 dw = (DateWritableV2) dateConverter.convert(arguments[0].get());
output.set(dw.getDays());
break;
default:
throw new UDFArgumentException(
"DATE_ADD() only takes STRING/TIMESTAMP/DATEWRITABLE types, got " + inputType1);
}
// 总体上的思路是把不同的格式转换成天 再相加上第二个参数 再把day转换成 Date类型
int newDays = output.getDays() + (signModifier * toBeAdded);
output.set(newDays);
return output;
}
@Override
public String getDisplayString(String[] children) {
return getStandardDisplayString("date_add", children);
}
}
源码分析
- 整体上架构都是对输入的类型进行校验与转换
- 把对输入的时间函数转换成天再进行相加操作,之后再转换成Date返回回去
总结
- 其实udf函数总体上都是都是比较简单的,所以核心的逻辑很少。
- 当看了几个udf源码之后就会发现 每个源码的校验逻辑有的是自己实现,有的是使用父类GenericUDF自带的公共方法写的,我猜测大家实现的方式不同有可能是每个开发者的风格不一样,还有就是再开发这个函数的时候有可能父类当中还没有这个功能的函数。