pentaho/pentaho-kettle · error · KettleValueException

Function LENGTH only works with a string

Error message

Function LENGTH only works with a string

What it means

The length() method of Kettle's legacy Value class returns the character length of the value, but only when the Value's type is VALUE_TYPE_STRING. For any other type (Number, Date, Boolean, etc.) it throws this KettleValueException. Unlike the numeric functions, this one checks for an exact type equality rather than isNumeric().

Solutions

  1. Convert the Value to String before calling length() (setType(VALUE_TYPE_STRING) or getString() conversion)
  2. Change the input step to read the column as String if it is genuinely textual
  3. Guard with getType() == VALUE_TYPE_STRING and convert otherwise

Example fix

// before
Value len = zipCode.length(); // zipCode is a Number
// after
if (zipCode.getType() != Value.VALUE_TYPE_STRING) {
  zipCode.setType(Value.VALUE_TYPE_STRING);
}
Value len = zipCode.length();
Defensive patterns

Strategy: type-guard

Validate before calling

if (value == null || value.getType() != Value.VALUE_TYPE_STRING) {
  throw new IllegalArgumentException("LENGTH requires a String value, got type " + (value == null ? "null" : value.getType()));
}

Type guard

boolean isStringValue(Value v) { return v != null && v.getType() == Value.VALUE_TYPE_STRING; }

Try / catch

try {
  result = value.length();
} catch (KettleValueException e) {
  // value was not a String: convert then retry
  value.setType(Value.VALUE_TYPE_STRING);
  result = value.length();
}

Prevention

When it happens

Trigger: Calling Value.length() on a Number-typed Value (e.g. an account number or ZIP code stored as numeric) or on a Date/Boolean Value instead of a String.

Common situations: Measuring length of numeric codes (phone numbers, IDs) that Kettle typed as Number; validation logic assuming all input fields are String when the input step auto-detected numbers.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

      setNull();
    } else {
      setValue( Const.initCap( getString() ) );
    }
    return this;
  }

  // implement the LENGTH function, arguments in args[]
  public Value length() throws KettleValueException {
    if ( isNull() ) {
      setType( VALUE_TYPE_INTEGER );
      setValue( 0L );
      return this;
    }

    if ( getType() == VALUE_TYPE_STRING ) {
      setValue( (double) getString().length() );
    } else {
      throw new KettleValueException( "Function LENGTH only works with a string" );
    }
    return this;
  }

  // implement the LOG function, arguments in args[]
  public Value log() throws KettleValueException {
    if ( isNull() ) {
      return this;
    }

    if ( isNumeric() ) {
      setValue( Math.log( getNumber() ) );
    } else {
      throw new KettleValueException( "Function LOG only works with a number" );
    }

    return this;
  }

View on GitHub (pinned to f3058517a1)