apache/beam · error · IllegalArgumentException

illegal trailing backslash

Error message

illegal trailing backslash

What it means

unescapeFieldName() throws this IllegalArgumentException when the quoted identifier's content ends with a lone backslash that starts an escape sequence but has no following character. A trailing \ cannot be interpreted as any valid escape, so the identifier is rejected.

Source

Thrown at sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/firestore/QueryUtils.java:272

      for (int i = 0; i < fieldName.length(); i++) {
        char c = fieldName.charAt(i);
        // Roughly speaking, there are 4 cases we care about:
        //   - carriage returns: \r and \r\n
        //   - unescaped quotes: `
        //   - non-escape sequences
        //   - escape sequences
        if (c == '`') {
          throw new IllegalArgumentException("quoted identifier cannot contain unescaped quote");
        } else if (c == '\r') {
          buf.append('\n');
          // Convert '\r\n' into '\n'
          if (i + 1 < fieldName.length() && fieldName.charAt(i + 1) == '\n') {
            i++;
          }
        } else if (c != '\\') {
          buf.append(c);
        } else if (i + 1 >= fieldName.length()) {
          throw new IllegalArgumentException("illegal trailing backslash");
        } else {
          i++;
          switch (fieldName.charAt(i)) {
            case 'a':
              buf.appendCodePoint(Ascii.BEL); // "Alert" control character
              break;
            case 'b':
              buf.append('\b');
              break;
            case 'f':
              buf.append('\f');
              break;
            case 'n':
              buf.append('\n');
              break;
            case 'r':
              buf.append('\r');
              break;

View on GitHub (pinned to 12126d8942)

Solutions

  1. Remove the trailing backslash if it is accidental
  2. Escape it as \\ so it represents a literal backslash
  3. Validate the quoted identifier has no trailing lone backslash before parsing

Example fix

// before
OrderByFieldPath.fromString("doc.`path\\`"); // ends with single \\
// after
OrderByFieldPath.fromString("doc.`path\\\\`");
Defensive patterns

Strategy: validation

Validate before calling

public static boolean endsWithLoneBackslash(String quotedContent) {
  int bs = 0;
  for (int i = quotedContent.length() - 1; i >= 0 && quotedContent.charAt(i) == '\\'; i--) bs++;
  return bs % 2 == 1;
}

Try / catch

try {
  OrderByFieldPath p = OrderByFieldPath.fromString(path);
} catch (IllegalArgumentException e) {
  LOG.warn("Trailing backslash in quoted identifier '{}'", path);
}

Prevention

When it happens

Trigger: A quoted segment like `abc\` where the final character is a backslash not followed by any escape character (a, b, f, n, r, t, v, \, `, 0-3, x, u, U).

Common situations: Config strings where a trailing backslash was intended as a literal (e.g. Windows-style name) and not doubled; string escaping done twice or truncated by upstream tooling.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/99d546e8881e86f8. Report an issue: GitHub.