{"record":{"id":"8747874b90df0326","repo":"MyCATApache/Mycat-Server","slug":"number-of-values-and-columns-have-to-match","errorCode":null,"errorMessage":"number of values and columns have to match","messagePattern":"number of values and columns have to match","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java","lineNumber":181,"sourceCode":"        }\r\n        if (statement instanceof SQLDropTableStatement) {\r\n        \treturn dropTable((SQLDropTableStatement)statement);\r\n        }\r\n        if (statement instanceof SQLDeleteStatement) {\r\n        \treturn DeleteDate((SQLDeleteStatement)statement);\r\n        }\r\n        if (statement instanceof SQLCreateTableStatement) {\r\n        \treturn 1;\r\n        }          \r\n\t\treturn 1;\r\n\t\t\r\n\t}\r\n\tprivate int InsertData(SQLInsertStatement state) {\r\n\t\tif (state.getValues().getValues().size() ==0 ){\r\n\t\t\tthrow new RuntimeException(\"number of  columns error\");\r\n\t\t}\t\t\r\n\t\tif (state.getValues().getValues().size() != state.getColumns().size()){\r\n\t\t\tthrow new RuntimeException(\"number of values and columns have to match\");\r\n\t\t}\r\n\t\tSQLTableSource table=state.getTableSource();\r\n\t\tBasicDBObject[] oList = new BasicDBObject[state.getValuesList().size()];\r\n\t\tint i = 0;\r\n\t\tfor(SQLInsertStatement.ValuesClause values : state.getValuesList()){\r\n\t\t\tint j = 0;\r\n\t\t\tBasicDBObject o = new BasicDBObject();\r\n\t\t\toList[i++] = o ;\r\n\t\t\tfor(SQLExpr col : state.getColumns()) {\r\n\t\t\t\to.put(getFieldName2(col), getExpValue(values.getValues().get(j++)));\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tDBCollection coll =this._db.getCollection(table.toString());\r\n\t\tWriteResult result = coll.insert(oList);\r\n\t\treturn i; // 这里result.getN 总是返回0 , 所以按插入数据量返回影响行数\r\n\t}\r\n\tprivate int UpData(SQLUpdateStatement state) {\r","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java#L163-L199","documentation":"InsertData() requires that the number of value expressions in the VALUES clause equals the number of columns listed in the INSERT. When they differ it throws RuntimeException(\"number of values and columns have to match\"). This is a structural validation of the INSERT statement before translation into MongoDB documents.","triggerScenarios":"INSERT INTO t (a,b,c) VALUES (1,2) — column count 3 vs value count 2 — executed via executeUpdate on the MongoDB backend handler.","commonSituations":"Hand-written INSERT with a missing value, dynamic SQL string concatenation that drops a value, adding a column to the schema without updating all INSERT statements, multi-row VALUES where one row has fewer values.","solutions":["Make the VALUES list contain exactly as many expressions as the column list","Use parameterized/generated SQL builders that pair columns and values together instead of concatenating strings","Count and validate placeholders vs columns before executing (cols.length == values.length)","Catch RuntimeException around executeUpdate and report which SQL statement had the mismatch"],"exampleFix":"// before\nstmt.executeUpdate(\"INSERT INTO users (id, name) VALUES (1)\");\n\n// after\nstmt.executeUpdate(\"INSERT INTO users (id, name) VALUES (1, 'alice')\");","handlingStrategy":"validation","validationCode":"SQLInsertStatement ins = (SQLInsertStatement) SQLUtils.parseSingleStatement(sql, dbType);\nint cols = ins.getColumns().size();\nint vals = ins.getValues().getValues().size();\nif (cols != vals) {\n    throw new IllegalArgumentException(\"INSERT column/value mismatch: \" + cols + \" columns vs \" + vals + \" values\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return stmt.executeUpdate(sql);\n} catch (RuntimeException e) {\n    if (\"number of values and columns have to match\".equals(e.getMessage())) {\n        throw new SQLException(\"INSERT column count != value count: \" + sql, e);\n    }\n    throw e;\n}","preventionTips":["Check columns.size() == values.size() before sending INSERT statements to the Mongo backend","Use prepared statements or SQL builders so columns and values stay paired","Update every INSERT when adding schema columns; cover multi-row VALUES in tests"],"tags":["mongodb","sql","insert","validation","argument-mismatch"],"backgroundTag":"schema-validation-failed","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}