@Override public String getDisplayString(String[] children) { return getStandardDisplayString(getFuncName(), children); } }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { checkArgsSize(arguments, 1, 1); checkArgPrimitive(arguments, 0); switch (((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory()) { case INTERVAL_YEAR_MONTH: case TIMESTAMPLOCALTZ: case VOID: obtainDateConverter(arguments, 0, inputTypes, converters); break; default: sb.append(getFuncName()); sb.append(" does not take "); sb.append(((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory());
private void compareToUDFYearDate(long t, int y) throws HiveException { UDFYear udf = new UDFYear(); udf.initialize(new ObjectInspector[]{PrimitiveObjectInspectorFactory.writableTimestampObjectInspector}); TimestampWritableV2 tsw = toTimestampWritable(t); IntWritable res = (IntWritable) udf.evaluate( new GenericUDF.DeferredObject[]{new GenericUDF.DeferredJavaObject(tsw)}); Assert.assertEquals(res.get(), y); }
@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, inputTypes, converters); if (date == null) { return null; } calendar.setTimeInMillis(date.toEpochMilli()); output.set(calendar.get(Calendar.YEAR)); } return output; }
break; case "year": udf = new UDFYear(); break; default:
private void compareToUDFYearLong(Timestamp t, int y) throws HiveException { UDFYear udf = new UDFYear(); udf.initialize(new ObjectInspector[]{PrimitiveObjectInspectorFactory.writableTimestampObjectInspector}); TimestampWritableV2 tsw = new TimestampWritableV2( org.apache.hadoop.hive.common.type.Timestamp.ofEpochMilli(t.getTime(), t.getNanos())); IntWritable res = (IntWritable) udf.evaluate( new GenericUDF.DeferredObject[]{new GenericUDF.DeferredJavaObject(tsw)}); Assert.assertEquals(res.get(), y); }