{"record":{"id":"5355cfddd2bbc28d","repo":"pentaho/pentaho-kettle","slug":"the-value-num-is-a-negative-number-the-sqrt-function-only","errorCode":null,"errorMessage":"The value: [${num}] is a negative number. The 'sqrt' function only works with non-negative numbers.","messagePattern":"The value: \\[(.+?)\\] is a negative number\\. The 'sqrt' function only works with non-negative numbers\\.","errorType":"exception","errorClass":"KettleValueException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/row/ValueDataUtil.java","lineNumber":931,"sourceCode":"      }\n    }\n    return MathContext.UNLIMITED;\n  }\n\n  public static Object sqrt( ValueMetaInterface metaA, Object dataA ) throws KettleValueException {\n    if ( dataA == null ) {\n      return null;\n    }\n\n    Double num;\n\n    switch ( metaA.getType() ) {\n      case ValueMetaInterface.TYPE_NUMBER:\n        num = metaA.getNumber( dataA );\n        if ( num >= 0 ) {\n          return new Double( Math.sqrt( num.doubleValue() ) );\n        }\n        throw new KettleValueException( valueNotNegative( num ) );\n      case ValueMetaInterface.TYPE_INTEGER:\n        num = metaA.getNumber( dataA );\n        if ( num >= 0 ) {\n          return new Long( Math.round( Math.sqrt( num.doubleValue() ) ) );\n        }\n        throw new KettleValueException( valueNotNegative( num ) );\n      case ValueMetaInterface.TYPE_BIGNUMBER:\n        num = metaA.getNumber( dataA );\n        if ( num >= 0 ) {\n          return BigDecimal.valueOf( Math.sqrt( num.doubleValue() ) );\n        }\n        throw new KettleValueException( valueNotNegative( num ) );\n\n      default:\n        throw new KettleValueException( \"The 'sqrt' function only works on numeric data.\" );\n    }\n  }\n","sourceCodeStart":913,"sourceCodeEnd":949,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/row/ValueDataUtil.java#L913-L949","documentation":"ValueDataUtil.sqrt() computes the square root of a numeric field (Number, Integer, or BigNumber). When the input value is negative, Math.sqrt would yield NaN, so Kettle throws KettleValueException instead of returning a silently wrong result. The exception message embeds the offending value so the failing row/data can be identified.","triggerScenarios":"Calling ValueDataUtil.sqrt(metaA, dataA) (or a Calculator step / formula using the SQRT function) where metaA.getType() is TYPE_NUMBER and dataA is a negative double (num < 0).","commonSituations":"Calculator step computing sqrt of a difference that can go negative (e.g. variance from mismatched column order), formula fields over raw measurements that can be negative, importing negative sensor values, sign errors in upstream transformations.","solutions":["Check the input data: find rows feeding the SQRT calculation where the value is negative and fix them at the source","Guard the expression: compute SQRT only when value >= 0, e.g. use an IF/ternary in the Calculator or Formula step: value < 0 ? null : SQRT(value)","Take ABS() first if sign is irrelevant: SQRT(ABS(value))","Apply the square root to a squared quantity (variance/stddev pattern) so the operand is always non-negative"],"exampleFix":"// before (Calculator/UDF via ValueDataUtil)\nObject r = ValueDataUtil.sqrt(metaA, dataA); // throws if dataA < 0\n// after\nObject r = (metaA.getNumber(dataA) >= 0)\n    ? ValueDataUtil.sqrt(metaA, dataA)\n    : null;","handlingStrategy":"validation","validationCode":"if (metaA != null && metaA.getType() == ValueMetaInterface.TYPE_NUMBER && metaA.getNumber(dataA) != null && metaA.getNumber(dataA) >= 0) { result = ValueDataUtil.sqrt(metaA, dataA); } else { result = null; /* or log & skip row */ }","typeGuard":"boolean isNonNegativeNumeric(ValueMetaInterface meta, Object data) {\n  if (meta == null || data == null) return false;\n  int t = meta.getType();\n  if (t != ValueMetaInterface.TYPE_NUMBER && t != ValueMetaInterface.TYPE_INTEGER && t != ValueMetaInterface.TYPE_BIGNUMBER) return false;\n  Number n = (Number) meta.convertData(data);\n  return n.doubleValue() >= 0;\n}","tryCatchPattern":"try {\n  result = ValueDataUtil.sqrt(metaA, dataA);\n} catch (KettleValueException e) {\n  logError(\"SQRT of negative value in row \" + getRowNr() + \": \" + e.getMessage());\n  putRow(outputRowMeta, RowDataUtil.addValueData(inputRow, outputRowMeta.size(), null)); // null result, keep pipeline flowing\n}","preventionTips":["Clamp or ABS() any expression that feeds SQRT and can go negative (differences, deltas)","Prefer computing sqrt of squared quantities (variance) rather than raw signed data","Add a Filter rows / validation step before the Calculator to catch negative values early","Set the Calculator's error handling to route bad rows to a separate stream"],"tags":["kettle","pdi","calculator","math"],"backgroundTag":"invalid-argument-value","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"}