Skip to content

Handle zero-index hits in utils #1655

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

anandgupta42
Copy link
Contributor

@anandgupta42 anandgupta42 commented May 17, 2025

Summary

  • fix get_start_and_end_position so zero index values are not treated as falsy
  • add regression test for detecting invalid tokens at the beginning of SQL

Testing

  • python -m unittest altimate_packages/tests/test_utils.py
  • npm ci (fails: Exit handler never called)

Important

Fix get_start_and_end_position to handle zero-index values and add regression test for SQL token parsing.

  • Bug Fix:
    • Fix get_start_and_end_position in utils.py to correctly handle zero-index values, ensuring they are not treated as falsy.
  • Testing:
    • Add regression test test_invalid_token_at_beginning in test_utils.py to verify handling of invalid tokens at the start of SQL strings.

This description was created by Ellipsis for 29a6455. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of start and end positions to correctly recognize zero or falsy indices as valid, preventing unintended skipping of valid results.
  • Tests

    • Added a new test to verify correct behavior when processing input strings that start with invalid tokens.

Copy link
Contributor

coderabbitai bot commented May 17, 2025

"""

Walkthrough

The function get_start_and_end_position in altimate/utils.py was updated to explicitly check for None values instead of relying on truthiness, ensuring correct handling of zero or falsy indices. Additionally, a new unit test was added to validate the function's behavior when processing input with an invalid token at the beginning.

Changes

File(s) Change Summary
altimate_packages/altimate/utils.py Updated condition in get_start_and_end_position to check for None explicitly.
altimate_packages/tests/test_utils.py Added a new unit test for get_start_and_end_position using Python’s unittest framework.

Sequence Diagram(s)

sequenceDiagram
    participant TestCase as TestGetStartEndPosition
    participant Utils as get_start_and_end_position

    TestCase->>Utils: Call get_start_and_end_position(sql_string)
    Utils-->>TestCase: Return start_pos, end_pos, count
    TestCase->>TestCase: Assert start_pos, end_pos, count
Loading

"""

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.

📜 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 29a6455 and 432515f.

📒 Files selected for processing (1)
  • altimate_packages/altimate/utils.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • altimate_packages/altimate/utils.py
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build (windows-latest)
  • GitHub Check: test (windows-latest)
  • GitHub Check: test (ubuntu-latest)
  • GitHub Check: test (macos-latest)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (javascript-typescript)
✨ 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.

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.

@anandgupta42 anandgupta42 requested a review from Copilot May 17, 2025 01:27
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where zero-based indices were treated as falsy in get_start_and_end_position, and adds a regression test to verify parsing of an invalid token at the start of an SQL string.

  • Updated the null-check in get_start_and_end_position to explicitly test for None.
  • Added test_invalid_token_at_beginning to cover zero-index occurrences.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
altimate_packages/altimate/utils.py Changed the falsy check to is not None for start/end indices to handle zero values correctly.
altimate_packages/tests/test_utils.py Added a unittest that ensures tokens at index 0 are detected as valid occurrences.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Caution

Changes requested ❌

Reviewed everything up to 29a6455 in 1 minute and 43 seconds. Click for details.
  • Reviewed 34 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. altimate_packages/altimate/utils.py:150
  • Draft comment:
    Good fix: using 'is not None' avoids misinterpreting a valid zero index. This change correctly handles the case where the start position is 0.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment is purely informative, explaining why 'is not None' is used. It doesn't provide a suggestion or ask for confirmation about the code's intention. It doesn't align with the rules for useful comments.
2. altimate_packages/altimate/utils.py:148
  • Draft comment:
    Please add a return type annotation for get_start_and_end_position to improve clarity and ease future refactoring.
  • Reason this comment was not posted:
    Comment was on unchanged code.
3. altimate_packages/altimate/utils.py:149
  • Draft comment:
    Typo: consider renaming 'num_occurences' to 'num_occurrences' for clarity and correctness.
  • Reason this comment was not posted:
    Comment was on unchanged code.

Workflow ID: wflow_tBwoB8PsWCTsT6oF

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@@ -0,0 +1,15 @@
import sys
import unittest
sys.path.insert(0, 'altimate_packages')
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider avoiding manual sys.path insertion; setting PYTHONPATH or using proper package installation can improve test reliability.

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

🧹 Nitpick comments (1)
altimate_packages/tests/test_utils.py (1)

8-9: Consider adding more test cases for completeness

While this test covers the specific edge case of an invalid token at the beginning, it would be beneficial to add additional test cases such as:

  1. An invalid token in the middle of the statement
  2. An invalid token at the end of the statement
  3. Multiple occurrences of the same invalid token
  4. Case where invalid_string is not found
def test_invalid_token_at_beginning(self):
    sql = "invalid_token SELECT * FROM table"
    start, end, count = get_start_and_end_position(sql, "invalid_token")
    self.assertEqual(start, [0, 1])
    self.assertEqual(end, [0, len("invalid_token") + 1])
    self.assertEqual(count, 1)

+ def test_invalid_token_in_middle(self):
+     sql = "SELECT * FROM invalid_token table"
+     start, end, count = get_start_and_end_position(sql, "invalid_token")
+     self.assertEqual(count, 1)
+     # Assert positions based on expected line and column
+
+ def test_invalid_token_not_found(self):
+     sql = "SELECT * FROM table"
+     start, end, count = get_start_and_end_position(sql, "invalid_token")
+     self.assertEqual(start, None)
+     self.assertEqual(end, None)
+     self.assertEqual(count, 0)
+
+ def test_multiple_occurrences(self):
+     sql = "invalid_token SELECT * FROM invalid_token"
+     start, end, count = get_start_and_end_position(sql, "invalid_token")
+     self.assertEqual(start, None)
+     self.assertEqual(end, None)
+     self.assertEqual(count, 2)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ed7345b and 29a6455.

📒 Files selected for processing (2)
  • altimate_packages/altimate/utils.py (1 hunks)
  • altimate_packages/tests/test_utils.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
altimate_packages/tests/test_utils.py (1)
altimate_packages/altimate/utils.py (1)
  • get_start_and_end_position (148-156)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build (windows-latest)
  • GitHub Check: test (windows-latest)
  • GitHub Check: test (ubuntu-latest)
  • GitHub Check: test (macos-latest)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (2)
altimate_packages/altimate/utils.py (1)

150-150: Good fix for zero-index handling!

This change fixes a critical bug by explicitly checking for None instead of relying on truthiness. In Python, zero is falsy, so the previous condition if start and end: would incorrectly skip valid indices at position 0. By changing to if start is not None and end is not None:, you ensure that zero indices are properly handled, which is essential when invalid tokens appear at the beginning of SQL statements.

altimate_packages/tests/test_utils.py (1)

1-16: Well-designed regression test for the zero-index fix

This test correctly verifies the fix by testing the specific edge case where an invalid token appears at the beginning of a SQL statement (position 0). The test ensures that the function properly identifies start and end positions even when they occur at index 0, which was previously being skipped due to the bug.

Copy link
Contributor

@mdesmet mdesmet left a comment

Choose a reason for hiding this comment

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

I don't think we want to handle 0-index hits.

start, end, count = get_start_and_end_position(sql, "invalid_token")
self.assertEqual(start, [0, 1])
self.assertEqual(end, [0, len("invalid_token") + 1])
self.assertEqual(count, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Test case, doesn't test new behavior.

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

Successfully merging this pull request may close these issues.

2 participants