Skip to content

Ensure function aliases allow retrying queries #54824

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 1 commit into from
Mar 28, 2025

Conversation

skipkayhil
Copy link
Member

Based on #54823 to avoid merge conflicts because they touch the same test

Motivation / Background

Previously, function aliases (ex. combining a #group and #count)
would make queries non-retryable because Function#as would always wrap
a given alias in a non-retryable SqlLiteral.

Detail

This could be addressed by making Function#as behave similarly to
AliasPredication (retry everything). However, Function#as can also
just be removed so that Function uses AliasPredication#as instead.
This behaves slightly differently because AliasPredication#as will now
return the Function wrapped in an Alias (meaning the return value
must be used). Only a single place in Active Record needed to be changed
and all others already did the correct thing.

Additional information

This also resolves a long standing FIXME suggesting removing the custom
Function#as in favor of using Alias nodes. The other half of the
comment suggesting Function could inherit from Unary does not make
sense because SQL functions could potentially take multiple parameters,
so the existing superclass should not be changed.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Previously, function aliases (ex. combining a `#group` and `#count`)
would make queries non-retryable because `Function#as` would always wrap
a given alias in a non-retryable `SqlLiteral`.

This could be addressed by making `Function#as` behave similarly to
`AliasPredication` (retry everything). However, `Function#as` can also
just be removed so that `Function` uses `AliasPredication#as` instead.
This behaves slightly differently because `AliasPredication#as` will now
return the `Function` wrapped in an `Alias` (meaning the return value
must be used). Only a single place in Active Record needed to be changed
and all others already did the correct thing.

This also resolves a long standing FIXME suggesting removing the custom
`Function#as` in favor of using `Alias` nodes. The other half of the
comment suggesting `Function` could inherit from `Unary` does not make
sense because SQL functions could potentially take multiple parameters,
so the existing superclass should not be changed.
@byroot byroot force-pushed the hm-retryable-func-alias branch from cd72234 to d31ef5c Compare March 28, 2025 08:09
@byroot byroot enabled auto-merge March 28, 2025 08:10
@byroot byroot merged commit ffacf9b into rails:main Mar 28, 2025
3 checks passed
@skipkayhil skipkayhil deleted the hm-retryable-func-alias branch March 28, 2025 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants