Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust, python): fix merge schema with overwrite #2623

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/core/src/operations/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,13 @@ impl std::future::IntoFuture for WriteBuilder {
try_cast_batch(schema.fields(), table_schema.fields())
{
schema_drift = true;
if this.mode == SaveMode::Overwrite && this.schema_mode.is_some() {
if this.mode == SaveMode::Overwrite
&& this.schema_mode == Some(SchemaMode::Merge)
{
new_schema =
Some(merge_schema(table_schema.clone(), schema.clone())?);
} else if this.mode == SaveMode::Overwrite && this.schema_mode.is_some()
{
new_schema = None // we overwrite anyway, so no need to cast
} else if this.schema_mode == Some(SchemaMode::Merge) {
new_schema =
Expand Down
28 changes: 28 additions & 0 deletions python/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,34 @@ def test_update_schema_rust_writer_invalid(existing_table: DeltaTable):
assert existing_table.schema().to_pyarrow() == new_data.schema


def test_merge_schema_rust_writer_with_overwrite(tmp_path: pathlib.Path):
data = pa.table(
{
"a": pa.array([1, 2, 3, 4]),
"b": pa.array([1, 1, 2, 2]),
"c": pa.array([10, 11, 12, 13]),
}
)
write_deltalake(
tmp_path,
data,
engine="rust",
)

new_data = pa.table({"a": pa.array([100, 200, 300]), "b": pa.array([1, 1, 1])})

write_deltalake(
tmp_path,
new_data,
mode="overwrite",
schema_mode="merge",
engine="rust",
)
assert set(DeltaTable(tmp_path).to_pyarrow_table().column_names) == set(
["a", "b", "c"]
)


@pytest.mark.parametrize("engine", ["pyarrow", "rust"])
def test_local_path(
tmp_path: pathlib.Path,
Expand Down
Loading