Skip to content

fix: fix unclosed literal error for consecutive backslashes#4387

Merged
rahul2393 merged 1 commit intogoogleapis:mainfrom
rayudu3745:main
Mar 17, 2026
Merged

fix: fix unclosed literal error for consecutive backslashes#4387
rahul2393 merged 1 commit intogoogleapis:mainfrom
rayudu3745:main

Conversation

@rayudu3745
Copy link
Contributor

Issue: SpannerStatementParser
failed to correctly parse SQL string literals containing consecutive backslashes (e.g., SELECT '\'). When two backslashes were placed together, the parser would incorrectly assume the character immediately following them (like a closing quote) was being escaped. This caused the parser to skip the closing quote and eventually throw an INVALID_ARGUMENT: SQL statement contains an unclosed literal exception on valid SQL.

Fix: Updated SpannerStatementParser.java
to toggle the boolean escape state (lastCharWasEscapeChar = !lastCharWasEscapeChar;) when encountering a backslash. This ensures consecutive backslashes correctly cancel each other out in pairs, perfectly preserving the string literal boundaries.

@rayudu3745 rayudu3745 requested review from a team as code owners March 17, 2026 11:51
@product-auto-label product-auto-label bot added size: s Pull request size is small. api: spanner Issues related to the googleapis/java-spanner API. labels Mar 17, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 resolves a critical parsing bug in the SpannerStatementParser that caused valid SQL statements containing consecutive backslashes within string literals to fail with an "unclosed literal" exception. The fix involves a precise adjustment to the escape character handling logic, ensuring that the parser correctly interprets backslash sequences and maintains accurate string literal boundaries. This enhancement improves the robustness of the SQL parser and prevents erroneous parsing failures.

Highlights

  • Fix for SQL string literal parsing: The SpannerStatementParser was incorrectly handling consecutive backslashes in SQL string literals, leading to INVALID_ARGUMENT: SQL statement contains an unclosed literal errors for valid SQL like SELECT '\\'. This issue has been resolved.
  • Updated escape character logic: The logic for tracking escape characters (lastCharWasEscapeChar) in the parser was modified to toggle its state (!lastCharWasEscapeChar) when a backslash is encountered. This ensures that pairs of backslashes correctly cancel each other out, preserving string literal boundaries.
  • Enhanced test coverage: New unit tests were added to SpannerStatementParserTest to specifically validate the correct parsing of SQL statements containing single and double escaped backslashes, and to confirm that expected SpannerExceptions are thrown for truly unclosed literals.
Changelog
  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerStatementParser.java
    • Updated the logic for handling backslash characters within string literals to correctly manage the escape state.
  • google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/SpannerStatementParserTest.java
    • Added new imports for ErrorCode.INVALID_ARGUMENT, SpannerException, and fail assertion.
    • Introduced a new test method testRemoveCommentsAndTrim to verify the parser's behavior with various valid and invalid SQL statements containing backslashes.
Activity
  • No specific activity has been recorded for this pull request yet.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

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
Contributor

@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 correctly fixes a parsing issue with consecutive backslashes in SQL string literals by toggling the escape state. The changes in SpannerStatementParser.java are logical and the new tests in SpannerStatementParserTest.java effectively validate the fix for removeCommentsAndTrim. I've added a couple of suggestions for further improvement.

@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Mar 17, 2026
@rahul2393 rahul2393 merged commit f4884a8 into googleapis:main Mar 17, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/java-spanner API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants