{"record":{"id":"18bbd3ae8884e402","repo":"apache/iceberg","slug":"field-dockey-of-schema-builder-name-is-not","errorCode":null,"errorMessage":"Field ${docKey} of schema ${builder.name()} is not the same type for all documents in the array.\nCheck option 'struct' of parameter 'array.encoding'","messagePattern":"Field (.+?) of schema (.+?) is not the same type for all documents in the array\\.\nCheck option 'struct' of parameter 'array\\.encoding'","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"kafka-connect/kafka-connect-transforms/src/main/java/org/debezium/connector/mongodb/transforms/MongoDataConverter.java","lineNumber":506,"sourceCode":"      }\n    }\n  }\n\n  private void testArrayElementType(\n      SchemaBuilder builder, Entry<String, BsonValue> arrayDoc, Map<String, BsonType> union) {\n    final String docKey = (String) arrayDoc.getKey();\n    final BsonType currentType = arrayDoc.getValue().getBsonType();\n    final BsonType prevType = union.putIfAbsent(docKey, currentType);\n\n    if (prevType != null) {\n      if ((prevType == BsonType.NULL || currentType == BsonType.NULL)\n          && !Objects.equals(prevType, currentType)) {\n        // set non-null type as real schema\n        if (prevType == BsonType.NULL) {\n          union.put(docKey, currentType);\n        }\n      } else if (!Objects.equals(prevType, currentType)) {\n        throw new RuntimeException(\n            \"Field \"\n                + docKey\n                + \" of schema \"\n                + builder.name()\n                + \" is not the same type for all documents in the array.\\n\"\n                + \"Check option 'struct' of parameter 'array.encoding'\");\n      }\n    }\n  }\n}\n","sourceCodeStart":488,"sourceCodeEnd":517,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/kafka-connect/kafka-connect-transforms/src/main/java/org/debezium/connector/mongodb/transforms/MongoDataConverter.java#L488-L517","documentation":"MongoDataConverter.testArrayElementType throws RuntimeException when, across documents in an array-of-documents (array.encoding=struct), a given key has differing BSON types between documents (ignoring NULL vs value unions). All documents in the array must agree on each field's type.","triggerScenarios":"subSchema/testType processing an array of documents where docKey has type STRING in one document and INT (or another non-null-differing type) in another.","commonSituations":"MongoDB arrays of objects where the same field is stored with inconsistent types across elements — e.g. amounts sometimes as number, sometimes as string; legacy documents mixed with new ones.","solutions":["Normalize the field's type across all array elements in MongoDB (e.g. cast via aggregation pipeline update).","Use a non-struct array.encoding so arrays are passed as JSON instead of inferred struct schemas.","Fix the writing application to use a consistent schema."],"exampleFix":"// before\ndb.coll.insertMany([{items:[{amt:1}]},{items:[{amt:\"2\"}]}])\n// after\ndb.coll.insertMany([{items:[{amt:1}]},{items:[{amt:2}]}])","handlingStrategy":"validation","validationCode":"// Verify each key has one non-null type across array documents\njava.util.Map<String, BsonType> seen = new java.util.HashMap<>();\nfor (BsonValue el : arr.asArray()) {\n  BsonDocument d = el.asDocument();\n  for (var e : d.entrySet()) {\n    BsonType t = e.getValue().isNull() ? BsonType.NULL : e.getValue().getBsonType();\n    BsonType prev = seen.putIfAbsent(e.getKey(), t);\n    if (prev != null && !prev.equals(t) && prev != BsonType.NULL && t != BsonType.NULL) throw new IllegalStateException(\"type conflict for \" + e.getKey());\n  }\n}","typeGuard":null,"tryCatchPattern":"try { ... } catch (RuntimeException e) { log.error(\"Inconsistent array document types: {}\", e.getMessage()); }","preventionTips":["Normalize field types across documents in arrays (e.g. always numeric amounts)","Use MongoDB $type validation rules per array element","Prefer array.encoding=json for legacy collections"],"tags":["kafka-connect","debezium","mongodb","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}