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

Make sure derived table column names are handled correctly #15588

Merged
merged 26 commits into from Apr 29, 2024

Conversation

systay
Copy link
Collaborator

@systay systay commented Mar 28, 2024

Description

For some situations, during planning we create derived tables. When this happens, we sometimes get the column names wrong, which leads to queries that are not correct.

Example query, with bad plan:

select id, id from user 
union 
select id, bar from user_extra 
order by 1

bad plan produced:

{
  "QueryType": "SELECT",
  "Original": "select id, id from user union select id, bar from user_extra order by 1",
  "Instructions": {
    "OperatorType": "Sort",
    "Variant": "Memory",
    "OrderBy": "(2|3) ASC",
    "ResultColumns": 2,
    "Inputs": [
      {
        "OperatorType": "Distinct",
        "Collations": [
          "(0:4)",
          "(1:4)",
          "(2:3)",
          "3"
        ],
        "Inputs": [
          {
            "OperatorType": "Route",
            "Variant": "Scatter",
            "Keyspace": {
              "Name": "user",
              "Sharded": true
            },
            "FieldQuery": "select dt.id, dt.id, id, weight_string(id), weight_string(dt.id) from (select id, id from `user` where 1 != 1 union select id, bar from user_extra where 1 != 1) as dt where 1 != 1",
            "Query": "select dt.id, dt.id, id, weight_string(id), weight_string(dt.id) from (select id, id from `user` union select id, bar from user_extra) as dt",
            "Table": "`user`, user_extra"
          }
        ]
      }
    ]
  },
  "TablesUsed": [
    "user.user",
    "user.user_extra"
  ]
}

As you can see from the plan, one of the queries being sent down to mysql looks like:

select dt.id, dt.id, id, weight_string(id), weight_string(dt.id)
from (
	select id, id from `user` 
	union 
	select id, bar from user_extra) as dt

This query would fail in MySQL with an ambiguous column error for dt.id.

Since this change is quite involved, and these queries have never worked well, I suggest we don't backport this change set.

Related Issue(s)

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 Mar 28, 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 Mar 28, 2024
@github-actions github-actions bot added this to the v20.0.0 milestone Mar 28, 2024
@systay systay 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 Mar 28, 2024
@GrahamCampbell GrahamCampbell mentioned this pull request Mar 28, 2024
21 tasks
@GrahamCampbell
Copy link

Is this aiming for the 19.0.2 release?

@systay systay force-pushed the derived-table-column-names branch from 071dc8e to 9720b76 Compare April 19, 2024 08:50
Copy link

codecov bot commented Apr 19, 2024

Codecov Report

Attention: Patch coverage is 70.58824% with 70 lines in your changes are missing coverage. Please review.

Project coverage is 68.41%. Comparing base (ca2659d) to head (340956c).

Files Patch % Lines
go/vt/vtgate/planbuilder/operators/aggregator.go 25.00% 30 Missing ⚠️
go/vt/vtgate/planbuilder/operators/projection.go 63.15% 7 Missing ⚠️
go/vt/vtgate/planbuilder/operators/route.go 89.36% 5 Missing ⚠️
...vtgate/planbuilder/operators/projection_pushing.go 88.00% 3 Missing ⚠️
go/vt/vtgate/planbuilder/operators/apply_join.go 94.73% 2 Missing ⚠️
go/vt/vtgate/planbuilder/operators/comments.go 0.00% 2 Missing ⚠️
go/vt/vtgate/planbuilder/operators/distinct.go 50.00% 2 Missing ⚠️
go/vt/vtgate/planbuilder/operators/filter.go 0.00% 2 Missing ⚠️
go/vt/vtgate/planbuilder/operators/hash_join.go 92.00% 2 Missing ⚠️
go/vt/vtgate/planbuilder/operators/horizon.go 0.00% 2 Missing ⚠️
... and 7 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15588      +/-   ##
==========================================
- Coverage   68.43%   68.41%   -0.03%     
==========================================
  Files        1558     1558              
  Lines      196186   196361     +175     
==========================================
+ Hits       134269   134331      +62     
- Misses      61917    62030     +113     

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

@systay systay mentioned this pull request Apr 19, 2024
5 tasks
@systay systay force-pushed the derived-table-column-names branch 4 times, most recently from a580ad4 to 983dabd Compare April 22, 2024 13:50
@systay systay marked this pull request as ready for review April 22, 2024 14:21
@systay systay marked this pull request as draft April 22, 2024 15:17
@systay systay force-pushed the derived-table-column-names branch 3 times, most recently from a9ae7e2 to b358743 Compare April 25, 2024 12:21
…ved table across a join

Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
@systay systay force-pushed the derived-table-column-names branch from 40c60de to a55df3f Compare April 26, 2024 13:08
Copy link
Member

@GuptaManan100 GuptaManan100 left a comment

Choose a reason for hiding this comment

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

Rest looks good to me!

go/vt/vtgate/planbuilder/operators/aggregator.go Outdated Show resolved Hide resolved
Comment on lines 721 to 723
// create new table ID
tableID := semantics.SingleTableSet(len(ctx.SemTable.Tables))
ctx.SemTable.Tables = append(ctx.SemTable.Tables, nil)
Copy link
Member

Choose a reason for hiding this comment

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

nit: more comments can be added here as adding nil to semtable tables is not clear. I understand the intention here but comments will help.

Copy link
Member

Choose a reason for hiding this comment

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

Ideally we can add the DerivedTable here which is created below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the Derived table that we create here is of a very different type than what we store in the semantic state, and the info that we normally store there does not really make sense to store here. we just need an ID

Copy link
Member

@harshit-gangal harshit-gangal left a comment

Choose a reason for hiding this comment

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

Looks good overall. some nits

Signed-off-by: Andres Taylor <andres@planetscale.com>
…n-names

Signed-off-by: Andres Taylor <andres@planetscale.com>
@systay systay merged commit 11c8d3e into vitessio:main Apr 29, 2024
104 checks passed
@systay systay deleted the derived-table-column-names branch April 29, 2024 13:10
@deepthi
Copy link
Member

deepthi commented Apr 29, 2024

@systay is there a user-reported issue for this fix that is still open and should be closed?

@systay
Copy link
Collaborator Author

systay commented Apr 30, 2024

@systay is there a user-reported issue for this fix that is still open and should be closed?

no, this was found during our own testing

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.

None yet

5 participants