-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Push down derived tables under route when possible #11379
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5055595
tests: add more plan_tests for derived tables and subqueries
systay 96c2f0c
refactor: make switch pretty
systay 26e548f
feat: push derived table under the route
harshit-gangal 1e09fa5
simplify logic
systay 7295868
check for valid derived tables also when doing JOIN on the vtgate
systay 06827e2
comments
systay 55dc31a
add more tests with derived tables
systay f8ea318
handle HAVING in derived tables correctly
systay 144db70
merge tests into single file
systay 9d8b02c
rename test keypspace
systay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| Copyright 2022 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package misc | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "vitess.io/vitess/go/test/endtoend/cluster" | ||
| "vitess.io/vitess/go/test/endtoend/utils" | ||
| ) | ||
|
|
||
| func start(t *testing.T) (utils.MySQLCompare, func()) { | ||
| mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams) | ||
| require.NoError(t, err) | ||
|
|
||
| deleteAll := func() { | ||
| tables := []string{"music"} | ||
| for _, table := range tables { | ||
| _, _ = mcmp.ExecAndIgnore("delete from " + table) | ||
| } | ||
| } | ||
|
|
||
| deleteAll() | ||
|
|
||
| return mcmp, func() { | ||
| deleteAll() | ||
| mcmp.Close() | ||
| cluster.PanicHandler(t) | ||
| } | ||
| } | ||
|
|
||
| func TestDerivedTableWithOrderByLimit(t *testing.T) { | ||
| mcmp, closer := start(t) | ||
| defer closer() | ||
|
|
||
| mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)") | ||
| mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')") | ||
|
|
||
| mcmp.Exec("select /*vt+ PLANNER=Gen4 */ music.id from music join (select id,name from user order by id limit 2) as d on music.user_id = d.id") | ||
| } | ||
|
|
||
| func TestDerivedAggregationOnRHS(t *testing.T) { | ||
| mcmp, closer := start(t) | ||
| defer closer() | ||
|
|
||
| mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)") | ||
| mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')") | ||
|
|
||
| mcmp.Exec("set sql_mode = ''") | ||
| mcmp.Exec("select /*vt+ PLANNER=Gen4 */ d.a from music join (select id, count(*) as a from user) as d on music.user_id = d.id group by 1") | ||
| } | ||
|
|
||
| func TestDerivedRemoveInnerOrderBy(t *testing.T) { | ||
| mcmp, closer := start(t) | ||
| defer closer() | ||
|
|
||
| mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)") | ||
| mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')") | ||
|
|
||
| mcmp.Exec("select /*vt+ PLANNER=Gen4 */ count(*) from (select user.id as oui, music.id as non from user join music on user.id = music.user_id order by user.name) as toto") | ||
| } | ||
|
|
||
| func TestDerivedTableWithHaving(t *testing.T) { | ||
| mcmp, closer := start(t) | ||
| defer closer() | ||
|
|
||
| mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)") | ||
| mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')") | ||
|
|
||
| mcmp.Exec("set sql_mode = ''") | ||
|
|
||
| // this is probably flaky? the id returned from the derived table could be any of the ids from user. | ||
| // works on my machine (TM) | ||
| mcmp.Exec("select /*vt+ PLANNER=Gen4 */ * from (select id from user having count(*) >= 1) s") | ||
|
||
| } | ||
|
|
||
| func TestDerivedTableColumns(t *testing.T) { | ||
| mcmp, closer := start(t) | ||
| defer closer() | ||
|
|
||
| mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')") | ||
| mcmp.AssertMatches(`SELECT /*vt+ PLANNER=gen4 */ t.id FROM (SELECT id FROM user) AS t(id) ORDER BY t.id DESC`, `[[INT64(5)] [INT64(4)] [INT64(3)] [INT64(2)] [INT64(1)]]`) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| create table user | ||
| ( | ||
| id bigint, | ||
| name varchar(255), | ||
| primary key (id) | ||
| ) Engine = InnoDB; | ||
|
|
||
| create table music | ||
| ( | ||
| id bigint, | ||
| user_id bigint, | ||
| primary key (id) | ||
| ) Engine = InnoDB; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "sharded": true, | ||
| "vindexes": { | ||
| "user_index": { | ||
| "type": "hash" | ||
| } | ||
| }, | ||
| "tables": { | ||
| "user": { | ||
| "column_vindexes": [ | ||
| { | ||
| "column": "id", | ||
| "name": "user_index" | ||
| } | ||
| ] | ||
| }, | ||
| "music": { | ||
| "column_vindexes": [ | ||
| { | ||
| "column": "user_id", | ||
| "name": "user_index" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should assert the results as well in all the tests in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, can do that. The bugs all showed up when comparing w mysql, but it might be interesting to assert on the values as well