Skip to content
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

fix: support repeated named parameters #237

Conversation

ayufan
Copy link

@ayufan ayufan commented May 20, 2024

This fixes got %v argument values, but found %v parameters in the sql string error when using repeated named arguments.

The "workaround" is to specify as many parameters as observed. The last one will win due to prepareSpannerStmt using map keyed by name. The fix will make the Bad examples to return an error, while Good will start to work.

Example:

// Bad: @name will be "value2"
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  sql.Named("name", "value"),
  sql.Named("name", "value2"),
)

// Bad: @name will be "value2"
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  "value",
  "value2",
)

// Good: works, ideal usage
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  sql.Named("name", "value"),
)

// Good: not ideal, inferred positional arguments
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  "value",
)

Disclaimer

GitLab is working on a new project Topology Service that plans to use Cloud Spanner. This has been an identified bug and part of effort to upstream fixes. Proof of concept is not public, below are public materials available:

@ayufan ayufan requested a review from a team as a code owner May 20, 2024 15:02
Copy link

google-cla bot commented May 20, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@ayufan ayufan force-pushed the support-many-positional-arguments branch 6 times, most recently from 68a9f06 to 3feca7c Compare May 20, 2024 20:06
This fixes `got %v argument values, but found %v parameters in the sql string`
error when using repeated named arguments.

The "workaround" is to specify as many parameters
as observed. The last one will win due to
`prepareSpannerStmt` using map keyed by name.

Due to this change the `Bad` examples will
start to return an error.

Example:

```golang
// Bad: @name will be "value2"
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  sql.Named("name", "value"),
  sql.Named("name", "value2"),
)

// Bad: @name will be "value2"
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  "value",
  "value2",
)

// Good: works, ideal usage
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  sql.Named("name", "value"),
)

// Good: not ideal, inferred positional arguments
s.db.QueryRow(
  "SELECT * FROM users WHERE (first_name=@name OR last_name=@name)",
  "value",
)
```

Signed-off-by: Kamil Trzciński <[email protected]>
@ayufan ayufan force-pushed the support-many-positional-arguments branch from 3feca7c to 90cff3d Compare May 20, 2024 20:08
@olavloite
Copy link
Collaborator

@ayufan Thanks for the contribution. While I agree that the proposed behavior would probably have been the best from the start, changing it in the way that this pull request does is a breaking change for anyone who currently uses any of the workarounds that are marked as 'bad' in the description, as it goes from working (albeit not in a great way), into returning an error.

Let me see if there is a combination that retains the current behavior, but also fixes it for the more logical use case (especially the one that uses named parameters).

olavloite added a commit that referenced this pull request May 29, 2024
Named parameters that occurred multiple times in the SQL string
had to be added to the list of arguments multiple times, even if
these were given as named arguments. This fix allows queries that
use named parameters to accept only one occurence of the named
parameter.
This also clarifies the use of named parameters and positional
parameters, and that mixing named parameters with positional
arguments is not a good idea.

Based on the issue reported in #237
@olavloite
Copy link
Collaborator

@ayufan We've created an alternative solution in #240 that retains the current behavior, but also adds support for re-using named parameters. It also updates the documentation to clarify what the recommended usage is of named vs positional parameters.

We are therefore going to close this pull request, but I would still like to thank you for pointing out this issue in the driver, and for the proposed fix.

@olavloite olavloite closed this May 30, 2024
olavloite added a commit that referenced this pull request May 30, 2024
Named parameters that occurred multiple times in the SQL string
had to be added to the list of arguments multiple times, even if
these were given as named arguments. This fix allows queries that
use named parameters to accept only one occurence of the named
parameter.
This also clarifies the use of named parameters and positional
parameters, and that mixing named parameters with positional
arguments is not a good idea.

Based on the issue reported in #237
@ayufan ayufan deleted the support-many-positional-arguments branch May 30, 2024 08:27
@ayufan
Copy link
Author

ayufan commented May 30, 2024

Thank you @olavloite for swift fix. I'm happy with a "balanced" solution for this.

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