Skip to content

fix: translate named parameters for PostgreSQL#749

Merged
olavloite merged 1 commit intomainfrom
pg-named-params
Feb 9, 2026
Merged

fix: translate named parameters for PostgreSQL#749
olavloite merged 1 commit intomainfrom
pg-named-params

Conversation

@olavloite
Copy link
Collaborator

Named parameters in the SQL string were not translated to PostgreSQL-style query parameters before sending these to Spanner. This made it impossible to use named query parameters with PostgreSQL-dialect databases.

This change also fixes an issue that PostgreSQL-style query parameters had to be used in order, and an issue that using a wrong value for a named parameter would not cause an error, and instead just silently send a null value for the parameter to Spanner.

Fixes #738
Fixes #601
Fixes #730

@olavloite olavloite requested a review from a team as a code owner February 6, 2026 18:00
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @olavloite, 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 significantly enhances the handling of SQL query parameters, particularly for PostgreSQL-dialect databases. It introduces the capability to correctly translate named parameters into the PostgreSQL-specific indexed format, which was previously a blocker for using named parameters with this dialect. Furthermore, it improves parameter validation by explicitly erroring on missing named parameter values instead of silently substituting nulls, and relaxes the ordering requirement for PostgreSQL-style parameters. The changes also enforce a single parameter style per statement to maintain clarity and prevent unexpected behavior.

Highlights

  • PostgreSQL Named Parameter Translation: Implemented the translation of named parameters (e.g., @param) in SQL queries to PostgreSQL-style indexed parameters (e.g., $1) when using a PostgreSQL-dialect database. This enables the use of named parameters which were previously unsupported.
  • Improved Parameter Validation: Fixed an issue where providing an incorrect named parameter value would silently result in a null value being sent to Spanner. The system now correctly returns an InvalidArgument error in such cases, preventing silent data issues.
  • Flexible PostgreSQL Parameter Ordering: Resolved a limitation where PostgreSQL-style query parameters previously had to be used in sequential order. They can now be provided in any order, improving usability.
  • Enforced Single Parameter Style: Added validation to ensure that SQL statements do not mix different parameter styles (named, positional, or indexed) within a single query. This prevents ambiguity and potential errors.
Changelog
  • conn.go
    • Updated PrepareContext to correctly receive and utilize the new namedParamsToIndexedParam map from the parser.
    • Ensured that the original SQL query string is retained for statement preparation, rather than the potentially modified string from parameter parsing, to maintain consistency.
  • driver_with_mockserver_test.go
    • Corrected test assertions and comments in TestQueryWithReusedPositionalParameter to reflect accurate parameter values.
  • parser/statement_parser.go
    • Introduced maps package for map operations.
    • Extended statementsCacheEntry to store the namedParamsToIndexedParam map for caching PostgreSQL parameter translations.
    • Added engineSupportsNamedParametersWithPrefix to abstract dialect-specific parameter prefix rules.
    • Modified ParseParameters to return the new namedParamsToIndexedParam map, specifically for PostgreSQL dialect.
    • Refactored calculateFindParamsResult to implement the translation logic for named parameters to PostgreSQL indexed parameters, manage parameter style conflicts, and update caching mechanisms.
    • Implemented strict validation to disallow mixing of named, positional, and indexed parameter styles within a single SQL statement.
  • stmt.go
    • Added fmt and strconv imports.
    • Modified prepareSpannerStmt to process the namedParamsToIndexedParam map, translating internal parameter names back to their original named form for error messages.
    • Enhanced error messages for missing parameters to display the user-friendly named parameter (e.g., @id) instead of the internal p1 representation.
  • stmt_with_mockserver_test.go
    • Added imports for database/sql and databasepb.
    • Introduced TestMissingNamedParam to specifically test the new error handling for missing named parameters.
    • Created a paramTest struct and wantErrOrExecErr helper to streamline parameter test definitions.
    • Added comprehensive test suites (TestPositionalParametersWithGoogleSQL, TestNamedParametersWithGoogleSQL, TestIndexedParametersWithPG, TestNamedParametersWithPG, TestPositionalParametersWithPG) to validate parameter parsing, translation, and error handling across different dialects and parameter styles.
Activity
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 introduces a fix for translating named parameters for PostgreSQL-dialect databases. The changes correctly handle the translation of @name style parameters to PostgreSQL's $n style, fix an issue with out-of-order parameter usage, and ensure that an error is thrown for missing named parameters instead of silently sending a null value. The implementation involves significant updates to the SQL statement parser, including a new parameter translation mechanism and more robust validation of parameter styles. The accompanying tests are comprehensive and cover a wide range of scenarios for both GoogleSQL and PostgreSQL dialects, providing strong confidence in the correctness of the solution. The changes are well-structured and I have no further suggestions for improvement.

Named parameters in the SQL string were not translated to PostgreSQL-style query
parameters before sending these to Spanner. This made it impossible to use named
query parameters with PostgreSQL-dialect databases.

This change also fixes an issue that PostgreSQL-style query parameters had to be
used in order, and an issue that using a wrong value for a named parameter would
not cause an error, and instead just silently send a null value for the parameter
to Spanner.

Fixes #738
Fixes #601
Fixes #730
@olavloite
Copy link
Collaborator Author

/gemini review

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 introduces a significant and well-implemented fix for handling named parameters in PostgreSQL-dialect databases. The changes correctly translate named parameters to PostgreSQL-style parameters, address issues with parameter ordering, and improve error handling for missing parameters. The logic is complex but appears correct, and it is backed by a comprehensive set of new tests that cover various scenarios and edge cases. The code is clean and the changes are consistent across the affected files. Overall, this is a high-quality contribution that improves the usability and correctness of the Spanner driver for PostgreSQL users.

Copy link
Contributor

@bhatt4982 bhatt4982 left a comment

Choose a reason for hiding this comment

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

LGTM...

@olavloite olavloite merged commit 32c334d into main Feb 9, 2026
43 checks passed
@olavloite olavloite deleted the pg-named-params branch February 9, 2026 13:50
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.

PostgreSQL positional argument must be used in order Support named parameters with PostgreSQL-dialect databases

2 participants