apache/cassandra · error · ParseError

Invalid row length %d should be %d

Error message

Invalid row length %d should be %d

What it means

Each CSV row in COPY FROM must have exactly as many fields as there are columns in the COPY column list (one converter/protector per column). A row with a different field count raises this ParseError before any conversion.

Source

Thrown at pylib/cqlshlib/copyutil.py:2163

        """
        Return the null value that is inserted for fields that are missing from csv files.
        For counters we should return zero so that the counter value won't be incremented.
        For everything else we return nulls, this means None if we use prepared statements
        or "NULL" otherwise. Note that for counters we never use prepared statements, so we
        only check is_counter when use_prepared_statements is false.
        """
        return None if self.use_prepared_statements else ("0" if self.is_counter else "NULL")

    def convert_row(self, row):
        """
        Convert the row into a list of parsed values if using prepared statements, else simply apply the
        protection functions to escape values with quotes when required. Also check on the row length and
        make sure primary partition key values aren't missing.
        """
        converters = self.converters if self.use_prepared_statements else self.protectors

        if len(row) != len(converters):
            raise ParseError('Invalid row length %d should be %d' % (len(row), len(converters)))

        for i in self.primary_key_indexes:
            if row[i] == self.nullval:
                raise ParseError(self.get_null_primary_key_message(i))

        def convert(c, v):
            try:
                return c(v) if v != self.nullval else self.get_null_val()
            except Exception as e:
                # if we could not convert an empty string, then self.nullval has been set to a marker
                # because the user needs to import empty strings, except that the converters for some types
                # will fail to convert an empty string, in this case the null value should be inserted
                # see CASSANDRA-12794
                if v == '':
                    return self.get_null_val()

                if self.debug:
                    traceback.print_exc()

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Fix the CSV row so field count matches the COPY column list; quote values containing commas/delimiters
  2. Regenerate the CSV with the same delimiter and QUOTE settings used by the COPY (WITH DELIMITER, WITH QUOTE)
  3. Adjust the COPY column list to match the CSV's actual number of fields

Example fix

// before
1,hello,world,extra
// after
1,"hello,world"
// or remove/quote the extra field so the count matches the column list
Defensive patterns

Strategy: validation

Validate before calling

import csv
with open(path) as f:
    for i, row in enumerate(csv.reader(f), 1):
        if len(row) != expected_cols:
            raise ValueError(f"line {i}: {len(row)} fields, expected {expected_cols}")

Try / catch

try:
    run_copy_from(...)
except ParseError as e:
    print(f"Row shape mismatch: {e}")  # fix that CSV line

Prevention

When it happens

Trigger: COPY FROM where a row has more fields than declared columns (unquoted comma inside a value) or fewer (trailing empty fields trimmed by the CSV writer, truncated line, missing trailing columns).

Common situations: CSV values containing commas that were not quoted during export, rows truncated by a broken writer, column list in the COPY statement not matching the CSV header, hand-edited rows.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/25242cd63f4df48b. Report an issue: GitHub.