private ObjectInspector initializeNumber(ObjectInspector[] arguments)
      throws UDFArgumentException {
    if (arguments.length < 1 || arguments.length > 2) {
      throw new UDFArgumentLengthException(
          "TRUNC requires one or two argument, got " + arguments.length);
    }

    if (arguments[0].getCategory() != Category.PRIMITIVE) {
      throw new UDFArgumentTypeException(0,
          "TRUNC 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,
            "TRUNC second argument only takes primitive types, got " + arguments[1].getTypeName());
      }

      inputScaleOI = (PrimitiveObjectInspector) arguments[1];
      inputSacleConst = arguments[1] instanceof ConstantObjectInspector;
      if (inputSacleConst) {
        try {
          Object obj = ((ConstantObjectInspector) arguments[1]).getWritableConstantValue();
          fmtInput = obj != null ? obj.toString() : null;
          scale = Integer.parseInt(fmtInput);
        } catch (Exception e) {
          throw new UDFArgumentException("TRUNC input only takes integer values, got " + fmtInput);
        }
      } else {
        switch (inputScaleOI.getPrimitiveCategory()) {
        case BYTE:
          byteConverter = ObjectInspectorConverters.getConverter(arguments[1],
              PrimitiveObjectInspectorFactory.writableByteObjectInspector);
          break;
        case SHORT:
          shortConverter = ObjectInspectorConverters.getConverter(arguments[1],
              PrimitiveObjectInspectorFactory.writableShortObjectInspector);
          break;
        case INT:
          intConverter = ObjectInspectorConverters.getConverter(arguments[1],
              PrimitiveObjectInspectorFactory.writableIntObjectInspector);
          break;
        case LONG:
          longConverter = ObjectInspectorConverters.getConverter(arguments[1],
              PrimitiveObjectInspectorFactory.writableLongObjectInspector);
          break;
        default:
          throw new UDFArgumentTypeException(1,
              getFuncName().toUpperCase() + " second argument only takes integer values");
        }
      }
    }

    inputType1 = inputOI.getPrimitiveCategory();
    ObjectInspector outputOI = null;
    switch (inputType1) {
    case DECIMAL:
      int outputScale = scale > 0 ? scale : 0;
      int outputPrecision = ((PrimitiveObjectInspector) arguments[0]).precision() - ((PrimitiveObjectInspector) arguments[0]).scale() + outputScale;
      DecimalTypeInfo t = new DecimalTypeInfo(outputPrecision, outputScale);
      outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(t);
      break;
    case VOID:
    case BYTE:
    case SHORT:
    case INT:
    case LONG:
    case FLOAT:
    case DOUBLE:
      outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputType1);
      break;
    default:
      throw new UDFArgumentTypeException(0,
          "Only numeric or string group data types are allowed for TRUNC function. Got "
              + inputType1.name());
    }

    return outputOI;
  }
GenericUDFTrunc.

Returns the first day of the month which the date belongs to. The time part of the date will be
ignored.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTrunc.java