Skip to content

chore(wren-ai-service): improve text2sql#1855

Merged
cyyeh merged 1 commit intomainfrom
chore/ai-service/improve-text-to-sql
Aug 1, 2025
Merged

chore(wren-ai-service): improve text2sql#1855
cyyeh merged 1 commit intomainfrom
chore/ai-service/improve-text-to-sql

Conversation

@cyyeh
Copy link
Copy Markdown
Member

@cyyeh cyyeh commented Aug 1, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of whitespace normalization in generated text results.
  • New Features

    • Added a rule to prevent the use of the "TO_CHAR" function in generated SQL queries.
  • Refactor

    • Enhanced SQL error handling to be more strict when unsupported features are encountered.

@cyyeh cyyeh added module/ai-service ai-service related ci/ai-service ai-service related labels Aug 1, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Aug 1, 2025

Walkthrough

The changes update SQL generation and cleaning logic. In engine.py, string cleaning no longer replaces literal \n with spaces, and SQL transpilation is made stricter by raising errors on unsupported features. In sql.py, the "TO_CHAR" function is now explicitly forbidden in generated SQL queries.

Changes

Cohort / File(s) Change Summary
String Cleaning and SQL Transpilation Strictness
wren-ai-service/src/core/engine.py
Removed .replace("\\n", " ") from string cleaning; made sqlglot.transpile raise on unsupported SQL features.
SQL Generation Rules Update
wren-ai-service/src/pipelines/generation/utils/sql.py
Added a rule to forbid the "TO_CHAR" function in generated SQL queries.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Engine
    participant SQLGlot
    participant SQLRules

    User->>Engine: Submit SQL generation request
    Engine->>SQLRules: Apply SQL constraints (including "TO_CHAR" check)
    SQLRules-->>Engine: Pass/fail based on constraints
    Engine->>SQLGlot: Transpile SQL (with strict error & unsupported level)
    SQLGlot-->>Engine: Return transpiled SQL or raise error
    Engine-->>User: Return result or error
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Possibly related PRs

  • chore(wren-ai-service): minor updates #1782: Also modifies the add_quotes function in engine.py, changing how the sqlglot.transpile dialect parameter is handled. Both PRs affect the same function but adjust different parameters.

Suggested labels

module/ai-service, ci/ai-service, wren-ai-service

Suggested reviewers

  • yichieh-lu

Poem

A rabbit hopped through SQL fields,
Forbidding "TO_CHAR" in all its yields.
With stricter checks, errors raise high,
No sneaky functions slipping by!
Whitespace left just as it came—
Clean code and queries, that’s the aim!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 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 43b39f8 and b59d7b4.

📒 Files selected for processing (2)
  • wren-ai-service/src/core/engine.py (1 hunks)
  • wren-ai-service/src/pipelines/generation/utils/sql.py (1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.
📚 Learning: in the generate_sql api, allow users to specify language codes not predefined in the wrenailanguage ...
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.

Applied to files:

  • wren-ai-service/src/pipelines/generation/utils/sql.py
  • wren-ai-service/src/core/engine.py
📚 Learning: in the wren-ai-service codebase, when adding new fields like "alias" to the output of functions that...
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.

Applied to files:

  • wren-ai-service/src/core/engine.py
📚 Learning: the typo in error code enum names ("identied_as_general" and "identied_as_misleading_query" instead ...
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.

Applied to files:

  • wren-ai-service/src/core/engine.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: pytest
  • GitHub Check: pytest
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
🔇 Additional comments (3)
wren-ai-service/src/pipelines/generation/utils/sql.py (1)

253-253: LGTM! Consistent SQL restriction addition.

The addition of the TO_CHAR function restriction aligns well with existing SQL generation constraints and maintains consistency with other forbidden functions and clauses in the TEXT_TO_SQL_RULES.

wren-ai-service/src/core/engine.py (2)

55-60: Excellent enhancement to SQL validation strictness.

Adding unsupported_level=sqlglot.ErrorLevel.RAISE makes SQL transpilation more robust by catching unsupported features early, which aligns well with the stricter SQL generation rules introduced in the companion changes.


30-42: Verify the clean_generation_result function changes.

The AI summary mentions that string cleaning no longer replaces literal \n with spaces, but I don't see any changes marked in the clean_generation_result function. Please confirm if there are missing changes or if the summary is inaccurate.

Likely an incorrect or invalid review comment.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chore/ai-service/improve-text-to-sql

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 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.

@cyyeh cyyeh merged commit c3e7fd5 into main Aug 1, 2025
15 checks passed
@cyyeh cyyeh deleted the chore/ai-service/improve-text-to-sql branch August 1, 2025 01:56
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