{"record":{"id":"406d906158661237","repo":"pentaho/pentaho-kettle","slug":"function-length-only-works-with-a-string","errorCode":null,"errorMessage":"Function LENGTH only works with a string","messagePattern":"Function LENGTH only works with a string","errorType":"exception","errorClass":"KettleValueException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/compatibility/Value.java","lineNumber":2514,"sourceCode":"      setNull();\n    } else {\n      setValue( Const.initCap( getString() ) );\n    }\n    return this;\n  }\n\n  // implement the LENGTH function, arguments in args[]\n  public Value length() throws KettleValueException {\n    if ( isNull() ) {\n      setType( VALUE_TYPE_INTEGER );\n      setValue( 0L );\n      return this;\n    }\n\n    if ( getType() == VALUE_TYPE_STRING ) {\n      setValue( (double) getString().length() );\n    } else {\n      throw new KettleValueException( \"Function LENGTH only works with a string\" );\n    }\n    return this;\n  }\n\n  // implement the LOG function, arguments in args[]\n  public Value log() throws KettleValueException {\n    if ( isNull() ) {\n      return this;\n    }\n\n    if ( isNumeric() ) {\n      setValue( Math.log( getNumber() ) );\n    } else {\n      throw new KettleValueException( \"Function LOG only works with a number\" );\n    }\n\n    return this;\n  }","sourceCodeStart":2496,"sourceCodeEnd":2532,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/compatibility/Value.java#L2496-L2532","documentation":"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().","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the Value to String before calling length() (setType(VALUE_TYPE_STRING) or getString() conversion)","Change the input step to read the column as String if it is genuinely textual","Guard with getType() == VALUE_TYPE_STRING and convert otherwise"],"exampleFix":"// before\nValue len = zipCode.length(); // zipCode is a Number\n// after\nif (zipCode.getType() != Value.VALUE_TYPE_STRING) {\n  zipCode.setType(Value.VALUE_TYPE_STRING);\n}\nValue len = zipCode.length();","handlingStrategy":"type-guard","validationCode":"if (value == null || value.getType() != Value.VALUE_TYPE_STRING) {\n  throw new IllegalArgumentException(\"LENGTH requires a String value, got type \" + (value == null ? \"null\" : value.getType()));\n}","typeGuard":"boolean isStringValue(Value v) { return v != null && v.getType() == Value.VALUE_TYPE_STRING; }","tryCatchPattern":"try {\n  result = value.length();\n} catch (KettleValueException e) {\n  // value was not a String: convert then retry\n  value.setType(Value.VALUE_TYPE_STRING);\n  result = value.length();\n}","preventionTips":["Read code-like fields (ZIPs, phone numbers, IDs) as String in the input step if length matters","Remember length() checks exact type equality, not isNumeric() - Numbers will throw","Convert to String explicitly before measuring length","Preview the field type in the transformation before adding validation logic"],"tags":["kettle","string-type","length","runtime-type-check"],"backgroundTag":"type-mismatch","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}