pentaho/pentaho-kettle · error · KettleValueException

Function add_months only works on a date!

Error message

Function add_months only works on a date!

What it means

Guard in the legacy Value add_months() function: the value's type is not VALUE_TYPE_DATE, so month arithmetic on a Calendar built from the value is refused. The input at fault is the current Value with a non-date type.

Solutions

  1. Ensure the field passed to add_months is a Date
  2. Convert the value to a Date before applying add_months
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at core/src/main/java/org/pentaho/di/compatibility/Value.java:3388 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/023c996dbe478c61. Report an issue: GitHub.

Appendix: source

Thrown at core/src/main/java/org/pentaho/di/compatibility/Value.java:3388

        int day = cal.get( Calendar.DAY_OF_MONTH );

        month += months;

        int newyear = year + (int) Math.floor( month / 12 );
        int newmonth = month % 12;

        cal.set( newyear, newmonth, 1 );
        int newday = cal.getActualMaximum( Calendar.DAY_OF_MONTH );
        if ( newday < day ) {
          cal.set( Calendar.DAY_OF_MONTH, newday );
        } else {
          cal.set( Calendar.DAY_OF_MONTH, day );
        }

        setValue( cal.getTime() );
      }
    } else {
      throw new KettleValueException( "Function add_months only works on a date!" );
    }
    return this;
  }

  /**
   * Add a number of days to a Date value.
   *
   * @param days
   *          The number of days to add to the current date value
   * @return The resulting value
   * @throws KettleValueException
   */
  public Value add_days( long days ) throws KettleValueException {
    if ( getType() == VALUE_TYPE_DATE ) {
      if ( !isNull() && getDate() != null ) {
        Calendar cal = Calendar.getInstance();
        cal.setTime( getDate() );
        cal.add( Calendar.DAY_OF_YEAR, (int) days );

View on GitHub (pinned to f3058517a1)