Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push down derived tables under route when possible (#11422)
Backport of #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> 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>
- Loading branch information
1 parent
4b5dd2b
commit fc6a7d1
Showing
14 changed files
with
2,183 additions
and
151 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 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 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.