-
Notifications
You must be signed in to change notification settings - Fork 647
feat(python): expose DatasetDeltaBuilder and relevant apis #5091
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc55498
feat(python): expose DatasetDeltaBuilder and relevant apis
yanghua f51ffb0
fix code style issue
yanghua a2a5355
fix code style issue
yanghua 006f04d
fix test issue
yanghua 11a24bd
add test case for diff update
yanghua 7447ab0
make it more functional style
jackye1995 ff2cc75
refactor: remove unnecessary parameter check logic
yanghua d292d66
fix test issue
yanghua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
|
||
| import pyarrow as pa | ||
| import pytest | ||
| from lance import write_dataset | ||
|
|
||
|
|
||
| def test_delta_get_inserted_rows(): | ||
| # Create initial dataset (version 1) | ||
| table1 = pa.table( | ||
| { | ||
| "id": pa.array([1, 2, 3], type=pa.int32()), | ||
| "val": pa.array(["a", "b", "c"], type=pa.string()), | ||
| } | ||
| ) | ||
| ds = write_dataset(table1, "memory://delta_api_test", enable_stable_row_ids=True) | ||
|
|
||
| # Append more rows to create version 2 | ||
| table2 = pa.table( | ||
| { | ||
| "id": pa.array([4, 5], type=pa.int32()), | ||
| "val": pa.array(["d", "e"], type=pa.string()), | ||
| } | ||
| ) | ||
| ds.insert(table2) | ||
|
|
||
| # Build delta compared to v1 and fetch inserted rows | ||
| delta = ds.delta(compared_against=1) | ||
| print(delta.list_transactions()) | ||
| reader = delta.get_inserted_rows() | ||
|
|
||
| # Sum rows from all batches | ||
| total_rows = 0 | ||
| for batch in reader: | ||
| total_rows += batch.num_rows | ||
|
|
||
| assert total_rows == 2 | ||
|
|
||
|
|
||
| def test_delta_get_updated_rows(): | ||
| # Create initial dataset (version 1) | ||
| table1 = pa.table( | ||
| { | ||
| "id": pa.array([1, 2, 3], type=pa.int32()), | ||
| "val": pa.array(["a", "b", "c"], type=pa.string()), | ||
| } | ||
| ) | ||
| ds = write_dataset( | ||
| table1, "memory://delta_api_test_update", enable_stable_row_ids=True | ||
| ) | ||
|
|
||
| # Update an existing row to create version 2 | ||
| update_stats = ds.update({"val": "'b_updated'"}, where="id = 2") | ||
| assert update_stats["num_rows_updated"] == 1 | ||
|
|
||
| # Build delta compared to v1 and fetch updated rows | ||
| delta = ds.delta(compared_against=1) | ||
|
|
||
| # Ensure the transaction is an Update (not an Append/Delete) | ||
| txs = delta.list_transactions() | ||
| assert len(txs) == 1 | ||
| assert type(txs[0].operation).__name__ == "Update" | ||
|
|
||
| reader = delta.get_updated_rows() | ||
|
|
||
| # Collect updated rows and validate contents | ||
| total_rows = 0 | ||
| for batch in reader: | ||
| total_rows += batch.num_rows | ||
|
|
||
| assert total_rows == 1 | ||
|
|
||
| # Ensure no inserted rows are present in this diff | ||
| inserted_reader = delta.get_inserted_rows() | ||
| total_inserted = 0 | ||
| for batch in inserted_reader: | ||
| total_inserted += batch.num_rows | ||
| assert total_inserted == 0 | ||
|
|
||
|
|
||
| def test_delta_with_explicit_version_range(): | ||
| # Create initial dataset (version 1) | ||
| table1 = pa.table( | ||
| { | ||
| "id": pa.array([1, 2, 3], type=pa.int32()), | ||
| "val": pa.array(["a", "b", "c"], type=pa.string()), | ||
| } | ||
| ) | ||
| ds = write_dataset( | ||
| table1, "memory://delta_version_range_test", enable_stable_row_ids=True | ||
| ) | ||
|
|
||
| # Append more rows to create version 2 | ||
| table2 = pa.table( | ||
| { | ||
| "id": pa.array([4, 5], type=pa.int32()), | ||
| "val": pa.array(["d", "e"], type=pa.string()), | ||
| } | ||
| ) | ||
| ds.insert(table2) | ||
|
|
||
| # Use explicit version range instead of compared_against | ||
| delta = ds.delta(begin_version=1, end_version=2) | ||
| reader = delta.get_inserted_rows() | ||
|
|
||
| total_rows = 0 | ||
| for batch in reader: | ||
| total_rows += batch.num_rows | ||
|
|
||
| assert total_rows == 2 | ||
|
|
||
|
|
||
| def test_delta_validation_errors(): | ||
| table = pa.table({"id": pa.array([1, 2, 3], type=pa.int32())}) | ||
| ds = write_dataset(table, "memory://delta_validation_test") | ||
|
|
||
| # Error: no parameters specified | ||
| with pytest.raises(ValueError, match="Must specify either"): | ||
| ds.delta() | ||
|
|
||
| # Error: only begin_version specified | ||
| with pytest.raises( | ||
| ValueError, | ||
| match="Invalid user input: Must specify both with_begin_version " | ||
| "and with_end_version", | ||
| ): | ||
| ds.delta(begin_version=1) | ||
|
|
||
| # Error: only end_version specified | ||
| with pytest.raises( | ||
| ValueError, | ||
| match="Invalid user input: Must specify both with_begin_version " | ||
| "and with_end_version", | ||
| ): | ||
| ds.delta(end_version=2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree
DatasetDeltaBuildercould be_DatasetDeltaBuilder. Can we reuse the arg check in thebuilder.build()function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? It should already use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh I see what you mean, you mean we don't need to duplicate the checks, agree