Skip to content

fix(sqlite): implement PARSE_COLNAMES column name parsing #5923

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 3 commits into from
Jul 9, 2025

Conversation

ever0de
Copy link
Contributor

@ever0de ever0de commented Jul 9, 2025

Reference

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added support for parsing and removing type annotations from column names when a specific flag is enabled, resulting in cleaner column names in query results.
  • Bug Fixes

    • Improved column description handling to correctly reflect connection-specific settings.

Copy link
Contributor

coderabbitai bot commented Jul 9, 2025

Walkthrough

The changes update how column names and descriptions are retrieved from SQLite statements. The columns_description and columns_name methods are modified to accept additional parameters, allowing access to connection-specific settings, particularly the detect_types flag. This enables conditional stripping of type annotations from column names based on the connection's configuration.

Changes

File(s) Change Summary
stdlib/src/sqlite.rs Updated columns_description and columns_name method signatures to accept connection state and flags. Logic added to strip type annotations from column names when the PARSE_COLNAMES flag is set. All relevant calls updated to pass new arguments.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Cursor
    participant Connection
    participant Statement

    User->>Cursor: Request column description
    Cursor->>Connection: Access detect_types flag
    Cursor->>Statement: columns_description(detect_types, vm)
    Statement->>Statement: columns_name(detect_types, vm)
    Statement->>Statement: Strip type annotations if PARSE_COLNAMES set
    Statement-->>Cursor: Return processed column names/descriptions
    Cursor-->>User: Return column description
Loading

Suggested reviewers

  • youknowone

Poem

In the warren of code, a clever tweak,
Now columns shed types when users seek.
With flags in tow, descriptions flow,
Cleaner names in every row!
A hop, a skip, a bug undone—
SQLite shines, the bunny won! 🐇


📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1e9c44c and b442fc9.

📒 Files selected for processing (1)
  • stdlib/src/sqlite.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • stdlib/src/sqlite.rs
⏰ 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). (11)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run tests under miri
  • GitHub Check: Check Rust code with rustfmt and clippy
✨ 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.
    • 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 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.

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.

@ever0de ever0de changed the title fix: implement PARSE_COLNAMES column name parsing in sqlite3 fix(sqlite): implement PARSE_COLNAMES column name parsing Jul 9, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @ever0de, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a fix to the sqlite3 module's column name parsing, specifically implementing the PARSE_COLNAMES behavior to align with CPython's sqlite3 module. This ensures that column names returned by queries can be automatically stripped of type information (e.g., [type]) when the PARSE_COLNAMES flag is enabled, improving compatibility and consistency.

Highlights

  • PARSE_COLNAMES Implementation: The columns_name function now correctly processes column names by stripping trailing type information (e.g., [INTEGER]) when the PARSE_COLNAMES flag is active. This involves identifying and removing the first [ character and any preceding space, mirroring CPython's behavior.
  • Contextual detect_types Passing: The columns_description function has been updated to receive the Connection object, allowing it to pass the connection's detect_types flag to columns_name. This ensures that the column name parsing logic is applied conditionally based on the connection's configuration.
  • API Adjustments: Calls to columns_description within the _sqlite module have been updated to provide the necessary connection object, reflecting the change in its function signature.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements the PARSE_COLNAMES option for the SQLite module, enabling column name parsing. The changes involve modifying the columns_name function to strip column names based on the detect_types flag and updating the columns_description function to pass the connection's detect_types value. Several suggestions were made to improve code readability and robustness.

@@ -2726,22 +2726,48 @@ mod _sqlite {
unsafe { sqlite3_column_name(self.st, pos) }
}

fn columns_name(self, vm: &VirtualMachine) -> PyResult<Vec<PyStrRef>> {
fn columns_name(self, detect_types: i32, vm: &VirtualMachine) -> PyResult<Vec<PyStrRef>> {

Choose a reason for hiding this comment

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

medium

The detect_types parameter is an integer, but it's being treated as a boolean flag using bitwise operations. It would improve readability to rename it to detect_colnames to clearly indicate its purpose.

fn columns_name(detect_colnames: i32, vm: &VirtualMachine) -> PyResult<Vec<PyStrRef>> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll leave the self requirement in columns_name for now, as it's part of the existing implementation.

Comment on lines 2741 to 2747
let end_pos = if bracket_pos > 0
&& name_str.chars().nth(bracket_pos - 1) == Some(' ')
{
bracket_pos - 1
} else {
bracket_pos
};

Choose a reason for hiding this comment

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

medium

This logic checks for a single space before the '[' character. It might be more robust to trim any whitespace before the bracket to handle cases with multiple spaces or tabs.

                            let end_pos = name_str[..bracket_pos].trim_end().len();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about this comment. It seems this isn't the case in CPython. Should we use trim_end?

if self.column_count() == 0 {
return Ok(None);
}
let columns = self
.columns_name(vm)?
.columns_name(connection.detect_types, vm)?

Choose a reason for hiding this comment

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

medium

Passing connection.detect_types directly into columns_name exposes the internal state of the Connection object. Consider creating a dedicated method on Connection to encapsulate this logic and avoid direct access to the detect_types field.

        .columns_name(connection.get_detect_types(), vm)?

Comment on lines 2737 to 2739
let processed_name = if detect_types & PARSE_COLNAMES != 0 {
// Find the position of the first '['
if let Some(bracket_pos) = name_str.find('[') {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let processed_name = if detect_types & PARSE_COLNAMES != 0 {
// Find the position of the first '['
if let Some(bracket_pos) = name_str.find('[') {
let processed_name = if detect_types & PARSE_COLNAMES != 0 &&
// Find the position of the first '['
let Some(bracket_pos) = name_str.find('[') {

The latest stable Rust allows to use let chaining

Comment on lines 2749 to 2751
} else {
name_str
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else {
name_str
}

Then this else condition can be removed

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

👍

@youknowone youknowone merged commit 3a6a766 into RustPython:main Jul 9, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants