{"record":{"id":"3a722f945ba897f1","repo":"apache/druid","slug":"needs-a-long-as-its-second-argument-but-got-null","errorCode":null,"errorMessage":"needs a LONG as its second argument but got null","messagePattern":"needs a LONG as its second argument but got null","errorType":"validation","errorClass":"ExpressionValidationException","httpStatus":400,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/math/expr/Function.java","lineNumber":4690,"sourceCode":"      /**\n       * only LONG and DOUBLE are allowed\n       * For a DOUBLE, it will be cast to LONG before format\n       */\n      if (valueParam.value() != null && !valueParam.type().anyOf(ExprType.LONG, ExprType.DOUBLE)) {\n        throw validationFailed(\n            \"needs a number as its first argument but got %s instead\",\n            valueParam.type()\n        );\n      }\n\n      /**\n       * By default, precision is 2\n       */\n      long precision = 2;\n      if (args.size() > 1) {\n        ExprEval precisionParam = args.get(1).eval(bindings);\n        if (precisionParam.value() == null) {\n          throw validationFailed(\"needs a LONG as its second argument but got null\");\n        }\n        if (!precisionParam.type().is(ExprType.LONG)) {\n          throw validationFailed(\"needs a LONG as its second argument but got %s instead\", precisionParam.type());\n        }\n        precision = precisionParam.asLong();\n        if (precision < 0 || precision > 3) {\n          throw validationFailed(\"given precision[%d] must be in the range of [0,3]\", precision);\n        }\n      }\n\n      return ExprEval.ofString(HumanReadableBytes.format(valueParam.asLong(), precision, this.getUnitSystem()));\n    }\n\n    @Override\n    public void validateArguments(List<Expr> args)\n    {\n      validationHelperCheckAnyOfArgumentCount(args, 1, 2);\n    }","sourceCodeStart":4672,"sourceCodeEnd":4708,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/math/expr/Function.java#L4672-L4708","documentation":"Thrown when HUMAN_READABLE_BYTES_FORMAT's optional second argument (precision) evaluates to SQL NULL. Because the precision must be a concrete LONG in [0,3], a null cannot be accepted and validation fails with this dedicated message.","triggerScenarios":"Calling human_readable_bytes_format(value, null) explicitly, or passing a nullable column/parameter whose value is NULL at evaluation time, e.g. human_readable_bytes_format(b, nullable_precision_col).","commonSituations":"Left-joined tables producing NULL precision columns; optional client parameters not supplied and defaulting to null; ETL writing nulls into the precision field.","solutions":["Use COALESCE(precision, 2) to supply the documented default of 2 when null.","Omit the second argument entirely so the built-in default (2) applies.","Filter out rows where the precision is NULL before applying the function.","Fix upstream data so the precision column is never null."],"exampleFix":"// before\nhuman_readable_bytes_format(b, prec_col) -- prec_col may be NULL\n// after\nhuman_readable_bytes_format(b, COALESCE(prec_col, 2))","handlingStrategy":"fallback","validationCode":"COALESCE(precision_expr, 2) -- wrap nullable precision","typeGuard":"if (precision == null) precision = 2;","tryCatchPattern":"try { runQuery(q); } catch (ExpressionValidationException e) { /* retry with default precision */ }","preventionTips":["Wrap nullable precision in COALESCE","Omit the second argument to use the default of 2","Filter NULL precision rows before applying the function"],"tags":["druid","expressions","null","validation"],"backgroundTag":"null-argument","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}