{"record":{"id":"4f19a1147e907319","repo":"MyCATApache/Mycat-Server","slug":"number-of-columns-error","errorCode":null,"errorMessage":"number of  columns error","messagePattern":"number of  columns error","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java","lineNumber":178,"sourceCode":"        }\t\r\n        if (statement instanceof SQLUpdateStatement) {\r\n        \treturn UpData((SQLUpdateStatement)statement);\r\n        }\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","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/jdbc/mongodb/MongoSQLParser.java#L160-L196","documentation":"InsertData() validates an INSERT statement before building MongoDB BasicDBObjects. If the VALUES clause contains zero value expressions, it throws RuntimeException(\"number of  columns error\"). This guards against INSERT statements with an empty VALUES list, which cannot be translated to a Mongo document insert.","triggerScenarios":"Executing INSERT INTO t () VALUES () or any INSERT whose values clause parses to an empty list, via executeUpdate on the MongoDB handler.","commonSituations":"Dynamically generated INSERT SQL where the value list was filtered to empty; SQL builders emitting INSERT with no rows; mistyped SQL with missing VALUES contents.","solutions":["Ensure every INSERT statement includes at least one row of values","Validate the generated SQL before executing: values list must be non-empty","Catch RuntimeException around executeUpdate and surface a clearer 'empty VALUES clause' message to the caller","If rows are optional, skip execution when the values list is empty instead of issuing the INSERT"],"exampleFix":"// before\nString sql = \"INSERT INTO users (name) VALUES ()\";\nstmt.executeUpdate(sql); // throws\n\n// after\nif (values.length == 0) {\n    return 0; // nothing to insert\n}\nstmt.executeUpdate(\"INSERT INTO users (name) VALUES ('\" + values[0] + \"')\");","handlingStrategy":"validation","validationCode":"SQLStatement st = SQLUtils.parseSingleStatement(sql, dbType);\nif (st instanceof SQLInsertStatement) {\n    SQLInsertStatement ins = (SQLInsertStatement) st;\n    if (ins.getValues() == null || ins.getValues().getValues().isEmpty()) {\n        throw new IllegalArgumentException(\"INSERT requires a non-empty VALUES clause\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return stmt.executeUpdate(sql);\n} catch (RuntimeException e) {\n    if (\"number of  columns error\".equals(e.getMessage())) {\n        throw new SQLException(\"INSERT has empty VALUES clause: \" + sql, e);\n    }\n    throw e;\n}","preventionTips":["Validate INSERT SQL with a parser before execution","Skip execution entirely when there are zero rows to insert","Avoid building INSERT strings by string concatenation; use a builder that pairs columns and values"],"tags":["mongodb","sql","insert","validation"],"backgroundTag":"empty-required-field","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"}