{"record":{"id":"0e6d0c5287662fc8","repo":"pentaho/pentaho-kettle","slug":"the-sqrt-function-only-works-on-numeric-data","errorCode":null,"errorMessage":"The 'sqrt' function only works on numeric data.","messagePattern":"The 'sqrt' function only works on numeric data\\.","errorType":"exception","errorClass":"KettleValueException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/row/ValueDataUtil.java","lineNumber":946,"sourceCode":"        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\n  private static String valueNotNegative( final Double num ) {\n    return \"The value: [\" + num + \"] is a negative number. \"\n      + \"The 'sqrt' function only works with non-negative numbers.\";\n  }\n\n  /**\n   * 100 * A / B\n   *\n   * @param metaA\n   * @param dataA\n   * @param metaB\n   * @param dataB\n   * @return\n   * @throws KettleValueException\n   */","sourceCodeStart":928,"sourceCodeEnd":964,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/row/ValueDataUtil.java#L928-L964","documentation":"ValueDataUtil.sqrt() only implements TYPE_NUMBER, TYPE_INTEGER, and TYPE_BIGNUMBER. Any other value type (string, date, boolean, binary) hits the switch's default branch and throws this KettleValueException. It signals that the field passed to SQRT is not numeric.","triggerScenarios":"ValueDataUtil.sqrt(metaA, dataA) (or SQRT in a Calculator/Formula step) where metaA.getType() is not TYPE_NUMBER, TYPE_INTEGER, or TYPE_BIGNUMBER — most commonly a String field.","commonSituations":"CSV/Excel inputs read entirely as strings, fields typed as String after a text-file input step, forgetting a 'Select values' / type-conversion step before the Calculator, boolean or date columns mistakenly wired into SQRT.","solutions":["Convert the field to a numeric type before SQRT (Select values step: String -> Number/Integer)","Fix the input step's type mapping (Text file input / Excel input column type) so the field is read as numeric","If the value is a numeric string, use a 'String cut/Replace' + conversion or 'If field value is null'/calculator conversion to Number first","Check the Calculator step's field A selection — a non-numeric field was probably picked"],"exampleFix":"// before\nValueMetaInterface metaA = new ValueMetaString(\"amount\");\nObject r = ValueDataUtil.sqrt(metaA, dataA); // throws\n// after\nValueMetaInterface metaA = new ValueMetaNumber(\"amount\");\nObject numeric = ValueDataUtil.convertData(new ValueMetaString(\"amount\"), dataA, metaA);\nObject r = ValueDataUtil.sqrt(metaA, numeric);","handlingStrategy":"type-guard","validationCode":"int t = metaA.getType();\nif (t == ValueMetaInterface.TYPE_NUMBER || t == ValueMetaInterface.TYPE_INTEGER || t == ValueMetaInterface.TYPE_BIGNUMBER) {\n  result = ValueDataUtil.sqrt(metaA, dataA);\n} else {\n  // convert first\n  ValueMetaInterface numMeta = new ValueMetaNumber(metaA.getName());\n  Object numData = ValueDataUtil.convertData(metaA, dataA, numMeta);\n  result = ValueDataUtil.sqrt(numMeta, numData);\n}","typeGuard":"boolean isNumericMeta(ValueMetaInterface meta) {\n  if (meta == null) return false;\n  int t = meta.getType();\n  return t == ValueMetaInterface.TYPE_NUMBER\n    || t == ValueMetaInterface.TYPE_INTEGER\n    || t == ValueMetaInterface.TYPE_BIGNUMBER;\n}","tryCatchPattern":"try {\n  result = ValueDataUtil.sqrt(metaA, dataA);\n} catch (KettleValueException e) {\n  if (e.getMessage().contains(\"only works on numeric data\")) {\n    logError(\"Non-numeric field \" + metaA.getName() + \" (type=\" + metaA.getTypeDesc() + \") passed to SQRT\");\n  }\n  throw e; // type errors are configuration bugs, do not swallow\n}","preventionTips":["Always set explicit column types in Text/Excel/CSV input steps — never rely on defaults for numeric math","Insert a 'Select values' type-conversion step between string sources and Calculator steps","Review Calculator step field selections after any upstream schema change","Use row-level metadata checks (getEmptyFields/row meta inspection) in unit tests for transformations"],"tags":["kettle","pdi","calculator","type-mismatch"],"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"}