vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/queries/union/union_test.go (about) 1 /* 2 Copyright 2021 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 union 18 19 import ( 20 "testing" 21 22 "vitess.io/vitess/go/test/endtoend/cluster" 23 24 "vitess.io/vitess/go/test/endtoend/utils" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func start(t *testing.T) (utils.MySQLCompare, func()) { 31 mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams) 32 require.NoError(t, err) 33 34 deleteAll := func() { 35 _, _ = utils.ExecAllowError(t, mcmp.VtConn, "set workload = oltp") 36 37 tables := []string{"t1", "t1_id2_idx", "t2", "t2_id4_idx"} 38 for _, table := range tables { 39 _, _ = mcmp.ExecAndIgnore("delete from " + table) 40 } 41 } 42 43 deleteAll() 44 45 return mcmp, func() { 46 deleteAll() 47 mcmp.Close() 48 cluster.PanicHandler(t) 49 } 50 } 51 52 func TestUnionDistinct(t *testing.T) { 53 mcmp, closer := start(t) 54 defer closer() 55 56 mcmp.Exec("insert into t1(id1, id2) values (1, 1), (2, 2), (3,3), (4,4)") 57 mcmp.Exec("insert into t2(id3, id4) values (2, 3), (3, 4), (4,4), (5,5)") 58 59 for _, workload := range []string{"oltp", "olap"} { 60 t.Run(workload, func(t *testing.T) { 61 utils.Exec(t, mcmp.VtConn, "set workload = "+workload) 62 mcmp.AssertMatches("select 1 union select null", "[[INT64(1)] [NULL]]") 63 mcmp.AssertMatches("select null union select null", "[[NULL]]") 64 mcmp.AssertMatches("select * from (select 1 as col union select 2) as t", "[[INT64(1)] [INT64(2)]]") 65 66 // test with real data coming from mysql 67 mcmp.AssertMatches("select id1 from t1 where id1 = 1 union select id1 from t1 where id1 = 5", "[[INT64(1)]]") 68 mcmp.AssertMatchesNoOrder("select id1 from t1 where id1 = 1 union select id1 from t1 where id1 = 4", "[[INT64(1)] [INT64(4)]]") 69 mcmp.AssertMatchesNoOrder("select id1 from t1 where id1 = 1 union select 452 union select id1 from t1 where id1 = 4", "[[INT64(1)] [INT64(452)] [INT64(4)]]") 70 mcmp.AssertMatchesNoOrder("select id1, id2 from t1 union select 827, 452 union select id3,id4 from t2", 71 "[[INT64(4) INT64(4)] [INT64(1) INT64(1)] [INT64(2) INT64(2)] [INT64(3) INT64(3)] [INT64(827) INT64(452)] [INT64(2) INT64(3)] [INT64(3) INT64(4)] [INT64(5) INT64(5)]]") 72 t.Run("skipped for now", func(t *testing.T) { 73 t.Skip() 74 mcmp.AssertMatches("select 1 from dual where 1 IN (select 1 as col union select 2)", "[[INT64(1)]]") 75 }) 76 }) 77 78 } 79 } 80 81 func TestUnionAll(t *testing.T) { 82 mcmp, closer := start(t) 83 defer closer() 84 85 mcmp.Exec("insert into t1(id1, id2) values(1, 1), (2, 2)") 86 mcmp.Exec("insert into t2(id3, id4) values(3, 3), (4, 4)") 87 88 for _, workload := range []string{"oltp", "olap"} { 89 t.Run(workload, func(t *testing.T) { 90 utils.Exec(t, mcmp.VtConn, "set workload = "+workload) 91 // union all between two selectuniqueequal 92 mcmp.AssertMatches("select id1 from t1 where id1 = 1 union all select id1 from t1 where id1 = 4", "[[INT64(1)]]") 93 94 // union all between two different tables 95 mcmp.AssertMatchesNoOrder("(select id1,id2 from t1 order by id1) union all (select id3,id4 from t2 order by id3)", 96 "[[INT64(1) INT64(1)] [INT64(2) INT64(2)] [INT64(3) INT64(3)] [INT64(4) INT64(4)]]") 97 98 // union all between two different tables 99 result := mcmp.Exec("(select id1,id2 from t1) union all (select id3,id4 from t2)") 100 assert.Equal(t, 4, len(result.Rows)) 101 102 // union all between two different tables 103 mcmp.AssertMatchesNoOrder("select tbl2.id1 FROM ((select id1 from t1 order by id1 limit 5) union all (select id1 from t1 order by id1 desc limit 5)) as tbl1 INNER JOIN t1 as tbl2 ON tbl1.id1 = tbl2.id1", 104 "[[INT64(1)] [INT64(2)] [INT64(2)] [INT64(1)]]") 105 106 // union all between two select unique in tables 107 mcmp.AssertMatchesNoOrder("select id1 from t1 where id1 in (1, 2, 3, 4, 5, 6, 7, 8) union all select id1 from t1 where id1 in (1, 2, 3, 4, 5, 6, 7, 8)", 108 "[[INT64(1)] [INT64(2)] [INT64(1)] [INT64(2)]]") 109 110 // 4 tables union all 111 mcmp.AssertMatchesNoOrder("select id1, id2 from t1 where id1 = 1 union all select id3,id4 from t2 where id3 = 3 union all select id1, id2 from t1 where id1 = 2 union all select id3,id4 from t2 where id3 = 4", 112 "[[INT64(1) INT64(1)] [INT64(2) INT64(2)] [INT64(3) INT64(3)] [INT64(4) INT64(4)]]") 113 }) 114 115 } 116 } 117 118 func TestUnion(t *testing.T) { 119 mcmp, closer := start(t) 120 defer closer() 121 122 mcmp.AssertMatches(`SELECT 1 UNION SELECT 1 UNION SELECT 1`, `[[INT64(1)]]`) 123 mcmp.AssertMatches(`SELECT 1,'a' UNION SELECT 1,'a' UNION SELECT 1,'a' ORDER BY 1`, `[[INT64(1) VARCHAR("a")]]`) 124 mcmp.AssertMatches(`SELECT 1,'z' UNION SELECT 2,'q' UNION SELECT 3,'b' ORDER BY 2`, `[[INT64(3) VARCHAR("b")] [INT64(2) VARCHAR("q")] [INT64(1) VARCHAR("z")]]`) 125 mcmp.AssertMatches(`SELECT 1,'a' UNION ALL SELECT 1,'a' UNION ALL SELECT 1,'a' ORDER BY 1`, `[[INT64(1) VARCHAR("a")] [INT64(1) VARCHAR("a")] [INT64(1) VARCHAR("a")]]`) 126 mcmp.AssertMatches(`(SELECT 1,'a') UNION ALL (SELECT 1,'a') UNION ALL (SELECT 1,'a') ORDER BY 1`, `[[INT64(1) VARCHAR("a")] [INT64(1) VARCHAR("a")] [INT64(1) VARCHAR("a")]]`) 127 mcmp.AssertMatches(`(SELECT 1,'a') ORDER BY 1`, `[[INT64(1) VARCHAR("a")]]`) 128 mcmp.AssertMatches(`(SELECT 1,'a' order by 1) union (SELECT 1,'a' ORDER BY 1)`, `[[INT64(1) VARCHAR("a")]]`) 129 }