{"record":{"id":"902bbf473f700217","repo":"pentaho/pentaho-kettle","slug":"function-sqrt-only-works-with-a-number","errorCode":null,"errorMessage":"Function SQRT only works with a number","messagePattern":"Function SQRT only works with a number","errorType":"exception","errorClass":"KettleValueException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/compatibility/Value.java","lineNumber":2876,"sourceCode":"    }\n    if ( isNumeric() ) {\n      setValue( Math.sin( getNumber() ) );\n    } else {\n      throw new KettleValueException( \"Function SIN only works with a number\" );\n    }\n\n    return this;\n  }\n\n  // implement the SQRT function, arguments in args[]\n  public Value sqrt() throws KettleValueException {\n    if ( isNull() ) {\n      return this;\n    }\n    if ( isNumeric() ) {\n      setValue( Math.sqrt( getNumber() ) );\n    } else {\n      throw new KettleValueException( \"Function SQRT only works with a number\" );\n    }\n\n    return this;\n  }\n\n  // implement the SUBSTR function, arguments in args[]\n  public Value substr( Value from, Value to ) {\n    return substr( (int) from.getNumber(), (int) to.getNumber() );\n  }\n\n  public Value substr( Value from ) {\n    return substr( (int) from.getNumber(), -1 );\n  }\n\n  public Value substr( int from ) {\n    return substr( from, -1 );\n  }\n","sourceCodeStart":2858,"sourceCodeEnd":2894,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/compatibility/Value.java#L2858-L2894","documentation":"Thrown by Value.sqrt() when the value is not numeric. SQRT computes Math.sqrt(getNumber()) after the isNumeric() check; any non-numeric type raises KettleValueException. Null values are returned unchanged before the check.","triggerScenarios":"Calling value.sqrt() on a String, Date, Boolean, or Binary typed Value.","commonSituations":"Square root of a string-typed measurement column; metadata drift after schema changes; applying SQRT to parsed-from-text data that never got converted.","solutions":["Check value.isNumeric() before calling sqrt().","Convert the field to a numeric type upstream (input step metadata or StringToNumber).","Note negative numbers do not throw here but yield NaN — validate sign separately if that matters.","Catch KettleValueException for non-numeric rows."],"exampleFix":"// before\nValue r = value.sqrt(); // throws for String-typed value\n// after\nValue r = value.isNumeric() ? value.sqrt() : null; // or convert first","handlingStrategy":"type-guard","validationCode":"if (!value.isNumeric()) { throw new IllegalArgumentException(\"SQRT requires a numeric value\"); }","typeGuard":"boolean sqrtSafe(Value v) { return v != null && v.isNumeric() && (v.isNull() || v.getNumber() >= 0); }","tryCatchPattern":"try { result = value.sqrt(); } catch (KettleValueException e) { result = null; }","preventionTips":["Validate non-negativity separately (SQRT of negative yields NaN, not an exception)","Type measurements as Number at the input step","Use isNumeric() guards before SQRT"],"tags":["kettle","type-mismatch","math"],"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"}