When appending a DataFrame to an existing Delta table, the most common and recommended pattern is to match columns by name, not by position/index.
Why match by column names?
- Column order can change: DataFrames and tables may have columns in different orders, especially in collaborative or evolving environments.
- Schema evolution: Delta Lake supports schema evolution, and new columns may be added over time.
- Safety: Matching by name ensures that data is written to the correct columns, avoiding silent data corruption.
How does this work in practice?
- Spark DataFrame
.write API (e.g., df.write.format("delta").mode("append").save(path) or .saveAsTable(tableName)) matches columns by name.
- If the DataFrame has extra columns not present in the table, or is missing columns, you may get an error unless you enable schema evolution or explicitly handle the schema.
Example
Suppose your Delta table has columns: id, name, age.
If your DataFrame has columns in a different order, e.g., age, id, name, Spark/Delta will match by column name when appending.
Reference
Summary Table
| Pattern | Default in Delta/Spark | Recommended? | Notes |
|---|
| By column name | Yes | Yes | Safe, robust to column order changes |
| By column index | No | No | Risky, not the default, not recommended |
In summary:
Delta Lake appends DataFrames to tables by matching column names, not by position. This is the safest and most common pattern.