Multi-Source Data Merger
Overview
This skill guides the process of merging data from multiple heterogeneous sources into unified output formats. It covers reading diverse file formats, mapping fields across different schemas, detecting and resolving conflicts based on priority rules, and producing clean merged output with comprehensive conflict documentation.
Workflow
Phase 1: Source Analysis and Schema Discovery
Before writing any code, thoroughly inspect all source files to understand their structure:
Identify all source files and their formats
- List all input files and determine their types (CSV, JSON, Parquet, XML, etc.)
- Note which formats require special libraries (e.g.,
pyarrow or pandas for Parquet)
Extract actual schemas from each source
- For readable formats (CSV, JSON, XML): Read and document exact field names
- For binary formats (Parquet): Use appropriate tools to inspect schema
- Never assume field names from task descriptions alone
Document field mappings explicitly
- Create a clear mapping table:
source_field -> canonical_field
- Note type differences (e.g.,
userId as string vs integer)
- Identify which fields exist in which sources
Identify the merge key
- Determine which field(s) uniquely identify records across sources
- Verify the key exists and is consistent across all sources
Phase 2: Environment Setup
Create isolated environment
- Use virtual environment (venv, conda, uv) for dependency isolation
- Document all required dependencies before installation
Install dependencies incrementally
- Install only what is needed for each format
- Verify each library works before proceeding
- Common dependencies:
pandas, pyarrow, openpyxl, xmltodict
Avoid repeated environment commands
- Set environment variables once at the start
- Create helper scripts for repeated operations if needed
Phase 3: Implementation Strategy
Modular Code Structure
Structure the solution with clear separation of concerns:
1. File readers (one function per format)
2. Field mappers (transform to canonical schema)
3. Merge logic (combine records by key)
4. Conflict detector (identify value differences)
5. Conflict resolver (apply priority rules)
6. Output writers (generate required formats)
Incremental Development
Start with file reading
- Implement and test each reader independently
- Verify data loads correctly before proceeding
- Print sample records to confirm structure
Implement field mapping
- Transform each source to canonical schema
- Handle type coercion explicitly (strings to integers, date parsing)
- Test mapping on sample records
Build merge logic
- Combine all records by merge key
- Track which source each value came from
- Handle records that appear in only one source
Add conflict detection
- Compare values across sources for same key
- Define what constitutes a conflict clearly
- Distinguish between: missing field, null value, different value
Implement conflict resolution
- Apply priority rules consistently
- Document which source "won" for each conflict
- Preserve conflict information for reporting
Phase 4: Verification Strategy
Verify Each Component
Source reading verification
- Count records per source
- Sample first/last records
- Verify all expected fields present
Merge verification
- Count unique keys in merged output
- Verify:
merged_count = unique_keys_across_all_sources
- Check records appearing in multiple sources
Conflict verification
- Manually trace at least one known conflict
- Verify conflict count matches expectations
- Check conflict resolution followed priority rules
Output verification
- Validate output format (JSON structure, CSV headers)
- Verify required fields present with correct types
- Check for unintended None/null values
Verification Script Pattern
Create a dedicated verification step that checks:
- Record counts match expectations
- All required fields present
- Data types are correct
- No unexpected null values
- Conflict counts are reasonable
Common Pitfalls
Schema and Field Mapping
| Pitfall |
Prevention |
| Assuming field names without verification |
Always read and inspect actual source files first |
| Missing field type coercion |
Explicitly convert types (especially IDs to integers) |
| Inconsistent date formats |
Normalize all dates to a single format during mapping |
| None vs null vs missing confusion |
Define explicit handling rules for each case |
Conflict Detection
| Pitfall |
Prevention |
| Unclear conflict definition |
Document exactly what constitutes a conflict before coding |
| Missing vs null not distinguished |
Treat "field not present" differently from "field is null" |
| Counting conflicts incorrectly |
Define: per-field, per-record, or per-user-field combination |
| Not detecting all conflict types |
Test with records that have conflicts in every field |
Implementation
| Pitfall |
Prevention |
| Writing full script without testing |
Build incrementally, test each component |
| Syntax errors in large scripts |
Validate script syntax before running |
| Truncated file writes |
Verify complete file was written (check line count or file size) |
| No error handling for edge cases |
Add try/catch for file operations and data parsing |
Output Quality
| Pitfall |
Prevention |
| String "None" instead of null |
Use proper JSON null values, not string representations |
| Inconsistent output format |
Validate output against schema/requirements |
| Missing records in merge |
Verify all unique keys from all sources appear in output |
| Duplicate records |
Check for and handle duplicates within single sources |
Edge Cases Checklist
Before considering the implementation complete, verify handling of:
Output Requirements Checklist
For the merged data output:
For the conflicts report:
References
This skill includes a reference guide for detailed information:
references/
data_merge_patterns.md - Detailed patterns for common merge scenarios, type coercion strategies, and conflict resolution approaches
1---2name: multi-source-data-merger3description: This skill provides guidance for merging data from multiple heterogeneous sources (CSV, JSON, Parquet, XML, etc.) into unified output formats with conflict detection and resolution. Use when tasks involve combining data from different file formats, field mapping between schemas, priority-based conflict resolution, or generating merged datasets with conflict reports.4---56# Multi-Source Data Merger78## Overview910This skill guides the process of merging data from multiple heterogeneous sources into unified output formats. It covers reading diverse file formats, mapping fields across different schemas, detecting and resolving conflicts based on priority rules, and producing clean merged output with comprehensive conflict documentation.1112## Workflow1314### Phase 1: Source Analysis and Schema Discovery1516Before writing any code, thoroughly inspect all source files to understand their structure:17181. **Identify all source files and their formats**19 - List all input files and determine their types (CSV, JSON, Parquet, XML, etc.)20 - Note which formats require special libraries (e.g., `pyarrow` or `pandas` for Parquet)21222. **Extract actual schemas from each source**23 - For readable formats (CSV, JSON, XML): Read and document exact field names24 - For binary formats (Parquet): Use appropriate tools to inspect schema25 - Never assume field names from task descriptions alone26273. **Document field mappings explicitly**28 - Create a clear mapping table: `source_field -> canonical_field`29 - Note type differences (e.g., `userId` as string vs integer)30 - Identify which fields exist in which sources31324. **Identify the merge key**33 - Determine which field(s) uniquely identify records across sources34 - Verify the key exists and is consistent across all sources3536### Phase 2: Environment Setup37381. **Create isolated environment**39 - Use virtual environment (venv, conda, uv) for dependency isolation40 - Document all required dependencies before installation41422. **Install dependencies incrementally**43 - Install only what is needed for each format44 - Verify each library works before proceeding45 - Common dependencies: `pandas`, `pyarrow`, `openpyxl`, `xmltodict`46473. **Avoid repeated environment commands**48 - Set environment variables once at the start49 - Create helper scripts for repeated operations if needed5051### Phase 3: Implementation Strategy5253#### Modular Code Structure5455Structure the solution with clear separation of concerns:5657```581. File readers (one function per format)592. Field mappers (transform to canonical schema)603. Merge logic (combine records by key)614. Conflict detector (identify value differences)625. Conflict resolver (apply priority rules)636. Output writers (generate required formats)64```6566#### Incremental Development67681. **Start with file reading**69 - Implement and test each reader independently70 - Verify data loads correctly before proceeding71 - Print sample records to confirm structure72732. **Implement field mapping**74 - Transform each source to canonical schema75 - Handle type coercion explicitly (strings to integers, date parsing)76 - Test mapping on sample records77783. **Build merge logic**79 - Combine all records by merge key80 - Track which source each value came from81 - Handle records that appear in only one source82834. **Add conflict detection**84 - Compare values across sources for same key85 - Define what constitutes a conflict clearly86 - Distinguish between: missing field, null value, different value87885. **Implement conflict resolution**89 - Apply priority rules consistently90 - Document which source "won" for each conflict91 - Preserve conflict information for reporting9293### Phase 4: Verification Strategy9495#### Verify Each Component96971. **Source reading verification**98 - Count records per source99 - Sample first/last records100 - Verify all expected fields present1011022. **Merge verification**103 - Count unique keys in merged output104 - Verify: `merged_count = unique_keys_across_all_sources`105 - Check records appearing in multiple sources1061073. **Conflict verification**108 - Manually trace at least one known conflict109 - Verify conflict count matches expectations110 - Check conflict resolution followed priority rules1111124. **Output verification**113 - Validate output format (JSON structure, CSV headers)114 - Verify required fields present with correct types115 - Check for unintended None/null values116117#### Verification Script Pattern118119Create a dedicated verification step that checks:120- Record counts match expectations121- All required fields present122- Data types are correct123- No unexpected null values124- Conflict counts are reasonable125126## Common Pitfalls127128### Schema and Field Mapping129130| Pitfall | Prevention |131|---------|------------|132| Assuming field names without verification | Always read and inspect actual source files first |133| Missing field type coercion | Explicitly convert types (especially IDs to integers) |134| Inconsistent date formats | Normalize all dates to a single format during mapping |135| None vs null vs missing confusion | Define explicit handling rules for each case |136137### Conflict Detection138139| Pitfall | Prevention |140|---------|------------|141| Unclear conflict definition | Document exactly what constitutes a conflict before coding |142| Missing vs null not distinguished | Treat "field not present" differently from "field is null" |143| Counting conflicts incorrectly | Define: per-field, per-record, or per-user-field combination |144| Not detecting all conflict types | Test with records that have conflicts in every field |145146### Implementation147148| Pitfall | Prevention |149|---------|------------|150| Writing full script without testing | Build incrementally, test each component |151| Syntax errors in large scripts | Validate script syntax before running |152| Truncated file writes | Verify complete file was written (check line count or file size) |153| No error handling for edge cases | Add try/catch for file operations and data parsing |154155### Output Quality156157| Pitfall | Prevention |158|---------|------------|159| String "None" instead of null | Use proper JSON null values, not string representations |160| Inconsistent output format | Validate output against schema/requirements |161| Missing records in merge | Verify all unique keys from all sources appear in output |162| Duplicate records | Check for and handle duplicates within single sources |163164## Edge Cases Checklist165166Before considering the implementation complete, verify handling of:167168- [ ] Records appearing in only one source (no conflict possible)169- [ ] Records appearing in all sources with identical values (no conflict)170- [ ] Records with conflicts in every mapped field171- [ ] Fields that exist in some sources but not others172- [ ] Explicit null/empty values vs missing fields173- [ ] Type variations (string "123" vs integer 123)174- [ ] Date format variations across sources175- [ ] Duplicate records within a single source176- [ ] Empty source files177- [ ] Very large files (memory considerations)178179## Output Requirements Checklist180181For the merged data output:182- [ ] Correct file format (JSON, CSV, etc.)183- [ ] All required fields present184- [ ] Correct data types (especially numeric IDs)185- [ ] Proper null handling (not string "None")186- [ ] Records sorted if required187188For the conflicts report:189- [ ] All conflicts documented190- [ ] Source of each conflicting value identified191- [ ] Resolution (winning value) clearly indicated192- [ ] Conflict count accurate and well-defined193194## References195196This skill includes a reference guide for detailed information:197198### references/199200- `data_merge_patterns.md` - Detailed patterns for common merge scenarios, type coercion strategies, and conflict resolution approaches