# Avro 反序列化失败：写入器和读取器模式之间联合类型顺序不匹配

- **ID:** `data/avro-union-type-ordering-mismatch`
- **领域:** data
- **类别:** schema_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

Avro 联合类型对顺序敏感；如果写入器模式的联合类型顺序与读取器模式不同（例如 ["null", "string"] 与 ["string", "null"]），反序列化会因索引不匹配而失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| avro 1.11.3 | active | — | — |
| confluent-kafka-avro 7.5.0 | active | — | — |
| fastavro 1.9.4 | active | — | — |

## 解决方案

1. ```
   Ensure union types are always in alphabetical order: e.g., use ["null", "string"] not ["string", "null"]. In Avro schema definition: {"name": "field", "type": ["null", "string"]}. This is a best practice that prevents ordering issues.
   ```
2. ```
   When reading, specify the writer schema explicitly: reader = fastavro.reader(fo, writer_schema=writer_schema). This bypasses the reader schema's union ordering.
   ```

## 无效尝试

- **** — Defaults in Avro apply to the field itself, not to union type ordering. The index mismatch still occurs regardless of defaults. (80% 失败率)
- **** — Compatibility checks validate schema evolution rules but do not enforce union type ordering consistency across different schema versions. (70% 失败率)
