vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/queries/derived/derived_test.go (about)

     1  /*
     2  Copyright 2022 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package misc
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"vitess.io/vitess/go/test/endtoend/cluster"
    25  	"vitess.io/vitess/go/test/endtoend/utils"
    26  )
    27  
    28  func start(t *testing.T) (utils.MySQLCompare, func()) {
    29  	mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams)
    30  	require.NoError(t, err)
    31  
    32  	deleteAll := func() {
    33  		tables := []string{"music", "user"}
    34  		for _, table := range tables {
    35  			_, _ = mcmp.ExecAndIgnore("delete from " + table)
    36  		}
    37  	}
    38  
    39  	deleteAll()
    40  
    41  	return mcmp, func() {
    42  		deleteAll()
    43  		mcmp.Close()
    44  		cluster.PanicHandler(t)
    45  	}
    46  }
    47  
    48  func TestDerivedTableWithOrderByLimit(t *testing.T) {
    49  	mcmp, closer := start(t)
    50  	defer closer()
    51  
    52  	mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)")
    53  	mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')")
    54  
    55  	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")
    56  }
    57  
    58  func TestDerivedAggregationOnRHS(t *testing.T) {
    59  	t.Skip("skipped for now, issue: https://github.com/vitessio/vitess/issues/11703")
    60  	mcmp, closer := start(t)
    61  	defer closer()
    62  
    63  	mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)")
    64  	mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')")
    65  
    66  	mcmp.Exec("set sql_mode = ''")
    67  	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")
    68  }
    69  
    70  func TestDerivedRemoveInnerOrderBy(t *testing.T) {
    71  	mcmp, closer := start(t)
    72  	defer closer()
    73  
    74  	mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)")
    75  	mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')")
    76  
    77  	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")
    78  }
    79  
    80  func TestDerivedTableWithHaving(t *testing.T) {
    81  	mcmp, closer := start(t)
    82  	defer closer()
    83  
    84  	mcmp.Exec("insert into music(id, user_id) values(1,1), (2,5), (3,1), (4,2), (5,3), (6,4), (7,5)")
    85  	mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')")
    86  
    87  	mcmp.Exec("set sql_mode = ''")
    88  	mcmp.AssertMatchesAnyNoCompare("select  /*vt+ PLANNER=Gen4 */ * from (select id from user having count(*) >= 1) s", "[[INT64(1)]]", "[[INT64(4)]]")
    89  }
    90  
    91  func TestDerivedTableColumns(t *testing.T) {
    92  	mcmp, closer := start(t)
    93  	defer closer()
    94  
    95  	mcmp.Exec("insert into user(id, name) values(1,'toto'), (2,'tata'), (3,'titi'), (4,'tete'), (5,'foo')")
    96  	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)]]`)
    97  }