apache/druid · error · IllegalArgumentException

count is negative,

Error message

count is negative, 

What it means

StringUtils.repeat(String, int) repeats a string a given number of times. It throws IllegalArgumentException when count is negative, mirroring the commons-lang behavior it replaces (JDK-8197594 workaround). A negative repeat count is meaningless and treated as a caller bug.

Solutions

  1. Clamp the count before calling: Math.max(0, count).
  2. Validate user/config-supplied lengths before passing them.
  3. Catch IllegalArgumentException and return "" for negative counts.
  4. Fix the computation so the difference is never negative (guard the subtraction).

Example fix

// before
String pad = StringUtils.repeat(" ", width - s.length());
// after
String pad = StringUtils.repeat(" ", Math.max(0, width - s.length()));
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) { throw new IllegalArgumentException("repeat count must be >= 0, got " + count); }

Prevention

When it happens

Trigger: Calling StringUtils.repeat(s, n) with n < 0 — typically when n comes from user input, computed padding widths (e.g. length difference), or unvalidated config.

Common situations: Formatting/padding code computing count as (targetLength - actualLength) when the string is already longer than target; SQL LPAD-like expressions with negative length parameters.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/3cd3899b93593995. Report an issue: GitHub.

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/java/util/common/StringUtils.java:608

   * <p>
   * If count or length is zero then the empty string is returned.
   * <p>
   * This method may be used to create space padding for
   * formatting text or zero padding for formatting numbers.
   *
   * @param count number of times to repeat
   *
   * @return A string composed of this string repeated
   * {@code count} times or the empty string if count
   * or length is zero.
   *
   * @throws IllegalArgumentException if the {@code count} is negative.
   * @link https://bugs.openjdk.java.net/browse/JDK-8197594
   */
  public static String repeat(String s, int count)
  {
    if (count < 0) {
      throw new IllegalArgumentException("count is negative, " + count);
    }
    if (count == 1) {
      return s;
    }
    byte[] value = s.getBytes(StandardCharsets.UTF_8);
    final int len = value.length;
    if (len == 0 || count == 0) {
      return "";
    }
    if (len == 1) {
      final byte[] single = new byte[count];
      Arrays.fill(single, value[0]);
      return new String(single, StandardCharsets.UTF_8);
    }
    if (Integer.MAX_VALUE / count < len) {
      throw new RuntimeException("The produced string is too large.");
    }
    final int limit = len * count;

View on GitHub (pinned to 9b90983fd2)