{"record":{"id":"25242cd63f4df48b","repo":"apache/cassandra","slug":"invalid-row-length-d-should-be-d","errorCode":null,"errorMessage":"Invalid row length %d should be %d","messagePattern":"Invalid row length (.+?) should be (.+?)","errorType":"validation","errorClass":"ParseError","httpStatus":null,"severity":"error","filePath":"pylib/cqlshlib/copyutil.py","lineNumber":2163,"sourceCode":"        \"\"\"\n        Return the null value that is inserted for fields that are missing from csv files.\n        For counters we should return zero so that the counter value won't be incremented.\n        For everything else we return nulls, this means None if we use prepared statements\n        or \"NULL\" otherwise. Note that for counters we never use prepared statements, so we\n        only check is_counter when use_prepared_statements is false.\n        \"\"\"\n        return None if self.use_prepared_statements else (\"0\" if self.is_counter else \"NULL\")\n\n    def convert_row(self, row):\n        \"\"\"\n        Convert the row into a list of parsed values if using prepared statements, else simply apply the\n        protection functions to escape values with quotes when required. Also check on the row length and\n        make sure primary partition key values aren't missing.\n        \"\"\"\n        converters = self.converters if self.use_prepared_statements else self.protectors\n\n        if len(row) != len(converters):\n            raise ParseError('Invalid row length %d should be %d' % (len(row), len(converters)))\n\n        for i in self.primary_key_indexes:\n            if row[i] == self.nullval:\n                raise ParseError(self.get_null_primary_key_message(i))\n\n        def convert(c, v):\n            try:\n                return c(v) if v != self.nullval else self.get_null_val()\n            except Exception as e:\n                # if we could not convert an empty string, then self.nullval has been set to a marker\n                # because the user needs to import empty strings, except that the converters for some types\n                # will fail to convert an empty string, in this case the null value should be inserted\n                # see CASSANDRA-12794\n                if v == '':\n                    return self.get_null_val()\n\n                if self.debug:\n                    traceback.print_exc()","sourceCodeStart":2145,"sourceCodeEnd":2181,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/pylib/cqlshlib/copyutil.py#L2145-L2181","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Fix the CSV row so field count matches the COPY column list; quote values containing commas/delimiters","Regenerate the CSV with the same delimiter and QUOTE settings used by the COPY (WITH DELIMITER, WITH QUOTE)","Adjust the COPY column list to match the CSV's actual number of fields"],"exampleFix":"// before\n1,hello,world,extra\n// after\n1,\"hello,world\"\n// or remove/quote the extra field so the count matches the column list","handlingStrategy":"validation","validationCode":"import csv\nwith open(path) as f:\n    for i, row in enumerate(csv.reader(f), 1):\n        if len(row) != expected_cols:\n            raise ValueError(f\"line {i}: {len(row)} fields, expected {expected_cols}\")","typeGuard":null,"tryCatchPattern":"try:\n    run_copy_from(...)\nexcept ParseError as e:\n    print(f\"Row shape mismatch: {e}\")  # fix that CSV line","preventionTips":["Quote fields containing the delimiter when exporting","Match WITH DELIMITER/QUOTE options between export and import","Keep the COPY column list aligned with the CSV header"],"tags":["cqlsh","copy","csv","parsing"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}