lancedb icon indicating copy to clipboard operation
lancedb copied to clipboard

feat: implement bindings to return merge stats

Open xaptronic opened this issue 8 months ago • 4 comments

Based on this comment: https://github.com/lancedb/lancedb/issues/2228#issuecomment-2730463075 and https://github.com/lancedb/lance/pull/2357

Here is my attempt at implementing bindings for returning merge stats from a merge_insert.execute call for lancedb.

Note: I have almost no idea what I am doing in Rust but tried to follow existing code patterns and pay attention to compiler hints.

  • The change in nodejs binding appeared to be necessary to get compilation to work, presumably this could actual work properly by returning some kind of NAPI JS object of the stats data?
  • I am unsure of what to do with the remote/table.rs changes - necessarily for compilation to work; I assume this is related to LanceDB cloud, but unsure the best way to handle that at this point.

Proof of function:

import pandas as pd
import lancedb


db = lancedb.connect("/tmp/test.db")

test_data = pd.DataFrame(
    {
        "title": ["Hello", "Test Document", "Example", "Data Sample", "Last One"],
        "id": [1, 2, 3, 4, 5],
        "content": [
            "World",
            "This is a test",
            "Another example",
            "More test data",
            "Final entry",
        ],
    }
)

table = db.create_table("documents", data=test_data, exist_ok=True, mode="overwrite")

update_data = pd.DataFrame(
    {
        "title": [
            "Hello, World",
            "Test Document, it's good",
            "Example",
            "Data Sample",
            "Last One",
            "New One",
        ],
        "id": [1, 2, 3, 4, 5, 6],
        "content": [
            "World",
            "This is a test",
            "Another example",
            "More test data",
            "Final entry",
            "New content",
        ],
    }
)

stats = (
    table.merge_insert(on="id")
    .when_matched_update_all()
    .when_not_matched_insert_all()
    .execute(update_data)
)

print(stats)

returns

{'num_inserted_rows': 1, 'num_updated_rows': 5, 'num_deleted_rows': 0}

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Merge-insert operations now return detailed statistics, including counts of inserted, updated, and deleted rows.
  • Bug Fixes
    • Tests updated to validate returned merge-insert statistics for accuracy.
  • Documentation
    • Method documentation improved to reflect new return values and clarify merge operation results.
    • Added documentation for the new MergeStats interface detailing operation statistics.

xaptronic avatar Apr 30 '25 13:04 xaptronic

Walkthrough

This set of changes updates the merge-insert operation across Rust, Python, and Node.js bindings to propagate and return detailed statistics (number of inserted, updated, and deleted rows) from the underlying Rust implementation up to the Python and Node.js layers. The Rust trait and method signatures for merge-insert are modified to return a MergeStats struct with operation statistics, and this return value is passed through the FFI boundary to Python and Node.js. Corresponding Python and Node.js methods are updated to return these statistics. The Python and Node.js tests are also modified to assert on the returned statistics, ensuring correctness of the merge-insert behavior.

Changes

File(s) Change Summary
rust/lancedb/src/table.rs, rust/lancedb/src/remote/table.rs, rust/lancedb/src/table/merge.rs Updated merge_insert and execute methods to return MergeStats instead of (). Publicly re-exported MergeStats. Updated trait and impl signatures accordingly.
nodejs/src/merge.rs, nodejs/lancedb/merge.ts, nodejs/test/table.test.ts Modified NativeMergeInsertBuilder::execute to return MergeStats. Updated Node.js MergeInsertBuilder.execute to return and propagate these stats. Added assertions in tests to verify returned statistics after merge-insert operations.
python/src/table.rs Modified Table::execute_merge_insert to return a Python dictionary with merge statistics (inserted, updated, deleted) instead of an empty result.
python/python/lancedb/table.py Updated LanceTable._do_merge and AsyncTable._do_merge to return the results of the underlying merge operations, passing through the statistics to the caller.
python/python/tests/docs/test_merge_insert.py Modified tests to capture and assert the statistics returned from .execute() calls, verifying the counts of inserted, updated, and deleted rows after merge-insert operations.

Sequence Diagram(s)

sequenceDiagram
    participant PythonCaller
    participant LanceTable/AsyncTable
    participant RustPyTable
    participant RustTable
    participant MergeInsertBuilder

    PythonCaller->>LanceTable/AsyncTable: _do_merge(...)
    LanceTable/AsyncTable->>RustPyTable: execute_merge_insert(...)
    RustPyTable->>RustTable: merge_insert(...)
    RustTable->>MergeInsertBuilder: execute(...)
    MergeInsertBuilder->>RustTable: merge_insert(...)
    RustTable-->>MergeInsertBuilder: MergeStats
    MergeInsertBuilder-->>RustTable: MergeStats
    RustTable-->>RustPyTable: MergeStats
    RustPyTable-->>LanceTable/AsyncTable: {inserted, updated, deleted}
    LanceTable/AsyncTable-->>PythonCaller: {inserted, updated, deleted}
sequenceDiagram
    participant Test
    participant Table
    participant .execute()
    Test->>Table: .execute()
    Table-->>Test: stats = {inserted, updated, deleted}
    Test->>Test: assert stats['inserted'] == expected
    Test->>Test: assert stats['updated'] == expected
    Test->>Test: assert stats['deleted'] == expected

📜 Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3b9941f12d945baa0ad4dabbbcf8b29d808f69ee and d4daa458d8c8da67b8e70e4d8f7a31d3ca0eafd0.

📒 Files selected for processing (1)
  • nodejs/lancedb/index.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • nodejs/lancedb/index.ts
⏰ Context from checks skipped due to timeout of 90000ms (23)
  • GitHub Check: windows (aarch64-pc-windows-msvc)
  • GitHub Check: Windows: x86
  • GitHub Check: windows (x86_64-pc-windows-msvc)
  • GitHub Check: MSRV Check - Rust v1.78.0
  • GitHub Check: Linux: python-3.12
  • GitHub Check: macos (macos-13)
  • GitHub Check: Mac: Arm
  • GitHub Check: build-no-lock
  • GitHub Check: Type Check
  • GitHub Check: Linux (NodeJS 20)
  • GitHub Check: linux
  • GitHub Check: Linux: python-3.9
  • GitHub Check: lint
  • GitHub Check: Linux (NodeJS 18)
  • GitHub Check: Mac: x86
  • GitHub Check: ubuntu-22.04 + Java 17
  • GitHub Check: Test doc python code
  • GitHub Check: macos
  • GitHub Check: pydantic1x
  • GitHub Check: Test doc nodejs code
  • GitHub Check: ubuntu-22.04 + Java 11
  • GitHub Check: Lint
  • GitHub Check: Doctest
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

coderabbitai[bot] avatar Apr 30 '25 13:04 coderabbitai[bot]

Thanks for working on this. Since it was so simple, I went ahead any filled in the node implementation.

Thank you, glad to contribute.

xaptronic avatar Apr 30 '25 17:04 xaptronic

(gah sorry, wrong button)

xaptronic avatar Apr 30 '25 19:04 xaptronic