Skip to content

chore(wren-ai-service): add retrieved_tables to sql correction api #1804

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 1 commit into from
Jun 30, 2025

Conversation

cyyeh
Copy link
Member

@cyyeh cyyeh commented Jun 30, 2025

Summary by CodeRabbit

  • New Features
    • Added support for including an optional list of retrieved table names when submitting SQL correction requests, allowing for more precise and efficient processing.

@cyyeh cyyeh requested a review from yichieh-lu June 30, 2025 01:34
@cyyeh cyyeh added module/ai-service ai-service related ci/ai-service ai-service related labels Jun 30, 2025
Copy link
Contributor

coderabbitai bot commented Jun 30, 2025

Walkthrough

This change introduces an optional retrieved_tables field to both the API request model and the internal correction request model for SQL correction. The service logic is updated to use this field if provided, bypassing table extraction from SQL when possible, and passing the supplied tables to schema retrieval.

Changes

File(s) Change Summary
wren-ai-service/src/web/v1/routers/sql_corrections.py Added optional retrieved_tables: Optional[List[str]] to the PostRequest Pydantic model.
wren-ai-service/src/web/v1/services/sql_corrections.py Added retrieved_tables to CorrectionRequest; updated correct method to use this field if provided.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant SqlCorrectionService
    participant Pipeline

    Client->>API: POST /sql_corrections (with optional retrieved_tables)
    API->>SqlCorrectionService: correct(request)
    alt retrieved_tables provided
        SqlCorrectionService->>SqlCorrectionService: Use request.retrieved_tables
    else not provided
        SqlCorrectionService->>Pipeline: Run "sql_tables_extraction"
        Pipeline-->>SqlCorrectionService: Extracted tables
    end
    SqlCorrectionService->>Pipeline: Run "db_schema_retrieval" with tables
    Pipeline-->>SqlCorrectionService: Schema
    SqlCorrectionService-->>API: Correction result
    API-->>Client: Response
Loading

Possibly related PRs

Suggested labels

wren-ai-service

Suggested reviewers

  • imAsterSun

Poem

In the warren where queries hop and play,
A new field joins the SQL ballet—
If you know your tables, just pass them along,
No need for extraction, it won’t take long!
With every request, the logic refines,
More carrots for code, and fewer tangled vines.
🥕

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch chore/ai-service/improve-sql-correction

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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 auto-generate unit tests to generate unit tests for 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.

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: 1

🧹 Nitpick comments (2)
wren-ai-service/src/web/v1/routers/sql_corrections.py (1)

19-32: Update documentation to include the new field.

The router documentation should be updated to reflect the new retrieved_tables field in the request body schema.

     {
       "sql": "SELECT * FROM table",        # Invalid SQL statement
       "error": "Error message"             # Error message
-      "project_id": "project-id"              # Optional project ID
+      "project_id": "project-id",             # Optional project ID
+      "retrieved_tables": ["table1", "table2"] # Optional list of table names
     }
wren-ai-service/src/web/v1/services/sql_corrections.py (1)

88-104: Consider error handling for invalid table names.

While the logic correctly bypasses extraction when tables are provided, consider adding validation to ensure the provided table names are valid and exist in the database schema. Invalid table names could cause downstream pipeline failures.

Example approach:

if not retrieved_tables:
    retrieved_tables = (
        await self._pipelines["sql_tables_extraction"].run(
            sql=sql,
        )
    )["post_process"]
else:
    # Optional: Validate table names exist in the database
    logger.info(f"Using provided tables: {retrieved_tables}")
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 876f016 and cc51192.

📒 Files selected for processing (2)
  • wren-ai-service/src/web/v1/routers/sql_corrections.py (2 hunks)
  • wren-ai-service/src/web/v1/services/sql_corrections.py (4 hunks)
🧰 Additional context used
🧠 Learnings (2)
wren-ai-service/src/web/v1/routers/sql_corrections.py (1)
Learnt from: cyyeh
PR: Canner/WrenAI#1763
File: wren-ai-service/src/pipelines/generation/semantics_description.py:31-33
Timestamp: 2025-06-20T02:37:21.292Z
Learning: In the wren-ai-service codebase, when adding new fields like "alias" to the output of functions that use Pydantic models for validation, the user prefers not to update the corresponding Pydantic model definitions to include these new fields.
wren-ai-service/src/web/v1/services/sql_corrections.py (1)
Learnt from: cyyeh
PR: Canner/WrenAI#1763
File: wren-ai-service/src/pipelines/generation/semantics_description.py:31-33
Timestamp: 2025-06-20T02:37:21.292Z
Learning: In the wren-ai-service codebase, when adding new fields like "alias" to the output of functions that use Pydantic models for validation, the user prefers not to update the corresponding Pydantic model definitions to include these new fields.
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: pytest
  • GitHub Check: pytest
  • GitHub Check: Analyze (go)
🔇 Additional comments (6)
wren-ai-service/src/web/v1/routers/sql_corrections.py (2)

3-3: LGTM: Import addition is correct.

The List import is properly added to support the new retrieved_tables field type.


70-70: LGTM: Field addition follows established patterns.

The retrieved_tables field is correctly defined as optional with appropriate typing.

wren-ai-service/src/web/v1/services/sql_corrections.py (4)

2-2: LGTM: Import addition is correct.

The List import is properly added to support the new retrieved_tables field type.


61-61: LGTM: Field addition follows established patterns.

The retrieved_tables field is correctly defined as optional with appropriate typing, consistent with the router model.


78-78: LGTM: Variable assignment is correct.

The retrieved_tables variable is properly assigned from the request.


99-99: LGTM: Pipeline parameter updated correctly.

The parameter is correctly updated to use retrieved_tables instead of the previously extracted tables variable.

@cyyeh cyyeh requested review from imAsterSun and removed request for yichieh-lu June 30, 2025 02:40
Copy link
Contributor

@imAsterSun imAsterSun left a comment

Choose a reason for hiding this comment

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

lgtm, thanks

@cyyeh cyyeh merged commit 7768d3f into main Jun 30, 2025
15 checks passed
@cyyeh cyyeh deleted the chore/ai-service/improve-sql-correction branch June 30, 2025 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci/ai-service ai-service related module/ai-service ai-service related wren-ai-service
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants