{"record":{"id":"1556a89ce3065628","repo":"apache/druid","slug":"needs-a-positive-integer-as-the-second-argument","errorCode":null,"errorMessage":"needs a positive integer as the second argument","messagePattern":"needs a positive integer as the second argument","errorType":"validation","errorClass":"ExpressionValidationException","httpStatus":400,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/math/expr/Function.java","lineNumber":2935,"sourceCode":"    @Override\n    public String name()\n    {\n      return \"right\";\n    }\n\n    @Nullable\n    @Override\n    public ExpressionType getOutputType(Expr.InputBindingInspector inspector, List<Expr> args)\n    {\n      return ExpressionType.STRING;\n    }\n\n    @Override\n    protected ExprEval eval(String x, long y)\n    {\n      int yInt = (int) y;\n      if (y < 0 || yInt != y) {\n        throw validationFailed(\"needs a positive integer as the second argument\");\n      }\n      int len = x.length();\n      return ExprEval.ofString(y < len ? x.substring(len - yInt) : x);\n    }\n  }\n\n  class LeftFunc extends StringLongFunction\n  {\n    @Override\n    public String name()\n    {\n      return \"left\";\n    }\n\n    @Nullable\n    @Override\n    public ExpressionType getOutputType(Expr.InputBindingInspector inspector, List<Expr> args)\n    {","sourceCodeStart":2917,"sourceCodeEnd":2953,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/math/expr/Function.java#L2917-L2953","documentation":"The RIGHT(string, n) expression function requires its second argument to be a positive whole number that fits in an int. A negative value, or a LONG too large to narrow to int without loss (yInt != y), fails validation.","triggerScenarios":"RIGHT('abc', -1) or RIGHT('abc', 5000000000) — a negative second argument, or one exceeding Integer.MAX_VALUE.","commonSituations":"Length arguments computed from data (LEN(col)-N) going negative on short strings; arithmetic overflow producing huge values; literal typos.","solutions":["Guard the length: CASE WHEN LEN(x) >= n THEN RIGHT(x, n) ELSE '' END","Clamp negative values: GREATEST(n, 0)","Ensure the argument is <= 2147483647 before calling","Review the expression producing the length for subtraction that can go negative"],"exampleFix":"// before: RIGHT(x, LEN(x) - 10) with x shorter than 10 -> negative -> throws\n// after: CASE WHEN LEN(x) >= 10 THEN RIGHT(x, LEN(x) - 10) ELSE '' END","handlingStrategy":"validation","validationCode":"if (!(len >= 0 && len <= Integer.MAX_VALUE)) {\n  throw new IllegalArgumentException(\"RIGHT length must be a non-negative int: \" + len);\n}","typeGuard":"static boolean validRightLen(long len) {\n  return len >= 0 && len <= Integer.MAX_VALUE;\n}","tryCatchPattern":null,"preventionTips":["Guard derived lengths (LEN(x)-N) against going negative","Clamp with GREATEST(len, 0)","Cap computed lengths at Integer.MAX_VALUE"],"tags":["druid","expressions","string-functions","argument-validation"],"backgroundTag":"invalid-argument-value","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"}