apache/druid · error · RuntimeException

The produced string is too large.

Error message

The produced string is too large.

What it means

StringUtils.repeat(String, int) builds the repeated string as a UTF-8 byte array. Before allocating len*count bytes it checks whether that would exceed Integer.MAX_VALUE and throws RuntimeException "The produced string is too large." to avoid overflow or impossible allocations.

Solutions

  1. Validate the multiplier and cap it so len * count stays well below Integer.MAX_VALUE (and realistic memory limits).
  2. Compute the intended size up front and refuse/refuse-with-default if it is unreasonable.
  3. Catch RuntimeException from repeat and return a truncated or empty value.
  4. Fix the expression/query logic producing the huge count.

Example fix

// before
String s = StringUtils.repeat("x", count);
// after
if (count < 0 || (long) count > (Integer.MAX_VALUE / Math.max(1, "x".length()))) {
  throw new IllegalArgumentException("repeat count too large: " + count);
}
String s = StringUtils.repeat("x", count);
Defensive patterns

Strategy: validation

Validate before calling

long total = (long) s.length() * (long) count;
if (count < 0 || total > Integer.MAX_VALUE) { throw new IllegalArgumentException("repeat result too large: " + total); }

Try / catch

try { return StringUtils.repeat(s, count); } catch (RuntimeException e) { LOG.warn(e, "repeat too large"); return s; }

Prevention

When it happens

Trigger: Calling StringUtils.repeat(s, count) where s.length() * count would exceed Integer.MAX_VALUE characters — e.g. repeating a non-trivial string millions of times, or an unbounded multiplier from user input/SQL expressions.

Common situations: SQL string functions (REPEAT/LPAD/SPACE) with huge or unvalidated count parameters; runaway expressions producing enormous padding values.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


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

Appendix: source

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

  {
    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;
    final byte[] multiple = new byte[limit];
    System.arraycopy(value, 0, multiple, 0, len);
    int copied = len;
    for (; copied < limit - copied; copied <<= 1) {
      System.arraycopy(multiple, 0, multiple, copied, copied);
    }
    System.arraycopy(multiple, 0, multiple, copied, limit - copied);
    return new String(multiple, StandardCharsets.UTF_8);
  }

  /**
   * Returns the string left-padded with the string pad to a length of len characters.
   * If str is longer than len, the return value is shortened to len characters.
   * This function is migrated from flink's scala function with minor refactor
   * https://github.com/apache/flink/blob/master/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala
   * - Modified to handle empty pad string.

View on GitHub (pinned to 9b90983fd2)