apache/druid · error · ExpressionValidationException
needs a LONG as its second argument but got %s instead
Error message
needs a LONG as its second argument but got %s instead
What it means
Thrown when HUMAN_READABLE_BYTES_FORMAT's optional second argument (precision) is present but not of type LONG. Precision must be an integer in [0,3]; STRING, DOUBLE, or other types are rejected with this message.
Source
Thrown at processing/src/main/java/org/apache/druid/math/expr/Function.java:4693
*/
if (valueParam.value() != null && !valueParam.type().anyOf(ExprType.LONG, ExprType.DOUBLE)) {
throw validationFailed(
"needs a number as its first argument but got %s instead",
valueParam.type()
);
}
/**
* By default, precision is 2
*/
long precision = 2;
if (args.size() > 1) {
ExprEval precisionParam = args.get(1).eval(bindings);
if (precisionParam.value() == null) {
throw validationFailed("needs a LONG as its second argument but got null");
}
if (!precisionParam.type().is(ExprType.LONG)) {
throw validationFailed("needs a LONG as its second argument but got %s instead", precisionParam.type());
}
precision = precisionParam.asLong();
if (precision < 0 || precision > 3) {
throw validationFailed("given precision[%d] must be in the range of [0,3]", precision);
}
}
return ExprEval.ofString(HumanReadableBytes.format(valueParam.asLong(), precision, this.getUnitSystem()));
}
@Override
public void validateArguments(List<Expr> args)
{
validationHelperCheckAnyOfArgumentCount(args, 1, 2);
}
@Nullable
@OverrideView on GitHub (pinned to 9b90983fd2)
Solutions
- Pass an integer literal: human_readable_bytes_format(b, 2).
- Cast the argument: CAST(prec AS BIGINT).
- Change the datasource/schema so the precision column is BIGINT.
- Remove the argument to accept the default precision of 2.
Example fix
// before human_readable_bytes_format(b, '2') // after human_readable_bytes_format(b, 2)
Defensive patterns
Strategy: type-guard
Validate before calling
CAST(precision_expr AS BIGINT) -- ensure integer type
Type guard
if (!(p instanceof Integer || p instanceof Long)) throw new IllegalArgumentException("precision must be a LONG"); Prevention
- Pass precision as an integer literal (2), not '2' or 2.0
- Keep precision columns typed BIGINT in the schema
- Beware JSON clients sending numbers as strings
When it happens
Trigger: Calling human_readable_bytes_format(value, '2') with a string precision, or a DOUBLE column/literal like 2.0 as the second argument.
Common situations: Clients sending precision as a JSON string; columns typed as DOUBLE or VARCHAR in the datasource; confusion with languages where 2.0 is a valid int.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- needs a number as its first argument but got %s instead
- Cannot implicitly cast [%s] to [%s]
- first argument must be constant STRING expression containing
- second argument must be a base64 encoded STRING value but go
- Cannot cast [%s] to [%s] (Types.InvalidCastException from in
AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07).
Data as JSON: /api/errors/e579e7e82c7d721f.
Report an issue: GitHub.