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: schema comparaison in writer #1854

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 15 additions & 1 deletion python/deltalake/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass
from datetime import date, datetime
from decimal import Decimal
from hashlib import md5
from math import inf
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -62,6 +63,14 @@ class AddAction:
stats: str


def _hash_schema(schema: pa.Schema) -> str:
dict_schema = dict(zip(schema.names, [str(pa_types) for pa_types in schema.types]))
hash_dt_schema = md5(
json.dumps(dict_schema, sort_keys=True).encode("utf-8")
).hexdigest()
return hash_dt_schema


def write_deltalake(
table_or_uri: Union[str, Path, DeltaTable],
data: Union[
Expand Down Expand Up @@ -195,7 +204,12 @@ def write_deltalake(
partition_by = [partition_by]

if table: # already exists
if schema != table.schema().to_pyarrow(as_large_types=large_dtypes) and not (
hash_table_schema = _hash_schema(
table.schema().to_pyarrow(as_large_types=large_dtypes)
)
hash_schema_provided = _hash_schema(schema)

if hash_schema_provided != hash_table_schema and not (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should compare actual objects and not just hashes. It is still possible for hashes to collide, despite that being unlikely.

You could write a method to normalize schema order:

def normalize_schema_order(schema: pa.Schema) -> pa.Schema:
    fields = sorted(iter(schema), key=lambda field: field.name)
    return pa.schema(fields)

And then compare those. How does that sound?

mode == "overwrite" and overwrite_schema
):
raise ValueError(
Expand Down
Loading