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

Update indexes when renaming/deleting fields #2

Closed
wants to merge 2 commits into from

Conversation

korbit-ai[bot]
Copy link

@korbit-ai korbit-ai bot commented Aug 21, 2024

When renaming and deleting fields, any indexes that touch the affected fields should also be updated, just like how dataset.app_config is automatically synchronized.

Note: unfortunately MongoDB does not support renaming indexes; they must be dropped and recreated

Summary by CodeRabbit

  • New Features

    • Enhanced management of dataset schemas with new methods for renaming and deleting indexes, ensuring accurate index updates.
  • Bug Fixes

    • Addressed potential inconsistencies between schema changes and index management.
  • Tests

    • Added new tests to validate the functionality of renaming and deleting indexes within a dataset.

Description by Korbit AI

What change is being made?

Update the code to handle index renaming and deletion when fields are renamed or deleted, and add corresponding unit tests.

Why are these changes being made?

This change addresses the issue where indexes were not being updated when fields were renamed or deleted, as reported in issue #3185. The new approach ensures that indexes are correctly managed, maintaining database integrity and improving the robustness of field operations.

Copy link
Author

korbit-ai bot commented Aug 21, 2024

Clone of the PR voxel51/fiftyone#4698

Copy link
Author

korbit-ai bot commented Aug 21, 2024

My review is in progress 📖 - I will have feedback for you in a few minutes!

Copy link
Contributor

coderabbitai bot commented Aug 21, 2024

Walkthrough

The recent modifications enhance the management of schema paths and indexes in the dataset, specifically through the _rename_fields, _clone_fields, and _delete_fields methods. New functionalities for index renaming and deletion have been introduced, ensuring better integrity during field operations. Additionally, test coverage has been expanded to validate these changes effectively.

Changes

Files Change Summary
fiftyone/core/odm/mixins.py Improved handling of schema paths in _rename_fields, _clone_fields, and _delete_fields; added new methods for index management.
tests/unittests/dataset_tests.py Introduced test_rename_delete_indexes to validate index renaming and deletion after field modifications.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Dataset
    participant IndexManager

    User->>Dataset: Modify fields
    Dataset->>IndexManager: Update schema paths
    IndexManager->>Dataset: Reload fields
    IndexManager->>IndexManager: Determine index updates
    IndexManager->>Dataset: Renaming/deleting indexes
    User->>Dataset: Verify changes
Loading

🐇 In the meadow, changes bloom,
Fields and indexes find more room.
Renamed with care, deleted with grace,
Our data dance in a happy place!
🥕 Hopping forth with joy anew,
Celebrating all that we can do! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1824ddb and f2160d5.

Files selected for processing (2)
  • fiftyone/core/odm/mixins.py (10 hunks)
  • tests/unittests/dataset_tests.py (1 hunks)
Additional context used
Ruff
fiftyone/core/odm/mixins.py

1140-1140: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

Additional comments not posted (9)
fiftyone/core/odm/mixins.py (8)

483-487: Field reloading after schema path processing is correctly placed.

The reloading of fields after processing schema paths ensures that the schema is up-to-date before any further operations.


569-573: Field reloading after schema path processing is correctly placed.

The reloading of fields after processing schema paths ensures that the schema is up-to-date before any further operations.


691-692: Field deletion is correctly handled.

The deletion of fields is correctly handled, ensuring that schema paths are processed before any further operations.


1129-1135: Index renaming logic is correctly implemented.

The logic for renaming indexes by dropping and recreating them is correctly implemented, considering MongoDB's limitations.


1137-1142: Index deletion logic is correctly implemented.

The logic for deleting indexes is correctly implemented, ensuring that outdated indexes are removed.

Tools
Ruff

1140-1140: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


1737-1785: Index update logic is correctly implemented.

The logic for determining necessary index updates based on field path changes is correctly implemented, handling both renaming and deletion scenarios.


707-709: Ensure index deletion is correctly handled.

The addition of index deletion after field deletion is a good change. Ensure that all indexes are correctly removed from the database.

Run the following script to verify that index deletion is correctly handled:

Verification successful

Index deletion is correctly handled and tested.

The _delete_indexes method is implemented to drop indexes from the dataset, and there is a corresponding test in tests/unittests/dataset_tests.py to verify this functionality.

  • Method implementation: fiftyone/core/odm/mixins.py
  • Test function: tests/unittests/dataset_tests.py
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that index deletion is correctly handled.

# Test: Search for the `_delete_indexes` method usage. Expect: Correct handling of index deletion.
rg --type python -A 5 $'_delete_indexes'

Length of output: 70


Script:

#!/bin/bash
# Description: Verify that index deletion is correctly handled.

# Test: Search for the `_delete_indexes` method usage. Expect: Correct handling of index deletion.
rg --type py -A 5 $'_delete_indexes'

Length of output: 1160


501-503: Ensure index renaming is correctly handled.

The addition of index renaming after schema path processing is a good change. Ensure that all indexes are correctly updated in the database.

Run the following script to verify that index renaming is correctly handled:

Verification successful

Index renaming is correctly handled.

The _rename_indexes method in fiftyone/core/odm/mixins.py correctly updates indexes by dropping old ones and creating new ones based on the provided paths. This ensures that all indexes are updated in the database as intended.

  • Location: fiftyone/core/odm/mixins.py
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that index renaming is correctly handled.

# Test: Search for the `_rename_indexes` method usage. Expect: Correct handling of index renaming.
rg --type python -A 5 $'_rename_indexes'

Length of output: 70


Script:

#!/bin/bash
# Description: Verify that index renaming is correctly handled.

# Test: Search for the `_rename_indexes` method usage. Expect: Correct handling of index renaming.
rg --type py -A 5 $'_rename_indexes'

Length of output: 853

tests/unittests/dataset_tests.py (1)

2217-2275: LGTM! The test logic is comprehensive.

The test method thoroughly covers the scenarios of renaming and deleting indexes, ensuring the integrity of index management in the dataset.

def _delete_indexes(cls, paths):
updates = _get_index_updates(cls._dataset, paths)

for name in updates.keys():
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize dictionary key check.

Use key in dict instead of key in dict.keys() for better performance.

Use this diff to optimize the code:

- for name in updates.keys():
+ for name in updates:
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for name in updates.keys():
for name in updates:
Tools
Ruff

1140-1140: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

Comment on lines +2217 to +2275
def test_rename_delete_indexes(self):
sample = fo.Sample(
filepath="video.mp4",
field=1,
predictions=fo.Detections(detections=[fo.Detection(field=1)]),
)
sample.frames[1] = fo.Frame(
field=1,
predictions=fo.Detections(detections=[fo.Detection(field=1)]),
)

dataset = fo.Dataset()
dataset.add_sample(sample, dynamic=True)

dataset.create_index("field")
dataset.create_index([("id", 1), ("field", -1)])
dataset.create_index("predictions.detections.field")

dataset.create_index("frames.field")
dataset.create_index([("frames.id", 1), ("frames.field", -1)])
dataset.create_index("frames.predictions.detections.field")

index_info = dataset.get_index_information()

self.assertIn("field", index_info)
self.assertIn("_id_1_field_-1", index_info)
self.assertIn("predictions.detections.field", index_info)

self.assertIn("frames.field", index_info)
self.assertIn("frames._id_1_field_-1", index_info)
self.assertIn("frames.predictions.detections.field", index_info)

dataset.rename_sample_fields({"field": "f", "predictions": "p"})
dataset.rename_frame_fields({"field": "f", "predictions": "p"})

index_info = dataset.get_index_information()

self.assertIn("f", index_info)
self.assertIn("_id_1_f_-1", index_info)
self.assertIn("p.detections.field", index_info)

self.assertIn("frames.f", index_info)
self.assertIn("frames._id_1_f_-1", index_info)
self.assertIn("frames.p.detections.field", index_info)

dataset.delete_sample_fields(["f", "p"])
dataset.delete_frame_fields(["f", "p"])

indexes = dataset.list_indexes()

self.assertSetEqual(
set(indexes),
{
"id",
"filepath",
"frames.id",
"frames._sample_id_1_frame_number_1",
},
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding assertions to verify the absence of old indexes.

The test method effectively verifies the presence of new indexes after renaming and deletion. However, it would be beneficial to add assertions to ensure that old indexes are no longer present after these operations.

You can add assertions like the following to verify the absence of old indexes:

self.assertNotIn("field", index_info)
self.assertNotIn("predictions.detections.field", index_info)
self.assertNotIn("frames.field", index_info)
self.assertNotIn("frames.predictions.detections.field", index_info)

Copy link
Author

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

I have reviewed your code and did not find any issues!


Please note that I can make mistakes, and you should still encourage your team to review your code as well.

@korbit-ai korbit-ai bot deleted the branch cloned_develop_1824d August 21, 2024 19:57
@korbit-ai korbit-ai bot closed this Aug 21, 2024
@furwellness
Copy link
Owner

/review

Copy link

PR Reviewer Guide 🔍

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Key issues to review

Code Duplication
The code for handling schema paths and reloading fields is duplicated in the _rename_fields and _clone_fields methods. Consider refactoring this common logic into a separate method to improve maintainability.

Performance Concern
The _get_index_updates method iterates over all indexes for each path. For datasets with many indexes and fields, this could become a performance bottleneck. Consider optimizing this logic if it becomes a problem in practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants