Skip to content

Push down derived tables under route when possible#11422

Merged
systay merged 1 commit intovitessio:release-14.0from
planetscale:11379-backport
Oct 3, 2022
Merged

Push down derived tables under route when possible#11422
systay merged 1 commit intovitessio:release-14.0from
planetscale:11379-backport

Conversation

@systay
Copy link
Collaborator

@systay systay commented Oct 3, 2022

Backport of #11379

Description

Fixes bugs with the following queries:

# 1
select music.id 
from music join (
  select id,name
  from user 
  order by id limit 2
) as d on music.user_id = d.id

#2 
select d.a 
from music join (
  select id, count(*) as a 
  from user) as d on music.user_id = d.id 
group by 1

#3
select count(*) 
from (
  select user.id as oui, music.id as 
  from user 
    join music on user.id = music.user_id 
  order by user.name) as toto
}

In all these queries, having aggregation and/or LIMIT in a derived table on the RHS of a join leads to potentially bad results.

Related Issue(s)

Fixes #11093
Backport of #11379

Backport of vitessio#11379

* tests: add more plan_tests for derived tables and subqueries

Co-authored-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>

* refactor: make switch pretty

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

* feat: push derived table under the route

Signed-off-by: Harshit Gangal <harshit@planetscale.com>

* simplify logic

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

* check for valid derived tables also when doing JOIN on the vtgate

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

* comments

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

* add more tests with derived tables

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

* handle HAVING in derived tables correctly

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

* merge tests into single file

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

* rename test keypspace

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

Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Co-authored-by: Arthur Schreiber <arthurschreiber@github.com>
Co-authored-by: Harshit Gangal <harshit@planetscale.com>
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Oct 3, 2022

Review Checklist

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

General

  • Ensure that the Pull Request has a descriptive title.
  • If this is a change that users need to know about, please apply the release notes (needs details) label so that merging is blocked unless the summary release notes document is included.

If a new flag is being introduced:

  • Is it really necessary to add this flag?
  • Flag names should be clear and intuitive (as far as possible)
  • Help text should be descriptive.
  • Flag names should use dashes (-) as word separators rather than underscores (_).

If a workflow is added or modified:

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

Bug fixes

  • There should be at least one unit or end-to-end test.
  • The Pull Request description should include a link to an issue that describes the bug.

Non-trivial changes

  • There should be some code comments as to why things are implemented the way they are.

New/Existing features

  • Should be documented, either by modifying the existing documentation or creating new documentation.
  • New features should have a link to a feature request issue or an RFC that documents the use cases, corner cases and test cases.

Backward compatibility

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants