vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/queries/lookup_queries/main_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 vtgate
    18  
    19  import (
    20  	_ "embed"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"vitess.io/vitess/go/test/endtoend/utils"
    29  
    30  	"vitess.io/vitess/go/vt/vtgate/planbuilder"
    31  
    32  	"vitess.io/vitess/go/mysql"
    33  	"vitess.io/vitess/go/test/endtoend/cluster"
    34  )
    35  
    36  var (
    37  	clusterInstance *cluster.LocalProcessCluster
    38  	vtParams        mysql.ConnParams
    39  	mysqlParams     mysql.ConnParams
    40  	shardedKs       = "ks"
    41  
    42  	shardedKsShards = []string{"-19a0", "19a0-20", "20-20c0", "20c0-"}
    43  	Cell            = "test"
    44  	//go:embed schema.sql
    45  	shardedSchemaSQL string
    46  
    47  	//go:embed vschema.json
    48  	shardedVSchema string
    49  )
    50  
    51  func TestMain(m *testing.M) {
    52  	defer cluster.PanicHandler(nil)
    53  	flag.Parse()
    54  
    55  	exitCode := func() int {
    56  		clusterInstance = cluster.NewCluster(Cell, "localhost")
    57  		defer clusterInstance.Teardown()
    58  
    59  		// Start topo server
    60  		err := clusterInstance.StartTopo()
    61  		if err != nil {
    62  			return 1
    63  		}
    64  
    65  		// Start keyspace
    66  		sKs := &cluster.Keyspace{
    67  			Name:      shardedKs,
    68  			SchemaSQL: shardedSchemaSQL,
    69  			VSchema:   shardedVSchema,
    70  		}
    71  
    72  		clusterInstance.VtTabletExtraArgs = []string{"--queryserver-config-schema-change-signal-interval", "0.1"}
    73  		err = clusterInstance.StartKeyspace(*sKs, shardedKsShards, 0, false)
    74  		if err != nil {
    75  			return 1
    76  		}
    77  
    78  		err = clusterInstance.VtctlclientProcess.ExecuteCommand("RebuildVSchemaGraph")
    79  		if err != nil {
    80  			return 1
    81  		}
    82  
    83  		// Start vtgate
    84  		clusterInstance.VtGatePlannerVersion = planbuilder.Gen4 // enable Gen4 planner.
    85  		err = clusterInstance.StartVtgate()
    86  		if err != nil {
    87  			return 1
    88  		}
    89  		vtParams = mysql.ConnParams{
    90  			Host: clusterInstance.Hostname,
    91  			Port: clusterInstance.VtgateMySQLPort,
    92  		}
    93  
    94  		conn, closer, err := utils.NewMySQL(clusterInstance, shardedKs, shardedSchemaSQL)
    95  		if err != nil {
    96  			fmt.Println(err)
    97  			return 1
    98  		}
    99  		defer closer()
   100  		mysqlParams = conn
   101  		return m.Run()
   102  	}()
   103  	os.Exit(exitCode)
   104  }
   105  
   106  func start(t *testing.T) (utils.MySQLCompare, func()) {
   107  	mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams)
   108  	require.NoError(t, err)
   109  	deleteAll := func() {
   110  		_, _ = utils.ExecAllowError(t, mcmp.VtConn, "set workload = oltp")
   111  
   112  		tables := []string{"user", "lookup"}
   113  		for _, table := range tables {
   114  			_, _ = mcmp.ExecAndIgnore("delete from " + table)
   115  		}
   116  	}
   117  
   118  	deleteAll()
   119  
   120  	return mcmp, func() {
   121  		deleteAll()
   122  		mcmp.Close()
   123  		cluster.PanicHandler(t)
   124  	}
   125  }
   126  
   127  func TestLookupQueries(t *testing.T) {
   128  	mcmp, closer := start(t)
   129  	defer closer()
   130  
   131  	mcmp.Exec(`insert into user 
   132      (id, lookup,   lookup_unique) values 
   133  	(1, 'apa',    'apa'), 
   134  	(2, 'apa',    'bandar'), 
   135  	(3, 'monkey', 'monkey')`)
   136  
   137  	for _, workload := range []string{"olap", "oltp"} {
   138  		t.Run(workload, func(t *testing.T) {
   139  			utils.Exec(t, mcmp.VtConn, "set workload = "+workload)
   140  
   141  			mcmp.AssertMatches("select id from user where lookup = 'apa'", "[[INT64(1)] [INT64(2)]]")
   142  			mcmp.AssertMatches("select id from user where lookup = 'not there'", "[]")
   143  			mcmp.AssertMatchesNoOrder("select id from user where lookup in ('apa', 'monkey')", "[[INT64(1)] [INT64(2)] [INT64(3)]]")
   144  			mcmp.AssertMatches("select count(*) from user where lookup in ('apa', 'monkey')", "[[INT64(3)]]")
   145  
   146  			mcmp.AssertMatches("select id from user where lookup_unique = 'apa'", "[[INT64(1)]]")
   147  			mcmp.AssertMatches("select id from user where lookup_unique = 'not there'", "[]")
   148  			mcmp.AssertMatchesNoOrder("select id from user where lookup_unique in ('apa', 'bandar')", "[[INT64(1)] [INT64(2)]]")
   149  			mcmp.AssertMatches("select count(*) from user where lookup_unique in ('apa', 'monkey', 'bandar')", "[[INT64(3)]]")
   150  		})
   151  	}
   152  }