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

chore: introduce some more writer tests to demonstrate schema promotion #2935

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
107 changes: 106 additions & 1 deletion python/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
import random
import threading
from datetime import date, datetime
from datetime import date, datetime, timezone
from decimal import Decimal
from math import inf
from typing import Any, Dict, Iterable, List, Literal
Expand Down Expand Up @@ -1888,3 +1888,108 @@ def test_predicate_out_of_bounds(tmp_path: pathlib.Path):
schema_mode="merge",
predicate=predicate,
)


@pytest.mark.pandas
def test_write_timestampntz(tmp_path: pathlib.Path):
import pandas as pd

data = [
("AAPL", "20240731", 100, 11.1),
("GOOG", "20240731", 200, 11.1),
]
columns = ["ins", "date", "f1", "f2"]
df = pd.DataFrame(data, columns=columns)

write_deltalake(
table_or_uri=tmp_path,
data=df,
partition_by="date",
mode="overwrite",
)

dt = DeltaTable(tmp_path)
protocol = dt.protocol()
# A fresh table with no special features should have the lowest possible
# minwriter feature
assert protocol.min_writer_version == 2

data = [
(datetime(2024, 7, 31, 9, 30, 0), "AAPL", "20240731", 666, 666),
(datetime(2024, 7, 31, 9, 30, 0), "GOOG", "20240731", 777, 777),
]
columns = ["ts", "ins", "date", "fb", "fc"]
df = pd.DataFrame(data, columns=columns)
write_deltalake(
table_or_uri=tmp_path,
data=df,
partition_by="date",
mode="append",
schema_mode="merge",
)

dt = DeltaTable(tmp_path)
protocol = dt.protocol()
# Now that a datetime has been passed through the writer version needs to
# be upgraded to 7 to support timestampNtz
assert protocol.min_writer_version == 7


@pytest.mark.pandas
def test_write_timestamp(tmp_path: pathlib.Path):
import pandas as pd

data = [
("AAPL", "20240731", 100, 11.1),
("GOOG", "20240731", 200, 11.1),
]
columns = ["ins", "date", "f1", "f2"]
df = pd.DataFrame(data, columns=columns)

write_deltalake(
table_or_uri=tmp_path,
data=df,
partition_by="date",
mode="overwrite",
)

dt = DeltaTable(tmp_path)
protocol = dt.protocol()
# A fresh table with no special features should have the lowest possible
# minwriter feature
assert protocol.min_writer_version == 2

# Performing schema evolution with a timestamp that *has* a timezone should
# not result in a writer version upgrade!
data = [
(
datetime(2024, 7, 31, 9, 30, 0, tzinfo=timezone.utc),
"AAPL",
"20240731",
666,
666,
),
(
datetime(2024, 7, 31, 9, 30, 0, tzinfo=timezone.utc),
"GOOG",
"20240731",
777,
777,
),
]
columns = ["ts", "ins", "date", "fb", "fc"]
df = pd.DataFrame(data, columns=columns)
write_deltalake(
table_or_uri=tmp_path,
data=df,
partition_by="date",
mode="append",
schema_mode="merge",
)

# Reload the table to make sure we have the latest protocol
dt = DeltaTable(tmp_path)
protocol = dt.protocol()
# Now that a datetime has been passed through the writer version needs to
# be upgraded to 7 to support timestampNtz
assert protocol.min_writer_version == 2
Loading