{"record":{"id":"8748d906382cf3cd","repo":"apache/druid","slug":"precision-d-must-be-in-the-range-of-0-3","errorCode":null,"errorMessage":"precision [%d] must be in the range of [0,3]","messagePattern":"precision \\[(.+?)\\] must be in the range of \\[0,3\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java","lineNumber":250,"sourceCode":"\n    /**\n     * simplified SI format without 'B' indicator\n     * eg: K, M, G ...\n     */\n    DECIMAL\n  }\n\n  /**\n   * Returns a human-readable string version of input value\n   *\n   * @param bytes      input value. Negative value is also allowed\n   * @param precision  [0,3]\n   * @param unitSystem which unit system is adopted to format the input value, see {@link UnitSystem}\n   */\n  public static String format(long bytes, long precision, UnitSystem unitSystem)\n  {\n    if (precision < 0 || precision > 3) {\n      throw new IAE(\"precision [%d] must be in the range of [0,3]\", precision);\n    }\n\n    String pattern = \"%.\" + precision + \"f %s%s\";\n    switch (unitSystem) {\n      case BINARY_BYTE:\n        return BinaryFormatter.format(bytes, pattern, \"B\");\n      case DECIMAL_BYTE:\n        return DecimalFormatter.format(bytes, pattern, \"B\");\n      case DECIMAL:\n        return DecimalFormatter.format(bytes, pattern, \"\").trim();\n      default:\n        throw new IAE(\"Unkonwn unit system[%s]\", unitSystem);\n    }\n  }\n\n  static class BinaryFormatter\n  {\n    private static final String[] UNITS = {\"\", \"Ki\", \"Mi\", \"Gi\", \"Ti\", \"Pi\", \"Ei\"};","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java#L232-L268","documentation":"The static HumanReadableBytes.format(long bytes, long precision, UnitSystem) validates that precision is between 0 and 3 inclusive and throws this IAE otherwise. Precision controls decimal places in the formatted string (pattern '%.<precision>f %s%s'). Any negative or >3 value is rejected before formatting.","triggerScenarios":"Calling HumanReadableBytes.format(size, precision, unit) with precision < 0 or precision > 3, e.g. precision 4 for extra decimals or -1 intending 'auto'.","commonSituations":"Developers passing a computed or user-supplied decimal-places setting that was not clamped; assuming any non-negative precision is allowed; confusing this precision with significant digits.","solutions":["Clamp precision to [0,3] before calling: Math.max(0, Math.min(3, p))","Use precision 2 for the conventional '1.50 GB' style output","If more decimals are needed, format manually with String.format outside this helper"],"exampleFix":"// before\nHumanReadableBytes.format(1500, 4, UnitSystem.DECIMAL_BYTE); // IAE\n// after\nHumanReadableBytes.format(1500, 2, UnitSystem.DECIMAL_BYTE); // \"1.50 KB\"","handlingStrategy":"validation","validationCode":"if (precision < 0 || precision > 3) {\n  throw new IllegalArgumentException(\"precision must be in [0,3]: \" + precision);\n}","typeGuard":"int safePrecision = Math.max(0, Math.min(3, precision));","tryCatchPattern":"try {\n  return HumanReadableBytes.format(bytes, precision, unitSystem);\n} catch (IllegalArgumentException e) {\n  return HumanReadableBytes.format(bytes, 2, unitSystem);\n}","preventionTips":["Clamp any user- or config-supplied precision to [0,3]","Use precision 2 unless a specific display requires otherwise","Document in helper wrappers that only 0–3 decimal places are supported"],"tags":["bytes","formatting","argument-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}