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

Column alias expanding on ORDER BY #15302

Merged
merged 12 commits into from
Feb 22, 2024
Merged

Conversation

systay
Copy link
Collaborator

@systay systay commented Feb 20, 2024

Description

In a recent PR, I did some work on how to rewrite column aliases used in ORDER BY, GROUP BY and HAVING.

I did not get that right, and introduced issues for a set of queries. This PR tries to correctly rewrite more column using the same rules that MySQL has.

Rewrite rules

Terminology first: an expression in the SELECT clause is a SELECT-expression. These can have aliased.
An ORDER BY expression is an expression listed in the ORDER BY clause, without the ASC/DESC modifier.

Rewrite rule:
An unqualified column name in the ORDER BY that matches an alias among the SELECT expressions will be replaced with the SELECT expression if:

  • ORDER BY is on SELECT and not UNION
  • It's the full ORDER BY expression, OR no table has a matching column name, OR it's used inside an aggregation function.

Related Issue(s)

Original PR #14935

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Copy link
Contributor

vitess-bot bot commented Feb 20, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Feb 20, 2024
@github-actions github-actions bot added this to the v20.0.0 milestone Feb 20, 2024
@systay systay force-pushed the orderby-resolution branch from b2a3c84 to 335b75a Compare February 20, 2024 15:26
@systay systay force-pushed the orderby-resolution branch from 0d74fe4 to cb1684d Compare February 21, 2024 08:49
@systay systay added Type: Bug Component: Query Serving and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Feb 21, 2024
Copy link

codecov bot commented Feb 21, 2024

Codecov Report

Attention: 19 lines in your changes are missing coverage. Please review.

Comparison is base (27be916) 67.53% compared to head (8175a33) 67.64%.
Report is 6 commits behind head on main.

Files Patch % Lines
go/vt/vtgate/semantics/early_rewriter.go 92.16% 13 Missing ⚠️
go/vt/vtgate/semantics/errors.go 33.33% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15302      +/-   ##
==========================================
+ Coverage   67.53%   67.64%   +0.10%     
==========================================
  Files        1561     1561              
  Lines      193387   193535     +148     
==========================================
+ Hits       130607   130913     +306     
+ Misses      62780    62622     -158     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@systay systay marked this pull request as ready for review February 21, 2024 10:11
@systay systay requested a review from deepthi as a code owner February 21, 2024 10:11
@dbussink dbussink added the Backport to: release-19.0 Needs to be back ported to release-19.0 label Feb 21, 2024
@systay systay marked this pull request as draft February 21, 2024 14:17
@systay systay changed the title Column alias expanding (take two) Column alias expanding on ORDER BY Feb 21, 2024
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
@systay systay marked this pull request as ready for review February 21, 2024 16:26

func TestOrderByComplex(t *testing.T) {
// tests written to try to trick the ORDER BY engine and planner
utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we change this once back ported? Since I think we need to back port this to v19 too since that has the previous PR and otherwise we have a significant regression?

@GuptaManan100 GuptaManan100 marked this pull request as draft February 22, 2024 07:00
@GuptaManan100 GuptaManan100 marked this pull request as ready for review February 22, 2024 07:02
@systay systay merged commit c536bb7 into vitessio:main Feb 22, 2024
102 checks passed
@systay systay deleted the orderby-resolution branch February 22, 2024 07:56
systay added a commit that referenced this pull request Feb 22, 2024
Co-authored-by: Harshit Gangal <[email protected]>
Co-authored-by: Manan Gupta <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
systay added a commit to planetscale/vitess that referenced this pull request Feb 22, 2024
dbussink pushed a commit that referenced this pull request Feb 22, 2024
Signed-off-by: Andres Taylor <[email protected]>
Co-authored-by: Andrés Taylor <[email protected]>
Co-authored-by: Harshit Gangal <[email protected]>
Co-authored-by: Manan Gupta <[email protected]>
dbussink pushed a commit that referenced this pull request Feb 22, 2024
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.

4 participants